From 94470a30036622ebde70434484670e5141638f5f Mon Sep 17 00:00:00 2001 From: Adam Kadlec Date: Thu, 10 Feb 2022 20:25:02 +0100 Subject: [PATCH] Use constants --- fastybird_fb_bus_connector/bootstrap.py | 2 +- fastybird_fb_bus_connector/entities.py | 16 +++-- .../events/listeners.py | 4 +- fastybird_fb_bus_connector/registry/model.py | 67 ++++++++++--------- fastybird_fb_bus_connector/types.py | 27 +++++++- src/Constants.php | 34 ---------- src/Entities/FbBusConnector.php | 9 ++- src/Types/ConnectorPropertyType.php | 48 +++++++++++++ 8 files changed, 127 insertions(+), 80 deletions(-) delete mode 100644 src/Constants.php create mode 100644 src/Types/ConnectorPropertyType.php diff --git a/fastybird_fb_bus_connector/bootstrap.py b/fastybird_fb_bus_connector/bootstrap.py index 5002184..1579301 100644 --- a/fastybird_fb_bus_connector/bootstrap.py +++ b/fastybird_fb_bus_connector/bootstrap.py @@ -71,7 +71,7 @@ def create_connector( di["fb-bus-connector_events-dispatcher"] = di[EventDispatcher] # Registers - di[RegistersRegistry] = RegistersRegistry(event_dispatcher=di[EventDispatcher]) + di[RegistersRegistry] = RegistersRegistry(event_dispatcher=di[EventDispatcher]) # type: ignore[call-arg] di["fb-bus-connector_registers-registry"] = di[RegistersRegistry] di[DevicesRegistry] = DevicesRegistry( diff --git a/fastybird_fb_bus_connector/entities.py b/fastybird_fb_bus_connector/entities.py index 44102fb..ec89662 100644 --- a/fastybird_fb_bus_connector/entities.py +++ b/fastybird_fb_bus_connector/entities.py @@ -27,14 +27,16 @@ ConnectorStaticPropertyEntity, ) from fastybird_devices_module.entities.device import DeviceEntity -from fastybird_metadata.devices_module import ConnectorPropertyName from fastybird_metadata.types import ConnectorSource, ModuleSource, PluginSource # Library libs from fastybird_fb_bus_connector.types import ( CONNECTOR_NAME, + DEFAULT_BAUD_RATE, + DEFAULT_SERIAL_INTERFACE, DEVICE_NAME, MASTER_ADDRESS, + ConnectorAttribute, ProtocolVersion, ) @@ -71,7 +73,7 @@ def source(self) -> Union[ModuleSource, ConnectorSource, PluginSource]: def address(self) -> int: """Connector communication master address""" address_property = next( - iter([record for record in self.properties if record.identifier == ConnectorPropertyName.ADDRESS.value]), + iter([record for record in self.properties if record.identifier == ConnectorAttribute.ADDRESS.value]), None, ) @@ -90,7 +92,7 @@ def address(self) -> int: def interface(self) -> str: """Connector serial interface""" interface_property = next( - iter([record for record in self.properties if record.identifier == ConnectorPropertyName.INTERFACE.value]), + iter([record for record in self.properties if record.identifier == ConnectorAttribute.INTERFACE.value]), None, ) @@ -99,7 +101,7 @@ def interface(self) -> str: or not isinstance(interface_property, ConnectorStaticPropertyEntity) or not isinstance(interface_property.value, str) ): - return "/dev/ttyAMA0" + return DEFAULT_SERIAL_INTERFACE return interface_property.value @@ -109,7 +111,7 @@ def interface(self) -> str: def baud_rate(self) -> int: """Connector communication baud rate""" baud_rate_property = next( - iter([record for record in self.properties if record.identifier == ConnectorPropertyName.INTERFACE.value]), + iter([record for record in self.properties if record.identifier == ConnectorAttribute.BAUD_RATE.value]), None, ) @@ -118,7 +120,7 @@ def baud_rate(self) -> int: or not isinstance(baud_rate_property, ConnectorStaticPropertyEntity) or not isinstance(baud_rate_property.value, int) ): - return 38400 + return DEFAULT_BAUD_RATE return baud_rate_property.value @@ -128,7 +130,7 @@ def baud_rate(self) -> int: def protocol(self) -> ProtocolVersion: """Connector communication protocol version""" protocol_property = next( - iter([record for record in self.properties if record.identifier == ConnectorPropertyName.INTERFACE.value]), + iter([record for record in self.properties if record.identifier == ConnectorAttribute.PROTOCOL.value]), None, ) diff --git a/fastybird_fb_bus_connector/events/listeners.py b/fastybird_fb_bus_connector/events/listeners.py index f79995b..dbe680e 100644 --- a/fastybird_fb_bus_connector/events/listeners.py +++ b/fastybird_fb_bus_connector/events/listeners.py @@ -21,7 +21,7 @@ # Python base dependencies import logging import uuid -from typing import Optional, Union +from typing import Union # Library dependencies import inflection @@ -542,7 +542,7 @@ def __write_channel_property_actual_value( state_data = { "actual_value": register.actual_value, "expected_value": register.expected_value, - "pending": register.expected_pending is not None + "pending": register.expected_pending is not None, } try: diff --git a/fastybird_fb_bus_connector/registry/model.py b/fastybird_fb_bus_connector/registry/model.py index 8f66761..529036a 100644 --- a/fastybird_fb_bus_connector/registry/model.py +++ b/fastybird_fb_bus_connector/registry/model.py @@ -29,8 +29,8 @@ # Library dependencies from fastybird_devices_module.repositories.state import ( - IChannelPropertiesStatesRepository, - IDevicePropertiesStatesRepository, + ChannelPropertiesStatesRepository, + DevicePropertiesStatesRepository, ) from fastybird_metadata.devices_module import ConnectionState from fastybird_metadata.types import ButtonPayload, DataType, SwitchPayload @@ -455,12 +455,7 @@ def __next__(self) -> DeviceRecord: raise StopIteration -@inject( - bind={ - "device_property_state_repository": IDevicePropertiesStatesRepository, - "channel_property_state_repository": IChannelPropertiesStatesRepository, - } -) +@inject class RegistersRegistry: """ Registers registry @@ -475,16 +470,16 @@ class RegistersRegistry: __event_dispatcher: EventDispatcher - __device_property_state_repository: Optional[IDevicePropertiesStatesRepository] = None - __channel_property_state_repository: Optional[IChannelPropertiesStatesRepository] = None + __device_property_state_repository: DevicePropertiesStatesRepository + __channel_property_state_repository: ChannelPropertiesStatesRepository # ----------------------------------------------------------------------------- def __init__( self, event_dispatcher: EventDispatcher, - device_property_state_repository: Optional[IDevicePropertiesStatesRepository] = None, - channel_property_state_repository: Optional[IChannelPropertiesStatesRepository] = None, + device_property_state_repository: DevicePropertiesStatesRepository, + channel_property_state_repository: ChannelPropertiesStatesRepository, ) -> None: self.__items = {} @@ -596,13 +591,17 @@ def append_input_register( # pylint: disable=too-many-arguments register_data_type=register_data_type, ) - if existing_register is None and self.__channel_property_state_repository is not None: - stored_state = self.__channel_property_state_repository.get_by_id(property_id=register_id) + if existing_register is None: + try: + stored_state = self.__channel_property_state_repository.get_by_id(property_id=register_id) + + if stored_state is not None: + register.actual_value = stored_state.actual_value + register.expected_value = stored_state.expected_value + register.expected_pending = stored_state.pending - if stored_state is not None: - register.actual_value = stored_state.actual_value - register.expected_value = stored_state.expected_value - register.expected_pending = stored_state.pending + except NotImplementedError: + pass self.__items[register.id.__str__()] = register @@ -627,13 +626,17 @@ def append_output_register( # pylint: disable=too-many-arguments register_data_type=register_data_type, ) - if existing_register is None and self.__channel_property_state_repository is not None: - stored_state = self.__channel_property_state_repository.get_by_id(property_id=register_id) + if existing_register is None: + try: + stored_state = self.__channel_property_state_repository.get_by_id(property_id=register_id) - if stored_state is not None: - register.actual_value = stored_state.actual_value - register.expected_value = stored_state.expected_value - register.expected_pending = stored_state.pending + if stored_state is not None: + register.actual_value = stored_state.actual_value + register.expected_value = stored_state.expected_value + register.expected_pending = stored_state.pending + + except NotImplementedError: + pass self.__items[register.id.__str__()] = register @@ -664,13 +667,17 @@ def append_attribute_register( # pylint: disable=too-many-arguments register_queryable=register_queryable, ) - if existing_register is None and self.__device_property_state_repository is not None: - stored_state = self.__device_property_state_repository.get_by_id(property_id=register_id) + if existing_register is None: + try: + stored_state = self.__device_property_state_repository.get_by_id(property_id=register_id) + + if stored_state is not None: + register.actual_value = stored_state.actual_value + register.expected_value = stored_state.expected_value + register.expected_pending = stored_state.pending - if stored_state is not None: - register.actual_value = stored_state.actual_value - register.expected_value = stored_state.expected_value - register.expected_pending = stored_state.pending + except NotImplementedError: + pass self.__items[register.id.__str__()] = register diff --git a/fastybird_fb_bus_connector/types.py b/fastybird_fb_bus_connector/types.py index 7004482..f8ec6c0 100644 --- a/fastybird_fb_bus_connector/types.py +++ b/fastybird_fb_bus_connector/types.py @@ -22,7 +22,7 @@ from enum import Enum, unique # Library dependencies -from fastybird_metadata.devices_module import DevicePropertyName +from fastybird_metadata.devices_module import ConnectorPropertyName, DevicePropertyName from fastybird_metadata.enum import ExtendedEnum CONNECTOR_NAME: str = "fb-bus" @@ -31,6 +31,9 @@ MASTER_ADDRESS: int = 254 UNASSIGNED_ADDRESS: int = 255 +DEFAULT_SERIAL_INTERFACE: str = "/dev/ttyAMA0" +DEFAULT_BAUD_RATE: int = 38400 + @unique class Packet(Enum): @@ -401,6 +404,28 @@ def __hash__(self) -> int: return hash(self._name_) # pylint: disable=no-member +@unique +class ConnectorAttribute(ExtendedEnum, Enum): + """ + Known connector attribute name + + @package FastyBird:FbBusConnector! + @module types + + @author Adam Kadlec + """ + + ADDRESS: str = ConnectorPropertyName.ADDRESS.value + INTERFACE: str = ConnectorPropertyName.INTERFACE.value + BAUD_RATE: str = ConnectorPropertyName.BAUD_RATE.value + PROTOCOL: str = "protocol" + + # ----------------------------------------------------------------------------- + + def __hash__(self) -> int: + return hash(self._name_) # pylint: disable=no-member + + @unique class ConnectorAction(ExtendedEnum, Enum): """ diff --git a/src/Constants.php b/src/Constants.php deleted file mode 100644 index 31d31ca..0000000 --- a/src/Constants.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @package FastyBird:FbBusConnector! - * @subpackage common - * @since 0.19.0 - * - * @date 10.02.22 - */ - -namespace FastyBird\FbBusConnector; - -/** - * Service constants - * - * @package FastyBird:FbBusConnector! - * @subpackage common - * - * @author Adam Kadlec - */ -final class Constants -{ - - /** - * Connector property names - */ - public const PROPERTY_NAME_PROTOCOL = 'protocol'; - -} diff --git a/src/Entities/FbBusConnector.php b/src/Entities/FbBusConnector.php index 1fceb3c..69a5882 100644 --- a/src/Entities/FbBusConnector.php +++ b/src/Entities/FbBusConnector.php @@ -18,7 +18,6 @@ use Consistence\Doctrine\Enum\EnumAnnotation as Enum; use Doctrine\ORM\Mapping as ORM; use FastyBird\DevicesModule\Entities as DevicesModuleEntities; -use FastyBird\FbBusConnector\Constants; use FastyBird\FbBusConnector\Types; use FastyBird\Metadata\Types as MetadataTypes; @@ -51,7 +50,7 @@ public function getType(): string */ public function getAddress(): int { - $property = $this->findProperty(MetadataTypes\ConnectorPropertyNameType::NAME_ADDRESS); + $property = $this->findProperty(Types\ConnectorPropertyType::NAME_ADDRESS); if ( $property === null @@ -69,7 +68,7 @@ public function getAddress(): int */ public function getInterface(): string { - $property = $this->findProperty(MetadataTypes\ConnectorPropertyNameType::NAME_INTERFACE); + $property = $this->findProperty(Types\ConnectorPropertyType::NAME_INTERFACE); if ( $property === null @@ -87,7 +86,7 @@ public function getInterface(): string */ public function getBaudRate(): int { - $property = $this->findProperty(MetadataTypes\ConnectorPropertyNameType::NAME_BAUD_RATE); + $property = $this->findProperty(Types\ConnectorPropertyType::NAME_BAUD_RATE); if ( $property === null @@ -105,7 +104,7 @@ public function getBaudRate(): int */ public function getProtocol(): Types\ProtocolVersionType { - $property = $this->findProperty(Constants::PROPERTY_NAME_PROTOCOL); + $property = $this->findProperty(Types\ConnectorPropertyType::NAME_PROTOCOL); if ( $property === null diff --git a/src/Types/ConnectorPropertyType.php b/src/Types/ConnectorPropertyType.php new file mode 100644 index 0000000..b547499 --- /dev/null +++ b/src/Types/ConnectorPropertyType.php @@ -0,0 +1,48 @@ + + * @package FastyBird:FbBusConnector! + * @subpackage Types + * @since 0.19.0 + * + * @date 10.02.22 + */ + +namespace FastyBird\FbBusConnector\Types; + +use Consistence; +use FastyBird\Metadata\Types as MetadataTypes; + +/** + * Connector property name types + * + * @package FastyBird:FbBusConnector! + * @subpackage Types + * + * @author Adam Kadlec + */ +class ConnectorPropertyType extends Consistence\Enum\Enum +{ + + /** + * Define device states + */ + public const NAME_BAUD_RATE = MetadataTypes\ConnectorPropertyNameType::NAME_BAUD_RATE; + public const NAME_INTERFACE = MetadataTypes\ConnectorPropertyNameType::NAME_INTERFACE; + public const NAME_ADDRESS = MetadataTypes\ConnectorPropertyNameType::NAME_ADDRESS; + public const NAME_PROTOCOL = 'protocol'; + + /** + * @return string + */ + public function __toString(): string + { + return strval(self::getValue()); + } + +}