Exemple #1
0
def send_connect(sock, host, port, dev_id):
    ''' Sends a Connect message to the controller '''
    msg = sensor_pb2.Msg()
    msg.connect_msg.dev_id.CopyFrom(dev_id)

    payload = msg.SerializeToString()
    return send_packet(sock, host, port, payload)
Exemple #2
0
def send_report(sock, host, port, dev_id, reading):
    ''' Sends a Report message to the controller '''
    msg = sensor_pb2.Msg()
    msg.report_msg.dev_id.CopyFrom(dev_id)
    msg.report_msg.data.temperature = int(reading.temperature * 10)
    msg.report_msg.data.humidity = int(reading.humidity * 10)

    payload = msg.SerializeToString()
    return send_packet(sock, host, port, payload)
Exemple #3
0
 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.Msg()
     msg.ParseFromString(data[1:])
     if msg.WhichOneof("msg_type") == "connect_msg":
         handle_connect(msg.connect_msg, self.client_address)
     elif msg.WhichOneof("msg_type") == "report_msg":
         handle_report(msg.report_msg)
     else:
         print("Unknown message type.")
Exemple #4
0
def main():
    args = parse_args()

    # Create our ID object
    my_id = sensor_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.Msg()
            msg.ParseFromString(packet[1:])
            if msg.WhichOneof("msg_type") == "command_msg":
                handle_command(sock, args.host, args.port,
                               msg.command_msg.cmd_type, 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)
Exemple #5
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.Msg()
            msg.command_msg.cmd_type = sensor_pb2.Command.REPORT_DATA
            cmd_name = 'REPORT'

            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 "",
                n.address))

        time.sleep(5)