Skip to content

Commit

Permalink
Added notification mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Mar 30, 2022
1 parent 2b7df4f commit 4bc14af
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion fastybird_devices_module/__init__.py
Expand Up @@ -18,4 +18,4 @@
Devices module
"""

__version__ = "0.44.0"
__version__ = "0.45.0"
46 changes: 40 additions & 6 deletions fastybird_devices_module/connectors/connector.py
Expand Up @@ -85,7 +85,7 @@
)


class IConnector(ABC):
class IConnector(ABC): # pylint: disable=too-many-public-methods
"""
Connector interface
Expand Down Expand Up @@ -134,6 +134,12 @@ def initialize_device_property(self, device: DeviceEntity, device_property: Devi

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

@abstractmethod
def notify_device_property(self, device: DeviceEntity, device_property: DevicePropertyEntity) -> None:
"""Notify device property was reported to connector"""

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

@abstractmethod
def remove_device_property(self, device: DeviceEntity, property_id: uuid.UUID) -> None:
"""Remove device from connector"""
Expand Down Expand Up @@ -174,6 +180,16 @@ def initialize_device_channel_property(

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

@abstractmethod
def notify_device_channel_property(
self,
channel: ChannelEntity,
channel_property: ChannelPropertyEntity,
) -> None:
"""Notify device channel property was reported to connector"""

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

@abstractmethod
def remove_device_channel_property(self, channel: ChannelEntity, property_id: uuid.UUID) -> None:
"""Remove device channel property from connector"""
Expand Down Expand Up @@ -660,7 +676,11 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
except ValueError:
return

if item.routing_key in (RoutingKey.DEVICE_PROPERTY_ENTITY_CREATED, RoutingKey.DEVICE_PROPERTY_ENTITY_UPDATED):
if item.routing_key in (
RoutingKey.DEVICE_PROPERTY_ENTITY_CREATED,
RoutingKey.DEVICE_PROPERTY_ENTITY_UPDATED,
RoutingKey.DEVICE_PROPERTY_ENTITY_REPORTED,
):
close_all_sessions()

try:
Expand All @@ -687,7 +707,14 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-

return

self.__connector.initialize_device_property(device=device_entity, device_property=device_property_entity)
if item.routing_key == RoutingKey.DEVICE_PROPERTY_ENTITY_REPORTED:
self.__connector.notify_device_property(device=device_entity, device_property=device_property_entity)

else:
self.__connector.initialize_device_property(
device=device_entity,
device_property=device_property_entity,
)

if item.routing_key == RoutingKey.DEVICE_PROPERTY_ENTITY_DELETED:
close_all_sessions()
Expand Down Expand Up @@ -769,6 +796,7 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
if item.routing_key in (
RoutingKey.CHANNEL_PROPERTY_ENTITY_CREATED,
RoutingKey.CHANNEL_PROPERTY_ENTITY_UPDATED,
RoutingKey.CHANNEL_PROPERTY_ENTITY_REPORTED,
):
close_all_sessions()

Expand Down Expand Up @@ -796,9 +824,15 @@ def __handle_entity_event( # pylint: disable=too-many-branches,too-many-return-
if channel_entity is None:
return

self.__connector.initialize_device_channel_property(
channel=channel_entity, channel_property=channel_property_entity
)
if item.routing_key == RoutingKey.CHANNEL_PROPERTY_ENTITY_REPORTED:
self.__connector.notify_device_channel_property(
channel=channel_entity, channel_property=channel_property_entity
)

else:
self.__connector.initialize_device_channel_property(
channel=channel_entity, channel_property=channel_property_entity
)

if item.routing_key == RoutingKey.CHANNEL_PROPERTY_ENTITY_DELETED:
close_all_sessions()
Expand Down
15 changes: 3 additions & 12 deletions fastybird_devices_module/managers/state.py
Expand Up @@ -203,9 +203,6 @@ def create(
if self.__manager is None:
raise NotImplementedError("Connector properties states manager is not implemented")

if connector_property.parent is not None:
raise AttributeError("Child property can't have state")

created_state = self.__manager.create(connector_property=connector_property, data=data)

self.__publish_entity(
Expand All @@ -227,9 +224,6 @@ def update(
if self.__manager is None:
raise NotImplementedError("Connector properties states manager is not implemented")

if connector_property.parent is not None:
raise AttributeError("Child property can't have state")

updated_state = self.__manager.update(connector_property=connector_property, state=state, data=data)

self.__publish_entity(
Expand All @@ -250,9 +244,6 @@ def delete(
if self.__manager is None:
raise NotImplementedError("Connector properties states manager is not implemented")

if connector_property.parent is not None:
raise AttributeError("Child property can't have state")

result = self.__manager.delete(connector_property=connector_property, state=state)

if result is True:
Expand Down Expand Up @@ -294,7 +285,7 @@ def __publish_entity(

self.__publisher.publish(
source=connector_property.source,
routing_key=RoutingKey.CONNECTOR_PROPERTY_ENTITY_UPDATED,
routing_key=RoutingKey.CONNECTOR_PROPERTY_ENTITY_REPORTED,
data={
**connector_property.to_dict(),
**{
Expand Down Expand Up @@ -454,7 +445,7 @@ def __publish_entity(

self.__publisher.publish(
source=device_property.source,
routing_key=RoutingKey.DEVICE_PROPERTY_ENTITY_UPDATED,
routing_key=RoutingKey.DEVICE_PROPERTY_ENTITY_REPORTED,
data={
**device_property.to_dict(),
**{
Expand Down Expand Up @@ -617,7 +608,7 @@ def __publish_entity(

self.__publisher.publish(
source=channel_property.source,
routing_key=RoutingKey.CHANNEL_PROPERTY_ENTITY_UPDATED,
routing_key=RoutingKey.CHANNEL_PROPERTY_ENTITY_REPORTED,
data={
**channel_property.to_dict(),
**{
Expand Down
3 changes: 0 additions & 3 deletions fastybird_devices_module/repositories/state.py
Expand Up @@ -124,9 +124,6 @@ def get_by_id(self, property_id: uuid.UUID) -> Optional[IConnectorPropertyState]
if connector_property is None:
raise AttributeError("Connector property was not found in registry")

if connector_property.parent is not None:
raise AttributeError("Child property can't have state")

return self.__repository.get_by_id(property_id=property_id)


Expand Down

0 comments on commit 4bc14af

Please sign in to comment.