From 87efeabd4a51c4a3de7b8cbc9bfcbe4c7798d808 Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 22 Aug 2023 11:33:36 +0000 Subject: [PATCH] [tuya-connector] Refactoring tuya connector to new specs (#143) --- composer.json | 10 +++ src/API/HttpClientFactory.php | 2 +- src/API/LanApi.php | 4 +- src/Clients/Device.php | 5 +- src/Clients/Discovery.php | 2 +- src/Clients/Gateway.php | 28 ++++----- src/Commands/Devices.php | 18 ++---- src/Commands/Discovery.php | 2 +- src/Commands/Execute.php | 2 +- src/Commands/Initialize.php | 4 +- src/DI/NsPanelExtension.php | 6 +- src/Queue/Consumer.php | 4 +- src/Queue/Consumers.php | 4 +- src/Queue/Consumers/DeviceProperty.php | 4 +- src/Queue/Consumers/StateWriter.php | 4 +- .../Consumers/StoreDeviceConnectionState.php | 13 ++-- src/Queue/Consumers/StoreDeviceState.php | 4 +- src/Queue/Consumers/StoreSubDevice.php | 4 +- src/Queue/Consumers/StoreThirdPartyDevice.php | 4 +- src/Queue/Consumers/WriteSubDeviceState.php | 8 +-- .../Consumers/WriteThirdPartyDeviceState.php | 4 +- src/Queue/Queue.php | 4 +- .../ns-panel-connector.en_us.neon | 3 - src/Writers/Event.php | 6 +- src/Writers/EventFactory.php | 4 +- src/Writers/Exchange.php | 11 +--- src/Writers/ExchangeFactory.php | 4 +- src/Writers/PeriodicFactory.php | 4 +- src/Writers/WriterFactory.php | 4 +- tests/cases/unit/API/LanApiTest.php | 20 +++--- tests/cases/unit/Clients/DiscoveryTest.php | 6 +- tests/cases/unit/DI/NsPanelExtensionTest.php | 61 +++++++++++++++++-- tests/cases/unit/Entities/API/HeaderTest.php | 4 +- .../API/Request/SetDeviceStatusTest.php | 4 +- 34 files changed, 158 insertions(+), 113 deletions(-) diff --git a/composer.json b/composer.json index 6a3c055..d94c682 100644 --- a/composer.json +++ b/composer.json @@ -35,18 +35,28 @@ "php": ">=8.1.0", "ext-iconv": "*", "brick/math": "^0.10", + "contributte/console": "^0.9", + "contributte/monolog": "^0.5", "cweagans/composer-patches": "^1.7", + "doctrine/orm": "2.15.*", "evenement/evenement": "^3.0", "fastybird/bootstrap-library": "dev-main", "fastybird/datetime-factory": "^0.6", "fastybird/devices-module": "dev-main", "fastybird/metadata-library": "dev-main", + "fig/http-message-util": "^1.1", "guzzlehttp/guzzle": "^7.5", + "ipub/doctrine-crud": "^3.0", "nette/di": "^3.0", "psr/log": "^1.1|^3.0", + "orisai/object-mapper": "^0.1", + "ramsey/uuid": "^4.5", "react/async": "^4.0", + "react/event-loop": "^1.3", "react/http": "^1.7", "react/promise": "~2.2", + "react/socket": "^1.12", + "ringcentral/psr7": "^1.3", "symplify/vendor-patches": "^11.2" }, "require-dev": { diff --git a/src/API/HttpClientFactory.php b/src/API/HttpClientFactory.php index 580bb3f..203d09b 100644 --- a/src/API/HttpClientFactory.php +++ b/src/API/HttpClientFactory.php @@ -52,7 +52,7 @@ public function __construct( * * @throws InvalidArgumentException */ - public function createClient(bool $async = true): GuzzleHttp\Client|Http\Io\Transaction + public function create(bool $async = true): GuzzleHttp\Client|Http\Io\Transaction { if ($async) { if ($this->asyncClient === null) { diff --git a/src/API/LanApi.php b/src/API/LanApi.php index 6a67457..32ca1fb 100644 --- a/src/API/LanApi.php +++ b/src/API/LanApi.php @@ -908,7 +908,7 @@ private function callRequest( if ($async) { try { $this->httpClientFactory - ->createClient() + ->create() ->send($request) ->then( function (Message\ResponseInterface $response) use ($deferred, $request): void { @@ -971,7 +971,7 @@ static function (Throwable $ex) use ($deferred, $request): void { try { $response = $this->httpClientFactory - ->createClient(false) + ->create(false) ->send($request); try { diff --git a/src/Clients/Device.php b/src/Clients/Device.php index d5b9c91..cb064c1 100644 --- a/src/Clients/Device.php +++ b/src/Clients/Device.php @@ -30,6 +30,7 @@ use FastyBird\Module\Devices\Exceptions as DevicesExceptions; use FastyBird\Module\Devices\Models as DevicesModels; use Nette; +use Nette\Utils; use React\Promise; use Throwable; use function array_diff; @@ -122,11 +123,11 @@ public function connect(): void } if ( - !$categoriesMetadata[$device->getDisplayCategory()->getValue()] instanceof Nette\Utils\ArrayHash + !$categoriesMetadata[$device->getDisplayCategory()->getValue()] instanceof Utils\ArrayHash || !$categoriesMetadata[$device->getDisplayCategory()->getValue()]->offsetExists('capabilities') || !$categoriesMetadata[$device->getDisplayCategory()->getValue()]->offsetGet( 'capabilities', - ) instanceof Nette\Utils\ArrayHash + ) instanceof Utils\ArrayHash ) { throw new DevicesExceptions\Terminate('Connector configuration is corrupted'); } diff --git a/src/Clients/Discovery.php b/src/Clients/Discovery.php index 11bcf9b..43388c2 100644 --- a/src/Clients/Discovery.php +++ b/src/Clients/Discovery.php @@ -203,7 +203,7 @@ private function handleFoundSubDevices( $processedSubDevices = []; foreach ($subDevices->getData()->getDevicesList() as $subDevice) { - // Ignore third-party sub devices (registered as virtual devices via connector) + // Ignore third-party sub-devices (registered as virtual devices via connector) if ($subDevice->getThirdSerialNumber() !== null) { continue; } diff --git a/src/Clients/Gateway.php b/src/Clients/Gateway.php index 408657b..9885ff9 100644 --- a/src/Clients/Gateway.php +++ b/src/Clients/Gateway.php @@ -84,9 +84,7 @@ public function __construct( private readonly EventLoop\LoopInterface $eventLoop, ) { - $this->lanApi = $lanApiFactory->create( - $this->connector->getIdentifier(), - ); + $this->lanApi = $lanApiFactory->create($this->connector->getIdentifier()); } public function connect(): void @@ -187,12 +185,12 @@ private function readDeviceInformation(Entities\Devices\Gateway $gateway): bool return true; } - if (!array_key_exists($gateway->getIdentifier(), $this->processedDevicesCommands)) { - $this->processedDevicesCommands[$gateway->getIdentifier()] = []; + if (!array_key_exists($gateway->getId()->toString(), $this->processedDevicesCommands)) { + $this->processedDevicesCommands[$gateway->getId()->toString()] = []; } - if (array_key_exists(self::CMD_HEARTBEAT, $this->processedDevicesCommands[$gateway->getIdentifier()])) { - $cmdResult = $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_HEARTBEAT]; + if (array_key_exists(self::CMD_HEARTBEAT, $this->processedDevicesCommands[$gateway->getId()->toString()])) { + $cmdResult = $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_HEARTBEAT]; if ( $cmdResult instanceof DateTimeInterface @@ -204,12 +202,12 @@ private function readDeviceInformation(Entities\Devices\Gateway $gateway): bool } } - $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow(); + $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow(); try { $this->lanApi->getGatewayInfo($gateway->getIpAddress()) ->then(function () use ($gateway): void { - $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow(); + $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow(); $this->queue->append( $this->entityHelper->create( @@ -335,12 +333,12 @@ private function readDeviceState(Entities\Devices\Gateway $gateway): bool return true; } - if (!array_key_exists($gateway->getIdentifier(), $this->processedDevicesCommands)) { - $this->processedDevicesCommands[$gateway->getIdentifier()] = []; + if (!array_key_exists($gateway->getId()->toString(), $this->processedDevicesCommands)) { + $this->processedDevicesCommands[$gateway->getId()->toString()] = []; } - if (array_key_exists(self::CMD_STATE, $this->processedDevicesCommands[$gateway->getIdentifier()])) { - $cmdResult = $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_STATE]; + if (array_key_exists(self::CMD_STATE, $this->processedDevicesCommands[$gateway->getId()->toString()])) { + $cmdResult = $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_STATE]; if ( $cmdResult instanceof DateTimeInterface @@ -352,12 +350,12 @@ private function readDeviceState(Entities\Devices\Gateway $gateway): bool } } - $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_STATE] = $this->dateTimeFactory->getNow(); + $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_STATE] = $this->dateTimeFactory->getNow(); try { $this->lanApi->getSubDevices($gateway->getIpAddress(), $gateway->getAccessToken()) ->then(function (Entities\API\Response\GetSubDevices $subDevices) use ($gateway): void { - $this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_STATE] = $this->dateTimeFactory->getNow(); + $this->processedDevicesCommands[$gateway->getId()->toString()][self::CMD_STATE] = $this->dateTimeFactory->getNow(); $this->queue->append( $this->entityHelper->create( diff --git a/src/Commands/Devices.php b/src/Commands/Devices.php index 57460a4..ddc0474 100644 --- a/src/Commands/Devices.php +++ b/src/Commands/Devices.php @@ -402,8 +402,7 @@ private function editGateway( $panelInfo = $this->askWhichPanel($io, $connector, $gateway); - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($gateway); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::IP_ADDRESS); @@ -412,8 +411,7 @@ private function editGateway( DevicesEntities\Devices\Properties\Variable::class, ); - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($gateway); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::DOMAIN); @@ -422,8 +420,7 @@ private function editGateway( DevicesEntities\Devices\Properties\Variable::class, ); - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($gateway); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::MAC_ADDRESS); @@ -432,8 +429,7 @@ private function editGateway( DevicesEntities\Devices\Properties\Variable::class, ); - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($gateway); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::FIRMWARE_VERSION); @@ -509,8 +505,7 @@ private function editGateway( } } - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($gateway); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::ACCESS_TOKEN); @@ -729,8 +724,7 @@ static function (Entities\Devices\Gateway $a, Entities\Devices\Gateway $b): int ]); foreach ($devices as $index => $device) { - /** @var DevicesQueries\FindDeviceProperties $findDevicePropertyQuery */ - $findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties(); + $findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties(); $findDevicePropertyQuery->forDevice($device); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::IP_ADDRESS); diff --git a/src/Commands/Discovery.php b/src/Commands/Discovery.php index ac03daa..241f790 100644 --- a/src/Commands/Discovery.php +++ b/src/Commands/Discovery.php @@ -173,7 +173,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $ } if (count($connectors) === 0) { - $io->warning($this->translator->translate('//ns-panel-connector.cmd.discovery.messages.noConnectors')); + $io->warning($this->translator->translate('//ns-panel-connector.cmd.base.messages.noConnectors')); return Console\Command\Command::FAILURE; } diff --git a/src/Commands/Execute.php b/src/Commands/Execute.php index 05b08b8..607726c 100644 --- a/src/Commands/Execute.php +++ b/src/Commands/Execute.php @@ -164,7 +164,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $ } if (count($connectors) === 0) { - $io->warning($this->translator->translate('//ns-panel-connector.cmd.execute.messages.noConnectors')); + $io->warning($this->translator->translate('//ns-panel-connector.cmd.base.messages.noConnectors')); return Console\Command\Command::SUCCESS; } diff --git a/src/Commands/Initialize.php b/src/Commands/Initialize.php index 87a103f..f25db4e 100644 --- a/src/Commands/Initialize.php +++ b/src/Commands/Initialize.php @@ -237,7 +237,7 @@ private function editConfiguration(Style\SymfonyStyle $io): void $connector = $this->askWhichConnector($io); if ($connector === null) { - $io->warning($this->translator->translate('//ns-panel-connector.cmd.initialize.messages.noConnectors')); + $io->warning($this->translator->translate('//ns-panel-connector.cmd.base.messages.noConnectors')); $question = new Console\Question\ConfirmationQuestion( $this->translator->translate('//ns-panel-connector.cmd.initialize.questions.create'), @@ -369,7 +369,7 @@ private function deleteConfiguration(Style\SymfonyStyle $io): void $connector = $this->askWhichConnector($io); if ($connector === null) { - $io->info($this->translator->translate('//ns-panel-connector.cmd.initialize.messages.noConnectors')); + $io->info($this->translator->translate('//ns-panel-connector.cmd.base.messages.noConnectors')); return; } diff --git a/src/DI/NsPanelExtension.php b/src/DI/NsPanelExtension.php index 5c3c863..efab046 100644 --- a/src/DI/NsPanelExtension.php +++ b/src/DI/NsPanelExtension.php @@ -146,6 +146,9 @@ public function loadConfiguration(): void * API */ + $builder->addDefinition($this->prefix('api.httpClient'), new DI\Definitions\ServiceDefinition()) + ->setType(API\HttpClientFactory::class); + $builder->addFactoryDefinition($this->prefix('api.lanApi')) ->setImplement(API\LanApiFactory::class) ->getResultDefinition() @@ -154,9 +157,6 @@ public function loadConfiguration(): void 'logger' => $logger, ]); - $builder->addDefinition($this->prefix('api.httpClient'), new DI\Definitions\ServiceDefinition()) - ->setType(API\HttpClientFactory::class); - $builder->addFactoryDefinition($this->prefix('server.http')) ->setImplement(Servers\HttpFactory::class) ->getResultDefinition() diff --git a/src/Queue/Consumer.php b/src/Queue/Consumer.php index 941fbb6..9b49276 100644 --- a/src/Queue/Consumer.php +++ b/src/Queue/Consumer.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 09.07.23 @@ -21,7 +21,7 @@ * Clients messages consumer interface * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Consumers.php b/src/Queue/Consumers.php index 1de579a..698bfc4 100644 --- a/src/Queue/Consumers.php +++ b/src/Queue/Consumers.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 09.08.23 @@ -24,7 +24,7 @@ * Clients message queue consumers container * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Consumers/DeviceProperty.php b/src/Queue/Consumers/DeviceProperty.php index 2013ffc..1c367ea 100644 --- a/src/Queue/Consumers/DeviceProperty.php +++ b/src/Queue/Consumers/DeviceProperty.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 18.07.23 @@ -33,7 +33,7 @@ * Device property consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec * diff --git a/src/Queue/Consumers/StateWriter.php b/src/Queue/Consumers/StateWriter.php index 96f78f7..bd0c38c 100644 --- a/src/Queue/Consumers/StateWriter.php +++ b/src/Queue/Consumers/StateWriter.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Queue * @since 1.0.0 * * @date 16.07.23 @@ -39,7 +39,7 @@ * Third-party device & sub-device property to state mapper * * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Queue * * @author Adam Kadlec * diff --git a/src/Queue/Consumers/StoreDeviceConnectionState.php b/src/Queue/Consumers/StoreDeviceConnectionState.php index 967c046..b262008 100644 --- a/src/Queue/Consumers/StoreDeviceConnectionState.php +++ b/src/Queue/Consumers/StoreDeviceConnectionState.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 18.07.23 @@ -29,12 +29,13 @@ use FastyBird\Module\Devices\States as DevicesStates; use FastyBird\Module\Devices\Utilities as DevicesUtilities; use Nette; +use Nette\Utils; /** * Store device connection state message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ @@ -117,7 +118,7 @@ public function consume(Entities\Messages\Entity $entity): bool ) as $property) { $this->devicePropertiesStatesManager->setValue( $property, - Nette\Utils\ArrayHash::from([ + Utils\ArrayHash::from([ DevicesStates\Property::VALID_KEY => false, ]), ); @@ -138,7 +139,7 @@ public function consume(Entities\Messages\Entity $entity): bool ) as $property) { $this->channelPropertiesStatesManager->setValue( $property, - Nette\Utils\ArrayHash::from([ + Utils\ArrayHash::from([ DevicesStates\Property::VALID_KEY => false, ]), ); @@ -176,7 +177,7 @@ public function consume(Entities\Messages\Entity $entity): bool ) as $property) { $this->devicePropertiesStatesManager->setValue( $property, - Nette\Utils\ArrayHash::from([ + Utils\ArrayHash::from([ DevicesStates\Property::VALID_KEY => false, ]), ); @@ -200,7 +201,7 @@ public function consume(Entities\Messages\Entity $entity): bool ) as $property) { $this->channelPropertiesStatesManager->setValue( $property, - Nette\Utils\ArrayHash::from([ + Utils\ArrayHash::from([ DevicesStates\Property::VALID_KEY => false, ]), ); diff --git a/src/Queue/Consumers/StoreDeviceState.php b/src/Queue/Consumers/StoreDeviceState.php index 35fb3c3..e649ee6 100644 --- a/src/Queue/Consumers/StoreDeviceState.php +++ b/src/Queue/Consumers/StoreDeviceState.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 18.07.23 @@ -42,7 +42,7 @@ * Store device state message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Consumers/StoreSubDevice.php b/src/Queue/Consumers/StoreSubDevice.php index afe5c93..d641162 100644 --- a/src/Queue/Consumers/StoreSubDevice.php +++ b/src/Queue/Consumers/StoreSubDevice.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 24.08.22 @@ -36,7 +36,7 @@ * Store NS Panel sub-device message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Consumers/StoreThirdPartyDevice.php b/src/Queue/Consumers/StoreThirdPartyDevice.php index 17e73f4..d852950 100644 --- a/src/Queue/Consumers/StoreThirdPartyDevice.php +++ b/src/Queue/Consumers/StoreThirdPartyDevice.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 04.08.23 @@ -33,7 +33,7 @@ * Store NS Panel third-party device message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Consumers/WriteSubDeviceState.php b/src/Queue/Consumers/WriteSubDeviceState.php index cbcda5e..ad19b4e 100644 --- a/src/Queue/Consumers/WriteSubDeviceState.php +++ b/src/Queue/Consumers/WriteSubDeviceState.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 18.07.23 @@ -42,7 +42,7 @@ * Write state to sub-device message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ @@ -337,7 +337,7 @@ public function consume(Entities\Messages\Entity $entity): bool } $this->logger->error( - 'Could not report device state to NS Panel', + 'Could write state to sub-device', array_merge( [ 'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_NS_PANEL, @@ -374,7 +374,7 @@ public function consume(Entities\Messages\Entity $entity): bool } $this->logger->debug( - 'Consumed write sub device state message', + 'Consumed write sub-device state message', [ 'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_NS_PANEL, 'type' => 'write-sub-device-state-message-consumer', diff --git a/src/Queue/Consumers/WriteThirdPartyDeviceState.php b/src/Queue/Consumers/WriteThirdPartyDeviceState.php index 05882e5..c68cfad 100644 --- a/src/Queue/Consumers/WriteThirdPartyDeviceState.php +++ b/src/Queue/Consumers/WriteThirdPartyDeviceState.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 18.07.23 @@ -42,7 +42,7 @@ * Write state to third-party device message consumer * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Queue/Queue.php b/src/Queue/Queue.php index d97f56e..d8bf74d 100644 --- a/src/Queue/Queue.php +++ b/src/Queue/Queue.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * @since 1.0.0 * * @date 09.07.23 @@ -25,7 +25,7 @@ * Clients messages queue * * @package FastyBird:NsPanelConnector! - * @subpackage Consumers + * @subpackage Queue * * @author Adam Kadlec */ diff --git a/src/Translations/ns-panel-connector.en_us.neon b/src/Translations/ns-panel-connector.en_us.neon index 372591d..75d09bd 100644 --- a/src/Translations/ns-panel-connector.en_us.neon +++ b/src/Translations/ns-panel-connector.en_us.neon @@ -129,7 +129,6 @@ cmd: remove: success: "Connector \"%name%\" was successfully removed." error: "Something went wrong, connector could not be removed. An error was logged." - noConnectors: "No NS Panel connectors registered in system." devices: title: "NS Panel connector - devices management" @@ -274,7 +273,6 @@ cmd: connector: notFound: "Connector was not found in system." disabled: "Connector is disabled. Disabled connector could not be executed." - noConnectors: "No NS Panel connectors registered in system." error: "Something went wrong, connector was terminated." discovery: @@ -296,7 +294,6 @@ cmd: connector: notFound: "Connector was not found in system." disabled: "Connector is disabled. Disabled connector could not be executed." - noConnectors: "No NS Panel connectors registered in system." starting: "Starting NS Panel connector discovery..." stopping: "Stopping NS Panel connector discovery..." foundDevices: "Found %d new devices" diff --git a/src/Writers/Event.php b/src/Writers/Event.php index 8c3d7ba..815d261 100644 --- a/src/Writers/Event.php +++ b/src/Writers/Event.php @@ -18,6 +18,7 @@ use FastyBird\Connector\NsPanel\Entities; use FastyBird\Connector\NsPanel\Exceptions; use FastyBird\Connector\NsPanel\Helpers; +use FastyBird\Connector\NsPanel\Queries; use FastyBird\Connector\NsPanel\Queue; use FastyBird\Library\Metadata\Exceptions as MetadataExceptions; use FastyBird\Library\Metadata\Types as MetadataTypes; @@ -25,7 +26,6 @@ use FastyBird\Module\Devices\Events as DevicesEvents; use FastyBird\Module\Devices\Exceptions as DevicesExceptions; use FastyBird\Module\Devices\Models as DevicesModels; -use FastyBird\Module\Devices\Queries as DevicesQueries; use Nette; use Symfony\Component\EventDispatcher; use function assert; @@ -90,10 +90,10 @@ public function stateChanged( $channel = $property->getChannel(); } else { - $findChannelQuery = new DevicesQueries\FindChannels(); + $findChannelQuery = new Queries\FindChannels(); $findChannelQuery->byId($property->getChannel()); - $channel = $this->channelsRepository->findOneBy($findChannelQuery); + $channel = $this->channelsRepository->findOneBy($findChannelQuery, Entities\NsPanelChannel::class); } if ($channel === null) { diff --git a/src/Writers/EventFactory.php b/src/Writers/EventFactory.php index 732c0db..e26e717 100644 --- a/src/Writers/EventFactory.php +++ b/src/Writers/EventFactory.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * @since 1.0.0 * * @date 09.08.23 @@ -21,7 +21,7 @@ * System event device state periodic writer factory * * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * * @author Adam Kadlec */ diff --git a/src/Writers/Exchange.php b/src/Writers/Exchange.php index 963dcde..4025edd 100644 --- a/src/Writers/Exchange.php +++ b/src/Writers/Exchange.php @@ -18,6 +18,7 @@ use FastyBird\Connector\NsPanel\Entities; use FastyBird\Connector\NsPanel\Exceptions; use FastyBird\Connector\NsPanel\Helpers; +use FastyBird\Connector\NsPanel\Queries; use FastyBird\Connector\NsPanel\Queue; use FastyBird\Library\Exchange\Consumers as ExchangeConsumers; use FastyBird\Library\Metadata\Entities as MetadataEntities; @@ -25,9 +26,7 @@ use FastyBird\Library\Metadata\Types as MetadataTypes; use FastyBird\Module\Devices\Exceptions as DevicesExceptions; use FastyBird\Module\Devices\Models as DevicesModels; -use FastyBird\Module\Devices\Queries as DevicesQueries; use Nette; -use function assert; /** * Exchange based properties writer @@ -81,10 +80,10 @@ public function consume( || $entity instanceof MetadataEntities\DevicesModule\ChannelMappedProperty || $entity instanceof MetadataEntities\DevicesModule\ChannelVariableProperty ) { - $findChannelQuery = new DevicesQueries\FindChannels(); + $findChannelQuery = new Queries\FindChannels(); $findChannelQuery->byId($entity->getChannel()); - $channel = $this->channelsRepository->findOneBy($findChannelQuery); + $channel = $this->channelsRepository->findOneBy($findChannelQuery, Entities\NsPanelChannel::class); if ($channel === null) { return; @@ -97,8 +96,6 @@ public function consume( } if ($device instanceof Entities\Devices\SubDevice) { - assert($channel instanceof Entities\NsPanelChannel); - if ( ( $entity instanceof MetadataEntities\DevicesModule\ChannelDynamicProperty @@ -115,8 +112,6 @@ public function consume( $this->writeSubDeviceChannelProperty($device, $channel); } elseif ($device instanceof Entities\Devices\ThirdPartyDevice) { - assert($channel instanceof Entities\NsPanelChannel); - if ( ( $entity instanceof MetadataEntities\DevicesModule\ChannelDynamicProperty diff --git a/src/Writers/ExchangeFactory.php b/src/Writers/ExchangeFactory.php index 7b739aa..4cbc509 100644 --- a/src/Writers/ExchangeFactory.php +++ b/src/Writers/ExchangeFactory.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * @since 1.0.0 * * @date 09.08.23 @@ -21,7 +21,7 @@ * Event bus exchange device state periodic writer factory * * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * * @author Adam Kadlec */ diff --git a/src/Writers/PeriodicFactory.php b/src/Writers/PeriodicFactory.php index 5343e5a..0a11a3a 100644 --- a/src/Writers/PeriodicFactory.php +++ b/src/Writers/PeriodicFactory.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * @since 1.0.0 * * @date 09.08.23 @@ -21,7 +21,7 @@ * Event loop device state periodic writer factory * * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * * @author Adam Kadlec */ diff --git a/src/Writers/WriterFactory.php b/src/Writers/WriterFactory.php index bd3e2e0..ee1362c 100644 --- a/src/Writers/WriterFactory.php +++ b/src/Writers/WriterFactory.php @@ -7,7 +7,7 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * @since 1.0.0 * * @date 09.08.23 @@ -21,7 +21,7 @@ * Device state writer interface factory * * @package FastyBird:NsPanelConnector! - * @subpackage Clients + * @subpackage Writers * * @author Adam Kadlec */ diff --git a/tests/cases/unit/API/LanApiTest.php b/tests/cases/unit/API/LanApiTest.php index f66cf64..0edeb11 100644 --- a/tests/cases/unit/API/LanApiTest.php +++ b/tests/cases/unit/API/LanApiTest.php @@ -7,7 +7,7 @@ use FastyBird\Connector\NsPanel\Entities; use FastyBird\Connector\NsPanel\Exceptions; use FastyBird\Connector\NsPanel\Queries; -use FastyBird\Connector\NsPanel\Tests\Cases\Unit\DbTestCase; +use FastyBird\Connector\NsPanel\Tests; use FastyBird\Connector\NsPanel\Tests\Tools; use FastyBird\Library\Bootstrap\Exceptions as BootstrapExceptions; use FastyBird\Library\Metadata\Exceptions as MetadataExceptions; @@ -22,7 +22,7 @@ use function str_replace; use function strval; -final class LanApiTest extends DbTestCase +final class LanApiTest extends Tests\Cases\Unit\DbTestCase { /** @@ -74,7 +74,7 @@ public function testGetGatewayInfo(): void $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -160,7 +160,7 @@ public function testGetGatewayAccessToken(): void $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -248,7 +248,7 @@ public function testGetGatewayAccessTokenError(): void $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -361,7 +361,7 @@ static function (string $expectation) use ($actual): string { $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -511,7 +511,7 @@ static function (string $expectation) use ($actual): string { $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -634,7 +634,7 @@ static function (string $expectation) use ($actual): string { $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -721,7 +721,7 @@ public function testGetSubDevices(): void $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( @@ -844,7 +844,7 @@ static function (string $expectation) use ($actual): string { $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( diff --git a/tests/cases/unit/Clients/DiscoveryTest.php b/tests/cases/unit/Clients/DiscoveryTest.php index 8bf0d37..3361bf8 100644 --- a/tests/cases/unit/Clients/DiscoveryTest.php +++ b/tests/cases/unit/Clients/DiscoveryTest.php @@ -9,7 +9,7 @@ use FastyBird\Connector\NsPanel\Exceptions; use FastyBird\Connector\NsPanel\Queries; use FastyBird\Connector\NsPanel\Queue; -use FastyBird\Connector\NsPanel\Tests\Cases\Unit\DbTestCase; +use FastyBird\Connector\NsPanel\Tests; use FastyBird\Connector\NsPanel\Types; use FastyBird\Library\Bootstrap\Exceptions as BootstrapExceptions; use FastyBird\Library\Metadata\Exceptions as MetadataExceptions; @@ -22,7 +22,7 @@ use React\EventLoop; use RuntimeException; -final class DiscoveryTest extends DbTestCase +final class DiscoveryTest extends Tests\Cases\Unit\DbTestCase { /** @@ -67,7 +67,7 @@ public function testDiscover(): void $httpClientFactory = $this->createMock(API\HttpClientFactory::class); $httpClientFactory - ->method('createClient') + ->method('create') ->willReturn($httpClient); $this->mockContainerService( diff --git a/tests/cases/unit/DI/NsPanelExtensionTest.php b/tests/cases/unit/DI/NsPanelExtensionTest.php index f02063b..202335e 100644 --- a/tests/cases/unit/DI/NsPanelExtensionTest.php +++ b/tests/cases/unit/DI/NsPanelExtensionTest.php @@ -3,13 +3,24 @@ namespace FastyBird\Connector\NsPanel\Tests\Cases\Unit\DI; use Error; +use FastyBird\Connector\NsPanel\API; +use FastyBird\Connector\NsPanel\Clients; +use FastyBird\Connector\NsPanel\Commands; +use FastyBird\Connector\NsPanel\Connector; +use FastyBird\Connector\NsPanel\Controllers; +use FastyBird\Connector\NsPanel\Helpers; use FastyBird\Connector\NsPanel\Hydrators; +use FastyBird\Connector\NsPanel\Middleware; +use FastyBird\Connector\NsPanel\Queue; use FastyBird\Connector\NsPanel\Schemas; -use FastyBird\Connector\NsPanel\Tests\Cases\Unit\BaseTestCase; +use FastyBird\Connector\NsPanel\Servers; +use FastyBird\Connector\NsPanel\Subscribers; +use FastyBird\Connector\NsPanel\Tests; +use FastyBird\Connector\NsPanel\Writers; use FastyBird\Library\Bootstrap\Exceptions as BootstrapExceptions; use Nette; -final class NsPanelExtensionTest extends BaseTestCase +final class NsPanelExtensionTest extends Tests\Cases\Unit\BaseTestCase { /** @@ -21,15 +32,53 @@ public function testServicesRegistration(): void { $container = $this->createContainer(); - self::assertNotNull($container->getByType(Hydrators\NsPanelConnector::class, false)); - self::assertNotNull($container->getByType(Hydrators\Devices\Gateway::class, false)); - self::assertNotNull($container->getByType(Hydrators\Devices\SubDevice::class, false)); - self::assertNotNull($container->getByType(Hydrators\Devices\ThirdPartyDevice::class, false)); + self::assertNotNull($container->getByType(Writers\WriterFactory::class, false)); + + self::assertNotNull($container->getByType(Clients\GatewayFactory::class, false)); + self::assertNotNull($container->getByType(Clients\DeviceFactory::class, false)); + self::assertNotNull($container->getByType(Clients\DiscoveryFactory::class, false)); + + self::assertNotNull($container->getByType(API\HttpClientFactory::class, false)); + self::assertNotNull($container->getByType(API\LanApiFactory::class, false)); + + self::assertNotNull($container->getByType(Servers\HttpFactory::class, false)); + + self::assertNotNull($container->getByType(Queue\Consumers\StoreDeviceState::class, false)); + self::assertNotNull($container->getByType(Queue\Consumers\StoreDeviceConnectionState::class, false)); + self::assertNotNull($container->getByType(Queue\Consumers\StoreThirdPartyDevice::class, false)); + self::assertNotNull($container->getByType(Queue\Consumers\StoreSubDevice::class, false)); + self::assertNotNull($container->getByType(Queue\Consumers\WriteSubDeviceState::class, false)); + self::assertNotNull($container->getByType(Queue\Consumers::class, false)); + self::assertNotNull($container->getByType(Queue\Queue::class, false)); + + self::assertNotNull($container->getByType(Subscribers\Properties::class, false)); + self::assertNotNull($container->getByType(Subscribers\Controls::class, false)); self::assertNotNull($container->getByType(Schemas\NsPanelConnector::class, false)); self::assertNotNull($container->getByType(Schemas\Devices\Gateway::class, false)); self::assertNotNull($container->getByType(Schemas\Devices\SubDevice::class, false)); self::assertNotNull($container->getByType(Schemas\Devices\ThirdPartyDevice::class, false)); + self::assertNotNull($container->getByType(Schemas\NsPanelChannel::class, false)); + + self::assertNotNull($container->getByType(Hydrators\NsPanelConnector::class, false)); + self::assertNotNull($container->getByType(Hydrators\Devices\Gateway::class, false)); + self::assertNotNull($container->getByType(Hydrators\Devices\SubDevice::class, false)); + self::assertNotNull($container->getByType(Hydrators\Devices\ThirdPartyDevice::class, false)); + self::assertNotNull($container->getByType(Hydrators\NsPanelChannel::class, false)); + + self::assertNotNull($container->getByType(Helpers\Loader::class, false)); + self::assertNotNull($container->getByType(Helpers\Entity::class, false)); + + self::assertNotNull($container->getByType(Middleware\Router::class, false)); + + self::assertNotNull($container->getByType(Controllers\DirectiveController::class, false)); + + self::assertNotNull($container->getByType(Commands\Initialize::class, false)); + self::assertNotNull($container->getByType(Commands\Devices::class, false)); + self::assertNotNull($container->getByType(Commands\Execute::class, false)); + self::assertNotNull($container->getByType(Commands\Discovery::class, false)); + + self::assertNotNull($container->getByType(Connector\ConnectorFactory::class, false)); } } diff --git a/tests/cases/unit/Entities/API/HeaderTest.php b/tests/cases/unit/Entities/API/HeaderTest.php index 9107ebc..b7abb65 100644 --- a/tests/cases/unit/Entities/API/HeaderTest.php +++ b/tests/cases/unit/Entities/API/HeaderTest.php @@ -4,7 +4,7 @@ use Error; use FastyBird\Connector\NsPanel\Entities; -use FastyBird\Connector\NsPanel\Tests\Cases\Unit\BaseTestCase; +use FastyBird\Connector\NsPanel\Tests; use FastyBird\Connector\NsPanel\Types; use FastyBird\Library\Bootstrap\Exceptions as BootstrapExceptions; use Nette; @@ -12,7 +12,7 @@ use Ramsey\Uuid; use function assert; -final class HeaderTest extends BaseTestCase +final class HeaderTest extends Tests\Cases\Unit\BaseTestCase { /** diff --git a/tests/cases/unit/Entities/API/Request/SetDeviceStatusTest.php b/tests/cases/unit/Entities/API/Request/SetDeviceStatusTest.php index af6b183..0df8219 100644 --- a/tests/cases/unit/Entities/API/Request/SetDeviceStatusTest.php +++ b/tests/cases/unit/Entities/API/Request/SetDeviceStatusTest.php @@ -4,14 +4,14 @@ use Error; use FastyBird\Connector\NsPanel\Entities; -use FastyBird\Connector\NsPanel\Tests\Cases\Unit\BaseTestCase; +use FastyBird\Connector\NsPanel\Tests; use FastyBird\Library\Bootstrap\Exceptions as BootstrapExceptions; use Nette; use Nette\Utils; use Orisai\ObjectMapper; use function assert; -final class SetDeviceStatusTest extends BaseTestCase +final class SetDeviceStatusTest extends Tests\Cases\Unit\BaseTestCase { /**