Skip to content

Commit

Permalink
Allow use uuid or metadata entity in states models
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jun 22, 2022
1 parent 3e347f5 commit 983916a
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 61 deletions.
2 changes: 1 addition & 1 deletion fastybird_devices_module/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
Devices module
"""

__version__ = "0.63.0"
__version__ = "0.64.0"
70 changes: 58 additions & 12 deletions src/Models/States/ChannelPropertiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
use FastyBird\DevicesModule\Utilities;
use FastyBird\Exchange\Entities as ExchangeEntities;
use FastyBird\Exchange\Publisher as ExchangePublisher;
use FastyBird\Metadata\Entities as MetadataEntities;
use FastyBird\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Metadata\Types as MetadataTypes;
use Nette;
use Nette\Utils;
use Ramsey\Uuid;

/**
* Channel property states manager
Expand All @@ -48,25 +51,33 @@ final class ChannelPropertiesManager
/** @var IChannelPropertiesManager|null */
protected ?IChannelPropertiesManager $manager;

/** @var Models\DataStorage\IChannelPropertiesRepository */
private Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository;

public function __construct(
Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository,
ExchangeEntities\EntityFactory $entityFactory,
?IChannelPropertiesManager $manager,
?ExchangePublisher\IPublisher $publisher
) {
$this->channelPropertiesRepository = $channelPropertiesRepository;
$this->entityFactory = $entityFactory;
$this->manager = $manager;
$this->publisher = $publisher;
}

/**
* @param Entities\Channels\Properties\IProperty $property
* @param Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property
* @param Utils\ArrayHash $values
* @param bool $publishState
*
* @return States\IChannelProperty
*
* @throws Utils\JsonException
* @throws MetadataExceptions\FileNotFoundException
*/
public function create(
Entities\Channels\Properties\IProperty $property,
$property,
Utils\ArrayHash $values,
bool $publishState = true
): States\IChannelProperty {
Expand All @@ -78,7 +89,6 @@ public function create(
throw new Exceptions\InvalidStateException('Child property can\'t have state');
}

/** @var States\IChannelProperty $createdState */
$createdState = $this->manager->create($property, $values);

if ($publishState) {
Expand All @@ -89,15 +99,18 @@ public function create(
}

/**
* @param Entities\Channels\Properties\IProperty $property
* @param Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property
* @param States\IChannelProperty $state
* @param Utils\ArrayHash $values
* @param bool $publishState
*
* @return States\IChannelProperty
*
* @throws Utils\JsonException
* @throws MetadataExceptions\FileNotFoundException
*/
public function update(
Entities\Channels\Properties\IProperty $property,
$property,
States\IChannelProperty $state,
Utils\ArrayHash $values,
bool $publishState = true
Expand All @@ -112,29 +125,42 @@ public function update(

$storedState = $state->toArray();

/** @var States\IChannelProperty $updatedState */
$updatedState = $this->manager->update($property, $state, $values);

if ($storedState !== $updatedState->toArray() && $publishState) {
$this->publishEntity($property, $updatedState);

foreach ($property->getChildren() as $child) {
$this->publishEntity($child, $updatedState);
if ($child instanceof Uuid\UuidInterface) {
$child = $this->channelPropertiesRepository->findById($child);

if (
$child instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity
|| $child instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity
) {
$this->publishEntity($child, $updatedState);
}
} else {
$this->publishEntity($child, $updatedState);
}
}
}

return $updatedState;
}

/**
* @param Entities\Channels\Properties\IProperty $property
* @param Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property
* @param States\IChannelProperty $state
* @param bool $publishState
*
* @return bool
*
* @throws Utils\JsonException
* @throws MetadataExceptions\FileNotFoundException
*/
public function delete(
Entities\Channels\Properties\IProperty $property,
$property,
States\IChannelProperty $state,
bool $publishState = true
): bool {
Expand All @@ -152,15 +178,35 @@ public function delete(
$this->publishEntity($property, null);

foreach ($property->getChildren() as $child) {
$this->publishEntity($child, null);
if ($child instanceof Uuid\UuidInterface) {
$child = $this->channelPropertiesRepository->findById($child);

if (
$child instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity
|| $child instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity
) {
$this->publishEntity($child, null);
}
} else {
$this->publishEntity($child, null);
}
}
}

return $result;
}

/**
* @param Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property
* @param States\IChannelProperty|null $state
*
* @return void
*
* @throws Utils\JsonException
* @throws MetadataExceptions\FileNotFoundException
*/
private function publishEntity(
Entities\Channels\Properties\IProperty $property,
$property,
?States\IChannelProperty $state
): void {
if ($this->publisher === null) {
Expand All @@ -171,7 +217,7 @@ private function publishEntity(
$expectedValue = $state === null ? null : Utilities\ValueHelper::normalizeValue($property->getDataType(), $state->getExpectedValue(), $property->getFormat(), $property->getInvalid());

$this->publisher->publish(
$property->getSource(),
MetadataTypes\ModuleSourceType::get(MetadataTypes\ModuleSourceType::SOURCE_MODULE_DEVICES),
MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_PROPERTY_ENTITY_REPORTED),
$this->entityFactory->create(Utils\Json::encode(array_merge($property->toArray(), [
'actual_value' => is_scalar($actualValue) || $actualValue === null ? $actualValue : strval($actualValue),
Expand Down
28 changes: 25 additions & 3 deletions src/Models/States/ChannelPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\DevicesModule\States;
use FastyBird\Metadata\Entities as MetadataEntities;
use Nette;
use Ramsey\Uuid;

/**
* Channel property repository
Expand All @@ -43,22 +45,42 @@ public function __construct(
}

/**
* @param Entities\Channels\Properties\IProperty $property
* @param Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property
*
* @return States\IChannelProperty|null
*/
public function findOne(
Entities\Channels\Properties\IProperty $property
$property
): ?States\IChannelProperty {
if ($this->repository === null) {
throw new Exceptions\NotImplementedException('Channel properties state repository is not registered');
}

if ($property->getParent() !== null) {
return $this->repository->findOne($property->getParent());
if ($property->getParent() instanceof Entities\Channels\Properties\IProperty) {
return $this->repository->findOne($property->getParent());

} else {
return $this->repository->findOneById($property->getParent());
}
}

return $this->repository->findOne($property);
}

/**
* @param Uuid\UuidInterface $id
*
* @return States\IChannelProperty|null
*/
public function findOneById(
Uuid\UuidInterface $id
): ?States\IChannelProperty {
if ($this->repository === null) {
throw new Exceptions\NotImplementedException('Channel properties state repository is not registered');
}

return $this->repository->findOneById($id);
}

}
38 changes: 28 additions & 10 deletions src/Models/States/ConnectorPropertiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use FastyBird\DevicesModule\Utilities;
use FastyBird\Exchange\Entities as ExchangeEntities;
use FastyBird\Exchange\Publisher as ExchangePublisher;
use FastyBird\Metadata\Entities as MetadataEntities;
use FastyBird\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Metadata\Types as MetadataTypes;
use Nette;
use Nette\Utils;
Expand Down Expand Up @@ -59,22 +61,24 @@ public function __construct(
}

/**
* @param Entities\Connectors\Properties\IProperty $property
* @param Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $property
* @param Utils\ArrayHash $values
* @param bool $publishState
*
* @return States\IConnectorProperty
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function create(
Entities\Connectors\Properties\IProperty $property,
$property,
Utils\ArrayHash $values,
bool $publishState = true
): States\IConnectorProperty {
if ($this->manager === null) {
throw new Exceptions\NotImplementedException('Connector properties state manager is not registered');
}

/** @var States\IConnectorProperty $createdState */
$createdState = $this->manager->create($property, $values);

if ($publishState) {
Expand All @@ -85,15 +89,18 @@ public function create(
}

/**
* @param Entities\Connectors\Properties\IProperty $property
* @param Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $property
* @param States\IConnectorProperty $state
* @param Utils\ArrayHash $values
* @param bool $publishState
*
* @return States\IConnectorProperty
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function update(
Entities\Connectors\Properties\IProperty $property,
$property,
States\IConnectorProperty $state,
Utils\ArrayHash $values,
bool $publishState = true
Expand All @@ -104,7 +111,6 @@ public function update(

$storedState = $state->toArray();

/** @var States\IConnectorProperty $updatedState */
$updatedState = $this->manager->update($property, $state, $values);

if ($storedState !== $updatedState->toArray() && $publishState) {
Expand All @@ -115,14 +121,17 @@ public function update(
}

/**
* @param Entities\Connectors\Properties\IProperty $property
* @param Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $property
* @param States\IConnectorProperty $state
* @param bool $publishState
*
* @return bool
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function delete(
Entities\Connectors\Properties\IProperty $property,
$property,
States\IConnectorProperty $state,
bool $publishState = true
): bool {
Expand All @@ -139,8 +148,17 @@ public function delete(
return $result;
}

/**
* @param Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $property
* @param States\IConnectorProperty|null $state
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
private function publishEntity(
Entities\Connectors\Properties\IProperty $property,
$property,
?States\IConnectorProperty $state
): void {
if ($this->publisher === null) {
Expand All @@ -151,7 +169,7 @@ private function publishEntity(
$expectedValue = $state === null ? null : Utilities\ValueHelper::normalizeValue($property->getDataType(), $state->getExpectedValue(), $property->getFormat(), $property->getInvalid());

$this->publisher->publish(
$property->getSource(),
MetadataTypes\ModuleSourceType::get(MetadataTypes\ModuleSourceType::SOURCE_MODULE_DEVICES),
MetadataTypes\RoutingKeyType::get(MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_PROPERTY_ENTITY_REPORTED),
$this->entityFactory->create(Utils\Json::encode(array_merge($property->toArray(), [
'actual_value' => is_scalar($actualValue) || $actualValue === null ? $actualValue : strval($actualValue),
Expand Down
21 changes: 19 additions & 2 deletions src/Models/States/ConnectorPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\DevicesModule\States;
use FastyBird\Metadata\Entities as MetadataEntities;
use Nette;
use Ramsey\Uuid;

/**
* Connector property repository
Expand All @@ -43,12 +45,12 @@ public function __construct(
}

/**
* @param Entities\Connectors\Properties\IProperty $property
* @param Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $property
*
* @return States\IConnectorProperty|null
*/
public function findOne(
Entities\Connectors\Properties\IProperty $property
$property
): ?States\IConnectorProperty {
if ($this->repository === null) {
throw new Exceptions\NotImplementedException('Connector properties state repository is not registered');
Expand All @@ -57,4 +59,19 @@ public function findOne(
return $this->repository->findOne($property);
}

/**
* @param Uuid\UuidInterface $id
*
* @return States\IConnectorProperty|null
*/
public function findOneById(
Uuid\UuidInterface $id
): ?States\IConnectorProperty {
if ($this->repository === null) {
throw new Exceptions\NotImplementedException('Connector properties state repository is not registered');
}

return $this->repository->findOneById($id);
}

}
Loading

0 comments on commit 983916a

Please sign in to comment.