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 914f7dc commit 52ca2e4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
9 changes: 5 additions & 4 deletions src/Clients/Cloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use InvalidArgumentException;
use Nette;
use Nette\Utils;
Expand Down Expand Up @@ -131,7 +132,7 @@ public function __construct(
private readonly DevicesModels\DataStorage\DevicesRepository $devicesRepository,
private readonly DevicesModels\DataStorage\ChannelsRepository $channelsRepository,
private readonly DevicesModels\DataStorage\ChannelPropertiesRepository $channelPropertiesRepository,
private readonly DevicesModels\States\DeviceConnectionStateManager $deviceConnectionStateManager,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly MetadataSchemas\Validator $schemaValidator,
private readonly EventLoop\LoopInterface $eventLoop,
Expand Down Expand Up @@ -352,7 +353,7 @@ private function handleCommunication(): void
foreach ($this->devicesRepository->findAllByConnector($this->connector->getId()) as $device) {
if (
!in_array($device->getId()->toString(), $this->processedDevices, true)
&& !$this->deviceConnectionStateManager->getState($device)->equalsValue(
&& !$this->deviceConnectionManager->getState($device)->equalsValue(
MetadataTypes\ConnectionState::STATE_STOPPED,
)
) {
Expand Down Expand Up @@ -397,7 +398,7 @@ private function processDevice(MetadataEntities\DevicesModule\Device $deviceItem
}

if (
$this->deviceConnectionStateManager->getState($deviceItem)->equalsValue(
$this->deviceConnectionManager->getState($deviceItem)->equalsValue(
MetadataTypes\ConnectionState::STATE_CONNECTED,
)
) {
Expand Down Expand Up @@ -450,7 +451,7 @@ private function readDeviceData(
if ($cmd === self::CMD_INFO || $cmd === self::CMD_HEARTBEAT) {
if (
$cmd === self::CMD_INFO
&& $this->deviceConnectionStateManager->getState($deviceItem)->equalsValue(
&& $this->deviceConnectionManager->getState($deviceItem)->equalsValue(
MetadataTypes\ConnectionState::STATE_CONNECTED,
)
) {
Expand Down
7 changes: 4 additions & 3 deletions src/Clients/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use Nette;
use Nette\Utils;
use Psr\Log;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function __construct(
private readonly DevicesModels\DataStorage\DevicesRepository $devicesRepository,
private readonly DevicesModels\DataStorage\ChannelsRepository $channelsRepository,
private readonly DevicesModels\DataStorage\ChannelPropertiesRepository $channelPropertiesRepository,
private readonly DevicesModels\States\DeviceConnectionStateManager $deviceConnectionStateManager,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly EventLoop\LoopInterface $eventLoop,
Log\LoggerInterface|null $logger = null,
Expand Down Expand Up @@ -161,7 +162,7 @@ private function handleCommunication(): void
foreach ($this->devicesRepository->findAllByConnector($this->connector->getId()) as $device) {
if (
!in_array($device->getId()->toString(), $this->processedDevices, true)
&& !$this->deviceConnectionStateManager->getState($device)->equalsValue(
&& !$this->deviceConnectionManager->getState($device)->equalsValue(
MetadataTypes\ConnectionState::STATE_STOPPED,
)
) {
Expand Down Expand Up @@ -227,7 +228,7 @@ private function processDevice(MetadataEntities\DevicesModule\Device $deviceItem
}

if (
!$this->deviceConnectionStateManager->getState($deviceItem)->equalsValue(
!$this->deviceConnectionManager->getState($deviceItem)->equalsValue(
MetadataTypes\ConnectionState::STATE_CONNECTED,
)
) {
Expand Down
7 changes: 4 additions & 3 deletions src/Consumers/Messages/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use IPub\DoctrineOrmQuery\Exceptions as DoctrineOrmQueryExceptions;
use Nette;
use Psr\Log;
Expand All @@ -48,7 +49,7 @@ public function __construct(
private readonly DevicesModels\DataStorage\DevicePropertiesRepository $devicePropertiesRepository,
private readonly DevicesModels\DataStorage\ChannelsRepository $channelsRepository,
private readonly DevicesModels\DataStorage\ChannelPropertiesRepository $channelPropertiesRepository,
private readonly DevicesModels\States\DeviceConnectionStateManager $deviceConnectionStateManager,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
Log\LoggerInterface|null $logger,
)
{
Expand Down Expand Up @@ -87,10 +88,10 @@ public function consume(Entities\Messages\Entity $entity): bool

// Check device state...
if (
!$this->deviceConnectionStateManager->getState($deviceItem)->equals($actualDeviceState)
!$this->deviceConnectionManager->getState($deviceItem)->equals($actualDeviceState)
) {
// ... and if it is not ready, set it to ready
$this->deviceConnectionStateManager->setState(
$this->deviceConnectionManager->setState(
$deviceItem,
$actualDeviceState,
);
Expand Down
6 changes: 3 additions & 3 deletions src/Consumers/Messages/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class Status implements Consumer

public function __construct(
private readonly DevicesModels\DataStorage\DevicesRepository $devicesDataStorageRepository,
private readonly DevicesModels\States\DeviceConnectionStateManager $deviceConnectionStateManager,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
private readonly Mappers\DataPoint $dataPointMapper,
private readonly Helpers\Property $propertyStateHelper,
Log\LoggerInterface|null $logger,
Expand Down Expand Up @@ -83,12 +83,12 @@ public function consume(Entities\Messages\Entity $entity): bool

// Check device state...
if (
!$this->deviceConnectionStateManager->getState($deviceItem)->equalsValue(
!$this->deviceConnectionManager->getState($deviceItem)->equalsValue(
Metadata\Types\ConnectionState::STATE_CONNECTED,
)
) {
// ... and if it is not ready, set it to ready
$this->deviceConnectionStateManager->setState(
$this->deviceConnectionManager->setState(
$deviceItem,
Metadata\Types\ConnectionState::get(Metadata\Types\ConnectionState::STATE_CONNECTED),
);
Expand Down
10 changes: 5 additions & 5 deletions src/Helpers/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use FastyBird\Library\Metadata\Entities as MetadataEntities;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use Nette;
use Nette\Utils;

Expand All @@ -36,8 +36,8 @@ final class Property
use Nette\SmartObject;

public function __construct(
private readonly DevicesModels\States\DevicePropertyStateManager $devicePropertyStateManager,
private readonly DevicesModels\States\ChannelPropertyStateManager $channelPropertyStateManager,
private readonly DevicesUtilities\DevicePropertiesStates $devicePropertiesStateManager,
private readonly DevicesUtilities\ChannelPropertiesStates $channelPropertiesStateManager,
)
{
}
Expand All @@ -57,9 +57,9 @@ public function setValue(
): void
{
if ($property instanceof MetadataEntities\DevicesModule\DeviceDynamicProperty) {
$this->devicePropertyStateManager->setValue($property, $data);
$this->devicePropertiesStateManager->setValue($property, $data);
} else {
$this->channelPropertyStateManager->setValue($property, $data);
$this->channelPropertiesStateManager->setValue($property, $data);
}
}

Expand Down

0 comments on commit 52ca2e4

Please sign in to comment.