def data_received(self, data): self.buffer += data while self.buffer: length, raw = variant.read_variant(self.buffer) if len(raw) < length: return data = raw[:length] self.buffer = raw[length:] if self.chacha: data = self.chacha.decrypt(data) parsed = protobuf.ProtocolMessage() parsed.ParseFromString(data) log_protobuf(_LOGGER, "<< Receive: Protobuf", parsed) try: name = parsed.Type.Name(parsed.type).lower().replace("_message", "") _LOGGER.debug("Received %s message", name) getattr(self, "handle_" + name)(parsed, parsed.inner()) except AttributeError: _LOGGER.exception("No message handler for " + str(parsed)) except Exception: _LOGGER.exception("Error while dispatching message")
def _handle_message(self, data): if self._chacha: data = self._chacha.decrypt(data) log_binary(_LOGGER, self._log_str + "<< Receive", Decrypted=data) parsed = protobuf.ProtocolMessage() parsed.ParseFromString(data) log_protobuf(_LOGGER, self._log_str + "<< Receive: Protobuf", parsed) self.listener.message_received(parsed, data)
def send(self, message): if not self.transport: return data = message.SerializeToString() if self.chacha: data = self.chacha.encrypt(data) length = variant.write_variant(len(data)) self.transport.write(length + data) log_protobuf(_LOGGER, ">> Send: Protobuf", message)
def send(self, message): """Send message to device.""" serialized = message.SerializeToString() log_binary(_LOGGER, '>> Send', Data=serialized) if self._chacha: serialized = self._chacha.encrypt(serialized) log_binary(_LOGGER, '>> Send', Encrypted=serialized) data = write_variant(len(serialized)) + serialized self._transport.write(data) log_protobuf(_LOGGER, '>> Send: Protobuf', message)
def send(self, message: protobuf.ProtocolMessage) -> None: """Send protobuf message to device.""" serialized = message.SerializeToString() log_binary(_LOGGER, self._log_str + ">> Send", Data=serialized) if self._chacha: serialized = self._chacha.encrypt(serialized) log_binary(_LOGGER, self._log_str + ">> Send", Encrypted=serialized) data = write_variant(len(serialized)) + serialized self._transport.write(data) log_protobuf(_LOGGER, self._log_str + ">> Send: Protobuf", message)
def test_protobuf_log_with_length_override(logger, message): log_protobuf(logger, "text", message) assert _debug_string(logger) == "text: id..."
def test_protobuf_log_limit_message_max_length(logger, message): message.identifier = "a" * 200 log_protobuf(logger, "test", message) assert len(_debug_string(logger)) == 156
def test_protobuf_log_message(logger, message): log_protobuf(logger, "test", message) assert _debug_string(logger) == 'test: identifier: "aaaaaaaaaa"'
def test_protobuf_no_log_if_not_debug(logger, message): logger.isEnabledFor.return_value = False log_protobuf(logger, "test", message) logger.isEnabledFor.assert_called_with(logging.DEBUG)
def handle_protobuf(self, message: protobuf.ProtocolMessage) -> None: """Handle incoming protobuf message.""" log_protobuf(_LOGGER, "<< Receive: Protobuf", message) self.listener.message_received(message, None) # pylint: disable=no-member
def send(self, message: protobuf.ProtocolMessage) -> None: """Send protobuf message to device.""" if self.data_channel is not None: self.data_channel.send_protobuf(message) log_protobuf(_LOGGER, ">> Send: Protobuf", message)
def test_log_with_length_override(self): log_protobuf(self.logger, "text", self.msg) self.assertEqual(_debug_string(self.logger), 'text: id...')
def test_log_limit_message_max_length(self): self.msg.identifier = "a" * 200 log_protobuf(self.logger, "test", self.msg) self.assertEqual(len(_debug_string(self.logger)), 156)
def test_log_message(self): log_protobuf(self.logger, "test", self.msg) self.assertEqual(_debug_string(self.logger), 'test: identifier: "aaaaaaaaaa"')
def test_no_log_if_not_debug(self): self.logger.isEnabledFor.return_value = False log_protobuf(self.logger, "test", self.msg) self.logger.isEnabledFor.assert_called_with(logging.DEBUG)