def send_report(sock, host, port, dev_id, reading, send_history): ''' Sends a Report message to the controller ''' global reading_history rpt_msg = sensor_pb2_pb2.Report() rpt_msg.dev_id.CopyFrom(dev_id) if send_history == True: for r in reading_history: data = rpt_msg.data_history.add() data.temperature = int(r.temperature * 10) data.humidity = int(r.humidity * 10) data.rainfall = int(r.rainfall) data.pressure = int(r.pressure) else: rpt_msg.data.temperature = int(reading.temperature * 10) rpt_msg.data.humidity = int(reading.humidity * 10) rpt_msg.data.rainfall = int(r.rainfall) rpt_msg.data.pressure = int(r.pressure) 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 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 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 my_id.keeps_history = True # 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)
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) hist_count = 0 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() if n.keeps_history: hist_count += 1 if hist_count < 12: cmd_msg.cmd_type = sensor_pb2_pb2.Command.REPORT_DATA_HISTORY cmd_name = 'REPORT_DATA_HISTORY' else: hist_count = 0 cmd_msg.cmd_type = sensor_pb2_pb2.Command.CLEAR_DATA_HISTORY cmd_name = 'CLEAR_DATA_HISTORY' else: cmd_msg.cmd_type = sensor_pb2_pb2.Command.REPORT_DATA cmd_name = 'REPORT'