Skip to content

Commit

Permalink
Refactored data storage refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jul 7, 2022
1 parent 464a932 commit 3cd4fe5
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 69 deletions.
18 changes: 15 additions & 3 deletions src/Events/StateEntityCreatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace FastyBird\DevicesModule\Events;

use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\States;
use FastyBird\Metadata\Entities as MetadataEntities;
use Symfony\Contracts\EventDispatcher;

/**
Expand All @@ -29,18 +31,28 @@
class StateEntityCreatedEvent extends EventDispatcher\Event
{

/** @var MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty */
private MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property;

/** @var States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty */
private States\IDeviceProperty|States\IChannelProperty|States\IConnectorProperty $state;

/**
* @param States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty $state
*/
public function __construct(
MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property,
States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty $state
) {
$this->property = $property;
$this->state = $state;
}

/**
* @return MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity
*/
public function getProperty(): MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity
{
return $this->property;
}

/**
* @return States\IChannelProperty|States\IConnectorProperty|States\IDeviceProperty
*/
Expand Down
17 changes: 9 additions & 8 deletions src/Events/StateEntityDeletedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace FastyBird\DevicesModule\Events;

use Ramsey\Uuid;
use FastyBird\DevicesModule\Entities;
use FastyBird\Metadata\Entities as MetadataEntities;
use Symfony\Contracts\EventDispatcher;

/**
Expand All @@ -29,21 +30,21 @@
class StateEntityDeletedEvent extends EventDispatcher\Event
{

/** @var Uuid\UuidInterface */
private Uuid\UuidInterface $id;
/** @var MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty */
private MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property;

public function __construct(
Uuid\UuidInterface $id
MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property
) {
$this->id = $id;
$this->property = $property;
}

/**
* @return Uuid\UuidInterface
* @return MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity
*/
public function getId(): Uuid\UuidInterface
public function getProperty(): MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity
{
return $this->id;
return $this->property;
}

}
19 changes: 15 additions & 4 deletions src/Events/StateEntityUpdatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

namespace FastyBird\DevicesModule\Events;

use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\States;
use FastyBird\Metadata\Entities as MetadataEntities;
use Symfony\Contracts\EventDispatcher;

/**
Expand All @@ -29,24 +31,33 @@
class StateEntityUpdatedEvent extends EventDispatcher\Event
{

/** @var MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty */
private MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property;

/** @var States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty */
private States\IDeviceProperty|States\IChannelProperty|States\IConnectorProperty $previousState;

/** @var States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty */
private States\IDeviceProperty|States\IChannelProperty|States\IConnectorProperty $state;

/**
* @param States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty $previousState
* @param States\IChannelProperty|States\IDeviceProperty|States\IConnectorProperty $state
*/
public function __construct(
MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|Entities\Channels\Properties\IProperty $property,
States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty $previousState,
States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty $state
) {
$this->property = $property;
$this->previousState = $previousState;
$this->state = $state;
}

/**
* @return MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity
*/
public function getProperty(): MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|Entities\Connectors\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|Entities\Devices\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|Entities\Channels\Properties\IProperty|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity
{
return $this->property;
}

/**
* @return States\IChannelProperty|States\IConnectorProperty|States\IDeviceProperty
*/
Expand Down
53 changes: 45 additions & 8 deletions src/Models/DataStorage/ChannelPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,11 @@ public function append(
}

if (!$this->properties->contains($entity)) {
try {
$propertyState = $this->statesRepository->findOneById($entity->getId());
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

$entity = $this->entityFactory->create(
Utils\Json::encode(array_merge($entity->toArray(), $propertyState))
Utils\Json::encode(array_merge(
$entity->toArray(),
$this->loadPropertyState($entity->getId())
))
);

if (
Expand All @@ -154,6 +150,31 @@ public function append(
}
}

/**
* @param Uuid\UuidInterface|null $id
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function loadState(?Uuid\UuidInterface $id = null): void
{
if ($id === null) {
foreach ($this->properties as $property) {
$this->append($property);
}
} else {
$property = $this->findById($id);

if ($property === null) {
return;
}

$this->append($property);
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -184,4 +205,20 @@ public function getIterator(): RecursiveArrayIterator
return new RecursiveArrayIterator($properties);
}

/**
* @param Uuid\UuidInterface $id
*
* @return mixed[]
*/
private function loadPropertyState(Uuid\UuidInterface $id): array
{
try {
$propertyState = $this->statesRepository->findOneById($id);

return $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
return [];
}
}

}
53 changes: 45 additions & 8 deletions src/Models/DataStorage/ConnectorPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,11 @@ public function append(
}

if (!$this->properties->contains($entity)) {
try {
$propertyState = $this->statesRepository->findOneById($entity->getId());
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

$entity = $this->entityFactory->create(
Utils\Json::encode(array_merge($entity->toArray(), $propertyState))
Utils\Json::encode(array_merge(
$entity->toArray(),
$this->loadPropertyState($entity->getId())
))
);

if (
Expand All @@ -154,6 +150,31 @@ public function append(
}
}

/**
* @param Uuid\UuidInterface|null $id
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function loadState(?Uuid\UuidInterface $id = null): void
{
if ($id === null) {
foreach ($this->properties as $property) {
$this->append($property);
}
} else {
$property = $this->findById($id);

if ($property === null) {
return;
}

$this->append($property);
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -184,4 +205,20 @@ public function getIterator(): RecursiveArrayIterator
return new RecursiveArrayIterator($properties);
}

/**
* @param Uuid\UuidInterface $id
*
* @return mixed[]
*/
private function loadPropertyState(Uuid\UuidInterface $id): array
{
try {
$propertyState = $this->statesRepository->findOneById($id);

return $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
return [];
}
}

}
53 changes: 45 additions & 8 deletions src/Models/DataStorage/DevicePropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,11 @@ public function append(
}

if (!$this->properties->contains($entity)) {
try {
$propertyState = $this->statesRepository->findOneById($entity->getId());
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

$entity = $this->entityFactory->create(
Utils\Json::encode(array_merge($entity->toArray(), $propertyState))
Utils\Json::encode(array_merge(
$entity->toArray(),
$this->loadPropertyState($entity->getId())
))
);

if (
Expand All @@ -154,6 +150,31 @@ public function append(
}
}

/**
* @param Uuid\UuidInterface|null $id
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function loadState(?Uuid\UuidInterface $id = null): void
{
if ($id === null) {
foreach ($this->properties as $property) {
$this->append($property);
}
} else {
$property = $this->findById($id);

if ($property === null) {
return;
}

$this->append($property);
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -184,4 +205,20 @@ public function getIterator(): RecursiveArrayIterator
return new RecursiveArrayIterator($properties);
}

/**
* @param Uuid\UuidInterface $id
*
* @return mixed[]
*/
private function loadPropertyState(Uuid\UuidInterface $id): array
{
try {
$propertyState = $this->statesRepository->findOneById($id);

return $propertyState !== null ? $propertyState->toArray() : [];
} catch (Exceptions\NotImplementedException $ex) {
return [];
}
}

}
12 changes: 12 additions & 0 deletions src/Models/DataStorage/IChannelPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
namespace FastyBird\DevicesModule\Models\DataStorage;

use FastyBird\Metadata\Entities as MetadataEntities;
use FastyBird\Metadata\Exceptions as MetadataExceptions;
use Nette\Utils;
use Ramsey\Uuid;

/**
Expand Down Expand Up @@ -65,6 +67,16 @@ public function append(
MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity $entity
): void;

/**
* @param Uuid\UuidInterface|null $id
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function loadState(?Uuid\UuidInterface $id = null): void;

/**
* @return void
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Models/DataStorage/IConnectorPropertiesRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
namespace FastyBird\DevicesModule\Models\DataStorage;

use FastyBird\Metadata\Entities as MetadataEntities;
use FastyBird\Metadata\Exceptions as MetadataExceptions;
use Nette\Utils;
use Ramsey\Uuid;

/**
Expand Down Expand Up @@ -65,6 +67,16 @@ public function append(
MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity $entity
): void;

/**
* @param Uuid\UuidInterface|null $id
*
* @return void
*
* @throws MetadataExceptions\FileNotFoundException
* @throws Utils\JsonException
*/
public function loadState(?Uuid\UuidInterface $id = null): void;

/**
* @return void
*/
Expand Down
Loading

0 comments on commit 3cd4fe5

Please sign in to comment.