Skip to content

Commit

Permalink
Refactoring states storage handlers (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Oct 26, 2022
1 parent bea9284 commit 1096a3b
Show file tree
Hide file tree
Showing 16 changed files with 376 additions and 352 deletions.
19 changes: 10 additions & 9 deletions src/Commands/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use FastyBird\Module\Devices\Events;
use FastyBird\Module\Devices\Exceptions;
use FastyBird\Module\Devices\Models;
use FastyBird\Module\Devices\Utilities;
use IPub\DoctrineOrmQuery\Exceptions as DoctrineOrmQueryExceptions;
use Psr\EventDispatcher as PsrEventDispatcher;
use Psr\Log;
Expand Down Expand Up @@ -68,10 +69,10 @@ public function __construct(
private readonly Models\DataStorage\DevicePropertiesRepository $devicesPropertiesRepository,
private readonly Models\DataStorage\ChannelsRepository $channelsRepository,
private readonly Models\DataStorage\ChannelPropertiesRepository $channelsPropertiesRepository,
private readonly Models\States\ConnectorConnectionStateManager $connectorConnectionStateManager,
private readonly Models\States\DeviceConnectionStateManager $deviceConnectionStateManager,
private readonly Models\States\DevicePropertyStateManager $devicePropertyStateManager,
private readonly Models\States\ChannelPropertyStateManager $channelPropertyStateManager,
private readonly Utilities\ConnectorConnection $connectorConnectionManager,
private readonly Utilities\DeviceConnection $deviceConnectionManager,
private readonly Utilities\DevicePropertiesStates $devicePropertiesStateManager,
private readonly Utilities\ChannelPropertiesStates $channelPropertiesStateManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly EventLoop\LoopInterface $eventLoop,
private readonly PsrEventDispatcher\EventDispatcherInterface|null $dispatcher,
Expand Down Expand Up @@ -314,7 +315,7 @@ private function executeConnector(Style\SymfonyStyle $io, Input\InputInterface $
// Start connector service
$service->execute();

$this->connectorConnectionStateManager->setState(
$this->connectorConnectionManager->setState(
$connector,
MetadataTypes\ConnectionState::get(MetadataTypes\ConnectionState::STATE_RUNNING),
);
Expand Down Expand Up @@ -359,7 +360,7 @@ private function executeConnector(Style\SymfonyStyle $io, Input\InputInterface $

$this->dispatcher?->dispatch(new Events\AfterConnectorTerminate($service));

$this->connectorConnectionStateManager->setState(
$this->connectorConnectionManager->setState(
$connector,
MetadataTypes\ConnectionState::get(MetadataTypes\ConnectionState::STATE_STOPPED),
);
Expand Down Expand Up @@ -401,15 +402,15 @@ private function resetConnectorDevices(
): void
{
foreach ($this->devicesRepository->findAllByConnector($connector->getId()) as $device) {
$this->deviceConnectionStateManager->setState($device, $state);
$this->deviceConnectionManager->setState($device, $state);

/** @var Array<MetadataEntities\DevicesModule\DeviceDynamicProperty> $properties */
$properties = $this->devicesPropertiesRepository->findAllByDevice(
$device->getId(),
MetadataEntities\DevicesModule\DeviceDynamicProperty::class,
);

$this->devicePropertyStateManager->setValidState($properties, false);
$this->devicePropertiesStateManager->setValidState($properties, false);

foreach ($this->channelsRepository->findAllByDevice($device->getId()) as $channel) {
/** @var Array<MetadataEntities\DevicesModule\ChannelDynamicProperty> $properties */
Expand All @@ -418,7 +419,7 @@ private function resetConnectorDevices(
MetadataEntities\DevicesModule\ChannelDynamicProperty::class,
);

$this->channelPropertyStateManager->setValidState($properties, false);
$this->channelPropertiesStateManager->setValidState($properties, false);
}
}
}
Expand Down
53 changes: 33 additions & 20 deletions src/DI/DevicesExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use FastyBird\Module\Devices\Router;
use FastyBird\Module\Devices\Schemas;
use FastyBird\Module\Devices\Subscribers;
use FastyBird\Module\Devices\Utilities;
use IPub\DoctrineCrud;
use IPub\SlimRouter\Routing as SlimRouterRouting;
use Nette;
Expand Down Expand Up @@ -436,26 +437,6 @@ public function loadConfiguration(): void
)
->setType(Models\States\ChannelPropertiesManager::class);

// Connection states manager
$builder->addDefinition(
$this->prefix('states.connectionState.connector'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Models\States\ConnectorConnectionStateManager::class);

$builder->addDefinition(
$this->prefix('states.connectionState.device'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Models\States\DeviceConnectionStateManager::class);

// Properties states manager
$builder->addDefinition($this->prefix('states.propertyState.device'), new DI\Definitions\ServiceDefinition())
->setType(Models\States\DevicePropertyStateManager::class);

$builder->addDefinition($this->prefix('states.propertyState.channel'), new DI\Definitions\ServiceDefinition())
->setType(Models\States\ChannelPropertyStateManager::class);

// Data storage
$builder->addDefinition($this->prefix('dataStorage.writer'), new DI\Definitions\ServiceDefinition())
->setType(DataStorage\Writer::class);
Expand Down Expand Up @@ -502,6 +483,38 @@ public function loadConfiguration(): void
)
->setType(Models\DataStorage\ChannelPropertiesRepository::class);

// Utilities

$builder->addDefinition(
$this->prefix('utilities.channels.states'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Utilities\ChannelPropertiesStates::class);

$builder->addDefinition(
$this->prefix('utilities.connectors.states'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Utilities\ConnectorPropertiesStates::class);

$builder->addDefinition(
$this->prefix('utilities.devices.states'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Utilities\DevicePropertiesStates::class);

$builder->addDefinition(
$this->prefix('utilities.devices.connection'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Utilities\DeviceConnection::class);

$builder->addDefinition(
$this->prefix('utilities.connector.connection'),
new DI\Definitions\ServiceDefinition(),
)
->setType(Utilities\ConnectorConnection::class);

// Console commands
$builder->addDefinition($this->prefix('commands.initialize'), new DI\Definitions\ServiceDefinition())
->setType(Commands\Initialize::class);
Expand Down
19 changes: 12 additions & 7 deletions src/Models/States/ChannelPropertiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public function __construct(
* @throws Exceptions\NotImplemented
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*
* @interal
*/
public function create(
MetadataEntities\DevicesModule\ChannelDynamicProperty|MetadataEntities\DevicesModule\ChannelMappedProperty|Entities\Channels\Properties\Dynamic|Entities\Channels\Properties\Mapped $property,
MetadataEntities\DevicesModule\ChannelDynamicProperty|Entities\Channels\Properties\Dynamic $property,
Utils\ArrayHash $values,
): States\ChannelProperty
{
Expand Down Expand Up @@ -93,7 +95,7 @@ public function create(
}
}

$createdState = $this->manager->create($property, $values);
$createdState = $this->manager->create($property->getId(), $values);

$this->dispatcher?->dispatch(new Events\StateEntityCreated($property, $createdState));

Expand All @@ -105,9 +107,11 @@ public function create(
* @throws Exceptions\NotImplemented
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*
* @interal
*/
public function update(
MetadataEntities\DevicesModule\ChannelDynamicProperty|MetadataEntities\DevicesModule\ChannelMappedProperty|Entities\Channels\Properties\Dynamic|Entities\Channels\Properties\Mapped $property,
MetadataEntities\DevicesModule\ChannelDynamicProperty|Entities\Channels\Properties\Dynamic $property,
States\ChannelProperty $state,
Utils\ArrayHash $values,
): States\ChannelProperty
Expand All @@ -120,7 +124,7 @@ public function update(
throw new Exceptions\InvalidState('Child property can\'t have state');
}

$updatedState = $this->manager->update($property, $state, $values);
$updatedState = $this->manager->update($state, $values);

$actualValue = Utilities\ValueHelper::normalizeValue(
$property->getDataType(),
Expand All @@ -138,7 +142,6 @@ public function update(

if ($expectedValue === $actualValue) {
$updatedState = $this->manager->update(
$property,
$updatedState,
Utils\ArrayHash::from([
'expectedValue' => null,
Expand All @@ -155,9 +158,11 @@ public function update(
/**
* @throws Exceptions\InvalidState
* @throws Exceptions\NotImplemented
*
* @interal
*/
public function delete(
MetadataEntities\DevicesModule\ChannelDynamicProperty|MetadataEntities\DevicesModule\ChannelMappedProperty|Entities\Channels\Properties\Dynamic|Entities\Channels\Properties\Mapped $property,
MetadataEntities\DevicesModule\ChannelDynamicProperty|Entities\Channels\Properties\Dynamic $property,
States\ChannelProperty $state,
): bool
{
Expand All @@ -169,7 +174,7 @@ public function delete(
throw new Exceptions\InvalidState('Child property can\'t have state');
}

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

$this->dispatcher?->dispatch(new Events\StateEntityDeleted($property));

Expand Down
51 changes: 11 additions & 40 deletions src/Models/States/ConnectorPropertiesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
namespace FastyBird\Module\Devices\Models\States;

use FastyBird\Library\Metadata\Entities as MetadataEntities;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Module\Devices\Entities;
use FastyBird\Module\Devices\Events;
use FastyBird\Module\Devices\Exceptions;
use FastyBird\Module\Devices\Models;
use FastyBird\Module\Devices\States;
use FastyBird\Module\Devices\Utilities;
use Nette;
use Nette\Utils;
use Psr\EventDispatcher as PsrEventDispatcher;
Expand Down Expand Up @@ -51,8 +49,8 @@ public function __construct(

/**
* @throws Exceptions\NotImplemented
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*
* @interal
*/
public function create(
MetadataEntities\DevicesModule\ConnectorDynamicProperty|Entities\Connectors\Properties\Dynamic $property,
Expand All @@ -67,27 +65,13 @@ public function create(
property_exists($values, 'actualValue')
&& property_exists($values, 'expectedValue')
) {
$actualValue = Utilities\ValueHelper::normalizeValue(
$property->getDataType(),
strval($values->offsetGet('actualValue')),
$property->getFormat(),
$property->getInvalid(),
);

$expectedValue = Utilities\ValueHelper::normalizeValue(
$property->getDataType(),
strval($values->offsetGet('expectedValue')),
$property->getFormat(),
$property->getInvalid(),
);

if ($expectedValue === $actualValue) {
if (strval($values->offsetGet('actualValue')) === strval($values->offsetGet('expectedValue'))) {
$values->offsetSet('expectedValue', null);
$values->offsetSet('pending', null);
}
}

$createdState = $this->manager->create($property, $values);
$createdState = $this->manager->create($property->getId(), $values);

$this->dispatcher?->dispatch(new Events\StateEntityCreated($property, $createdState));

Expand All @@ -96,8 +80,8 @@ public function create(

/**
* @throws Exceptions\NotImplemented
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*
* @interal
*/
public function update(
MetadataEntities\DevicesModule\ConnectorDynamicProperty|Entities\Connectors\Properties\Dynamic $property,
Expand All @@ -109,25 +93,10 @@ public function update(
throw new Exceptions\NotImplemented('Connector properties state manager is not registered');
}

$updatedState = $this->manager->update($property, $state, $values);

$actualValue = Utilities\ValueHelper::normalizeValue(
$property->getDataType(),
$updatedState->getActualValue(),
$property->getFormat(),
$property->getInvalid(),
);

$expectedValue = Utilities\ValueHelper::normalizeValue(
$property->getDataType(),
$updatedState->getExpectedValue(),
$property->getFormat(),
$property->getInvalid(),
);
$updatedState = $this->manager->update($state, $values);

if ($expectedValue === $actualValue) {
if (strval($updatedState->getActualValue()) === strval($updatedState->getExpectedValue())) {
$updatedState = $this->manager->update(
$property,
$updatedState,
Utils\ArrayHash::from([
'expectedValue' => null,
Expand All @@ -143,6 +112,8 @@ public function update(

/**
* @throws Exceptions\NotImplemented
*
* @interal
*/
public function delete(
MetadataEntities\DevicesModule\ConnectorDynamicProperty|Entities\Connectors\Properties\Dynamic $property,
Expand All @@ -153,7 +124,7 @@ public function delete(
throw new Exceptions\NotImplemented('Connector properties state manager is not registered');
}

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

$this->dispatcher?->dispatch(new Events\StateEntityDeleted($property));

Expand Down
Loading

0 comments on commit 1096a3b

Please sign in to comment.