From 0d33e2a88cc3d045d697086cc96622e2db6d87ae Mon Sep 17 00:00:00 2001 From: Adam Kadlec Date: Thu, 3 Feb 2022 11:53:02 +0100 Subject: [PATCH] Refactoring reading process --- fastybird_fb_bus_connector/__init__.py | 2 +- fastybird_fb_bus_connector/clients/apiv1.py | 152 ++++++++------------ 2 files changed, 61 insertions(+), 93 deletions(-) diff --git a/fastybird_fb_bus_connector/__init__.py b/fastybird_fb_bus_connector/__init__.py index 56e08e6..0a33604 100644 --- a/fastybird_fb_bus_connector/__init__.py +++ b/fastybird_fb_bus_connector/__init__.py @@ -20,7 +20,7 @@ from .bootstrap import create_connector -__version__ = "0.13.0" +__version__ = "0.14.0" __all__ = ["connector", "bootstrap"] diff --git a/fastybird_fb_bus_connector/clients/apiv1.py b/fastybird_fb_bus_connector/clients/apiv1.py index d7feb96..febae1e 100644 --- a/fastybird_fb_bus_connector/clients/apiv1.py +++ b/fastybird_fb_bus_connector/clients/apiv1.py @@ -88,9 +88,7 @@ class ApiV1Client(IClient): # pylint: disable=too-few-public-methods, too-many- __discovery_last_broadcast_request_send_timestamp: float = 0.0 __processed_devices: List[str] = [] - __processed_devices_inputs_registers: Dict[str, Set[str]] = {} - __processed_devices_outputs_registers: Dict[str, Set[str]] = {} - __processed_devices_attributes_registers: Dict[str, Set[str]] = {} + __processed_devices_registers: Dict[str, Dict[int, Set[str]]] = {} __logger: Union[Logger, logging.Logger] @@ -129,9 +127,7 @@ def __init__( # pylint: disable=too-many-arguments self.__logger = logger self.__processed_devices = [] - self.__processed_devices_inputs_registers = {} - self.__processed_devices_outputs_registers = {} - self.__processed_devices_attributes_registers = {} + self.__processed_devices_registers = {} # ----------------------------------------------------------------------------- @@ -423,106 +419,78 @@ def __read_registers_handler( # pylint: disable=too-many-return-statements,too- device_address: int, ) -> bool: """Process devices registers reading""" + for registers_type in [ # pylint: disable=too-many-nested-blocks + RegisterType.INPUT, + RegisterType.OUTPUT, + RegisterType.ATTRIBUTE, + ]: + if device.id.__str__() not in self.__processed_devices_registers: + self.__processed_devices_registers[device.id.__str__()] = {} - # INPUT REGISTERS - if device.id.__str__() not in self.__processed_devices_inputs_registers: - self.__processed_devices_inputs_registers[device.id.__str__()] = set() + if registers_type.value not in self.__processed_devices_registers[device.id.__str__()]: + self.__processed_devices_registers[device.id.__str__()][registers_type.value] = set() - registers = self.__registers_registry.get_all_for_device( - device_id=device.id, - register_type=RegisterType.INPUT, - ) - - if 0 < len(registers) != len(self.__processed_devices_inputs_registers[device.id.__str__()]): - if self.__is_all_registers_readable(device=device, registers_count=len(registers)): - reading_result = self.__read_multiple_registers( - device=device, - device_address=device_address, - registers_type=RegisterType.INPUT, - registers_count=len(registers), - ) - - for register in registers: - self.__processed_devices_inputs_registers[device.id.__str__()].add(register.id.__str__()) - - return reading_result - - for register in registers: - reading_result = self.__read_single_register( - device=device, - device_address=device_address, - register_type=register.type, - register_address=register.address, - ) + processed_length = len(self.__processed_devices_registers[device.id.__str__()][registers_type.value]) - self.__processed_devices_inputs_registers[device.id.__str__()].add(register.id.__str__()) + registers = self.__registers_registry.get_all_for_device( + device_id=device.id, + register_type=registers_type, + ) - return reading_result + if registers_type == RegisterType.ATTRIBUTE: + registers = [register for register in registers if register.queryable] - # OUTPUT REGISTERS - if device.id.__str__() not in self.__processed_devices_outputs_registers: - self.__processed_devices_outputs_registers[device.id.__str__()] = set() + if 0 < len(registers) != processed_length: + # Try to read all registers of one type at once + if registers_type != RegisterType.ATTRIBUTE and self.__is_all_registers_readable( + device=device, registers_count=len(registers) + ): + reading_result = self.__read_multiple_registers( + device=device, + device_address=device_address, + registers_type=registers_type, + registers_count=len(registers), + ) - registers = self.__registers_registry.get_all_for_device( - device_id=device.id, - register_type=RegisterType.OUTPUT, - ) + if reading_result: + for register in registers: + self.__processed_devices_registers[device.id.__str__()][registers_type.value].add( + register.id.__str__(), + ) - if 0 < len(registers) != len(self.__processed_devices_outputs_registers[device.id.__str__()]): - if self.__is_all_registers_readable(device=device, registers_count=len(registers)): - reading_result = self.__read_multiple_registers( - device=device, - device_address=device_address, - registers_type=RegisterType.OUTPUT, - registers_count=len(registers), - ) + return reading_result + # Registers have to be read one by one for register in registers: - self.__processed_devices_outputs_registers[device.id.__str__()].add(register.id.__str__()) - - return reading_result - - for register in registers: - reading_result = self.__read_single_register( - device=device, - device_address=device_address, - register_type=register.type, - register_address=register.address, - ) - - self.__processed_devices_outputs_registers[device.id.__str__()].add(register.id.__str__()) - - return reading_result - - # ATTRIBUTES REGISTERS - if device.id.__str__() not in self.__processed_devices_attributes_registers: - self.__processed_devices_attributes_registers[device.id.__str__()] = set() - - registers = self.__registers_registry.get_all_for_device( - device_id=device.id, - register_type=RegisterType.ATTRIBUTE, - ) - registers = [register for register in registers if register.queryable] + if ( + register.id.__str__() + in self.__processed_devices_registers[device.id.__str__()][registers_type.value] + ): + continue - if 0 < len(registers) != len(self.__processed_devices_attributes_registers[device.id.__str__()]): - for register in registers: - reading_result = self.__read_single_register( - device=device, - device_address=device_address, - register_type=register.type, - register_address=register.address, - ) + reading_result = self.__read_single_register( + device=device, + device_address=device_address, + register_type=registers_type, + register_address=register.address, + ) - self.__processed_devices_attributes_registers[device.id.__str__()].add(register.id.__str__()) + if reading_result: + self.__processed_devices_registers[device.id.__str__()][registers_type.value].add( + register.id.__str__(), + ) - return reading_result + return reading_result - if time.time() - device.last_reading_packet_timestamp >= device.sampling_time: + if time.time() - device.last_reading_packet_timestamp < device.sampling_time: return True - self.__processed_devices_inputs_registers[device.id.__str__()] = set() - self.__processed_devices_outputs_registers[device.id.__str__()] = set() - self.__processed_devices_attributes_registers[device.id.__str__()] = set() + for registers_type in [ # pylint: disable=too-many-nested-blocks + RegisterType.INPUT, + RegisterType.OUTPUT, + RegisterType.ATTRIBUTE, + ]: + self.__processed_devices_registers[device.id.__str__()][registers_type.value] = set() return True @@ -993,7 +961,7 @@ def __finalize_discovered_device( # ----------------------------------------------------------------------------- def __is_all_registers_readable(self, device: DeviceRecord, registers_count: int) -> bool: - """Check if all registers could be read at once """ + """Check if all registers could be read at once""" # Calculate maximum count registers per one packet # e.g. max_packet_length = 24 => max_readable_registers_count = 4 # - only 4 registers could be read in one packet