Skip to content

Commit

Permalink
Make exchange publis optional
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Apr 7, 2022
1 parent 0070091 commit aec3628
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 45 deletions.
2 changes: 1 addition & 1 deletion fastybird_devices_module/__init__.py
Expand Up @@ -18,4 +18,4 @@
Devices module
"""

__version__ = "0.53.0"
__version__ = "0.54.0"
48 changes: 30 additions & 18 deletions fastybird_devices_module/managers/state.py
Expand Up @@ -198,17 +198,19 @@ 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:
raise NotImplementedError("Connector properties states manager is not implemented")

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

Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -242,14 +245,15 @@ def delete(
self,
connector_property: ConnectorPropertyEntity,
state: IConnectorPropertyState,
publish_state: bool = True,
) -> bool:
"""Delete existing connector property state"""
if self.__manager is None:
raise NotImplementedError("Connector properties states manager is not implemented")

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

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

Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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:
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
30 changes: 21 additions & 9 deletions src/Models/States/ChannelPropertiesManager.php
Expand Up @@ -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');
Expand All @@ -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;
}
Expand All @@ -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');
Expand All @@ -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;
Expand All @@ -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');
Expand All @@ -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) {
Expand All @@ -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),
Expand Down
26 changes: 19 additions & 7 deletions src/Models/States/ConnectorPropertiesManager.php
Expand Up @@ -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');
Expand All @@ -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;
}
Expand All @@ -78,43 +82,51 @@ 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;
}

/**
* @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');
}

$result = $this->manager->delete($property, $state);

if ($result) {
if ($result && $publishState) {
$this->publishEntity($property, null);
}

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

0 comments on commit aec3628

Please sign in to comment.