From 4623dff66546eb948718688ef40f05c12922cbd6 Mon Sep 17 00:00:00 2001 From: Andreas Berthoud Date: Fri, 23 Jul 2021 13:29:44 +0200 Subject: [PATCH] backend: Add root_logger to request and respons --- backend/monsun_backend/command_endpoint.py | 4 +- backend/monsun_backend/command_execution.py | 14 ++++++- backend/monsun_backend/commands.py | 41 +++++++++++++-------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/backend/monsun_backend/command_endpoint.py b/backend/monsun_backend/command_endpoint.py index 92ff40a..4fcd8ff 100644 --- a/backend/monsun_backend/command_endpoint.py +++ b/backend/monsun_backend/command_endpoint.py @@ -29,7 +29,9 @@ def command(role: str): command_id = get_command_id_from_name(cmd) try: - command = get_request_class(command_id=command_id)(**arguments) + command = get_request_class(command_id=command_id)( + root_logger=logging.getLogger(role), **arguments + ) except Exception: return Response(status=status.HTTP_400_BAD_REQUEST) diff --git a/backend/monsun_backend/command_execution.py b/backend/monsun_backend/command_execution.py index 2f73c5e..d4e8873 100644 --- a/backend/monsun_backend/command_execution.py +++ b/backend/monsun_backend/command_execution.py @@ -4,6 +4,7 @@ import time from enum import Enum from multiprocessing import Process from multiprocessing import Queue +from struct import error from struct import unpack from typing import Dict from typing import List @@ -97,7 +98,11 @@ def enter_fsm( while True: if state == State.heart_beat: if time.time() - heartbeat_interval > last_heart_beat_time: - command_queue.put(commands.HeartbeatRequest()) + command_queue.put( + commands.HeartbeatRequest( + root_logger=root_logger, + ), + ) last_heart_beat_time = time.time() state = State.executing_command @@ -213,6 +218,12 @@ def receive( logger.debug(f"bytes: {bytes_read.hex()}") try: command_id_int, data_length, _ = unpack(">BHB", bytes_read[:header_size]) + except error: + logger.error("error while interpreting command header") + bytes_read = None + continue + + try: payload = bytes(bytes_read[header_size : header_size + data_length]) stop_byte = bytes_read[header_size + data_length] except IndexError: @@ -244,6 +255,7 @@ def receive( if command_id == CommandId.command_log: command = commands.LogCommand( + root_logger=root_logger, data=payload, ) commands_received.append(command) diff --git a/backend/monsun_backend/commands.py b/backend/monsun_backend/commands.py index b3ca8bb..4283215 100644 --- a/backend/monsun_backend/commands.py +++ b/backend/monsun_backend/commands.py @@ -77,8 +77,11 @@ class Response(abc.ABC): class Command(abc.ABC): - def __init__(self) -> None: - self._logger = logging.getLogger(self.__class__.__name__) + def __init__( + self, + root_logger: logging.Logger, + ) -> None: + self._logger = root_logger.getChild(self.__class__.__name__) @property @abc.abstractmethod @@ -107,8 +110,11 @@ class Command(abc.ABC): class Request(Command): - def __init__(self) -> None: - super().__init__() + def __init__( + self, + root_logger: logging.Logger, + ) -> None: + super().__init__(root_logger=root_logger) self.response_identifier = randint(0, pow(2, 16) - 1) @@ -144,9 +150,10 @@ class LogCommand(Command): def __init__( self, + root_logger: logging.Logger, data: bytes, ) -> None: - super().__init__() + super().__init__(root_logger=root_logger) self._logger.setLevel(logging.INFO) @@ -160,7 +167,7 @@ class LogCommand(Command): self._logger.debug("logger_name " + str(logger_name)) self._logger.debug("Message: " + str(message)) - self.received_logger = logging.getLogger(logger_name.decode()) + self.received_logger = root_logger.getChild(logger_name.decode()) self.received_logger.setLevel(logging.DEBUG) self.level = level self.message = message.decode() @@ -215,15 +222,12 @@ class LEDResponse(Response): data: bytes, ): self.was_successful = bool(data[0]) - if self.was_successful: - self._logger.debug("LED command was successful") - else: - self._logger.debug("LED command was not successful") class LEDRequest(Request): def __init__( self, + root_logger: logging.Logger, id: Union[int, str], command: Union[int, str], ) -> None: @@ -240,7 +244,7 @@ class LEDRequest(Request): 1: on 2: toggle """ - super().__init__() + super().__init__(root_logger=root_logger) try: self.led_id = int(id) except ValueError: @@ -270,6 +274,10 @@ class LEDRequest(Request): def process_response(self, response: Response): if not isinstance(response, LEDResponse): raise TypeError(f"{response} is not a {LEDResponse}") + if response.was_successful: + self._logger.debug("LED command was successful") + else: + self._logger.debug("LED command was not successful") def execute(self, serial: Serial): payload = pack( @@ -291,18 +299,15 @@ class GPResponse(Response): data: bytes, ): self.was_successful = bool(data[0]) - if self.was_successful: - self._logger.debug("GP command was successful") - else: - self._logger.debug("GP command was not successful") class GPRequest(Request): def __init__( self, + root_logger: logging.Logger, command_id: Union[int, str], ) -> None: - super().__init__() + super().__init__(root_logger=root_logger) self.command_id = int(command_id) @property @@ -316,6 +321,10 @@ class GPRequest(Request): def process_response(self, response: Response): if not isinstance(response, GPResponse): raise TypeError(f"{response} is not a {GPResponse}") + if response.was_successful: + self._logger.debug("GP command was successful") + else: + self._logger.debug("GP command was not successful") def execute(self, serial: Serial): payload = pack(