def package(state): """Helper function that packages the current state. Parses through the state dictionary in key value pairs, creates a new message in the proto for each sensor, and adds corresponding data to each field. Currently only supports a single limit_switch switch as the rest of the state is just test fields. """ try: proto_message = runtime_pb2.RuntimeData() proto_message.robot_state = state['studentCodeState'][0] for uid, values in state['hibike'][0]['devices'][0].items(): sensor = proto_message.sensor_data.add() sensor.uid = str(uid) sensor.device_type = SENSOR_TYPE[uid >> 72] for param, value in values[0].items(): if value[0] is None: continue param_value_pair = sensor.param_value.add() param_value_pair.param = param if isinstance(value[0], bool): param_value_pair.bool_value = value[0] elif isinstance(value[0], float): param_value_pair.float_value = value[0] elif isinstance(value[0], int): param_value_pair.int_value = value[0] return proto_message.SerializeToString() except Exception as e: bad_things_queue.put( BadThing(sys.exc_info(), "UDP packager thread has crashed with error:" + str(e), event=BAD_EVENTS.UDP_SEND_ERROR, printStackTrace=True))
def package(state): """Helper function that packages the current state. Parses through the state dictionary in key value pairs, creates a new message in the proto for each sensor, and adds corresponding data to each field. Currently only supports a single limit_switch switch as the rest of the state is just test fields. """ try: proto_message = runtime_pb2.RuntimeData() for devID, devVal in state.items(): if (devID == 'studentCodeState'): proto_message.robot_state = devVal[ 0] #check if we are dealing with sensor data or student code state elif devID == 'limit_switch': test_sensor = proto_message.sensor_data.add() test_sensor.device_name = devID test_sensor.device_type = devVal[0][0] test_sensor.value = devVal[0][1] test_sensor.uid = devVal[0][2] return proto_message.SerializeToString() except Exception as e: badThingsQueue.put( BadThing(sys.exc_info(), "UDP packager thread has crashed with error:" + str(e), event=BAD_EVENTS.UDP_SEND_ERROR, printStackTrace=True))
def receiver(_port, receive_queue): """Receive messages on port to receive queue.""" host = '127.0.0.1' sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((host, RECV_PORT)) while True: msg, _ = sock.recvfrom(2048) runtime_message = runtime_pb2.RuntimeData() runtime_message.ParseFromString(msg) receive_queue[0] = msg
def receiver(port, receive_queue): host = '127.0.0.1' s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind((host, recv_port)) while True: msg, addr = s.recvfrom(2048) runtime_message = runtime_pb2.RuntimeData() runtime_message.ParseFromString(msg) print(runtime_message.robot_state) for sensor in runtime_message.sensor_data: print(sensor.device_type) print(sensor.device_name) print(sensor.value) print(sensor.uid) receive_queue[0] = msg