Skip to content

Commit

Permalink
Small bugfixes for connector
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jan 31, 2022
1 parent 02d0109 commit 2ffe395
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
2 changes: 1 addition & 1 deletion fastybird_devices_module/__init__.py
Expand Up @@ -18,4 +18,4 @@
Devices module
"""

__version__ = "0.26.0"
__version__ = "0.27.0"
55 changes: 45 additions & 10 deletions fastybird_devices_module/connectors/connector.py
Expand Up @@ -28,8 +28,10 @@

# Library libs
from fastybird_metadata.routing import RoutingKey
from fastybird_metadata.types import ControlAction, PropertyAction
from inflection import underscore
from kink import di
from sqlalchemy.orm import close_all_sessions

# Library dependencies
from fastybird_devices_module.connectors.queue import (
Expand Down Expand Up @@ -195,6 +197,7 @@ def write_control(
self,
control_item: Union[ConnectorControlEntity, DeviceControlEntity, ChannelControlEntity],
data: Optional[Dict],
action: Union[ControlAction, PropertyAction],
) -> None:
"""Write connector control action"""

Expand Down Expand Up @@ -277,7 +280,7 @@ def start(self) -> None:
"message": str(ex),
"code": type(ex).__name__,
},
}
},
)

raise TerminateConnectorException("Connector couldn't be started. An unexpected error occurred") from ex
Expand Down Expand Up @@ -315,7 +318,7 @@ def stop(self) -> None:
"message": str(ex),
"code": type(ex).__name__,
},
}
},
)

raise TerminateConnectorException("Connector couldn't be stopped. An unexpected error occurred") from ex
Expand All @@ -339,7 +342,7 @@ def handle(self) -> None:
"message": str(ex),
"code": type(ex).__name__,
},
}
},
)

raise TerminateConnectorException("An unexpected error occurred during connector handling process") from ex
Expand All @@ -365,7 +368,7 @@ def handle(self) -> None:
"message": str(ex),
"code": type(ex).__name__,
},
}
},
)

raise TerminateConnectorException("An unexpected error occurred during processing queue item") from ex
Expand Down Expand Up @@ -449,7 +452,7 @@ def __write_property_command(self, item: ConsumePropertyActionMessageQueueItem)
# -----------------------------------------------------------------------------

def __write_control_command(self, item: ConsumeControlActionMessageQueueItem) -> None:
if item.routing_key == RoutingKey.DEVICE_ACTION:
if item.routing_key == RoutingKey.DEVICE_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
connector_control = self.__connectors_control_repository.get_by_name(
connector_id=uuid.UUID(item.data.get("connector"), version=4),
Expand All @@ -462,9 +465,13 @@ def __write_control_command(self, item: ConsumeControlActionMessageQueueItem) ->
if connector_control is None:
return

self.__connector.write_control(control_item=connector_control, data=item.data)
self.__connector.write_control(
control_item=connector_control,
data=item.data,
action=ControlAction(item.data.get("name")),
)

if item.routing_key == RoutingKey.CHANNEL_ACTION:
if item.routing_key == RoutingKey.CHANNEL_ACTION and PropertyAction.has_value(str(item.data.get("name"))):
try:
device_control = self.__devices_control_repository.get_by_name(
device_id=uuid.UUID(item.data.get("device"), version=4), control_name=str(item.data.get("name"))
Expand All @@ -476,9 +483,13 @@ def __write_control_command(self, item: ConsumeControlActionMessageQueueItem) ->
if device_control is None:
return

self.__connector.write_control(control_item=device_control, data=item.data)
self.__connector.write_control(
control_item=device_control,
data=item.data,
action=PropertyAction(item.data.get("name")),
)

if item.routing_key == RoutingKey.CONNECTOR_ACTION:
if item.routing_key == RoutingKey.CONNECTOR_ACTION and PropertyAction.has_value(str(item.data.get("name"))):
try:
channel_control = self.__channels_control_repository.get_by_name(
channel_id=uuid.UUID(item.data.get("channel"), version=4), control_name=str(item.data.get("name"))
Expand All @@ -490,7 +501,11 @@ def __write_control_command(self, item: ConsumeControlActionMessageQueueItem) ->
if channel_control is None:
return

self.__connector.write_control(control_item=channel_control, data=item.data)
self.__connector.write_control(
control_item=channel_control,
data=item.data,
action=PropertyAction(item.data.get("name")),
)

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

Expand All @@ -499,6 +514,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
item: ConsumeEntityMessageQueueItem,
) -> None:
if item.routing_key == RoutingKey.CONNECTORS_ENTITY_UPDATED:
close_all_sessions()

try:
connector_entity = self.__connectors_repository.get_by_id(
connector_id=uuid.UUID(item.data.get("connector"), version=4),
Expand All @@ -520,6 +537,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
)

if item.routing_key == RoutingKey.CONNECTORS_ENTITY_DELETED:
close_all_sessions()

try:
if not di["connector"].id.__eq__(uuid.UUID(item.data.get("connector"), version=4)):
return
Expand All @@ -530,6 +549,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
return

if item.routing_key in (RoutingKey.DEVICES_ENTITY_CREATED, RoutingKey.DEVICES_ENTITY_UPDATED):
close_all_sessions()

try:
device_entity = self.__devices_repository.get_by_id(
device_id=uuid.UUID(item.data.get("device"), version=4),
Expand All @@ -546,13 +567,17 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
self.__connector.initialize_device(device=device_entity)

if item.routing_key == RoutingKey.DEVICES_ENTITY_DELETED:
close_all_sessions()

try:
self.__connector.remove_device(uuid.UUID(item.data.get("device"), version=4))

except ValueError:
return

if item.routing_key in (RoutingKey.DEVICES_PROPERTY_ENTITY_CREATED, RoutingKey.DEVICES_PROPERTY_ENTITY_UPDATED):
close_all_sessions()

try:
device_property_entity = self.__devices_properties_repository.get_by_id(
property_id=uuid.UUID(item.data.get("property"), version=4),
Expand All @@ -569,6 +594,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
self.__connector.initialize_device_property(device_property=device_property_entity)

if item.routing_key == RoutingKey.DEVICES_PROPERTY_ENTITY_DELETED:
close_all_sessions()

try:
self.__connector.remove_device_channel_property(
property_id=uuid.UUID(item.data.get("property"), version=4),
Expand All @@ -578,6 +605,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
return

if item.routing_key in (RoutingKey.CHANNELS_ENTITY_CREATED, RoutingKey.CHANNELS_ENTITY_UPDATED):
close_all_sessions()

try:
channel_entity = self.__channels_repository.get_by_id(
channel_id=uuid.UUID(item.data.get("channel"), version=4),
Expand All @@ -594,6 +623,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
self.__connector.initialize_device_channel(channel=channel_entity)

if item.routing_key == RoutingKey.CHANNELS_ENTITY_DELETED:
close_all_sessions()

try:
self.__connector.remove_device_channel(channel_id=uuid.UUID(item.data.get("channel"), version=4))

Expand All @@ -604,6 +635,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
RoutingKey.CHANNELS_PROPERTY_ENTITY_CREATED,
RoutingKey.CHANNELS_PROPERTY_ENTITY_UPDATED,
):
close_all_sessions()

try:
channel_property_entity = self.__channels_properties_repository.get_by_id(
property_id=uuid.UUID(item.data.get("property"), version=4),
Expand All @@ -620,6 +653,8 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
self.__connector.initialize_device_channel_property(channel_property=channel_property_entity)

if item.routing_key == RoutingKey.CHANNELS_PROPERTY_ENTITY_DELETED:
close_all_sessions()

try:
self.__connector.remove_device_channel_property(
property_id=uuid.UUID(item.data.get("property"), version=4),
Expand Down
1 change: 0 additions & 1 deletion fastybird_devices_module/connectors/consumer.py
Expand Up @@ -61,7 +61,6 @@ class ConnectorConsumer(IConsumer): # pylint: disable=too-few-public-methods
RoutingKey.CONNECTOR_ACTION,
RoutingKey.DEVICE_ACTION,
RoutingKey.CHANNEL_ACTION,
RoutingKey.TRIGGER_ACTION,
]

__queue: ConnectorQueue
Expand Down
12 changes: 6 additions & 6 deletions fastybird_devices_module/entities/base.py
Expand Up @@ -58,7 +58,7 @@ class EntityCreatedMixin: # pylint: disable=too-few-public-methods
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""

_created_at: Optional[datetime.datetime] = Column( # type: ignore[assignment]
col_created_at: Optional[datetime.datetime] = Column( # type: ignore[assignment]
DateTime, name="created_at", nullable=True, default=None
)

Expand All @@ -67,14 +67,14 @@ class EntityCreatedMixin: # pylint: disable=too-few-public-methods
@property
def created_at(self) -> Optional[datetime.datetime]:
"""Entity created timestamp"""
return self._created_at
return self.col_created_at

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

@created_at.setter
def created_at(self, created_at: Optional[datetime.datetime]) -> None:
"""Entity created timestamp setter"""
self._created_at = created_at
self.col_created_at = created_at


class EntityUpdatedMixin: # pylint: disable=too-few-public-methods
Expand All @@ -87,7 +87,7 @@ class EntityUpdatedMixin: # pylint: disable=too-few-public-methods
@author Adam Kadlec <adam.kadlec@fastybird.com>
"""

_updated_at: Optional[datetime.datetime] = Column( # type: ignore[assignment]
col_updated_at: Optional[datetime.datetime] = Column( # type: ignore[assignment]
DateTime, name="updated_at", nullable=True, default=None
)

Expand All @@ -96,11 +96,11 @@ class EntityUpdatedMixin: # pylint: disable=too-few-public-methods
@property
def updated_at(self) -> Optional[datetime.datetime]:
"""Entity updated timestamp"""
return self._updated_at
return self.col_updated_at

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

@updated_at.setter
def updated_at(self, updated_at: Optional[datetime.datetime]) -> None:
"""Entity updated timestamp setter"""
self._updated_at = updated_at
self.col_updated_at = updated_at
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@fastybird/devices-module",
"version": "0.26.0",
"version": "0.27.0",
"description": "Devices module data model plugin",
"keywords": [
"devices",
Expand Down

0 comments on commit 2ffe395

Please sign in to comment.