Skip to content

Commit

Permalink
Refactoring registers storing
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Mar 27, 2022
1 parent 096fe37 commit ebe77f4
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 300 deletions.
2 changes: 1 addition & 1 deletion fastybird_fb_bus_connector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from .bootstrap import create_connector

__version__ = "0.23.0"
__version__ = "0.24.0"

__all__ = ["connector", "bootstrap"]

Expand Down
36 changes: 14 additions & 22 deletions fastybird_fb_bus_connector/api/v1builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,16 @@ def build_write_single_register_value( # pylint: disable=too-many-arguments
output_content.append(register_address >> 8)
output_content.append(register_address & 0xFF)

if (
register_data_type
in (
DataType.CHAR,
DataType.UCHAR,
DataType.SHORT,
DataType.USHORT,
DataType.INT,
DataType.UINT,
DataType.FLOAT,
DataType.BOOLEAN,
)
and isinstance(write_value, (int, float, bool))
):
if register_data_type in (
DataType.CHAR,
DataType.UCHAR,
DataType.SHORT,
DataType.USHORT,
DataType.INT,
DataType.UINT,
DataType.FLOAT,
DataType.BOOLEAN,
) and isinstance(write_value, (int, float, bool)):
transformed_value = ValueTransformHelpers.transform_to_bytes(
data_type=register_data_type,
value=write_value,
Expand All @@ -173,14 +169,10 @@ def build_write_single_register_value( # pylint: disable=too-many-arguments
for value in transformed_value:
output_content.append(value)

elif (
register_data_type
in (
DataType.BUTTON,
DataType.SWITCH,
)
and isinstance(write_value, (SwitchPayload, ButtonPayload))
):
elif register_data_type in (
DataType.BUTTON,
DataType.SWITCH,
) and isinstance(write_value, (SwitchPayload, ButtonPayload)):
transformed_value = ValueTransformHelpers.transform_to_bytes(
data_type=register_data_type,
value=write_value,
Expand Down
2 changes: 1 addition & 1 deletion fastybird_fb_bus_connector/clients/apiv1.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ApiV1Client(IClient): # pylint: disable=too-few-public-methods, too-many-
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""

__discovery_enabled: bool = False
__discovery_enabled: bool = True

__devices_registry: DevicesRegistry
__registers_registry: RegistersRegistry
Expand Down
152 changes: 68 additions & 84 deletions fastybird_fb_bus_connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
ChannelDynamicPropertyEntity,
ChannelEntity,
ChannelPropertyEntity,
ChannelStaticPropertyEntity,
)
from fastybird_devices_module.entities.connector import ConnectorControlEntity
from fastybird_devices_module.entities.device import (
Expand Down Expand Up @@ -65,12 +64,9 @@
from fastybird_fb_bus_connector.registry.model import DevicesRegistry, RegistersRegistry

# Library libs
from fastybird_fb_bus_connector.registry.records import OutputRegisterRecord
from fastybird_fb_bus_connector.transporters.transporter import ITransporter
from fastybird_fb_bus_connector.types import (
ConnectorAction,
RegisterAttribute,
RegisterName,
)
from fastybird_fb_bus_connector.types import ConnectorAction, RegisterName, RegisterType


@inject(alias=IConnector)
Expand Down Expand Up @@ -171,14 +167,12 @@ def initialize_device(self, device: FbBusDeviceEntity) -> None:
firmware_version=device.firmware_version,
)

# Channel & channel properties have to be initialized first!
for channel in device.channels:
self.initialize_device_channel(device=device, channel=channel)

# Device properties have to be initialized after channel!
for device_property in device.properties:
self.initialize_device_property(device=device, device_property=device_property)

for channel in device.channels:
self.initialize_device_channel(device=device, channel=channel)

if device.enabled:
self.__devices_registry.enable(device=device_record)

Expand All @@ -198,88 +192,93 @@ def reset_devices(self) -> None:

def initialize_device_property(self, device: FbBusDeviceEntity, device_property: DevicePropertyEntity) -> None:
"""Initialize device property aka attribute register in connector registry"""
match = re.compile("(?P<name>[a-zA-Z_]+)_(?P<address>[0-9]+)")

parsed_property_identifier = match.fullmatch(device_property.identifier)

if parsed_property_identifier is not None:
self.__registers_registry.append_attribute_register(
device_id=device.id,
register_id=device_property.id,
register_address=int(parsed_property_identifier.group("address")),
register_data_type=device_property.data_type,
register_name=str(parsed_property_identifier.group("name")),
register_settable=device_property.settable,
register_queryable=device_property.queryable,
)

# -----------------------------------------------------------------------------

def remove_device_property(self, device: FbBusDeviceEntity, property_id: uuid.UUID) -> None:
"""Remove device property from connector registry"""
self.__registers_registry.remove(register_id=property_id)

# -----------------------------------------------------------------------------

def reset_devices_properties(self, device: FbBusDeviceEntity) -> None:
"""Reset devices properties registry to initial state"""
self.__registers_registry.reset(device_id=device.id, registers_type=RegisterType.ATTRIBUTE)

# -----------------------------------------------------------------------------

def initialize_device_channel(self, device: FbBusDeviceEntity, channel: ChannelEntity) -> None:
"""Initialize device channel aka registers group in connector registry"""
register_id: Optional[uuid.UUID] = None
register_address: Optional[int] = None
register_data_type: Optional[DataType] = None
register_queryable: bool = False
register_settable: bool = False

for channel_property in channel.properties:
self.initialize_device_channel_property(channel=channel, channel_property=channel_property)

# -----------------------------------------------------------------------------

def remove_device_channel(self, device: FbBusDeviceEntity, channel_id: uuid.UUID) -> None:
"""Remove device channel from connector registry"""
io_registers = self.__registers_registry.get_all_for_device(
device_id=device.id,
register_type=[RegisterType.OUTPUT, RegisterType.INPUT],
)

for register in io_registers:
if (
channel_property.identifier == RegisterAttribute.ADDRESS.value
and isinstance(channel_property, ChannelStaticPropertyEntity)
and isinstance(channel_property.value, int)
isinstance(register, OutputRegisterRecord)
and register.channel_id is not None
and register.channel_id.__eq__(channel_id)
):
register_address = channel_property.value
self.__registers_registry.remove(register_id=register.id)

if channel_property.identifier == RegisterAttribute.VALUE.value and isinstance(
channel_property, ChannelDynamicPropertyEntity
):
register_id = channel_property.id
register_data_type = channel_property.data_type
register_queryable = channel_property.queryable
register_settable = channel_property.settable

if register_id is None or register_address is None or register_data_type is None:
self.__logger.warning(
"Channel does not have expected properties and can't be mapped to register",
extra={
"device": {
"id": channel.device.id.__str__(),
},
"channel": {
"id": channel.device.id.__str__(),
},
},
)
# -----------------------------------------------------------------------------

return
def reset_devices_channels(self, device: FbBusDeviceEntity) -> None:
"""Reset devices channels registry to initial state"""
self.__registers_registry.reset(device_id=device.id, registers_type=RegisterType.OUTPUT)
self.__registers_registry.reset(device_id=device.id, registers_type=RegisterType.INPUT)

match = re.compile("(?P<name>[a-zA-Z_]+)_(?P<address>[0-9]+)")
# -----------------------------------------------------------------------------

parsed_channel_identifier = match.fullmatch(channel.identifier)
def initialize_device_channel_property(
self,
channel: ChannelEntity,
channel_property: ChannelPropertyEntity,
) -> None:
"""Initialize device channel property aka input or output register in connector registry"""
match = re.compile("(?P<name>[a-zA-Z_]+)_(?P<address>[0-9]+)")

if parsed_channel_identifier is None:
self.__registers_registry.append_attribute_register(
device_id=channel.device.id,
register_id=register_id,
register_address=register_address,
register_data_type=register_data_type,
register_name=channel.identifier,
register_settable=register_settable,
register_queryable=register_queryable,
)
parsed_property_identifier = match.fullmatch(channel_property.identifier)

else:
if parsed_channel_identifier.group("name") == RegisterName.OUTPUT.value:
if parsed_property_identifier is not None:
if channel.identifier == RegisterName.OUTPUT.value:
self.__registers_registry.append_output_register(
device_id=channel.device.id,
register_id=register_id,
register_address=register_address,
register_data_type=register_data_type,
register_id=channel_property.id,
register_address=int(parsed_property_identifier.group("address")),
register_data_type=channel_property.data_type,
channel_id=channel.id,
)

elif parsed_channel_identifier.group("name") == RegisterName.INPUT.value:
elif channel.identifier == RegisterName.INPUT.value:
self.__registers_registry.append_input_register(
device_id=channel.device.id,
register_id=register_id,
register_address=register_address,
register_data_type=register_data_type,
register_id=channel_property.id,
register_address=int(parsed_property_identifier.group("address")),
register_data_type=channel_property.data_type,
channel_id=channel.id,
)

else:
Expand All @@ -297,34 +296,19 @@ def initialize_device_channel(self, device: FbBusDeviceEntity, channel: ChannelE

# -----------------------------------------------------------------------------

def remove_device_channel(self, device: FbBusDeviceEntity, channel_id: uuid.UUID) -> None:
"""Remove device channel from connector registry"""
self.__registers_registry.remove(register_id=channel_id)

# -----------------------------------------------------------------------------

def reset_devices_channels(self, device: FbBusDeviceEntity) -> None:
"""Reset devices channels registry to initial state"""
self.__registers_registry.reset(device_id=device.id)

# -----------------------------------------------------------------------------

def initialize_device_channel_property(
self,
channel: ChannelEntity,
channel_property: ChannelPropertyEntity,
) -> None:
"""Initialize device channel property aka input or output register in connector registry"""

# -----------------------------------------------------------------------------

def remove_device_channel_property(self, channel: ChannelEntity, property_id: uuid.UUID) -> None:
"""Remove device channel property from connector registry"""
self.__registers_registry.remove(register_id=property_id)

# -----------------------------------------------------------------------------

def reset_devices_channels_properties(self, channel: ChannelEntity) -> None:
"""Reset devices channels properties registry to initial state"""
if channel.identifier == RegisterName.OUTPUT.value:
self.__registers_registry.reset(device_id=channel.device.id, registers_type=RegisterType.OUTPUT)

elif channel.identifier == RegisterName.INPUT.value:
self.__registers_registry.reset(device_id=channel.device.id, registers_type=RegisterType.INPUT)

# -----------------------------------------------------------------------------

Expand Down
Loading

0 comments on commit ebe77f4

Please sign in to comment.