-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring process for viera connector (#183)
- Loading branch information
1 parent
0c4b0ff
commit 738dda7
Showing
4 changed files
with
263 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
], | ||
], | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters