def send_message(self, data, priority='11', count_incr=True): cksm = Checksum.calculate(data) message = priority + json.dumps(data) + cksm try: self.sock.sendto(message, (self.rxip, self.rxport)) #TODO implement catch, fail, etc except Exception: pass if count_incr: self.counter.increment()
def handle_message(self,message): #parse parts self.priority = message[:2] self.cksm = message[-32:] self.msg = message[2:-32] self.vprint(self.msg) #calculate checksum calc_cksm = Checksum.calculate(json.loads(self.msg)) self.vprint("Calculated checksum: " + str(calc_cksm)) self.vprint("Delivered checksum: " + str(self.cksm)) if(calc_cksm != self.cksm): return False #decode msg json msg_dict = json.loads(self.msg) #handle update count message if(self.priority == '01'): new_count = int(msg_dict["new_count"]) self.counter.update_count(new_count) return #retrieve counter msg_count = int(msg_dict["active_count"]) self.vprint("Active Count: " + str(msg_count)) #increment counter self.counter.increment() if(self.counter.check_value(msg_count)): return msg_dict elif(self.priority == '00'): #allow emergency messages return msg_dict else: self.counter.decrement() return False
msg2 = { "hello": "world" } msg3 = { "active_count":2, "yaw_x" : -5, "abort" : False, "speed" : 25.6, "hello": "world", "more_data": 23235 } print msg chksm = Checksum.calculate(msg) print Checksum.calculate(msg2) print Checksum.calculate(msg3) print chksm print len(chksm) log.info('test') message = '11' + json.dumps(msg) + chksm print message x = imh.handle_message(message) print x print x['abort']