Skip to content

Commit

Permalink
Use constants
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Feb 10, 2022
1 parent 35b3f4c commit 94470a3
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 80 deletions.
2 changes: 1 addition & 1 deletion fastybird_fb_bus_connector/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 9 additions & 7 deletions fastybird_fb_bus_connector/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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,
)

Expand All @@ -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,
)

Expand All @@ -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

Expand All @@ -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,
)

Expand All @@ -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

Expand All @@ -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,
)

Expand Down
4 changes: 2 additions & 2 deletions fastybird_fb_bus_connector/events/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Python base dependencies
import logging
import uuid
from typing import Optional, Union
from typing import Union

# Library dependencies
import inflection
Expand Down Expand Up @@ -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:
Expand Down
67 changes: 37 additions & 30 deletions fastybird_fb_bus_connector/registry/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {}

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
27 changes: 26 additions & 1 deletion fastybird_fb_bus_connector/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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):
Expand Down Expand Up @@ -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 <adam.kadlec@fastybird.com>
"""

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):
"""
Expand Down
34 changes: 0 additions & 34 deletions src/Constants.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/Entities/FbBusConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/Types/ConnectorPropertyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

/**
* ConnectorPropertyType.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @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 <adam.kadlec@fastybird.com>
*/
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());
}

}

0 comments on commit 94470a3

Please sign in to comment.