Skip to content

Commit

Permalink
[tuya-connector] Refactoring tuya connector to new specs (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 22, 2023
1 parent 9c9696d commit 87efeab
Show file tree
Hide file tree
Showing 34 changed files with 158 additions and 113 deletions.
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/API/HttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/API/LanApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -971,7 +971,7 @@ static function (Throwable $ex) use ($deferred, $request): void {

try {
$response = $this->httpClientFactory
->createClient(false)
->create(false)
->send($request);

try {
Expand Down
5 changes: 3 additions & 2 deletions src/Clients/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
28 changes: 13 additions & 15 deletions src/Clients/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
18 changes: 6 additions & 12 deletions src/Commands/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,7 @@ private function editGateway(

$panelInfo = $this->askWhichPanel($io, $connector, $gateway);

/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($gateway);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::IP_ADDRESS);

Expand All @@ -412,8 +411,7 @@ private function editGateway(
DevicesEntities\Devices\Properties\Variable::class,
);

/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($gateway);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::DOMAIN);

Expand All @@ -422,8 +420,7 @@ private function editGateway(
DevicesEntities\Devices\Properties\Variable::class,
);

/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($gateway);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::MAC_ADDRESS);

Expand All @@ -432,8 +429,7 @@ private function editGateway(
DevicesEntities\Devices\Properties\Variable::class,
);

/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($gateway);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::FIRMWARE_VERSION);

Expand Down Expand Up @@ -509,8 +505,7 @@ private function editGateway(
}
}

/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($gateway);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::ACCESS_TOKEN);

Expand Down Expand Up @@ -729,8 +724,7 @@ static function (Entities\Devices\Gateway $a, Entities\Devices\Gateway $b): int
]);

foreach ($devices as $index => $device) {
/** @var DevicesQueries\FindDeviceProperties<DevicesEntities\Devices\Properties\Variable> $findDevicePropertyQuery */
$findDevicePropertyQuery = new DevicesQueries\FindDeviceProperties();
$findDevicePropertyQuery = new DevicesQueries\FindDeviceVariableProperties();
$findDevicePropertyQuery->forDevice($device);
$findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::IP_ADDRESS);

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Execute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/DI/NsPanelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
* @since 1.0.0
*
* @date 09.07.23
Expand All @@ -21,7 +21,7 @@
* Clients messages consumer interface
*
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
* @since 1.0.0
*
* @date 09.08.23
Expand All @@ -24,7 +24,7 @@
* Clients message queue consumers container
*
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers/DeviceProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
* @since 1.0.0
*
* @date 18.07.23
Expand All @@ -33,7 +33,7 @@
* Device property consumer
*
* @package FastyBird:NsPanelConnector!
* @subpackage Consumers
* @subpackage Queue
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers/StateWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:NsPanelConnector!
* @subpackage Clients
* @subpackage Queue
* @since 1.0.0
*
* @date 16.07.23
Expand Down Expand Up @@ -39,7 +39,7 @@
* Third-party device & sub-device property to state mapper
*
* @package FastyBird:NsPanelConnector!
* @subpackage Clients
* @subpackage Queue
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*
Expand Down
Loading

0 comments on commit 87efeab

Please sign in to comment.