From aec36284cf765272c0b7fe1343b0ced0bd0cff94 Mon Sep 17 00:00:00 2001 From: Adam Kadlec Date: Thu, 7 Apr 2022 22:49:52 +0200 Subject: [PATCH] Make exchange publis optional --- fastybird_devices_module/__init__.py | 2 +- fastybird_devices_module/managers/state.py | 48 ++++++++++++------- package.json | 2 +- .../States/ChannelPropertiesManager.php | 30 ++++++++---- .../States/ConnectorPropertiesManager.php | 26 +++++++--- src/Models/States/DevicePropertiesManager.php | 30 ++++++++---- 6 files changed, 93 insertions(+), 45 deletions(-) diff --git a/fastybird_devices_module/__init__.py b/fastybird_devices_module/__init__.py index ac1d2170..4d845e63 100644 --- a/fastybird_devices_module/__init__.py +++ b/fastybird_devices_module/__init__.py @@ -18,4 +18,4 @@ Devices module """ -__version__ = "0.53.0" +__version__ = "0.54.0" diff --git a/fastybird_devices_module/managers/state.py b/fastybird_devices_module/managers/state.py index ae439e84..0aa6af63 100644 --- a/fastybird_devices_module/managers/state.py +++ b/fastybird_devices_module/managers/state.py @@ -198,6 +198,7 @@ def create( self, connector_property: ConnectorPropertyEntity, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IConnectorPropertyState: """Create new connector property state record""" if self.__manager is None: @@ -205,10 +206,11 @@ def create( created_state = self.__manager.create(connector_property=connector_property, data=data) - self.__publish_entity( - connector_property=connector_property, - state=created_state, - ) + if publish_state: + self.__publish_entity( + connector_property=connector_property, + state=created_state, + ) return created_state @@ -219,6 +221,7 @@ def update( connector_property: ConnectorPropertyEntity, state: IConnectorPropertyState, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IConnectorPropertyState: """Update existing connector property state record""" if self.__manager is None: @@ -228,7 +231,7 @@ def update( updated_state = self.__manager.update(connector_property=connector_property, state=state, data=data) - if stored_data != updated_state.to_dict(): + if stored_data != updated_state.to_dict() and publish_state: self.__publish_entity( connector_property=connector_property, state=updated_state, @@ -242,6 +245,7 @@ def delete( self, connector_property: ConnectorPropertyEntity, state: IConnectorPropertyState, + publish_state: bool = True, ) -> bool: """Delete existing connector property state""" if self.__manager is None: @@ -249,7 +253,7 @@ def delete( result = self.__manager.delete(connector_property=connector_property, state=state) - if result is True: + if result is True and publish_state: self.__publish_entity( connector_property=connector_property, state=None, @@ -343,6 +347,7 @@ def create( self, device_property: DevicePropertyEntity, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IDevicePropertyState: """Create new device property state record""" if self.__manager is None: @@ -353,10 +358,11 @@ def create( created_state = self.__manager.create(device_property=device_property, data=data) - self.__publish_entity( - device_property=device_property, - state=created_state, - ) + if publish_state: + self.__publish_entity( + device_property=device_property, + state=created_state, + ) return created_state @@ -367,6 +373,7 @@ def update( device_property: DevicePropertyEntity, state: IDevicePropertyState, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IDevicePropertyState: """Update existing device property state record""" if self.__manager is None: @@ -379,7 +386,7 @@ def update( updated_state = self.__manager.update(device_property=device_property, state=state, data=data) - if stored_data != updated_state.to_dict(): + if stored_data != updated_state.to_dict() and publish_state: self.__publish_entity( device_property=device_property, state=updated_state, @@ -399,6 +406,7 @@ def delete( self, device_property: DevicePropertyEntity, state: IDevicePropertyState, + publish_state: bool = True, ) -> bool: """Delete existing device property state""" if self.__manager is None: @@ -409,7 +417,7 @@ def delete( result = self.__manager.delete(device_property=device_property, state=state) - if result is True: + if result is True and publish_state: self.__publish_entity( device_property=device_property, state=None, @@ -512,6 +520,7 @@ def create( self, channel_property: ChannelPropertyEntity, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IChannelPropertyState: """Create new channel property state record""" if self.__manager is None: @@ -522,10 +531,11 @@ def create( created_state = self.__manager.create(channel_property=channel_property, data=data) - self.__publish_entity( - channel_property=channel_property, - state=created_state, - ) + if publish_state: + self.__publish_entity( + channel_property=channel_property, + state=created_state, + ) return created_state @@ -536,6 +546,7 @@ def update( channel_property: ChannelPropertyEntity, state: IChannelPropertyState, data: Dict[str, Union[str, int, float, bool, datetime, ButtonPayload, SwitchPayload, None]], + publish_state: bool = True, ) -> IChannelPropertyState: """Update existing channel property state record""" if self.__manager is None: @@ -548,7 +559,7 @@ def update( updated_state = self.__manager.update(channel_property=channel_property, state=state, data=data) - if stored_data != updated_state.to_dict(): + if stored_data != updated_state.to_dict() and publish_state: self.__publish_entity( channel_property=channel_property, state=updated_state, @@ -568,6 +579,7 @@ def delete( self, channel_property: ChannelPropertyEntity, state: IChannelPropertyState, + publish_state: bool = True, ) -> bool: """Delete existing channel property state""" if self.__manager is None: @@ -578,7 +590,7 @@ def delete( result = self.__manager.delete(channel_property=channel_property, state=state) - if result is True: + if result is True and publish_state: self.__publish_entity( channel_property=channel_property, state=None, diff --git a/package.json b/package.json index 4e08e36c..421f7e46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fastybird/devices-module", - "version": "0.52.0", + "version": "0.54.0", "description": "Devices module data model plugin", "keywords": [ "devices", diff --git a/src/Models/States/ChannelPropertiesManager.php b/src/Models/States/ChannelPropertiesManager.php index 8b162457..01be22c8 100644 --- a/src/Models/States/ChannelPropertiesManager.php +++ b/src/Models/States/ChannelPropertiesManager.php @@ -55,12 +55,14 @@ public function __construct( /** * @param Entities\Channels\Properties\IProperty $property * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IChannelProperty */ public function create( Entities\Channels\Properties\IProperty $property, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IChannelProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Channel properties state manager is not registered'); @@ -73,7 +75,9 @@ public function create( /** @var States\IChannelProperty $createdState */ $createdState = $this->manager->create($property, $values); - $this->publishEntity($property, $createdState); + if ($publishState) { + $this->publishEntity($property, $createdState); + } return $createdState; } @@ -82,13 +86,15 @@ public function create( * @param Entities\Channels\Properties\IProperty $property * @param States\IChannelProperty $state * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IChannelProperty */ public function update( Entities\Channels\Properties\IProperty $property, States\IChannelProperty $state, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IChannelProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Channel properties state manager is not registered'); @@ -98,13 +104,17 @@ public function update( throw new Exceptions\InvalidStateException('Child property can\'t have state'); } + $storedState = $state->toArray(); + /** @var States\IChannelProperty $updatedState */ $updatedState = $this->manager->update($property, $state, $values); - $this->publishEntity($property, $updatedState); + if ($storedState !== $updatedState->toArray() && $publishState) { + $this->publishEntity($property, $updatedState); - foreach ($property->getChildren() as $child) { - $this->publishEntity($child, $updatedState); + foreach ($property->getChildren() as $child) { + $this->publishEntity($child, $updatedState); + } } return $updatedState; @@ -113,12 +123,14 @@ public function update( /** * @param Entities\Channels\Properties\IProperty $property * @param States\IChannelProperty $state + * @param bool $publishState * * @return bool */ public function delete( Entities\Channels\Properties\IProperty $property, - States\IChannelProperty $state + States\IChannelProperty $state, + bool $publishState = true ): bool { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Channel properties state manager is not registered'); @@ -130,7 +142,7 @@ public function delete( $result = $this->manager->delete($property, $state); - if ($result) { + if ($result && $publishState) { $this->publishEntity($property, null); foreach ($property->getChildren() as $child) { @@ -154,7 +166,7 @@ private function publishEntity( $this->publisher->publish( $property->getSource(), - MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_PROPERTY_ENTITY_UPDATED), + MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_PROPERTY_ENTITY_REPORTED), Utils\ArrayHash::from(array_merge($property->toArray(), [ 'actual_value' => is_scalar($actualValue) || $actualValue === null ? $actualValue : strval($actualValue), 'expected_value' => is_scalar($expectedValue) || $expectedValue === null ? $expectedValue : strval($expectedValue), diff --git a/src/Models/States/ConnectorPropertiesManager.php b/src/Models/States/ConnectorPropertiesManager.php index 8f7f710d..d9af0732 100644 --- a/src/Models/States/ConnectorPropertiesManager.php +++ b/src/Models/States/ConnectorPropertiesManager.php @@ -55,12 +55,14 @@ public function __construct( /** * @param Entities\Connectors\Properties\IProperty $property * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IConnectorProperty */ public function create( Entities\Connectors\Properties\IProperty $property, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IConnectorProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Connector properties state manager is not registered'); @@ -69,7 +71,9 @@ public function create( /** @var States\IConnectorProperty $createdState */ $createdState = $this->manager->create($property, $values); - $this->publishEntity($property, $createdState); + if ($publishState) { + $this->publishEntity($property, $createdState); + } return $createdState; } @@ -78,22 +82,28 @@ public function create( * @param Entities\Connectors\Properties\IProperty $property * @param States\IConnectorProperty $state * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IConnectorProperty */ public function update( Entities\Connectors\Properties\IProperty $property, States\IConnectorProperty $state, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IConnectorProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Connector properties state manager is not registered'); } + $storedState = $state->toArray(); + /** @var States\IConnectorProperty $updatedState */ $updatedState = $this->manager->update($property, $state, $values); - $this->publishEntity($property, $updatedState); + if ($storedState !== $updatedState->toArray() && $publishState) { + $this->publishEntity($property, $updatedState); + } return $updatedState; } @@ -101,12 +111,14 @@ public function update( /** * @param Entities\Connectors\Properties\IProperty $property * @param States\IConnectorProperty $state + * @param bool $publishState * * @return bool */ public function delete( Entities\Connectors\Properties\IProperty $property, - States\IConnectorProperty $state + States\IConnectorProperty $state, + bool $publishState = true ): bool { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Connector properties state manager is not registered'); @@ -114,7 +126,7 @@ public function delete( $result = $this->manager->delete($property, $state); - if ($result) { + if ($result && $publishState) { $this->publishEntity($property, null); } @@ -134,7 +146,7 @@ private function publishEntity( $this->publisher->publish( $property->getSource(), - MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_PROPERTY_ENTITY_UPDATED), + MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_PROPERTY_ENTITY_REPORTED), Utils\ArrayHash::from(array_merge($property->toArray(), [ 'actual_value' => is_scalar($actualValue) || $actualValue === null ? $actualValue : strval($actualValue), 'expected_value' => is_scalar($expectedValue) || $expectedValue === null ? $expectedValue : strval($expectedValue), diff --git a/src/Models/States/DevicePropertiesManager.php b/src/Models/States/DevicePropertiesManager.php index a79dd999..8026ac79 100644 --- a/src/Models/States/DevicePropertiesManager.php +++ b/src/Models/States/DevicePropertiesManager.php @@ -55,12 +55,14 @@ public function __construct( /** * @param Entities\Devices\Properties\IProperty $property * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IDeviceProperty */ public function create( Entities\Devices\Properties\IProperty $property, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IDeviceProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Device properties state manager is not registered'); @@ -73,7 +75,9 @@ public function create( /** @var States\IDeviceProperty $createdState */ $createdState = $this->manager->create($property, $values); - $this->publishEntity($property, $createdState); + if ($publishState) { + $this->publishEntity($property, $createdState); + } return $createdState; } @@ -82,13 +86,15 @@ public function create( * @param Entities\Devices\Properties\IProperty $property * @param States\IDeviceProperty $state * @param Utils\ArrayHash $values + * @param bool $publishState * * @return States\IDeviceProperty */ public function update( Entities\Devices\Properties\IProperty $property, States\IDeviceProperty $state, - Utils\ArrayHash $values + Utils\ArrayHash $values, + bool $publishState = true ): States\IDeviceProperty { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Device properties state manager is not registered'); @@ -98,13 +104,17 @@ public function update( throw new Exceptions\InvalidStateException('Child property can\'t have state'); } + $storedState = $state->toArray(); + /** @var States\IDeviceProperty $updatedState */ $updatedState = $this->manager->update($property, $state, $values); - $this->publishEntity($property, $updatedState); + if ($storedState !== $updatedState->toArray() && $publishState) { + $this->publishEntity($property, $updatedState); - foreach ($property->getChildren() as $child) { - $this->publishEntity($child, $updatedState); + foreach ($property->getChildren() as $child) { + $this->publishEntity($child, $updatedState); + } } return $updatedState; @@ -113,12 +123,14 @@ public function update( /** * @param Entities\Devices\Properties\IProperty $property * @param States\IDeviceProperty $state + * @param bool $publishState * * @return bool */ public function delete( Entities\Devices\Properties\IProperty $property, - States\IDeviceProperty $state + States\IDeviceProperty $state, + bool $publishState = true ): bool { if ($this->manager === null) { throw new Exceptions\NotImplementedException('Device properties state manager is not registered'); @@ -130,7 +142,7 @@ public function delete( $result = $this->manager->delete($property, $state); - if ($result) { + if ($result && $publishState) { $this->publishEntity($property, null); foreach ($property->getChildren() as $child) { @@ -154,7 +166,7 @@ private function publishEntity( $this->publisher->publish( $property->getSource(), - MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_DEVICE_PROPERTY_ENTITY_UPDATED), + MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_DEVICE_PROPERTY_ENTITY_REPORTED), Utils\ArrayHash::from(array_merge($property->toArray(), [ 'actual_value' => is_scalar($actualValue) || $actualValue === null ? $actualValue : strval($actualValue), 'expected_value' => is_scalar($expectedValue) || $expectedValue === null ? $expectedValue : strval($expectedValue),