Skip to content

Commit

Permalink
Refactoring process for viera connector (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Nov 21, 2023
1 parent 0c4b0ff commit 738dda7
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 74 deletions.
225 changes: 225 additions & 0 deletions src/Queue/Consumers/ChannelProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
<?php declare(strict_types = 1);

/**
* ChannelProperty.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <[email protected]>
* @package FastyBird:TuyaConnector!
* @subpackage Queue
* @since 1.0.0
*
* @date 21.11.23
*/

namespace FastyBird\Connector\Tuya\Queue\Consumers;

use Doctrine\DBAL;
use FastyBird\Connector\Tuya;
use FastyBird\Library\Metadata\Exceptions as MetadataExceptions;
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Entities as DevicesEntities;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Queries as DevicesQueries;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use Nette\Utils;
use Ramsey\Uuid;
use function array_merge;

/**
* Device channel property consumer trait
*
* @package FastyBird:TuyaConnector!
* @subpackage Queue
*
* @author Adam Kadlec <[email protected]>
*
* @property-read DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository
* @property-read DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository
* @property-read DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager
* @property-read DevicesUtilities\Database $databaseHelper
* @property-read Tuya\Logger $logger
*/
trait ChannelProperty
{

/**
* @param class-string<DevicesEntities\Channels\Properties\Variable|DevicesEntities\Channels\Properties\Dynamic> $type
* @param string|array<int, string>|array<int, string|int|float|array<int, string|int|float>|Utils\ArrayHash|null>|array<int, array<int, string|array<int, string|int|float|bool>|Utils\ArrayHash|null>>|null $format
*
* @throws DBAL\Exception
* @throws DevicesExceptions\InvalidState
* @throws DevicesExceptions\Runtime
* @throws MetadataExceptions\InvalidArgument
* @throws MetadataExceptions\InvalidState
*/
private function setChannelProperty(
string $type,
Uuid\UuidInterface $channelId,
string|bool|int|null $value,
MetadataTypes\DataType $dataType,
string $identifier,
string|null $name = null,
array|string|null $format = null,
string|null $unit = null,
float|int|string|null $invalid = null,
float|int|null $scale = null,
float|int|null $step = null,
bool $settable = false,
bool $queryable = false,
): void
{
$findChannelPropertyQuery = new DevicesQueries\Entities\FindChannelProperties();
$findChannelPropertyQuery->byChannelId($channelId);
$findChannelPropertyQuery->byIdentifier($identifier);

$property = $this->channelsPropertiesRepository->findOneBy($findChannelPropertyQuery);

if ($property !== null && $value === null && $type === DevicesEntities\Channels\Properties\Variable::class) {
$this->databaseHelper->transaction(
function () use ($property): void {
$this->channelsPropertiesManager->delete($property);
},
);

return;
}

if ($value === null && $type === DevicesEntities\Channels\Properties\Variable::class) {
return;
}

if (
$property instanceof DevicesEntities\Channels\Properties\Variable
&& $property->getValue() === $value
) {
return;
}

if ($property !== null && !$property instanceof $type) {
$findChannelPropertyQuery = new DevicesQueries\Entities\FindChannelProperties();
$findChannelPropertyQuery->byId($property->getId());

$property = $this->channelsPropertiesRepository->findOneBy($findChannelPropertyQuery);

if ($property !== null) {
$this->databaseHelper->transaction(function () use ($property): void {
$this->channelsPropertiesManager->delete($property);
});

$this->logger->warning(
'Stored channel property was not of valid type',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'channel' => [
'id' => $channelId->toString(),
],
'property' => [
'id' => $property->getId()->toString(),
'identifier' => $identifier,
],
],
);
}

$property = null;
}

if ($property === null) {
$findChannelQuery = new DevicesQueries\Entities\FindChannels();
$findChannelQuery->byId($channelId);

$channel = $this->channelsRepository->findOneBy($findChannelQuery);

if ($channel === null) {
return;
}

$property = $this->databaseHelper->transaction(
fn (): DevicesEntities\Channels\Properties\Property => $this->channelsPropertiesManager->create(
Utils\ArrayHash::from(array_merge(
[
'entity' => $type,
'channel' => $channel,
'identifier' => $identifier,
'name' => $name,
'dataType' => $dataType,
'format' => $format,
'unit' => $unit,
'invalid' => $invalid,
'scale' => $scale,
'step' => $step,
],
$type === DevicesEntities\Channels\Properties\Variable::class
? [
'value' => $value,
]
: [
'settable' => $settable,
'queryable' => $queryable,
],
)),
),
);

$this->logger->debug(
'Channel property was created',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'channel' => [
'id' => $channelId->toString(),
],
'property' => [
'id' => $property->getId()->toString(),
'identifier' => $identifier,
],
],
);

} else {
$property = $this->databaseHelper->transaction(
fn (): DevicesEntities\Channels\Properties\Property => $this->channelsPropertiesManager->update(
$property,
Utils\ArrayHash::from(array_merge(
[
'dataType' => $dataType,
'format' => $format,
'unit' => $unit,
'invalid' => $invalid,
'scale' => $scale,
'step' => $step,
],
$type === DevicesEntities\Channels\Properties\Variable::class
? [
'value' => $value,
]
: [
'settable' => $settable,
'queryable' => $queryable,
],
)),
),
);

$this->logger->debug(
'Channel property was updated',
[
'source' => MetadataTypes\ConnectorSource::SOURCE_CONNECTOR_TUYA,
'type' => 'message-consumer',
'channel' => [
'id' => $channelId->toString(),
],
'property' => [
'id' => $property->getId()->toString(),
'identifier' => $identifier,
],
],
);
}
}

}
10 changes: 4 additions & 6 deletions src/Queue/Consumers/DeviceProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
use FastyBird\Library\Metadata\Types as MetadataTypes;
use FastyBird\Module\Devices\Entities as DevicesEntities;
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models\Entities\Devices\DevicesRepository;
use FastyBird\Module\Devices\Models\Entities\Devices\Properties\PropertiesManager;
use FastyBird\Module\Devices\Models\Entities\Devices\Properties\PropertiesRepository;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Queries as DevicesQueries;
use FastyBird\Module\Devices\Utilities as DevicesUtilities;
use Nette\Utils;
Expand All @@ -39,9 +37,9 @@
*
* @author Adam Kadlec <[email protected]>
*
* @property-read DevicesRepository $devicesRepository
* @property-read PropertiesRepository $devicesPropertiesRepository
* @property-read PropertiesManager $devicesPropertiesManager
* @property-read DevicesModels\Entities\Devices\DevicesRepository $devicesRepository
* @property-read DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository
* @property-read DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager
* @property-read DevicesUtilities\Database $databaseHelper
* @property-read Tuya\Logger $logger
*/
Expand Down
52 changes: 17 additions & 35 deletions src/Queue/Consumers/StoreCloudDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ final class StoreCloudDevice implements Queue\Consumer

use Nette\SmartObject;
use DeviceProperty;
use ChannelProperty;

public function __construct(
protected readonly Tuya\Logger $logger,
protected readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository,
protected readonly DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository,
protected readonly DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager,
protected readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository,
protected readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository,
protected readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager,
protected readonly DevicesUtilities\Database $databaseHelper,
private readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository,
private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository,
private readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager,
private readonly DevicesModels\Entities\Connectors\ConnectorsRepository $connectorsRepository,
private readonly DevicesModels\Entities\Devices\DevicesManager $devicesManager,
private readonly DevicesModels\Entities\Channels\ChannelsManager $channelsManager,
Expand Down Expand Up @@ -254,40 +255,21 @@ function () use ($entity, $device): Entities\TuyaDevice {
}

foreach ($entity->getDataPoints() as $dataPoint) {
$findChannelPropertyQuery = new DevicesQueries\Entities\FindChannelDynamicProperties();
$findChannelPropertyQuery->forChannel($channel);
$findChannelPropertyQuery->byIdentifier($dataPoint->getCode());

$property = $this->channelsPropertiesRepository->findOneBy(
$findChannelPropertyQuery,
$this->setChannelProperty(
DevicesEntities\Channels\Properties\Dynamic::class,
$channel->getId(),
null,
$dataPoint->getDataType(),
$dataPoint->getCode(),
$dataPoint->getCode(),
$dataPoint->getFormat(),
$dataPoint->getUnit(),
null,
$dataPoint->getScale(),
$dataPoint->getStep(),
$dataPoint->isSettable(),
$dataPoint->isQueryable(),
);

if ($property === null) {
$this->channelsPropertiesManager->create(Utils\ArrayHash::from([
'channel' => $channel,
'entity' => DevicesEntities\Channels\Properties\Dynamic::class,
'identifier' => $dataPoint->getCode(),
'dataType' => $dataPoint->getDataType(),
'unit' => $dataPoint->getUnit(),
'format' => $dataPoint->getFormat(),
'scale' => $dataPoint->getScale(),
'step' => $dataPoint->getStep(),
'queryable' => $dataPoint->isQueryable(),
'settable' => $dataPoint->isSettable(),
]));

} else {
$this->channelsPropertiesManager->update($property, Utils\ArrayHash::from([
'dataType' => $dataPoint->getDataType(),
'unit' => $dataPoint->getUnit(),
'format' => $dataPoint->getFormat(),
'scale' => $dataPoint->getScale(),
'step' => $dataPoint->getStep(),
'queryable' => $dataPoint->isQueryable(),
'settable' => $dataPoint->isSettable(),
]));
}
}

return true;
Expand Down
50 changes: 17 additions & 33 deletions src/Queue/Consumers/StoreLocalDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ final class StoreLocalDevice implements Queue\Consumer

use Nette\SmartObject;
use DeviceProperty;
use ChannelProperty;

public function __construct(
protected readonly Tuya\Logger $logger,
protected readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository,
protected readonly DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository,
protected readonly DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager,
protected readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository,
protected readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository,
protected readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager,
protected readonly DevicesUtilities\Database $databaseHelper,
private readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository,
private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository,
private readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager,
private readonly DevicesModels\Entities\Connectors\ConnectorsRepository $connectorsRepository,
private readonly DevicesModels\Entities\Devices\DevicesManager $devicesManager,
private readonly DevicesModels\Entities\Channels\ChannelsManager $channelsManager,
Expand Down Expand Up @@ -317,38 +318,21 @@ function () use ($device, $parent): void {
}

foreach ($entity->getDataPoints() as $dataPoint) {
$findChannelPropertyQuery = new DevicesQueries\Entities\FindChannelDynamicProperties();
$findChannelPropertyQuery->forChannel($channel);
$findChannelPropertyQuery->byIdentifier($dataPoint->getCode());

$property = $this->channelsPropertiesRepository->findOneBy(
$findChannelPropertyQuery,
$this->setChannelProperty(
DevicesEntities\Channels\Properties\Dynamic::class,
$channel->getId(),
null,
$dataPoint->getDataType(),
$dataPoint->getCode(),
$dataPoint->getCode(),
$dataPoint->getFormat(),
$dataPoint->getUnit(),
null,
null,
$dataPoint->getStep(),
$dataPoint->isSettable(),
$dataPoint->isQueryable(),
);

if ($property === null) {
$this->channelsPropertiesManager->create(Utils\ArrayHash::from([
'channel' => $channel,
'entity' => DevicesEntities\Channels\Properties\Dynamic::class,
'identifier' => $dataPoint->getCode(),
'name' => $dataPoint->getCode(),
'dataType' => $dataPoint->getDataType(),
'unit' => $dataPoint->getUnit(),
'format' => $dataPoint->getFormat(),
'queryable' => $dataPoint->isQueryable(),
'settable' => $dataPoint->isSettable(),
]));

} else {
$this->channelsPropertiesManager->update($property, Utils\ArrayHash::from([
'name' => $property->getName() ?? $dataPoint->getCode(),
'dataType' => $dataPoint->getDataType(),
'unit' => $dataPoint->getUnit(),
'format' => $dataPoint->getFormat(),
'queryable' => $dataPoint->isQueryable(),
'settable' => $dataPoint->isSettable(),
]));
}
}

return true;
Expand Down

0 comments on commit 738dda7

Please sign in to comment.