From f5249cff5454aa2dedb2f229b8f54d5130bee089 Mon Sep 17 00:00:00 2001 From: Adam Kadlec Date: Sat, 9 Jul 2022 20:56:25 +0200 Subject: [PATCH] After testing updates & fixes (#4) * After testing updates & fixes * QA fixes * QA fixes --- composer.json | 4 +- .../connectors/connector.py | 6 +- .../connectors/consumer.py | 6 +- requirements.txt | 4 +- src/Commands/ConnectorCommand.php | 48 ++--- src/Commands/DataStorageCommand.php | 113 ++++++++++ src/Commands/InitializeCommand.php | 7 - src/Connectors/ConnectorFactory.php | 5 - src/Connectors/IConnector.php | 9 - src/Connectors/IConnectorFactory.php | 8 - src/Constants.php | 6 +- src/Consumers/DataExchangeConsumer.php | 22 -- src/DI/DevicesModuleExtension.php | 30 +-- src/DataStorage/Reader.php | 149 +------------- src/DataStorage/Writer.php | 3 - .../Channels/Properties/MappedProperty.php | 5 +- .../Channels/Properties/StaticProperty.php | 11 - .../Devices/Properties/MappedProperty.php | 5 +- .../Devices/Properties/StaticProperty.php | 11 - src/Entities/IEntity.php | 2 +- src/Entities/IEntityParams.php | 2 +- src/Entities/Property.php | 11 +- src/Entities/TEntityParams.php | 4 +- src/Events/AfterConnectorStartEvent.php | 7 - src/Events/AfterConnectorTerminateEvent.php | 7 - src/Events/BeforeConnectorStartEvent.php | 7 - src/Events/BeforeConnectorTerminateEvent.php | 7 - src/Events/StateEntityCreatedEvent.php | 8 - src/Events/StateEntityDeletedEvent.php | 4 - src/Events/StateEntityUpdatedEvent.php | 12 -- .../DataStorage/ChannelControlsRepository.php | 55 +++-- .../ChannelPropertiesRepository.php | 193 +++++++++++------- src/Models/DataStorage/ChannelsRepository.php | 67 +++--- .../ConnectorControlsRepository.php | 55 +++-- .../ConnectorPropertiesRepository.php | 193 +++++++++++------- .../DataStorage/ConnectorsRepository.php | 58 +++--- .../DeviceAttributesRepository.php | 67 +++--- .../DataStorage/DeviceControlsRepository.php | 55 +++-- .../DevicePropertiesRepository.php | 193 +++++++++++------- src/Models/DataStorage/DevicesRepository.php | 67 +++--- .../IChannelControlsRepository.php | 5 +- .../IChannelPropertiesRepository.php | 19 +- .../DataStorage/IChannelsRepository.php | 5 +- .../IConnectorControlsRepository.php | 5 +- .../IConnectorPropertiesRepository.php | 19 +- .../DataStorage/IConnectorsRepository.php | 5 +- .../IDeviceAttributesRepository.php | 5 +- .../DataStorage/IDeviceControlsRepository.php | 5 +- .../IDevicePropertiesRepository.php | 19 +- src/Models/DataStorage/IDevicesRepository.php | 5 +- src/States/IProperty.php | 40 +--- src/Subscribers/DataStorageSubscriber.php | 4 - src/Subscribers/EntitiesSubscriber.php | 108 +++------- src/Subscribers/ExchangeSubscriber.php | 4 - src/Subscribers/StatesSubscriber.php | 58 ------ src/Utilities/ValueHelper.php | 5 - .../Unit/Connectors/ConnectorFactoryTest.phpt | 16 +- tests/cases/Unit/DataStorage/ReaderTest.phpt | 26 +-- tests/cases/Unit/DataStorage/WriterTest.phpt | 5 +- .../DataStorage/ConnectorsRepositoryTest.phpt | 26 +-- 60 files changed, 829 insertions(+), 1081 deletions(-) create mode 100644 src/Commands/DataStorageCommand.php diff --git a/composer.json b/composer.json index 2061a7f4..0f24f3d6 100644 --- a/composer.json +++ b/composer.json @@ -43,9 +43,9 @@ "ext-pcntl": "*", "contributte/flysystem": "^0.3.0", "cweagans/composer-patches": "^1.7", - "fastybird/exchange" : "^0.50", + "fastybird/exchange" : "^0.51", "fastybird/json-api" : "^0.11", - "fastybird/metadata" : "^0.68", + "fastybird/metadata" : "^0.69", "fastybird/simple-auth" : "^0.4", "ipub/doctrine-dynamic-discriminator-map": "^1.4", "ipub/doctrine-timestampable": "^1.5", diff --git a/fastybird_devices_module/connectors/connector.py b/fastybird_devices_module/connectors/connector.py index 1b998d5a..9bdd081c 100644 --- a/fastybird_devices_module/connectors/connector.py +++ b/fastybird_devices_module/connectors/connector.py @@ -575,7 +575,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements if self.__connector is None: return - if item.routing_key == RoutingKey.CONNECTOR_ACTION and ControlAction.has_value(str(item.data.get("name"))): + if item.routing_key == RoutingKey.CONNECTOR_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))): try: connector_control = self.__connectors_control_repository.get_by_name( connector_id=uuid.UUID(item.data.get("connector"), version=4), @@ -596,7 +596,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements action=ControlAction(item.data.get("name")), ) - if item.routing_key == RoutingKey.DEVICE_ACTION and ControlAction.has_value(str(item.data.get("name"))): + if item.routing_key == RoutingKey.DEVICE_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))): try: device_control = self.__devices_control_repository.get_by_name( device_id=uuid.UUID(item.data.get("device"), version=4), control_name=str(item.data.get("name")) @@ -616,7 +616,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements action=ControlAction(item.data.get("name")), ) - if item.routing_key == RoutingKey.CHANNEL_ACTION and ControlAction.has_value(str(item.data.get("name"))): + if item.routing_key == RoutingKey.CHANNEL_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))): try: channel_control = self.__channels_control_repository.get_by_name( channel_id=uuid.UUID(item.data.get("channel"), version=4), control_name=str(item.data.get("name")) diff --git a/fastybird_devices_module/connectors/consumer.py b/fastybird_devices_module/connectors/consumer.py index ab7a70c5..ee8d2a85 100644 --- a/fastybird_devices_module/connectors/consumer.py +++ b/fastybird_devices_module/connectors/consumer.py @@ -58,9 +58,9 @@ class ConnectorConsumer(IConsumer): # pylint: disable=too-few-public-methods ] __CONTROLS_ACTIONS_ROUTING_KEYS: List[RoutingKey] = [ - RoutingKey.CONNECTOR_ACTION, - RoutingKey.DEVICE_ACTION, - RoutingKey.CHANNEL_ACTION, + RoutingKey.CONNECTOR_CONTROL_ACTION, + RoutingKey.DEVICE_CONTROL_ACTION, + RoutingKey.CHANNEL_CONTROL_ACTION, ] __queue: ConnectorQueue diff --git a/requirements.txt b/requirements.txt index 1a0c5022..bf295cad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ fastnumbers>=3.2 -fastybird-exchange>=0.50 -fastybird-metadata>=0.68 +fastybird-exchange>=0.51 +fastybird-metadata>=0.69 inflection>=0.5 kink>=0.6 setuptools>=57.4 diff --git a/src/Commands/ConnectorCommand.php b/src/Commands/ConnectorCommand.php index 0c5fad9d..14da8015 100644 --- a/src/Commands/ConnectorCommand.php +++ b/src/Commands/ConnectorCommand.php @@ -49,37 +49,26 @@ class ConnectorCommand extends Console\Command\Command private const SHUTDOWN_WAITING_DELAY = 3; - /** @var Connectors\ConnectorFactory */ private Connectors\ConnectorFactory $factory; - /** @var Models\DataStorage\IConnectorsRepository */ private Models\DataStorage\IConnectorsRepository $connectorsRepository; - /** @var Models\Connectors\Properties\IPropertiesRepository */ private Models\Connectors\Properties\IPropertiesRepository $connectorPropertiesRepository; - /** @var Models\Connectors\Properties\IPropertiesManager */ private Models\Connectors\Properties\IPropertiesManager $connectorPropertiesManager; - /** @var Models\States\ConnectorPropertiesRepository */ private Models\States\ConnectorPropertiesRepository $connectorPropertiesStateRepository; - /** @var Models\States\ConnectorPropertiesManager */ private Models\States\ConnectorPropertiesManager $connectorPropertiesStateManager; - /** @var DateTimeFactory\DateTimeFactory */ private DateTimeFactory\DateTimeFactory $dateTimeFactory; - /** @var Localization\Translator */ private Localization\Translator $translator; - /** @var Log\LoggerInterface */ private Log\LoggerInterface $logger; - /** @var PsrEventDispatcher\EventDispatcherInterface|null */ private ?PsrEventDispatcher\EventDispatcherInterface $dispatcher; - /** @var EventLoop\LoopInterface */ private EventLoop\LoopInterface $eventLoop; public function __construct( @@ -114,9 +103,6 @@ public function __construct( parent::__construct($name); } - /** - * {@inheritDoc} - */ protected function configure(): void { $this @@ -126,9 +112,6 @@ protected function configure(): void ->setDescription('Run connector service.'); } - /** - * {@inheritDoc} - */ protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int { $symfonyApp = $this->getApplication(); @@ -174,7 +157,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $ $this->eventLoop->futureTick(function () use ($connector, $service): void { $this->dispatcher?->dispatch(new Events\BeforeConnectorStartEvent($connector)); - $this->logger->debug('Starting connector...', [ + $this->logger->info('Starting connector...', [ 'source' => 'devices-module', 'type' => 'connector', ]); @@ -195,7 +178,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $ }); $this->eventLoop->addSignal(SIGINT, function (int $signal) use ($connector, $service): void { - $this->logger->debug('Stopping connector...', [ + $this->logger->info('Stopping connector...', [ 'source' => 'devices-module', 'type' => 'connector', ]); @@ -236,23 +219,34 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $ 'code' => $ex->getCode(), ], ]); + + throw new Exceptions\TerminateException( + 'Error during connector termination process', + $ex->getCode(), + $ex + ); } }); $this->eventLoop->run(); - } catch (Exceptions\TerminateException $ex) { + } catch (Throwable $ex) { + if (!$ex instanceof Exceptions\TerminateException) { + $this->logger->error('An unhandled error occurred', [ + 'source' => 'devices-module', + 'type' => 'connector', + 'exception' => [ + 'message' => $ex->getMessage(), + 'code' => $ex->getCode(), + ], + ]); + } + $this->eventLoop->stop(); } return 0; } - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector - * @param MetadataTypes\ConnectionStateType $state - * - * @return void - */ private function setConnectorState( MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector, MetadataTypes\ConnectionStateType $state @@ -268,7 +262,7 @@ private function setConnectorState( 'connector' => $connector->getId(), 'entity' => Entities\Connectors\Properties\DynamicProperty::class, 'identifier' => MetadataTypes\ConnectorPropertyNameType::NAME_STATE, - 'data_type' => MetadataTypes\DataTypeType::get(MetadataTypes\DataTypeType::DATA_TYPE_ENUM), + 'dataType' => MetadataTypes\DataTypeType::get(MetadataTypes\DataTypeType::DATA_TYPE_ENUM), 'unit' => null, 'format' => [ MetadataTypes\ConnectionStateType::STATE_RUNNING, diff --git a/src/Commands/DataStorageCommand.php b/src/Commands/DataStorageCommand.php new file mode 100644 index 00000000..16d1c7ee --- /dev/null +++ b/src/Commands/DataStorageCommand.php @@ -0,0 +1,113 @@ + + * @package FastyBird:DevicesModule! + * @subpackage Commands + * @since 0.71.0 + * + * @date 08.07.22 + */ + +namespace FastyBird\DevicesModule\Commands; + +use FastyBird\DevicesModule\DataStorage; +use Psr\Log; +use RuntimeException; +use Symfony\Component\Console; +use Symfony\Component\Console\Input; +use Symfony\Component\Console\Output; +use Symfony\Component\Console\Style; +use Throwable; + +/** + * Data storage initialize command + * + * @package FastyBird:DevicesModule! + * @subpackage Commands + * + * @author Adam Kadlec + */ +class DataStorageCommand extends Console\Command\Command +{ + + private DataStorage\Writer $writer; + + private Log\LoggerInterface $logger; + + public function __construct( + DataStorage\Writer $writer, + ?Log\LoggerInterface $logger = null, + ?string $name = null + ) { + $this->writer = $writer; + + $this->logger = $logger ?? new Log\NullLogger(); + + parent::__construct($name); + } + + protected function configure(): void + { + $this + ->setName('fb:devices-module:data-storage') + ->addOption('noconfirm', null, Input\InputOption::VALUE_NONE, 'do not ask for any confirmation') + ->setDescription('Initialize module data storage.'); + } + + protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int + { + $symfonyApp = $this->getApplication(); + + if ($symfonyApp === null) { + return 1; + } + + $io = new Style\SymfonyStyle($input, $output); + + $io->title('FB devices module - data storage initialization'); + + $io->note('This action will create|update module data storage configuration.'); + + /** @var bool $continue */ + $continue = $io->ask('Would you like to continue?', 'n', function ($answer): bool { + if (!in_array($answer, ['y', 'Y', 'n', 'N'], true)) { + throw new RuntimeException('You must type Y or N'); + } + + return in_array($answer, ['y', 'Y'], true); + }); + + if (!$continue) { + return 0; + } + + try { + $this->writer->write(); + + $io->success('Devices module data storage has been successfully initialized.'); + + return 0; + + } catch (Throwable $ex) { + // Log caught exception + $this->logger->error('An unhandled error occurred', [ + 'source' => 'devices-module', + 'type' => 'data-storage-cmd', + 'exception' => [ + 'message' => $ex->getMessage(), + 'code' => $ex->getCode(), + ], + ]); + + $io->error('Something went wrong, initialization could not be finished. Error was logged.'); + + return 1; + } + } + +} diff --git a/src/Commands/InitializeCommand.php b/src/Commands/InitializeCommand.php index 37f09df5..1818b25e 100644 --- a/src/Commands/InitializeCommand.php +++ b/src/Commands/InitializeCommand.php @@ -34,7 +34,6 @@ class InitializeCommand extends Console\Command\Command { - /** @var Log\LoggerInterface */ private Log\LoggerInterface $logger; public function __construct( @@ -46,9 +45,6 @@ public function __construct( parent::__construct($name); } - /** - * {@inheritDoc} - */ protected function configure(): void { $this @@ -57,9 +53,6 @@ protected function configure(): void ->setDescription('Initialize module.'); } - /** - * {@inheritDoc} - */ protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int { $symfonyApp = $this->getApplication(); diff --git a/src/Connectors/ConnectorFactory.php b/src/Connectors/ConnectorFactory.php index 393e57d9..ebdb3135 100644 --- a/src/Connectors/ConnectorFactory.php +++ b/src/Connectors/ConnectorFactory.php @@ -46,11 +46,6 @@ public function __construct( } } - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector - * - * @return IConnector - */ public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector { /** @var IConnectorFactory $factory */ diff --git a/src/Connectors/IConnector.php b/src/Connectors/IConnector.php index 750609ad..70abf93f 100644 --- a/src/Connectors/IConnector.php +++ b/src/Connectors/IConnector.php @@ -26,19 +26,10 @@ interface IConnector { - /** - * @return void - */ public function execute(): void; - /** - * @return void - */ public function terminate(): void; - /** - * @return bool - */ public function hasUnfinishedTasks(): bool; } diff --git a/src/Connectors/IConnectorFactory.php b/src/Connectors/IConnectorFactory.php index 7d98ef1f..023b9e05 100644 --- a/src/Connectors/IConnectorFactory.php +++ b/src/Connectors/IConnectorFactory.php @@ -28,16 +28,8 @@ interface IConnectorFactory { - /** - * @return string - */ public function getType(): string; - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector - * - * @return IConnector - */ public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector; } diff --git a/src/Constants.php b/src/Constants.php index 2754f9d0..22f22b10 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -126,9 +126,9 @@ final class Constants ]; public const CONTROLS_ACTIONS_ROUTING_KEYS = [ - MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_ACTION, - MetadataTypes\RoutingKeyType::ROUTE_DEVICE_ACTION, - MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_ACTION, + MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_CONTROL_ACTION, + MetadataTypes\RoutingKeyType::ROUTE_DEVICE_CONTROL_ACTION, + MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_CONTROL_ACTION, ]; } diff --git a/src/Consumers/DataExchangeConsumer.php b/src/Consumers/DataExchangeConsumer.php index 5d26850e..eca49c8c 100644 --- a/src/Consumers/DataExchangeConsumer.php +++ b/src/Consumers/DataExchangeConsumer.php @@ -40,43 +40,30 @@ final class DataExchangeConsumer implements ExchangeConsumer\IConsumer use Nette\SmartObject; - /** @var Models\DataStorage\IConnectorPropertiesRepository */ private Models\DataStorage\IConnectorPropertiesRepository $connectorPropertiesRepository; - /** @var Models\DataStorage\IDevicePropertiesRepository */ private Models\DataStorage\IDevicePropertiesRepository $devicePropertiesRepository; - /** @var Models\DataStorage\IChannelPropertiesRepository */ private Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository; - /** @var Models\States\ConnectorPropertiesManager */ private Models\States\ConnectorPropertiesManager $connectorPropertiesStatesManager; - /** @var Models\States\ConnectorPropertiesRepository */ private Models\States\ConnectorPropertiesRepository $connectorPropertiesStatesRepository; - /** @var Models\States\DevicePropertiesManager */ private Models\States\DevicePropertiesManager $devicePropertiesStatesManager; - /** @var Models\States\DevicePropertiesRepository */ private Models\States\DevicePropertiesRepository $devicePropertiesStatesRepository; - /** @var Models\States\ChannelPropertiesManager */ private Models\States\ChannelPropertiesManager $channelPropertiesStatesManager; - /** @var Models\States\ChannelPropertiesRepository */ private Models\States\ChannelPropertiesRepository $channelPropertiesStatesRepository; - /** @var ExchangeEntities\EntityFactory */ private ExchangeEntities\EntityFactory $entityFactory; - /** @var ExchangePublisher\IPublisher|null */ private ?ExchangePublisher\IPublisher $publisher; - /** @var EventDispatcher\EventDispatcherInterface|null */ private ?EventDispatcher\EventDispatcherInterface $dispatcher; - /** @var Log\LoggerInterface */ private Log\LoggerInterface $logger; public function __construct( @@ -112,9 +99,6 @@ public function __construct( $this->logger = $logger ?? new Log\NullLogger(); } - /** - * {@inheritDoc} - */ public function consume( $source, MetadataTypes\RoutingKeyType $routingKey, @@ -285,12 +269,6 @@ public function consume( } } - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $property - * @param float|bool|int|string|null $expectedValue - * - * @return bool|float|int|string|null - */ private function normalizeValue( MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity $property, float|bool|int|string|null $expectedValue diff --git a/src/DI/DevicesModuleExtension.php b/src/DI/DevicesModuleExtension.php index 97e6998b..f3c371ae 100644 --- a/src/DI/DevicesModuleExtension.php +++ b/src/DI/DevicesModuleExtension.php @@ -47,12 +47,6 @@ class DevicesModuleExtension extends DI\CompilerExtension { - /** - * @param Nette\Configurator $config - * @param string $extensionName - * - * @return void - */ public static function register( Nette\Configurator $config, string $extensionName = 'fbDevicesModule' @@ -65,9 +59,6 @@ public static function register( }; } - /** - * {@inheritdoc} - */ public function getConfigSchema(): Schema\Schema { return Schema\Expect::structure([ @@ -75,9 +66,6 @@ public function getConfigSchema(): Schema\Schema ]); } - /** - * {@inheritDoc} - */ public function loadConfiguration(): void { $builder = $this->getContainerBuilder(); @@ -99,6 +87,9 @@ public function loadConfiguration(): void $builder->addDefinition($this->prefix('commands.initialize'), new DI\Definitions\ServiceDefinition()) ->setType(Commands\InitializeCommand::class); + $builder->addDefinition($this->prefix('commands.dataStorage'), new DI\Definitions\ServiceDefinition()) + ->setType(Commands\DataStorageCommand::class); + $builder->addDefinition($this->prefix('commands.connector'), new DI\Definitions\ServiceDefinition()) ->setType(Commands\ConnectorCommand::class); @@ -349,8 +340,7 @@ public function loadConfiguration(): void ->setType(DataStorage\Writer::class); $builder->addDefinition($this->prefix('dataStorage.reader'), new DI\Definitions\ServiceDefinition()) - ->setType(DataStorage\Reader::class) - ->addSetup('read'); + ->setType(DataStorage\Reader::class); $builder->addDefinition($this->prefix('dataStorage.repository.connectors'), new DI\Definitions\ServiceDefinition()) ->setType(Models\DataStorage\ConnectorsRepository::class); @@ -391,9 +381,6 @@ public function loadConfiguration(): void ->setType(Consumers\DataExchangeConsumer::class); } - /** - * {@inheritDoc} - */ public function beforeCompile(): void { parent::beforeCompile(); @@ -433,14 +420,13 @@ public function beforeCompile(): void } } - /** - * {@inheritDoc} - */ public function afterCompile( PhpGenerator\ClassType $class ): void { $builder = $this->getContainerBuilder(); + $initialize = $class->getMethods()['initialize']; + $entityFactoryServiceName = $builder->getByType(DoctrineCrud\Crud\IEntityCrudFactory::class, true); $devicesManagerService = $class->getMethod('createService' . ucfirst($this->name) . '__models__devicesManager'); @@ -472,6 +458,10 @@ public function afterCompile( $connectorsControlsManagerService = $class->getMethod('createService' . ucfirst($this->name) . '__models__connectorsControlsManager'); $connectorsControlsManagerService->setBody('return new ' . Models\Connectors\Controls\ControlsManager::class . '($this->getService(\'' . $entityFactoryServiceName . '\')->create(\'' . Entities\Connectors\Controls\Control::class . '\'));'); + + $dataStorageReaderServiceName = $builder->getByType(DataStorage\Reader::class, true); + + $initialize->addBody('$this->getService(\'' . $dataStorageReaderServiceName . '\')->read();'); } } diff --git a/src/DataStorage/Reader.php b/src/DataStorage/Reader.php index 387db486..4af70b96 100644 --- a/src/DataStorage/Reader.php +++ b/src/DataStorage/Reader.php @@ -17,11 +17,9 @@ use FastyBird\DevicesModule; use FastyBird\DevicesModule\Models; -use FastyBird\Metadata\Entities as MetadataEntities; use League\Flysystem; use Nette\Utils; use Ramsey\Uuid\Uuid; -use Throwable; /** * Data storage configuration reader @@ -34,67 +32,26 @@ final class Reader { - /** @var Models\DataStorage\IConnectorsRepository */ private Models\DataStorage\IConnectorsRepository $connectorsRepository; - /** @var Models\DataStorage\IConnectorPropertiesRepository */ private Models\DataStorage\IConnectorPropertiesRepository $connectorPropertiesRepository; - /** @var Models\DataStorage\IConnectorControlsRepository */ private Models\DataStorage\IConnectorControlsRepository $connectorControlsRepository; - /** @var Models\DataStorage\IDevicesRepository */ private Models\DataStorage\IDevicesRepository $devicesRepository; - /** @var Models\DataStorage\IDevicePropertiesRepository */ private Models\DataStorage\IDevicePropertiesRepository $devicePropertiesRepository; - /** @var Models\DataStorage\IDeviceControlsRepository */ private Models\DataStorage\IDeviceControlsRepository $deviceControlsRepository; - /** @var Models\DataStorage\IDeviceAttributesRepository */ private Models\DataStorage\IDeviceAttributesRepository $deviceAttributesRepository; - /** @var Models\DataStorage\IChannelsRepository */ private Models\DataStorage\IChannelsRepository $channelsRepository; - /** @var Models\DataStorage\IChannelPropertiesRepository */ private Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository; - /** @var Models\DataStorage\IChannelControlsRepository */ private Models\DataStorage\IChannelControlsRepository $channelControlsRepository; - /** @var MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $connectorEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory $connectorPropertyEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory $connectorControlEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\DeviceEntityFactory */ - private MetadataEntities\Modules\DevicesModule\DeviceEntityFactory $deviceEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\DevicePropertyEntityFactory */ - private MetadataEntities\Modules\DevicesModule\DevicePropertyEntityFactory $devicePropertyEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\DeviceControlEntityFactory */ - private MetadataEntities\Modules\DevicesModule\DeviceControlEntityFactory $deviceControlEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\DeviceAttributeEntityFactory */ - private MetadataEntities\Modules\DevicesModule\DeviceAttributeEntityFactory $deviceAttributeEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\ChannelEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ChannelEntityFactory $channelEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\ChannelPropertyEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ChannelPropertyEntityFactory $channelPropertyEntityFactory; - - /** @var MetadataEntities\Modules\DevicesModule\ChannelControlEntityFactory */ - private MetadataEntities\Modules\DevicesModule\ChannelControlEntityFactory $channelControlEntityFactory; - - /** @var Flysystem\Filesystem */ private Flysystem\Filesystem $filesystem; public function __construct( @@ -108,16 +65,6 @@ public function __construct( Models\DataStorage\IChannelsRepository $channelsRepository, Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository, Models\DataStorage\IChannelControlsRepository $channelControlsRepository, - MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $connectorEntityFactory, - MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory $connectorPropertyEntityFactory, - MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory $connectorControlEntityFactory, - MetadataEntities\Modules\DevicesModule\DeviceEntityFactory $deviceEntityFactory, - MetadataEntities\Modules\DevicesModule\DevicePropertyEntityFactory $devicePropertyEntityFactory, - MetadataEntities\Modules\DevicesModule\DeviceControlEntityFactory $deviceControlEntityFactory, - MetadataEntities\Modules\DevicesModule\DeviceAttributeEntityFactory $deviceAttributeEntityFactory, - MetadataEntities\Modules\DevicesModule\ChannelEntityFactory $channelEntityFactory, - MetadataEntities\Modules\DevicesModule\ChannelPropertyEntityFactory $channelPropertyEntityFactory, - MetadataEntities\Modules\DevicesModule\ChannelControlEntityFactory $channelControlEntityFactory, Flysystem\Filesystem $filesystem ) { $this->connectorsRepository = $connectorsRepository; @@ -131,17 +78,6 @@ public function __construct( $this->channelPropertiesRepository = $channelPropertiesRepository; $this->channelControlsRepository = $channelControlsRepository; - $this->connectorEntityFactory = $connectorEntityFactory; - $this->connectorPropertyEntityFactory = $connectorPropertyEntityFactory; - $this->connectorControlEntityFactory = $connectorControlEntityFactory; - $this->deviceEntityFactory = $deviceEntityFactory; - $this->devicePropertyEntityFactory = $devicePropertyEntityFactory; - $this->deviceControlEntityFactory = $deviceControlEntityFactory; - $this->deviceAttributeEntityFactory = $deviceAttributeEntityFactory; - $this->channelEntityFactory = $channelEntityFactory; - $this->channelPropertyEntityFactory = $channelPropertyEntityFactory; - $this->channelControlEntityFactory = $channelControlEntityFactory; - $this->filesystem = $filesystem; } @@ -245,19 +181,7 @@ public function read(): void continue; } - try { - $entity = $this->channelPropertyEntityFactory->create(Utils\Json::encode($propertyData)); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity - ) { - $this->channelPropertiesRepository->append($entity); - } - } catch (Throwable $ex) { - continue; - } + $this->channelPropertiesRepository->append(Uuid::fromString($propertyId), $propertyData); } foreach ($channelData[DevicesModule\Constants::DATA_STORAGE_CONTROLS_KEY] as $controlId => $controlData) { @@ -271,18 +195,10 @@ public function read(): void continue; } - try { - $this->channelControlsRepository->append($this->channelControlEntityFactory->create(Utils\Json::encode($controlData))); - } catch (Throwable $ex) { - continue; - } + $this->channelControlsRepository->append(Uuid::fromString($controlId), $controlData); } - try { - $this->channelsRepository->append($this->channelEntityFactory->create(Utils\Json::encode($channelData))); - } catch (Throwable $ex) { - continue; - } + $this->channelsRepository->append(Uuid::fromString($channelId), $channelData); } foreach ($deviceData[DevicesModule\Constants::DATA_STORAGE_PROPERTIES_KEY] as $propertyId => $propertyData) { @@ -296,20 +212,7 @@ public function read(): void continue; } - try { - $entity = $this->devicePropertyEntityFactory->create(Utils\Json::encode($propertyData)); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity - ) { - $this->devicePropertiesRepository->append($entity); - } - } catch (Throwable $ex) { - var_dump($ex->getMessage()); - continue; - } + $this->devicePropertiesRepository->append(Uuid::fromString($propertyId), $propertyData); } foreach ($deviceData[DevicesModule\Constants::DATA_STORAGE_CONTROLS_KEY] as $controlId => $controlData) { @@ -323,11 +226,7 @@ public function read(): void continue; } - try { - $this->deviceControlsRepository->append($this->deviceControlEntityFactory->create(Utils\Json::encode($controlData))); - } catch (Throwable $ex) { - continue; - } + $this->deviceControlsRepository->append(Uuid::fromString($controlId), $controlData); } foreach ($deviceData[DevicesModule\Constants::DATA_STORAGE_ATTRIBUTES_KEY] as $attributeId => $attributeData) { @@ -341,18 +240,10 @@ public function read(): void continue; } - try { - $this->deviceAttributesRepository->append($this->deviceAttributeEntityFactory->create(Utils\Json::encode($attributeData))); - } catch (Throwable $ex) { - continue; - } + $this->deviceAttributesRepository->append(Uuid::fromString($attributeId), $attributeData); } - try { - $this->devicesRepository->append($this->deviceEntityFactory->create(Utils\Json::encode($deviceData))); - } catch (Throwable $ex) { - continue; - } + $this->devicesRepository->append(Uuid::fromString($deviceId), $deviceData); } foreach ($connectorData[DevicesModule\Constants::DATA_STORAGE_PROPERTIES_KEY] as $propertyId => $propertyData) { @@ -366,19 +257,7 @@ public function read(): void continue; } - try { - $entity = $this->connectorPropertyEntityFactory->create(Utils\Json::encode($propertyData)); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity - ) { - $this->connectorPropertiesRepository->append($entity); - } - } catch (Throwable $ex) { - continue; - } + $this->connectorPropertiesRepository->append(Uuid::fromString($propertyId), $propertyData); } foreach ($connectorData[DevicesModule\Constants::DATA_STORAGE_CONTROLS_KEY] as $controlId => $controlData) { @@ -392,18 +271,10 @@ public function read(): void continue; } - try { - $this->connectorControlsRepository->append($this->connectorControlEntityFactory->create(Utils\Json::encode($controlData))); - } catch (Throwable $ex) { - continue; - } + $this->connectorControlsRepository->append(Uuid::fromString($controlId), $controlData); } - try { - $this->connectorsRepository->append($this->connectorEntityFactory->create(Utils\Json::encode($connectorData))); - } catch (Throwable $ex) { - continue; - } + $this->connectorsRepository->append(Uuid::fromString($connectorId), $connectorData); } } diff --git a/src/DataStorage/Writer.php b/src/DataStorage/Writer.php index 2411ef14..d9ad9ee9 100644 --- a/src/DataStorage/Writer.php +++ b/src/DataStorage/Writer.php @@ -34,13 +34,10 @@ final class Writer { - /** @var Models\Connectors\IConnectorsRepository */ private Models\Connectors\IConnectorsRepository $connectorsRepository; - /** @var Flysystem\Filesystem */ private Flysystem\Filesystem $filesystem; - /** @var EventDispatcher\EventDispatcherInterface|null */ private ?EventDispatcher\EventDispatcherInterface $dispatcher; public function __construct( diff --git a/src/Entities/Channels/Properties/MappedProperty.php b/src/Entities/Channels/Properties/MappedProperty.php index 4a4cca2c..561ea81f 100644 --- a/src/Entities/Channels/Properties/MappedProperty.php +++ b/src/Entities/Channels/Properties/MappedProperty.php @@ -18,6 +18,7 @@ use Doctrine\ORM\Mapping as ORM; use FastyBird\DevicesModule\Entities; use FastyBird\DevicesModule\Exceptions; +use FastyBird\DevicesModule\Utilities; use FastyBird\Metadata\Types as MetadataTypes; use Ramsey\Uuid; use Throwable; @@ -74,8 +75,8 @@ public function toArray(): array { if ($this->getParent() instanceof Entities\Channels\Properties\StaticProperty) { return array_merge(parent::toArray(), [ - 'value' => $this->getValue(), - 'default' => $this->getDefault(), + 'default' => Utilities\ValueHelper::flattenValue($this->getDefault()), + 'value' => Utilities\ValueHelper::flattenValue($this->getValue()), ]); } diff --git a/src/Entities/Channels/Properties/StaticProperty.php b/src/Entities/Channels/Properties/StaticProperty.php index 7e9f65f5..ff194fbd 100644 --- a/src/Entities/Channels/Properties/StaticProperty.php +++ b/src/Entities/Channels/Properties/StaticProperty.php @@ -58,15 +58,4 @@ public function setValue(?string $value): void parent::setValue($value); } - /** - * {@inheritDoc} - */ - public function toArray(): array - { - return array_merge(parent::toArray(), [ - 'value' => $this->getValue(), - 'default' => $this->getDefault(), - ]); - } - } diff --git a/src/Entities/Devices/Properties/MappedProperty.php b/src/Entities/Devices/Properties/MappedProperty.php index 16e8d292..9d8f5d84 100644 --- a/src/Entities/Devices/Properties/MappedProperty.php +++ b/src/Entities/Devices/Properties/MappedProperty.php @@ -18,6 +18,7 @@ use Doctrine\ORM\Mapping as ORM; use FastyBird\DevicesModule\Entities; use FastyBird\DevicesModule\Exceptions; +use FastyBird\DevicesModule\Utilities; use FastyBird\Metadata\Types as MetadataTypes; use Ramsey\Uuid; use Throwable; @@ -74,8 +75,8 @@ public function toArray(): array { if ($this->getParent() instanceof Entities\Devices\Properties\StaticProperty) { return array_merge(parent::toArray(), [ - 'value' => $this->getValue(), - 'default' => $this->getDefault(), + 'default' => Utilities\ValueHelper::flattenValue($this->getDefault()), + 'value' => Utilities\ValueHelper::flattenValue($this->getValue()), ]); } diff --git a/src/Entities/Devices/Properties/StaticProperty.php b/src/Entities/Devices/Properties/StaticProperty.php index 40daca4b..b3ef12c8 100644 --- a/src/Entities/Devices/Properties/StaticProperty.php +++ b/src/Entities/Devices/Properties/StaticProperty.php @@ -58,15 +58,4 @@ public function setValue(?string $value): void parent::setValue($value); } - /** - * {@inheritDoc} - */ - public function toArray(): array - { - return array_merge(parent::toArray(), [ - 'value' => $this->getValue(), - 'default' => $this->getDefault(), - ]); - } - } diff --git a/src/Entities/IEntity.php b/src/Entities/IEntity.php index 27cb7150..040f8c1d 100644 --- a/src/Entities/IEntity.php +++ b/src/Entities/IEntity.php @@ -41,7 +41,7 @@ public function getId(): Uuid\UuidInterface; public function getPlainId(): string; /** - * @return mixed[] + * @return Array */ public function toArray(): array; diff --git a/src/Entities/IEntityParams.php b/src/Entities/IEntityParams.php index a298a297..105be6df 100644 --- a/src/Entities/IEntityParams.php +++ b/src/Entities/IEntityParams.php @@ -29,7 +29,7 @@ interface IEntityParams { /** - * @param mixed[] $params + * @param Array $params * * @return void */ diff --git a/src/Entities/Property.php b/src/Entities/Property.php index 60c4be81..21a7239a 100644 --- a/src/Entities/Property.php +++ b/src/Entities/Property.php @@ -511,7 +511,7 @@ public function setDefault(?string $default): void */ public function toArray(): array { - return [ + $data = [ 'id' => $this->getPlainId(), 'type' => $this->getType()->getValue(), 'identifier' => $this->getIdentifier(), @@ -524,6 +524,15 @@ public function toArray(): array 'invalid' => $this->getInvalid(), 'number_of_decimals' => $this->getNumberOfDecimals(), ]; + + if ($this->getType()->equalsValue(MetadataTypes\PropertyTypeType::TYPE_STATIC)) { + return array_merge($data, [ + 'default' => Utilities\ValueHelper::flattenValue($this->getDefault()), + 'value' => Utilities\ValueHelper::flattenValue($this->getValue()), + ]); + } + + return $data; } } diff --git a/src/Entities/TEntityParams.php b/src/Entities/TEntityParams.php index aaace3f5..77aa3b08 100644 --- a/src/Entities/TEntityParams.php +++ b/src/Entities/TEntityParams.php @@ -31,7 +31,7 @@ trait TEntityParams { /** - * @var mixed[]|null + * @var Array|null * * @IPubDoctrine\Crud(is="writable") * @ORM\Column(type="json", name="params", nullable=true) @@ -47,7 +47,7 @@ public function getParams(): Utils\ArrayHash } /** - * @param mixed[] $params + * @param Array $params * * @return void */ diff --git a/src/Events/AfterConnectorStartEvent.php b/src/Events/AfterConnectorStartEvent.php index 36970b94..63147cb2 100644 --- a/src/Events/AfterConnectorStartEvent.php +++ b/src/Events/AfterConnectorStartEvent.php @@ -29,21 +29,14 @@ class AfterConnectorStartEvent extends EventDispatcher\Event { - /** @var MetadataEntities\Modules\DevicesModule\IConnectorEntity */ private MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector; - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector - */ public function __construct( MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector ) { $this->connector = $connector; } - /** - * @return MetadataEntities\Modules\DevicesModule\IConnectorEntity - */ public function getConnector(): MetadataEntities\Modules\DevicesModule\IConnectorEntity { return $this->connector; diff --git a/src/Events/AfterConnectorTerminateEvent.php b/src/Events/AfterConnectorTerminateEvent.php index b62dc82a..374d83bb 100644 --- a/src/Events/AfterConnectorTerminateEvent.php +++ b/src/Events/AfterConnectorTerminateEvent.php @@ -29,21 +29,14 @@ class AfterConnectorTerminateEvent extends EventDispatcher\Event { - /** @var Connectors\IConnector */ private Connectors\IConnector $connector; - /** - * @param Connectors\IConnector $connector - */ public function __construct( Connectors\IConnector $connector ) { $this->connector = $connector; } - /** - * @return Connectors\IConnector - */ public function getConnector(): Connectors\IConnector { return $this->connector; diff --git a/src/Events/BeforeConnectorStartEvent.php b/src/Events/BeforeConnectorStartEvent.php index f2b50e0b..7827d596 100644 --- a/src/Events/BeforeConnectorStartEvent.php +++ b/src/Events/BeforeConnectorStartEvent.php @@ -29,21 +29,14 @@ class BeforeConnectorStartEvent extends EventDispatcher\Event { - /** @var MetadataEntities\Modules\DevicesModule\IConnectorEntity */ private MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector; - /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector - */ public function __construct( MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector ) { $this->connector = $connector; } - /** - * @return MetadataEntities\Modules\DevicesModule\IConnectorEntity - */ public function getConnector(): MetadataEntities\Modules\DevicesModule\IConnectorEntity { return $this->connector; diff --git a/src/Events/BeforeConnectorTerminateEvent.php b/src/Events/BeforeConnectorTerminateEvent.php index 7ce44676..9f2a2edd 100644 --- a/src/Events/BeforeConnectorTerminateEvent.php +++ b/src/Events/BeforeConnectorTerminateEvent.php @@ -29,21 +29,14 @@ class BeforeConnectorTerminateEvent extends EventDispatcher\Event { - /** @var Connectors\IConnector */ private Connectors\IConnector $connector; - /** - * @param Connectors\IConnector $connector - */ public function __construct( Connectors\IConnector $connector ) { $this->connector = $connector; } - /** - * @return Connectors\IConnector - */ public function getConnector(): Connectors\IConnector { return $this->connector; diff --git a/src/Events/StateEntityCreatedEvent.php b/src/Events/StateEntityCreatedEvent.php index 1bb8b981..a17e137d 100644 --- a/src/Events/StateEntityCreatedEvent.php +++ b/src/Events/StateEntityCreatedEvent.php @@ -31,10 +31,8 @@ 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; public function __construct( @@ -45,17 +43,11 @@ public function __construct( $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 - */ public function getState(): States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty { return $this->state; diff --git a/src/Events/StateEntityDeletedEvent.php b/src/Events/StateEntityDeletedEvent.php index a82e01ec..15be8e2b 100644 --- a/src/Events/StateEntityDeletedEvent.php +++ b/src/Events/StateEntityDeletedEvent.php @@ -30,7 +30,6 @@ class StateEntityDeletedEvent 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; public function __construct( @@ -39,9 +38,6 @@ public function __construct( $this->property = $property; } - /** - * @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; diff --git a/src/Events/StateEntityUpdatedEvent.php b/src/Events/StateEntityUpdatedEvent.php index ec2f1316..10f1dd4a 100644 --- a/src/Events/StateEntityUpdatedEvent.php +++ b/src/Events/StateEntityUpdatedEvent.php @@ -31,13 +31,10 @@ 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; public function __construct( @@ -50,25 +47,16 @@ public function __construct( $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 - */ public function getPreviousState(): States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty { return $this->previousState; } - /** - * @return States\IChannelProperty|States\IConnectorProperty|States\IDeviceProperty - */ public function getState(): States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty { return $this->state; diff --git a/src/Models/DataStorage/ChannelControlsRepository.php b/src/Models/DataStorage/ChannelControlsRepository.php index a1a32c4b..b05a1bba 100644 --- a/src/Models/DataStorage/ChannelControlsRepository.php +++ b/src/Models/DataStorage/ChannelControlsRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage channel controls repository @@ -38,25 +38,28 @@ final class ChannelControlsRepository implements IChannelControlsRepository, Cou use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $controls; + /** @var Array> */ + private array $controls; - public function __construct() - { - $this->controls = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\ChannelControlEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\ChannelControlEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->controls = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IChannelControlEntity { - $this->controls->rewind(); - - foreach ($this->controls as $control) { - if ($control->getId()->equals($id)) { - return $control; - } + if (array_key_exists($id->toString(), $this->controls)) { + return $this->entityFactory->create($this->controls[$id->toString()]); } return null; @@ -64,16 +67,16 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByChannel(Uuid\UuidInterface $channel): array { $controls = []; - $this->controls->rewind(); - foreach ($this->controls as $control) { - if ($control->getChannel()->equals($channel)) { - $controls[] = $control; + if (array_key_exists('channel', $control) && $channel->toString() === $control['channel']) { + $controls[] = $this->entityFactory->create($control); } } @@ -83,17 +86,9 @@ public function findAllByChannel(Uuid\UuidInterface $channel): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IChannelControlEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->controls->detach($existing); - } - - if (!$this->controls->contains($entity)) { - $this->controls->attach($entity, $entity->getId()->toString()); - } + $this->controls[$id->toString()] = $entity; } /** @@ -101,7 +96,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IChannelControlEnt */ public function reset(): void { - $this->controls = new SplObjectStorage(); + $this->controls = []; } /** @@ -109,18 +104,20 @@ public function reset(): void */ public function count(): int { - return $this->controls->count(); + return count($this->controls); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $controls = []; foreach ($this->controls as $control) { - $controls[] = $control; + $controls[] = $this->entityFactory->create($control); } return new RecursiveArrayIterator($controls); diff --git a/src/Models/DataStorage/ChannelPropertiesRepository.php b/src/Models/DataStorage/ChannelPropertiesRepository.php index 3a366359..410a123d 100644 --- a/src/Models/DataStorage/ChannelPropertiesRepository.php +++ b/src/Models/DataStorage/ChannelPropertiesRepository.php @@ -20,12 +20,11 @@ use FastyBird\DevicesModule\Models; use FastyBird\Metadata\Entities as MetadataEntities; use FastyBird\Metadata\Exceptions as MetadataExceptions; +use FastyBird\Metadata\Types as MetadataTypes; use IteratorAggregate; use Nette; -use Nette\Utils; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage channel properties repository @@ -42,13 +41,11 @@ final class ChannelPropertiesRepository implements IChannelPropertiesRepository, use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $properties; + /** @var Array> */ + private array $properties; - /** @var Models\States\ChannelPropertiesRepository */ private Models\States\ChannelPropertiesRepository $statesRepository; - /** @var MetadataEntities\Modules\DevicesModule\ChannelPropertyEntityFactory */ private MetadataEntities\Modules\DevicesModule\ChannelPropertyEntityFactory $entityFactory; public function __construct( @@ -58,21 +55,43 @@ public function __construct( $this->statesRepository = $statesRepository; $this->entityFactory = $entityFactory; - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById( Uuid\UuidInterface $id ): MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|null { - $this->properties->rewind(); + if (array_key_exists($id->toString(), $this->properties)) { + $data = $this->properties[$id->toString()]; + + $state = []; + + if ( + array_key_exists('type', $data) + && ( + $data['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $data['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState($id); + } - foreach ($this->properties as $property) { - if ($property->getId()->equals($id)) { - return $property; + $entity = $this->entityFactory->create(array_merge($state, $data)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity + ) { + return $entity; } + + return null; } return null; @@ -80,19 +99,43 @@ public function findById( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $channel, string $identifier ): MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|null { - $this->properties->rewind(); - - foreach ($this->properties as $property) { + foreach ($this->properties as $id => $property) { if ( - $property->getChannel()->equals($channel) - && $property->getIdentifier() === $identifier + array_key_exists('channel', $property) + && $channel->toString() === $property['channel'] + && array_key_exists('identifier', $property) + && $property['identifier'] === $identifier ) { - return $property; + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity + ) { + return $entity; + } + + return null; } } @@ -101,16 +144,36 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByChannel(Uuid\UuidInterface $channel): array { $properties = []; - $this->properties->rewind(); - - foreach ($this->properties as $property) { - if ($property->getChannel()->equals($channel)) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + if (array_key_exists('channel', $property) && $channel->toString() === $property['channel']) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity + ) { + $properties[] = $entity; + } } } @@ -119,60 +182,10 @@ public function findAllByChannel(Uuid\UuidInterface $channel): array /** * {@inheritDoc} - * - * @throws Utils\JsonException - * @throws MetadataExceptions\FileNotFoundException - */ - public function append( - MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity $entity - ): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->properties->detach($existing); - } - - if (!$this->properties->contains($entity)) { - $entity = $this->entityFactory->create( - Utils\Json::encode(array_merge( - $entity->toArray(), - $this->loadPropertyState($entity->getId()) - )) - ); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity - ) { - $this->properties->attach($entity, $entity->getId()->toString()); - } - } - } - - /** - * @param Uuid\UuidInterface|null $id - * - * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException */ - public function loadState(?Uuid\UuidInterface $id = null): void + public function append(Uuid\UuidInterface $id, array $entity): void { - if ($id === null) { - foreach ($this->properties as $property) { - $this->append($property); - } - } else { - $property = $this->findById($id); - - if ($property === null) { - return; - } - - $this->append($property); - } + $this->properties[$id->toString()] = $entity; } /** @@ -180,7 +193,7 @@ public function loadState(?Uuid\UuidInterface $id = null): void */ public function reset(): void { - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** @@ -188,18 +201,40 @@ public function reset(): void */ public function count(): int { - return $this->properties->count(); + return count($this->properties); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $properties = []; - foreach ($this->properties as $property) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity + ) { + $properties[] = $entity; + } } return new RecursiveArrayIterator($properties); @@ -208,7 +243,7 @@ public function getIterator(): RecursiveArrayIterator /** * @param Uuid\UuidInterface $id * - * @return mixed[] + * @return Array */ private function loadPropertyState(Uuid\UuidInterface $id): array { diff --git a/src/Models/DataStorage/ChannelsRepository.php b/src/Models/DataStorage/ChannelsRepository.php index e4019e0d..657e2bca 100644 --- a/src/Models/DataStorage/ChannelsRepository.php +++ b/src/Models/DataStorage/ChannelsRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage channels repository @@ -38,25 +38,28 @@ final class ChannelsRepository implements IChannelsRepository, Countable, Iterat use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $channels; + /** @var Array> */ + private array $channels; - public function __construct() - { - $this->channels = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\ChannelEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\ChannelEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->channels = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IChannelEntity { - $this->channels->rewind(); - - foreach ($this->channels as $channel) { - if ($channel->getId()->equals($id)) { - return $channel; - } + if (array_key_exists($id->toString(), $this->channels)) { + return $this->entityFactory->create($this->channels[$id->toString()]); } return null; @@ -64,19 +67,21 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $device, string $identifier ): ?MetadataEntities\Modules\DevicesModule\IChannelEntity { - $this->channels->rewind(); - foreach ($this->channels as $channel) { if ( - $channel->getDevice()->equals($device) - && $channel->getIdentifier() === $identifier + array_key_exists('device', $channel) + && $device->toString() === $channel['device'] + && array_key_exists('identifier', $channel) + && $channel['identifier'] === $identifier ) { - return $channel; + return $this->entityFactory->create($channel); } } @@ -85,16 +90,16 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByDevice(Uuid\UuidInterface $device): array { $channels = []; - $this->channels->rewind(); - foreach ($this->channels as $channel) { - if ($channel->getDevice()->equals($device)) { - $channels[] = $channel; + if (array_key_exists('device', $channel) && $device->toString() === $channel['device']) { + $channels[] = $this->entityFactory->create($channel); } } @@ -104,17 +109,9 @@ public function findAllByDevice(Uuid\UuidInterface $device): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IChannelEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->channels->detach($existing); - } - - if (!$this->channels->contains($entity)) { - $this->channels->attach($entity, $entity->getId()->toString()); - } + $this->channels[$id->toString()] = $entity; } /** @@ -122,7 +119,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IChannelEntity $en */ public function reset(): void { - $this->channels = new SplObjectStorage(); + $this->channels = []; } /** @@ -130,18 +127,20 @@ public function reset(): void */ public function count(): int { - return $this->channels->count(); + return count($this->channels); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $channels = []; foreach ($this->channels as $channel) { - $channels[] = $channel; + $channels[] = $this->entityFactory->create($channel); } return new RecursiveArrayIterator($channels); diff --git a/src/Models/DataStorage/ConnectorControlsRepository.php b/src/Models/DataStorage/ConnectorControlsRepository.php index 8f317c46..1fb05178 100644 --- a/src/Models/DataStorage/ConnectorControlsRepository.php +++ b/src/Models/DataStorage/ConnectorControlsRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage connector controls repository @@ -38,25 +38,28 @@ final class ConnectorControlsRepository implements IConnectorControlsRepository, use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $controls; + /** @var Array> */ + private array $controls; - public function __construct() - { - $this->controls = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->controls = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IConnectorControlEntity { - $this->controls->rewind(); - - foreach ($this->controls as $control) { - if ($control->getId()->equals($id)) { - return $control; - } + if (array_key_exists($id->toString(), $this->controls)) { + return $this->entityFactory->create($this->controls[$id->toString()]); } return null; @@ -64,16 +67,16 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByConnector(Uuid\UuidInterface $connector): array { $controls = []; - $this->controls->rewind(); - foreach ($this->controls as $control) { - if ($control->getConnector()->equals($connector)) { - $controls[] = $control; + if (array_key_exists('connector', $control) && $connector->toString() === $control['connector']) { + $controls[] = $this->entityFactory->create($control); } } @@ -83,17 +86,9 @@ public function findAllByConnector(Uuid\UuidInterface $connector): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IConnectorControlEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->controls->detach($existing); - } - - if (!$this->controls->contains($entity)) { - $this->controls->attach($entity, $entity->getId()->toString()); - } + $this->controls[$id->toString()] = $entity; } /** @@ -101,7 +96,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IConnectorControlE */ public function reset(): void { - $this->controls = new SplObjectStorage(); + $this->controls = []; } /** @@ -109,18 +104,20 @@ public function reset(): void */ public function count(): int { - return $this->controls->count(); + return count($this->controls); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $controls = []; foreach ($this->controls as $control) { - $controls[] = $control; + $controls[] = $this->entityFactory->create($control); } return new RecursiveArrayIterator($controls); diff --git a/src/Models/DataStorage/ConnectorPropertiesRepository.php b/src/Models/DataStorage/ConnectorPropertiesRepository.php index 48cdfbc2..5699f26d 100644 --- a/src/Models/DataStorage/ConnectorPropertiesRepository.php +++ b/src/Models/DataStorage/ConnectorPropertiesRepository.php @@ -20,12 +20,11 @@ use FastyBird\DevicesModule\Models; use FastyBird\Metadata\Entities as MetadataEntities; use FastyBird\Metadata\Exceptions as MetadataExceptions; +use FastyBird\Metadata\Types as MetadataTypes; use IteratorAggregate; use Nette; -use Nette\Utils; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage connector properties repository @@ -42,13 +41,11 @@ final class ConnectorPropertiesRepository implements IConnectorPropertiesReposit use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $properties; + /** @var Array> */ + private array $properties; - /** @var Models\States\ConnectorPropertiesRepository */ private Models\States\ConnectorPropertiesRepository $statesRepository; - /** @var MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory */ private MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory $entityFactory; public function __construct( @@ -58,21 +55,43 @@ public function __construct( $this->statesRepository = $statesRepository; $this->entityFactory = $entityFactory; - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById( Uuid\UuidInterface $id ): MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity|null { - $this->properties->rewind(); + if (array_key_exists($id->toString(), $this->properties)) { + $data = $this->properties[$id->toString()]; + + $state = []; + + if ( + array_key_exists('type', $data) + && ( + $data['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $data['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState($id); + } - foreach ($this->properties as $property) { - if ($property->getId()->equals($id)) { - return $property; + $entity = $this->entityFactory->create(array_merge($state, $data)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity + ) { + return $entity; } + + return null; } return null; @@ -80,19 +99,43 @@ public function findById( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $connector, string $identifier ): MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity|null { - $this->properties->rewind(); - - foreach ($this->properties as $property) { + foreach ($this->properties as $id => $property) { if ( - $property->getConnector()->equals($connector) - && $property->getIdentifier() === $identifier + array_key_exists('connector', $property) + && $connector->toString() === $property['connector'] + && array_key_exists('identifier', $property) + && $property['identifier'] === $identifier ) { - return $property; + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity + ) { + return $entity; + } + + return null; } } @@ -101,16 +144,36 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByConnector(Uuid\UuidInterface $connector): array { $properties = []; - $this->properties->rewind(); - - foreach ($this->properties as $property) { - if ($property->getConnector()->equals($connector)) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + if (array_key_exists('connector', $property) && $connector->toString() === $property['connector']) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity + ) { + $properties[] = $entity; + } } } @@ -119,60 +182,10 @@ public function findAllByConnector(Uuid\UuidInterface $connector): array /** * {@inheritDoc} - * - * @throws Utils\JsonException - * @throws MetadataExceptions\FileNotFoundException */ - public function append( - MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity $entity - ): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->properties->detach($existing); - } - - if (!$this->properties->contains($entity)) { - $entity = $this->entityFactory->create( - Utils\Json::encode(array_merge( - $entity->toArray(), - $this->loadPropertyState($entity->getId()) - )) - ); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity - ) { - $this->properties->attach($entity, $entity->getId()->toString()); - } - } - } - - /** - * @param Uuid\UuidInterface|null $id - * - * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException - */ - public function loadState(?Uuid\UuidInterface $id = null): void + public function append(Uuid\UuidInterface $id, array $entity): void { - if ($id === null) { - foreach ($this->properties as $property) { - $this->append($property); - } - } else { - $property = $this->findById($id); - - if ($property === null) { - return; - } - - $this->append($property); - } + $this->properties[$id->toString()] = $entity; } /** @@ -180,7 +193,7 @@ public function loadState(?Uuid\UuidInterface $id = null): void */ public function reset(): void { - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** @@ -188,18 +201,40 @@ public function reset(): void */ public function count(): int { - return $this->properties->count(); + return count($this->properties); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $properties = []; - foreach ($this->properties as $property) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity + ) { + $properties[] = $entity; + } } return new RecursiveArrayIterator($properties); @@ -208,7 +243,7 @@ public function getIterator(): RecursiveArrayIterator /** * @param Uuid\UuidInterface $id * - * @return mixed[] + * @return Array */ private function loadPropertyState(Uuid\UuidInterface $id): array { diff --git a/src/Models/DataStorage/ConnectorsRepository.php b/src/Models/DataStorage/ConnectorsRepository.php index 4fd170f2..6304f8a1 100644 --- a/src/Models/DataStorage/ConnectorsRepository.php +++ b/src/Models/DataStorage/ConnectorsRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage connectors repository @@ -38,25 +38,28 @@ final class ConnectorsRepository implements IConnectorsRepository, Countable, It use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $connectors; + /** @var Array> */ + private array $connectors; - public function __construct() - { - $this->connectors = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->connectors = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IConnectorEntity { - $this->connectors->rewind(); - - foreach ($this->connectors as $connector) { - if ($connector->getId()->equals($id)) { - return $connector; - } + if (array_key_exists($id->toString(), $this->connectors)) { + return $this->entityFactory->create($this->connectors[$id->toString()]); } return null; @@ -64,14 +67,17 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier(string $identifier): ?MetadataEntities\Modules\DevicesModule\IConnectorEntity { - $this->connectors->rewind(); - foreach ($this->connectors as $connector) { - if ($connector->getIdentifier() === $identifier) { - return $connector; + if ( + array_key_exists('identifier', $connector) + && $connector['identifier'] === $identifier + ) { + return $this->entityFactory->create($connector); } } @@ -81,17 +87,9 @@ public function findByIdentifier(string $identifier): ?MetadataEntities\Modules\ /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IConnectorEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->connectors->detach($existing); - } - - if (!$this->connectors->contains($entity)) { - $this->connectors->attach($entity, $entity->getId()->toString()); - } + $this->connectors[$id->toString()] = $entity; } /** @@ -99,7 +97,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IConnectorEntity $ */ public function reset(): void { - $this->connectors = new SplObjectStorage(); + $this->connectors = []; } /** @@ -107,18 +105,20 @@ public function reset(): void */ public function count(): int { - return $this->connectors->count(); + return count($this->connectors); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $connectors = []; foreach ($this->connectors as $connector) { - $connectors[] = $connector; + $connectors[] = $this->entityFactory->create($connector); } return new RecursiveArrayIterator($connectors); diff --git a/src/Models/DataStorage/DeviceAttributesRepository.php b/src/Models/DataStorage/DeviceAttributesRepository.php index a039ab66..b409cb3a 100644 --- a/src/Models/DataStorage/DeviceAttributesRepository.php +++ b/src/Models/DataStorage/DeviceAttributesRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage device attributes repository @@ -38,25 +38,28 @@ final class DeviceAttributesRepository implements IDeviceAttributesRepository, C use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $attributes; + /** @var Array> */ + private array $attributes; - public function __construct() - { - $this->attributes = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\DeviceAttributeEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\DeviceAttributeEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->attributes = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IDeviceAttributeEntity { - $this->attributes->rewind(); - - foreach ($this->attributes as $attribute) { - if ($attribute->getId()->equals($id)) { - return $attribute; - } + if (array_key_exists($id->toString(), $this->attributes)) { + return $this->entityFactory->create($this->attributes[$id->toString()]); } return null; @@ -64,19 +67,21 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $device, string $identifier ): ?MetadataEntities\Modules\DevicesModule\IDeviceAttributeEntity { - $this->attributes->rewind(); - foreach ($this->attributes as $attribute) { if ( - $attribute->getDevice()->equals($device) - && $attribute->getIdentifier() === $identifier + array_key_exists('device', $attribute) + && $device->toString() === $attribute['device'] + && array_key_exists('identifier', $attribute) + && $attribute['identifier'] === $identifier ) { - return $attribute; + return $this->entityFactory->create($attribute); } } @@ -85,16 +90,16 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByDevice(Uuid\UuidInterface $device): array { $attributes = []; - $this->attributes->rewind(); - foreach ($this->attributes as $attribute) { - if ($attribute->getDevice()->equals($device)) { - $attributes[] = $attribute; + if (array_key_exists('device', $attribute) && $device->toString() === $attribute['device']) { + $attributes[] = $this->entityFactory->create($attribute); } } @@ -104,17 +109,9 @@ public function findAllByDevice(Uuid\UuidInterface $device): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceAttributeEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->attributes->detach($existing); - } - - if (!$this->attributes->contains($entity)) { - $this->attributes->attach($entity, $entity->getId()->toString()); - } + $this->attributes[$id->toString()] = $entity; } /** @@ -122,7 +119,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IDeviceAttributeEn */ public function reset(): void { - $this->attributes = new SplObjectStorage(); + $this->attributes = []; } /** @@ -130,18 +127,20 @@ public function reset(): void */ public function count(): int { - return $this->attributes->count(); + return count($this->attributes); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $attributes = []; foreach ($this->attributes as $attribute) { - $attributes[] = $attribute; + $attributes[] = $this->entityFactory->create($attribute); } return new RecursiveArrayIterator($attributes); diff --git a/src/Models/DataStorage/DeviceControlsRepository.php b/src/Models/DataStorage/DeviceControlsRepository.php index 4d73d021..ae152049 100644 --- a/src/Models/DataStorage/DeviceControlsRepository.php +++ b/src/Models/DataStorage/DeviceControlsRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage device controls repository @@ -38,25 +38,28 @@ final class DeviceControlsRepository implements IDeviceControlsRepository, Count use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $controls; + /** @var Array> */ + private array $controls; - public function __construct() - { - $this->controls = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\DeviceControlEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\DeviceControlEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->controls = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IDeviceControlEntity { - $this->controls->rewind(); - - foreach ($this->controls as $control) { - if ($control->getId()->equals($id)) { - return $control; - } + if (array_key_exists($id->toString(), $this->controls)) { + return $this->entityFactory->create($this->controls[$id->toString()]); } return null; @@ -64,16 +67,16 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByDevice(Uuid\UuidInterface $device): array { $controls = []; - $this->controls->rewind(); - foreach ($this->controls as $control) { - if ($control->getDevice()->equals($device)) { - $controls[] = $control; + if (array_key_exists('device', $control) && $device->toString() === $control['device']) { + $controls[] = $this->entityFactory->create($control); } } @@ -83,17 +86,9 @@ public function findAllByDevice(Uuid\UuidInterface $device): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceControlEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->controls->detach($existing); - } - - if (!$this->controls->contains($entity)) { - $this->controls->attach($entity, $entity->getId()->toString()); - } + $this->controls[$id->toString()] = $entity; } /** @@ -101,7 +96,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IDeviceControlEnti */ public function reset(): void { - $this->controls = new SplObjectStorage(); + $this->controls = []; } /** @@ -109,18 +104,20 @@ public function reset(): void */ public function count(): int { - return $this->controls->count(); + return count($this->controls); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $controls = []; foreach ($this->controls as $control) { - $controls[] = $control; + $controls[] = $this->entityFactory->create($control); } return new RecursiveArrayIterator($controls); diff --git a/src/Models/DataStorage/DevicePropertiesRepository.php b/src/Models/DataStorage/DevicePropertiesRepository.php index d1381589..c0470532 100644 --- a/src/Models/DataStorage/DevicePropertiesRepository.php +++ b/src/Models/DataStorage/DevicePropertiesRepository.php @@ -20,12 +20,11 @@ use FastyBird\DevicesModule\Models; use FastyBird\Metadata\Entities as MetadataEntities; use FastyBird\Metadata\Exceptions as MetadataExceptions; +use FastyBird\Metadata\Types as MetadataTypes; use IteratorAggregate; use Nette; -use Nette\Utils; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage device properties repository @@ -42,13 +41,11 @@ final class DevicePropertiesRepository implements IDevicePropertiesRepository, C use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $properties; + /** @var Array> */ + private array $properties; - /** @var Models\States\DevicePropertiesRepository */ private Models\States\DevicePropertiesRepository $statesRepository; - /** @var MetadataEntities\Modules\DevicesModule\DevicePropertyEntityFactory */ private MetadataEntities\Modules\DevicesModule\DevicePropertyEntityFactory $entityFactory; public function __construct( @@ -58,21 +55,43 @@ public function __construct( $this->statesRepository = $statesRepository; $this->entityFactory = $entityFactory; - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById( Uuid\UuidInterface $id ): MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity|null { - $this->properties->rewind(); + if (array_key_exists($id->toString(), $this->properties)) { + $data = $this->properties[$id->toString()]; + + $state = []; + + if ( + array_key_exists('type', $data) + && ( + $data['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $data['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState($id); + } - foreach ($this->properties as $property) { - if ($property->getId()->equals($id)) { - return $property; + $entity = $this->entityFactory->create(array_merge($state, $data)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity + ) { + return $entity; } + + return null; } return null; @@ -80,19 +99,43 @@ public function findById( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $device, string $identifier ): MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity|null { - $this->properties->rewind(); - - foreach ($this->properties as $property) { + foreach ($this->properties as $id => $property) { if ( - $property->getDevice()->equals($device) - && $property->getIdentifier() === $identifier + array_key_exists('device', $property) + && $device->toString() === $property['device'] + && array_key_exists('identifier', $property) + && $property['identifier'] === $identifier ) { - return $property; + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity + ) { + return $entity; + } + + return null; } } @@ -101,16 +144,36 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByDevice(Uuid\UuidInterface $device): array { $properties = []; - $this->properties->rewind(); - - foreach ($this->properties as $property) { - if ($property->getDevice()->equals($device)) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + if (array_key_exists('device', $property) && $device->toString() === $property['device']) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity + ) { + $properties[] = $entity; + } } } @@ -119,60 +182,10 @@ public function findAllByDevice(Uuid\UuidInterface $device): array /** * {@inheritDoc} - * - * @throws Utils\JsonException - * @throws MetadataExceptions\FileNotFoundException */ - public function append( - MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity $entity - ): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->properties->detach($existing); - } - - if (!$this->properties->contains($entity)) { - $entity = $this->entityFactory->create( - Utils\Json::encode(array_merge( - $entity->toArray(), - $this->loadPropertyState($entity->getId()) - )) - ); - - if ( - $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity - || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity - ) { - $this->properties->attach($entity, $entity->getId()->toString()); - } - } - } - - /** - * @param Uuid\UuidInterface|null $id - * - * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException - */ - public function loadState(?Uuid\UuidInterface $id = null): void + public function append(Uuid\UuidInterface $id, array $entity): void { - if ($id === null) { - foreach ($this->properties as $property) { - $this->append($property); - } - } else { - $property = $this->findById($id); - - if ($property === null) { - return; - } - - $this->append($property); - } + $this->properties[$id->toString()] = $entity; } /** @@ -180,7 +193,7 @@ public function loadState(?Uuid\UuidInterface $id = null): void */ public function reset(): void { - $this->properties = new SplObjectStorage(); + $this->properties = []; } /** @@ -188,18 +201,40 @@ public function reset(): void */ public function count(): int { - return $this->properties->count(); + return count($this->properties); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $properties = []; - foreach ($this->properties as $property) { - $properties[] = $property; + foreach ($this->properties as $id => $property) { + $state = []; + + if ( + array_key_exists('type', $property) + && ( + $property['type'] === MetadataTypes\PropertyTypeType::TYPE_DYNAMIC + || $property['type'] === MetadataTypes\PropertyTypeType::TYPE_MAPPED + ) + ) { + $state = $this->loadPropertyState(Uuid\Uuid::fromString($id)); + } + + $entity = $this->entityFactory->create(array_merge($state, $property)); + + if ( + $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity + || $entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity + ) { + $properties[] = $entity; + } } return new RecursiveArrayIterator($properties); @@ -208,7 +243,7 @@ public function getIterator(): RecursiveArrayIterator /** * @param Uuid\UuidInterface $id * - * @return mixed[] + * @return Array */ private function loadPropertyState(Uuid\UuidInterface $id): array { diff --git a/src/Models/DataStorage/DevicesRepository.php b/src/Models/DataStorage/DevicesRepository.php index 8fbfd139..267998b2 100644 --- a/src/Models/DataStorage/DevicesRepository.php +++ b/src/Models/DataStorage/DevicesRepository.php @@ -17,11 +17,11 @@ use Countable; use FastyBird\Metadata\Entities as MetadataEntities; +use FastyBird\Metadata\Exceptions as MetadataExceptions; use IteratorAggregate; use Nette; use Ramsey\Uuid; use RecursiveArrayIterator; -use SplObjectStorage; /** * Data storage devices repository @@ -38,25 +38,28 @@ final class DevicesRepository implements IDevicesRepository, Countable, Iterator use Nette\SmartObject; - /** @var SplObjectStorage */ - private SplObjectStorage $devices; + /** @var Array> */ + private array $devices; - public function __construct() - { - $this->devices = new SplObjectStorage(); + private MetadataEntities\Modules\DevicesModule\DeviceEntityFactory $entityFactory; + + public function __construct( + MetadataEntities\Modules\DevicesModule\DeviceEntityFactory $entityFactory + ) { + $this->entityFactory = $entityFactory; + + $this->devices = []; } /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\DevicesModule\IDeviceEntity { - $this->devices->rewind(); - - foreach ($this->devices as $device) { - if ($device->getId()->equals($id)) { - return $device; - } + if (array_key_exists($id->toString(), $this->devices)) { + return $this->entityFactory->create($this->devices[$id->toString()]); } return null; @@ -64,19 +67,21 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findByIdentifier( Uuid\UuidInterface $connector, string $identifier ): ?MetadataEntities\Modules\DevicesModule\IDeviceEntity { - $this->devices->rewind(); - foreach ($this->devices as $device) { if ( - $device->getConnector()->equals($connector) - && $device->getIdentifier() === $identifier + array_key_exists('device', $device) + && $connector->toString() === $device['device'] + && array_key_exists('identifier', $device) + && $device['identifier'] === $identifier ) { - return $device; + return $this->entityFactory->create($device); } } @@ -85,16 +90,16 @@ public function findByIdentifier( /** * {@inheritDoc} + * + * @throws MetadataExceptions\FileNotFoundException */ public function findAllByConnector(Uuid\UuidInterface $connector): array { $devices = []; - $this->devices->rewind(); - foreach ($this->devices as $device) { - if ($device->getConnector()->equals($connector)) { - $devices[] = $device; + if (array_key_exists('device', $device) && $connector->toString() === $device['device']) { + $devices[] = $this->entityFactory->create($device); } } @@ -104,17 +109,9 @@ public function findAllByConnector(Uuid\UuidInterface $connector): array /** * {@inheritDoc} */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceEntity $entity): void + public function append(Uuid\UuidInterface $id, array $entity): void { - $existing = $this->findById($entity->getId()); - - if ($existing !== null) { - $this->devices->detach($existing); - } - - if (!$this->devices->contains($entity)) { - $this->devices->attach($entity, $entity->getId()->toString()); - } + $this->devices[$id->toString()] = $entity; } /** @@ -122,7 +119,7 @@ public function append(MetadataEntities\Modules\DevicesModule\IDeviceEntity $ent */ public function reset(): void { - $this->devices = new SplObjectStorage(); + $this->devices = []; } /** @@ -130,18 +127,20 @@ public function reset(): void */ public function count(): int { - return $this->devices->count(); + return count($this->devices); } /** * @return RecursiveArrayIterator + * + * @throws MetadataExceptions\FileNotFoundException */ public function getIterator(): RecursiveArrayIterator { $devices = []; foreach ($this->devices as $device) { - $devices[] = $device; + $devices[] = $this->entityFactory->create($device); } return new RecursiveArrayIterator($devices); diff --git a/src/Models/DataStorage/IChannelControlsRepository.php b/src/Models/DataStorage/IChannelControlsRepository.php index 551cf1c5..c87f4dd8 100644 --- a/src/Models/DataStorage/IChannelControlsRepository.php +++ b/src/Models/DataStorage/IChannelControlsRepository.php @@ -44,11 +44,12 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi public function findAllByChannel(Uuid\UuidInterface $channel): array; /** - * @param MetadataEntities\Modules\DevicesModule\IChannelControlEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IChannelControlEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IChannelPropertiesRepository.php b/src/Models/DataStorage/IChannelPropertiesRepository.php index 75f1a369..d1509b71 100644 --- a/src/Models/DataStorage/IChannelPropertiesRepository.php +++ b/src/Models/DataStorage/IChannelPropertiesRepository.php @@ -16,8 +16,6 @@ namespace FastyBird\DevicesModule\Models\DataStorage; use FastyBird\Metadata\Entities as MetadataEntities; -use FastyBird\Metadata\Exceptions as MetadataExceptions; -use Nette\Utils; use Ramsey\Uuid; /** @@ -59,23 +57,12 @@ public function findByIdentifier( public function findAllByChannel(Uuid\UuidInterface $channel): array; /** - * @param MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity $entity - * - * @return void - */ - public function append( - MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity $entity - ): void; - - /** - * @param Uuid\UuidInterface|null $id + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException */ - public function loadState(?Uuid\UuidInterface $id = null): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IChannelsRepository.php b/src/Models/DataStorage/IChannelsRepository.php index a90fc830..810a3360 100644 --- a/src/Models/DataStorage/IChannelsRepository.php +++ b/src/Models/DataStorage/IChannelsRepository.php @@ -55,11 +55,12 @@ public function findByIdentifier( public function findAllByDevice(Uuid\UuidInterface $device): array; /** - * @param MetadataEntities\Modules\DevicesModule\IChannelEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IChannelEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IConnectorControlsRepository.php b/src/Models/DataStorage/IConnectorControlsRepository.php index 8d765dfe..dd04a825 100644 --- a/src/Models/DataStorage/IConnectorControlsRepository.php +++ b/src/Models/DataStorage/IConnectorControlsRepository.php @@ -44,11 +44,12 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi public function findAllByConnector(Uuid\UuidInterface $connector): array; /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorControlEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IConnectorControlEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IConnectorPropertiesRepository.php b/src/Models/DataStorage/IConnectorPropertiesRepository.php index 6bcf5891..ea9cad76 100644 --- a/src/Models/DataStorage/IConnectorPropertiesRepository.php +++ b/src/Models/DataStorage/IConnectorPropertiesRepository.php @@ -16,8 +16,6 @@ namespace FastyBird\DevicesModule\Models\DataStorage; use FastyBird\Metadata\Entities as MetadataEntities; -use FastyBird\Metadata\Exceptions as MetadataExceptions; -use Nette\Utils; use Ramsey\Uuid; /** @@ -59,23 +57,12 @@ public function findByIdentifier( public function findAllByConnector(Uuid\UuidInterface $connector): array; /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity $entity - * - * @return void - */ - public function append( - MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity $entity - ): void; - - /** - * @param Uuid\UuidInterface|null $id + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException */ - public function loadState(?Uuid\UuidInterface $id = null): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IConnectorsRepository.php b/src/Models/DataStorage/IConnectorsRepository.php index 39980c09..7500fdf7 100644 --- a/src/Models/DataStorage/IConnectorsRepository.php +++ b/src/Models/DataStorage/IConnectorsRepository.php @@ -44,11 +44,12 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi public function findByIdentifier(string $identifier): ?MetadataEntities\Modules\DevicesModule\IConnectorEntity; /** - * @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IConnectorEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IDeviceAttributesRepository.php b/src/Models/DataStorage/IDeviceAttributesRepository.php index 30bd3f6d..097f46ca 100644 --- a/src/Models/DataStorage/IDeviceAttributesRepository.php +++ b/src/Models/DataStorage/IDeviceAttributesRepository.php @@ -55,11 +55,12 @@ public function findByIdentifier( public function findAllByDevice(Uuid\UuidInterface $device): array; /** - * @param MetadataEntities\Modules\DevicesModule\IDeviceAttributeEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceAttributeEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IDeviceControlsRepository.php b/src/Models/DataStorage/IDeviceControlsRepository.php index 1a75a9a7..73356fd6 100644 --- a/src/Models/DataStorage/IDeviceControlsRepository.php +++ b/src/Models/DataStorage/IDeviceControlsRepository.php @@ -44,11 +44,12 @@ public function findById(Uuid\UuidInterface $id): ?MetadataEntities\Modules\Devi public function findAllByDevice(Uuid\UuidInterface $device): array; /** - * @param MetadataEntities\Modules\DevicesModule\IDeviceControlEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceControlEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IDevicePropertiesRepository.php b/src/Models/DataStorage/IDevicePropertiesRepository.php index 552d28ef..a8096c89 100644 --- a/src/Models/DataStorage/IDevicePropertiesRepository.php +++ b/src/Models/DataStorage/IDevicePropertiesRepository.php @@ -16,8 +16,6 @@ namespace FastyBird\DevicesModule\Models\DataStorage; use FastyBird\Metadata\Entities as MetadataEntities; -use FastyBird\Metadata\Exceptions as MetadataExceptions; -use Nette\Utils; use Ramsey\Uuid; /** @@ -59,23 +57,12 @@ public function findByIdentifier( public function findAllByDevice(Uuid\UuidInterface $device): array; /** - * @param MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity $entity - * - * @return void - */ - public function append( - MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity $entity - ): void; - - /** - * @param Uuid\UuidInterface|null $id + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void - * - * @throws MetadataExceptions\FileNotFoundException - * @throws Utils\JsonException */ - public function loadState(?Uuid\UuidInterface $id = null): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/Models/DataStorage/IDevicesRepository.php b/src/Models/DataStorage/IDevicesRepository.php index b027e2d4..c740db59 100644 --- a/src/Models/DataStorage/IDevicesRepository.php +++ b/src/Models/DataStorage/IDevicesRepository.php @@ -55,11 +55,12 @@ public function findByIdentifier( public function findAllByConnector(Uuid\UuidInterface $connector): array; /** - * @param MetadataEntities\Modules\DevicesModule\IDeviceEntity $entity + * @param Uuid\UuidInterface $id + * @param Array $entity * * @return void */ - public function append(MetadataEntities\Modules\DevicesModule\IDeviceEntity $entity): void; + public function append(Uuid\UuidInterface $id, array $entity): void; /** * @return void diff --git a/src/States/IProperty.php b/src/States/IProperty.php index b02ca28d..9cd09255 100644 --- a/src/States/IProperty.php +++ b/src/States/IProperty.php @@ -28,66 +28,28 @@ interface IProperty { - /** - * @return Uuid\UuidInterface - */ public function getId(): Uuid\UuidInterface; - /** - * @param float|bool|int|string|null $actual - * - * @return void - */ public function setActualValue(float|bool|int|string|null $actual): void; - /** - * @return bool|float|int|string|null - */ public function getActualValue(): float|bool|int|string|null; - /** - * @param float|bool|int|string|null $expected - * - * @return void - */ public function setExpectedValue(float|bool|int|string|null $expected): void; - /** - * @return bool|float|int|string|null - */ public function getExpectedValue(): float|bool|int|string|null; - /** - * @param bool $pending - * - * @return void - */ public function setPending(bool|string|null $pending): void; - /** - * @return bool|string|null - */ public function getPending(): bool|string|null; - /** - * @return bool - */ public function isPending(): bool; - /** - * @param bool $valid - * - * @return void - */ public function setValid(bool $valid): void; - /** - * @return bool - */ public function isValid(): bool; /** - * @return mixed[] + * @return Array */ public function toArray(): array; diff --git a/src/Subscribers/DataStorageSubscriber.php b/src/Subscribers/DataStorageSubscriber.php index 16075795..0c07dcff 100644 --- a/src/Subscribers/DataStorageSubscriber.php +++ b/src/Subscribers/DataStorageSubscriber.php @@ -35,7 +35,6 @@ final class DataStorageSubscriber implements EventDispatcher\EventSubscriberInte use Nette\SmartObject; - /** @var DataStorage\Reader */ private DataStorage\Reader $reader; public function __construct( @@ -44,9 +43,6 @@ public function __construct( $this->reader = $reader; } - /** - * {@inheritDoc} - */ public static function getSubscribedEvents(): array { return [ diff --git a/src/Subscribers/EntitiesSubscriber.php b/src/Subscribers/EntitiesSubscriber.php index 4dc815a6..3fd3d104 100644 --- a/src/Subscribers/EntitiesSubscriber.php +++ b/src/Subscribers/EntitiesSubscriber.php @@ -23,7 +23,6 @@ use FastyBird\DevicesModule\Entities; use FastyBird\DevicesModule\Exceptions; use FastyBird\DevicesModule\Models; -use FastyBird\DevicesModule\Utilities; use FastyBird\Exchange\Entities as ExchangeEntities; use FastyBird\Exchange\Publisher as ExchangePublisher; use FastyBird\Metadata\Exceptions as MetadataExceptions; @@ -51,34 +50,24 @@ final class EntitiesSubscriber implements Common\EventSubscriber use Nette\SmartObject; - /** @var Models\States\DevicePropertiesRepository */ private Models\States\DevicePropertiesRepository $devicePropertiesStatesRepository; - /** @var Models\States\DevicePropertiesManager */ private Models\States\DevicePropertiesManager $devicePropertiesStatesManager; - /** @var Models\States\ChannelPropertiesRepository */ private Models\States\ChannelPropertiesRepository $channelPropertiesStatesRepository; - /** @var Models\States\ChannelPropertiesManager */ private Models\States\ChannelPropertiesManager $channelPropertiesStatesManager; - /** @var Models\States\ConnectorPropertiesRepository */ private Models\States\ConnectorPropertiesRepository $connectorPropertiesStatesRepository; - /** @var Models\States\ConnectorPropertiesManager */ private Models\States\ConnectorPropertiesManager $connectorPropertiesStatesManager; - /** @var DataStorage\Writer */ private DataStorage\Writer $configurationDataWriter; - /** @var ExchangeEntities\EntityFactory */ private ExchangeEntities\EntityFactory $entityFactory; - /** @var ExchangePublisher\Publisher|null */ private ?ExchangePublisher\Publisher $publisher; - /** @var ORM\EntityManagerInterface */ private ORM\EntityManagerInterface $entityManager; public function __construct( @@ -169,7 +158,7 @@ public function onFlush(): void $this->publishEntity($entity, self::ACTION_DELETED); // Property states cleanup - if ($entity instanceof DevicesModule\Entities\Connectors\Properties\IProperty) { + if ($entity instanceof DevicesModule\Entities\Connectors\Properties\IDynamicProperty) { try { $state = $this->connectorPropertiesStatesRepository->findOne($entity); @@ -179,10 +168,7 @@ public function onFlush(): void } catch (Exceptions\NotImplementedException $ex) { return; } - } elseif ( - $entity instanceof DevicesModule\Entities\Devices\Properties\IProperty - && $entity->getParent() === null - ) { + } elseif ($entity instanceof DevicesModule\Entities\Devices\Properties\IDynamicProperty) { try { $state = $this->devicePropertiesStatesRepository->findOne($entity); @@ -192,10 +178,7 @@ public function onFlush(): void } catch (Exceptions\NotImplementedException $ex) { return; } - } elseif ( - $entity instanceof DevicesModule\Entities\Channels\Properties\IProperty - && $entity->getParent() === null - ) { + } elseif ($entity instanceof DevicesModule\Entities\Channels\Properties\IDynamicProperty) { try { $state = $this->channelPropertiesStatesRepository->findOne($entity); @@ -292,11 +275,6 @@ private function getHash(Entities\IEntity $entity, array $identifier): string ); } - /** - * @param string $class - * - * @return string - */ private function getRealClass(string $class): string { $pos = strrpos($class, '\\' . Persistence\Proxy::MARKER . '\\'); @@ -355,25 +333,22 @@ private function publishEntity(Entities\IEntity $entity, string $action): void } if ($publishRoutingKey !== null) { - if ( - $entity instanceof Entities\Devices\Properties\IProperty - && $entity->getType()->equalsValue(MetadataTypes\PropertyTypeType::TYPE_DYNAMIC) - ) { + if ($entity instanceof Entities\Devices\Properties\IDynamicProperty) { try { $state = $this->devicePropertiesStatesRepository->findOne($entity); - $actualValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getActualValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $expectedValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getExpectedValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $this->publisher->publish( MetadataTypes\ModuleSourceType::get(MetadataTypes\ModuleSourceType::SOURCE_MODULE_DEVICES), $publishRoutingKey, - $this->entityFactory->create(Utils\Json::encode(array_merge($state !== null ? [ - 'actualValue' => Utilities\ValueHelper::flattenValue($actualValue), - 'expectedValue' => Utilities\ValueHelper::flattenValue($expectedValue), - 'pending' => $state->isPending(), - 'valid' => $state->isValid(), - ] : [], $entity->toArray())), $publishRoutingKey) + $this->entityFactory->create( + Utils\Json::encode( + array_merge( + $entity->toArray(), + $state !== null ? $state->toArray() : [] + ) + ), + $publishRoutingKey + ) ); } catch (Exceptions\NotImplementedException $ex) { @@ -383,25 +358,22 @@ private function publishEntity(Entities\IEntity $entity, string $action): void $this->entityFactory->create(Utils\Json::encode($entity->toArray()), $publishRoutingKey) ); } - } elseif ( - $entity instanceof Entities\Channels\Properties\IProperty - && $entity->getType()->equalsValue(MetadataTypes\PropertyTypeType::TYPE_DYNAMIC) - ) { + } elseif ($entity instanceof Entities\Channels\Properties\IDynamicProperty) { try { $state = $this->channelPropertiesStatesRepository->findOne($entity); - $actualValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getActualValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $expectedValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getExpectedValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $this->publisher->publish( MetadataTypes\ModuleSourceType::get(MetadataTypes\ModuleSourceType::SOURCE_MODULE_DEVICES), $publishRoutingKey, - $this->entityFactory->create(Utils\Json::encode(array_merge($state !== null ? [ - 'actualValue' => Utilities\ValueHelper::flattenValue($actualValue), - 'expectedValue' => Utilities\ValueHelper::flattenValue($expectedValue), - 'pending' => $state->isPending(), - 'valid' => $state->isValid(), - ] : [], $entity->toArray())), $publishRoutingKey) + $this->entityFactory->create( + Utils\Json::encode( + array_merge( + $entity->toArray(), + $state !== null ? $state->toArray() : [] + ) + ), + $publishRoutingKey + ) ); } catch (Exceptions\NotImplementedException $ex) { @@ -411,25 +383,22 @@ private function publishEntity(Entities\IEntity $entity, string $action): void $this->entityFactory->create(Utils\Json::encode($entity->toArray()), $publishRoutingKey) ); } - } elseif ( - $entity instanceof Entities\Connectors\Properties\IProperty - && $entity->getType()->equalsValue(MetadataTypes\PropertyTypeType::TYPE_DYNAMIC) - ) { + } elseif ($entity instanceof Entities\Connectors\Properties\IDynamicProperty) { try { $state = $this->connectorPropertiesStatesRepository->findOne($entity); - $actualValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getActualValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $expectedValue = $state ? Utilities\ValueHelper::normalizeValue($entity->getDataType(), $state->getExpectedValue(), $entity->getFormat(), $entity->getInvalid()) : null; - $this->publisher->publish( MetadataTypes\ModuleSourceType::get(MetadataTypes\ModuleSourceType::SOURCE_MODULE_DEVICES), $publishRoutingKey, - $this->entityFactory->create(Utils\Json::encode(array_merge($state !== null ? [ - 'actualValue' => Utilities\ValueHelper::flattenValue($actualValue), - 'expectedValue' => Utilities\ValueHelper::flattenValue($expectedValue), - 'pending' => $state->isPending(), - 'valid' => $state->isValid(), - ] : [], $entity->toArray())), $publishRoutingKey) + $this->entityFactory->create( + Utils\Json::encode( + array_merge( + $entity->toArray(), + $state !== null ? $state->toArray() : [] + ) + ), + $publishRoutingKey + ) ); } catch (Exceptions\NotImplementedException $ex) { @@ -449,12 +418,6 @@ private function publishEntity(Entities\IEntity $entity, string $action): void } } - /** - * @param Entities\IEntity $entity - * @param string $class - * - * @return bool - */ private function validateEntity(Entities\IEntity $entity, string $class): bool { $result = false; @@ -470,11 +433,6 @@ private function validateEntity(Entities\IEntity $entity, string $class): bool return $result; } - /** - * @param object $entity - * - * @return bool - */ private function validateNamespace(object $entity): bool { try { diff --git a/src/Subscribers/ExchangeSubscriber.php b/src/Subscribers/ExchangeSubscriber.php index 728d7a9f..6e16907a 100644 --- a/src/Subscribers/ExchangeSubscriber.php +++ b/src/Subscribers/ExchangeSubscriber.php @@ -36,7 +36,6 @@ final class ExchangeSubscriber implements EventDispatcher\EventSubscriberInterfa use Nette\SmartObject; - /** @var DataStorage\Reader */ private DataStorage\Reader $reader; public function __construct( @@ -45,9 +44,6 @@ public function __construct( $this->reader = $reader; } - /** - * {@inheritDoc} - */ public static function getSubscribedEvents(): array { return [ diff --git a/src/Subscribers/StatesSubscriber.php b/src/Subscribers/StatesSubscriber.php index 7b511588..ac2b805f 100644 --- a/src/Subscribers/StatesSubscriber.php +++ b/src/Subscribers/StatesSubscriber.php @@ -42,19 +42,14 @@ final class StatesSubscriber implements EventDispatcher\EventSubscriberInterface use Nette\SmartObject; - /** @var Models\DataStorage\IConnectorPropertiesRepository */ private Models\DataStorage\IConnectorPropertiesRepository $connectorPropertiesRepository; - /** @var Models\DataStorage\IDevicePropertiesRepository */ private Models\DataStorage\IDevicePropertiesRepository $devicePropertiesRepository; - /** @var Models\DataStorage\IChannelPropertiesRepository */ private Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository; - /** @var ExchangeEntities\EntityFactory */ private ExchangeEntities\EntityFactory $entityFactory; - /** @var ExchangePublisher\IPublisher|null */ private ?ExchangePublisher\IPublisher $publisher; public function __construct( @@ -73,9 +68,6 @@ public function __construct( $this->publisher = $publisher; } - /** - * {@inheritDoc} - */ public static function getSubscribedEvents(): array { return [ @@ -95,25 +87,6 @@ public static function getSubscribedEvents(): array */ public function stateCreated(Events\StateEntityCreatedEvent $event): void { - if ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity - ) { - $this->connectorPropertiesRepository->loadState($event->getState()->getId()); - - } elseif ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity - ) { - $this->devicePropertiesRepository->loadState($event->getState()->getId()); - - } elseif ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity - ) { - $this->channelPropertiesRepository->loadState($event->getState()->getId()); - } - $property = $this->findProperty($event->getState()->getId(), $event->getState()); if ($property !== null) { @@ -139,25 +112,6 @@ public function stateCreated(Events\StateEntityCreatedEvent $event): void */ public function stateUpdated(Events\StateEntityUpdatedEvent $event): void { - if ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity - ) { - $this->connectorPropertiesRepository->loadState($event->getState()->getId()); - - } elseif ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity - ) { - $this->devicePropertiesRepository->loadState($event->getState()->getId()); - - } elseif ( - $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity - || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity - ) { - $this->channelPropertiesRepository->loadState($event->getState()->getId()); - } - $property = $this->findProperty($event->getState()->getId(), $event->getState()); if ($property !== null) { @@ -187,24 +141,18 @@ public function stateDeleted(Events\StateEntityDeletedEvent $event): void $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity ) { - $this->connectorPropertiesRepository->loadState($event->getProperty()->getId()); - $property = $this->connectorPropertiesRepository->findById($event->getProperty()->getId()); } elseif ( $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity ) { - $this->devicePropertiesRepository->loadState($event->getProperty()->getId()); - $property = $this->devicePropertiesRepository->findById($event->getProperty()->getId()); } elseif ( $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity || $event->getProperty() instanceof MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity ) { - $this->channelPropertiesRepository->loadState($event->getProperty()->getId()); - $property = $this->channelPropertiesRepository->findById($event->getProperty()->getId()); } else { @@ -286,12 +234,6 @@ private function publishEntity( ); } - /** - * @param Uuid\UuidInterface $id - * @param States\IConnectorProperty|States\IDeviceProperty|States\IChannelProperty $state - * - * @return MetadataEntities\Modules\DevicesModule\IChannelDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceDynamicPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceMappedPropertyEntity|MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity|null - */ private function findProperty( Uuid\UuidInterface $id, States\IConnectorProperty|States\IChannelProperty|States\IDeviceProperty $state diff --git a/src/Utilities/ValueHelper.php b/src/Utilities/ValueHelper.php index b306b5c9..1625c1b0 100644 --- a/src/Utilities/ValueHelper.php +++ b/src/Utilities/ValueHelper.php @@ -177,11 +177,6 @@ public static function normalizeValue( return $value; } - /** - * @param bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayloadType|MetadataTypes\SwitchPayloadType|null $value - * - * @return bool|float|int|string|null - */ public static function flattenValue( bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayloadType|MetadataTypes\SwitchPayloadType|null $value ): bool|float|int|string|null { diff --git a/tests/cases/Unit/Connectors/ConnectorFactoryTest.phpt b/tests/cases/Unit/Connectors/ConnectorFactoryTest.phpt index ad02165b..6699c5ef 100644 --- a/tests/cases/Unit/Connectors/ConnectorFactoryTest.phpt +++ b/tests/cases/Unit/Connectors/ConnectorFactoryTest.phpt @@ -21,23 +21,25 @@ require_once __DIR__ . '/../../../tools/DummyConnectorFactory.php'; final class ConnectorFactoryTest extends DbTestCase { - public function testCreateConnector() + public function setUp(): void { - /** @var DataStorage\Writer $writer */ - $writer = $this->getContainer()->getByType(DataStorage\Writer::class); + parent::setUp(); - /** @var DataStorage\Reader $reader */ + $writer = $this->getContainer()->getByType(DataStorage\Writer::class); $reader = $this->getContainer()->getByType(DataStorage\Reader::class); + $writer->write(); + $reader->read(); + } + + public function testCreateConnector() + { /** @var Connectors\ConnectorFactory $factory */ $factory = $this->getContainer()->getByType(Connectors\ConnectorFactory::class); /** @var Models\DataStorage\ConnectorsRepository $connectorsRepository */ $connectorsRepository = $this->getContainer()->getByType(Models\DataStorage\ConnectorsRepository::class); - $writer->write(); - $reader->read(); - $connectorEntity = $connectorsRepository->findById(Uuid\Uuid::fromString('7a3dd94c-7294-46fd-8c61-1b375c313d4d')); Assert::notNull($connectorEntity); diff --git a/tests/cases/Unit/DataStorage/ReaderTest.phpt b/tests/cases/Unit/DataStorage/ReaderTest.phpt index 3c101e10..56469edf 100644 --- a/tests/cases/Unit/DataStorage/ReaderTest.phpt +++ b/tests/cases/Unit/DataStorage/ReaderTest.phpt @@ -2,12 +2,8 @@ namespace Tests\Cases; -use FastyBird\DevicesModule; use FastyBird\DevicesModule\DataStorage; use FastyBird\DevicesModule\Models; -use League\Flysystem; -use Mockery; -use Nette\Utils; use Tester\Assert; require_once __DIR__ . '/../../../bootstrap.php'; @@ -19,21 +15,19 @@ require_once __DIR__ . '/../DbTestCase.php'; final class ReaderTest extends DbTestCase { - public function testReadConfiguration(): void + public function setUp(): void { - $filesystem = Mockery::mock(Flysystem\Filesystem::class); - $filesystem - ->shouldReceive('read') - ->withArgs([DevicesModule\Constants::CONFIGURATION_FILE_FILENAME]) - ->andReturn(Utils\FileSystem::read('./../../../fixtures/DataStorage/devices-module-data.json')); - - $this->mockContainerService( - Flysystem\Filesystem::class, - $filesystem - ); + parent::setUp(); + $writer = $this->getContainer()->getByType(DataStorage\Writer::class); $reader = $this->getContainer()->getByType(DataStorage\Reader::class); + $writer->write(); + $reader->read(); + } + + public function testReadConfiguration(): void + { $connectorsRepository = $this->getContainer()->getByType(Models\DataStorage\IConnectorsRepository::class); $connectorPropertiesRepository = $this->getContainer() ->getByType(Models\DataStorage\IConnectorPropertiesRepository::class); @@ -54,8 +48,6 @@ final class ReaderTest extends DbTestCase $channelControlsRepository = $this->getContainer() ->getByType(Models\DataStorage\IChannelControlsRepository::class); - $reader->read(); - Assert::count(2, $connectorsRepository); Assert::count(0, $connectorPropertiesRepository); Assert::count(1, $connectorControlsRepository); diff --git a/tests/cases/Unit/DataStorage/WriterTest.phpt b/tests/cases/Unit/DataStorage/WriterTest.phpt index 63b6fcf9..8c16f193 100644 --- a/tests/cases/Unit/DataStorage/WriterTest.phpt +++ b/tests/cases/Unit/DataStorage/WriterTest.phpt @@ -18,15 +18,16 @@ require_once __DIR__ . '/../DbTestCase.php'; final class WriterTest extends DbTestCase { - public function testReadConfiguration(): void + public function testWriteConfiguration(): void { $filesystem = Mockery::mock(Flysystem\Filesystem::class); $filesystem ->shouldReceive('write') ->withArgs(function (string $filename, string $data): bool { Assert::same(DevicesModule\Constants::CONFIGURATION_FILE_FILENAME, $filename); + Tools\JsonAssert::assertFixtureMatch( - './../../../fixtures/DataStorage/devices-module-data.json', + __DIR__ . '/../../../fixtures/DataStorage/devices-module-data.json', $data ); diff --git a/tests/cases/Unit/Models/DataStorage/ConnectorsRepositoryTest.phpt b/tests/cases/Unit/Models/DataStorage/ConnectorsRepositoryTest.phpt index a29ca03e..58391411 100644 --- a/tests/cases/Unit/Models/DataStorage/ConnectorsRepositoryTest.phpt +++ b/tests/cases/Unit/Models/DataStorage/ConnectorsRepositoryTest.phpt @@ -2,13 +2,9 @@ namespace Tests\Cases; -use FastyBird\DevicesModule; use FastyBird\DevicesModule\DataStorage; use FastyBird\DevicesModule\Models; use FastyBird\Metadata\Entities as MetadataEntities; -use League\Flysystem; -use Mockery; -use Nette\Utils; use Ramsey\Uuid; use Tester\Assert; @@ -21,24 +17,20 @@ require_once __DIR__ . '/../../DbTestCase.php'; final class ConnectorsRepositoryTest extends DbTestCase { - public function testReadConfiguration(): void + public function setUp(): void { - $filesystem = Mockery::mock(Flysystem\Filesystem::class); - $filesystem - ->shouldReceive('read') - ->withArgs([DevicesModule\Constants::CONFIGURATION_FILE_FILENAME]) - ->andReturn(Utils\FileSystem::read('./../../../../fixtures/DataStorage/devices-module-data.json')); - - $this->mockContainerService( - Flysystem\Filesystem::class, - $filesystem - ); + parent::setUp(); + $writer = $this->getContainer()->getByType(DataStorage\Writer::class); $reader = $this->getContainer()->getByType(DataStorage\Reader::class); - $connectorsRepository = $this->getContainer()->getByType(Models\DataStorage\IConnectorsRepository::class); - + $writer->write(); $reader->read(); + } + + public function testReadConfiguration(): void + { + $connectorsRepository = $this->getContainer()->getByType(Models\DataStorage\IConnectorsRepository::class); Assert::count(2, $connectorsRepository);