async def received_data(self, data, addr, allow_zero=False): """What to do when we get some data""" if type(data) is bytes: log.debug( hp.lc("Received bytes", bts=binascii.hexlify(data).decode())) try: protocol_register = self.transport_target.protocol_register protocol, pkt_type, Packet, PacketKls, data = Messages.get_packet_type( data, protocol_register) if protocol == 1024 and pkt_type == 45: if isinstance(data, bytes): source = struct.unpack("<I", data[4:8])[0] target = data[8:16] sequence = data[23] else: source = data.source target = data.target sequence = data.sequence serial = binascii.hexlify(target[:6]).decode() pkt = FakeAck(source, sequence, target, serial, addr) else: if PacketKls is None: PacketKls = Packet if isinstance(data, PacketKls): pkt = data.clone() else: pkt = PacketKls.create(data) except Exception as error: log.exception(error) else: await self.receiver.recv(pkt, addr, allow_zero=allow_zero)
@pytest.fixture() def protocol_register(self, TestMessages): protocol_register = ProtocolRegister() protocol_register.add(1024, LIFXPacket) protocol_register.message_register(1024).add(TestMessages) return protocol_register describe "get_packet_type": it "can get us information about our data", protocol_register, TestMessages: data = mock.Mock(name="data") packet_type = mock.Mock(name="packet_type", return_value=(1024, 78)) with mock.patch.object(PacketTypeExtractor, "packet_type", packet_type): info = Messages.get_packet_type(data, protocol_register) assert info == (1024, 78, LIFXPacket, TestMessages.One, data) packet_type.assert_called_once_with(data) data = mock.Mock(name="data") packet_type = mock.Mock(name="packet_type", return_value=(1024, 99)) with mock.patch.object(PacketTypeExtractor, "packet_type", packet_type): info = Messages.get_packet_type(data, protocol_register) assert info == (1024, 99, LIFXPacket, TestMessages.Two, data) packet_type.assert_called_once_with(data) it "can get us information about unknown pkt types (known protocol)", protocol_register: data = mock.Mock(name="data")