def send_connect(sock, host, port, dev_id): ''' Sends a Connect message to the controller ''' con_msg = sensor_pb2_pb2.Connect() con_msg.dev_id.CopyFrom(dev_id) msg = sensor_pb2_pb2.Msg() msg.msg_type = sensor_pb2_pb2.Msg.CONNECT msg.connect_msg.CopyFrom(con_msg) payload = msg.SerializeToString() return send_packet(sock, host, port, payload)
def send_report(sock, host, port, dev_id, reading): ''' Sends a Report message to the controller ''' rpt_msg = sensor_pb2_pb2.Report() rpt_msg.dev_id.CopyFrom(dev_id) rpt_msg.data.temperature = int(reading.temperature * 10) rpt_msg.data.humidity = int(reading.humidity * 10) msg = sensor_pb2_pb2.Msg() msg.msg_type = sensor_pb2_pb2.Msg.REPORT msg.report_msg.CopyFrom(rpt_msg) payload = msg.SerializeToString() return send_packet(sock, host, port, payload)
def handle(self): data = self.request[0] socket = self.request[1] print("Packet from {0}:{1}".format(self.client_address[0], self.client_address[1])) #print(binascii.hexlify(data[1:])) msg = sensor_pb2_pb2.Msg() msg.ParseFromString(data[1:]) if msg.msg_type == sensor_pb2_pb2.Msg.CONNECT: handle_connect(msg.connect_msg, self.client_address) elif msg.msg_type == sensor_pb2_pb2.Msg.REPORT: handle_report(msg.report_msg) else: print("Unknown message type.")
def main(): args = parse_args() # Create our ID object my_id = sensor_pb2_pb2.DeviceIdentification() my_id.id = args.dev_id if args.dev_name: my_id.name = args.dev_name # Set up UDP socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.setblocking(0) # Tell the controller we exist send_connect(sock, args.host, args.port, my_id) time_to_next_reading = 5 while True: # See if we've gotten anything from the controller. try: packet = sock.recv(1024) msg = sensor_pb2_pb2.Msg() msg.ParseFromString(packet[1:]) if msg.msg_type == sensor_pb2_pb2.Msg.COMMAND: handle_command(sock, args.host, args.port, msg.command_msg, my_id) else: print("Unexpected message type rec'd") except Exception as ex: #template = "An exception of type {0} occurred. Arguments:\n{1!r}" #message = template.format(type(ex).__name__, ex.args) #print message time.sleep(1) # Take another sensor reading? time_to_next_reading -= 1 if time_to_next_reading <= 0: take_reading() time_to_next_reading = 5 sys.exit(0)
server = ThreadedServer((HOST, PORT), UDPHandler) ip, port = server.server_address server_thread = threading.Thread(target=server.serve_forever) server_thread.daemon = True server_thread.start() print("Server loop running in thread:" + server_thread.name) cmd_name = 'REPORT' while True: # Tell all attached nodes to report their current data. if len(attached_nodes): print("{} node(s) attached".format(len(attached_nodes))) for n in attached_nodes: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) msg = sensor_pb2_pb2.Msg() msg.msg_type = sensor_pb2_pb2.Msg.COMMAND cmd_msg = sensor_pb2_pb2.Command() cmd_msg.cmd_type = sensor_pb2_pb2.Command.REPORT_DATA cmd_name = 'REPORT' msg.command_msg.CopyFrom(cmd_msg) payload = msg.SerializeToString() header = bytearray(1) header[0] = len(payload) sock.sendto(header + payload, (n.address[0], n.address[1])) print("Sent '{0}' command to {1:d} (0x{1:x}){2} {3}".format( cmd_name, n.id, " \"" + n.name + "\"" if n.name else "",