From d4b3fa10f696996c066e0ad27b35b4bfb09bac8d Mon Sep 17 00:00:00 2001 From: GitHub Date: Tue, 27 Aug 2024 11:03:41 +0000 Subject: [PATCH] [viera-connector] Fixed viera device installation (#303) --- src/API/TelevisionApi.php | 38 +- src/Clients/Discovery.php | 6 +- src/Commands/Discover.php | 80 +-- src/Commands/Install.php | 628 ++++++++---------- src/Constants.php | 60 ++ src/DI/VieraExtension.php | 15 + .../Consumers => Helpers}/ChannelProperty.php | 42 +- .../Consumers => Helpers}/DeviceProperty.php | 44 +- src/Queue/Consumers/StoreDevice.php | 57 +- src/Subscribers/Device.php | 217 ++++++ src/Subscribers/Properties.php | 257 +++---- src/Types/ChannelPropertyIdentifier.php | 4 + tests/cases/unit/DI/VieraExtensionTest.php | 4 + 13 files changed, 750 insertions(+), 702 deletions(-) create mode 100644 src/Constants.php rename src/{Queue/Consumers => Helpers}/ChannelProperty.php (85%) rename src/{Queue/Consumers => Helpers}/DeviceProperty.php (85%) create mode 100644 src/Subscribers/Device.php diff --git a/src/API/TelevisionApi.php b/src/API/TelevisionApi.php index 9eb26c2..e7b3b44 100644 --- a/src/API/TelevisionApi.php +++ b/src/API/TelevisionApi.php @@ -44,6 +44,7 @@ use function array_merge; use function array_pop; use function array_values; +use function assert; use function base64_decode; use function boolval; use function chr; @@ -1436,6 +1437,7 @@ function (Socket\ConnectionInterface $connection): void { strval($this->eventsServer->getAddress()), $matches, ); + assert(array_key_exists('port', $matches)); try { $client = $this->httpClientFactory->create(false); @@ -1653,10 +1655,6 @@ private function parseGetApps( $this->session !== null && preg_match('/(?.*?)<\/X_EncResult>/', $body, $matches) === 1 ) { - if (!array_key_exists('encrypted', $matches)) { - throw new Exceptions\TelevisionApiCall('Could not parse received response'); - } - try { $payload = Crypto::decryptPayload( $matches['encrypted'], @@ -1748,10 +1746,6 @@ private function parseGetVectorInfo( $this->session !== null && preg_match('/(?.*?)<\/X_EncResult>/', $body, $matches) === 1 ) { - if (!array_key_exists('encrypted', $matches)) { - throw new Exceptions\TelevisionApiCall('Could not parse received response'); - } - try { $payload = Crypto::decryptPayload( $matches['encrypted'], @@ -1821,10 +1815,6 @@ private function parseGetVolume( $this->session !== null && preg_match('/(?.*?)<\/X_EncResult>/', $body, $matches) === 1 ) { - if (!array_key_exists('encrypted', $matches)) { - throw new Exceptions\TelevisionApiCall('Could not parse received response', $request, $response); - } - try { $payload = Crypto::decryptPayload( $matches['encrypted'], @@ -1842,20 +1832,14 @@ private function parseGetVolume( ); } - if ( - preg_match('/(?.*?)<\/CurrentVolume>/', $payload, $matches) !== 1 - || !array_key_exists('volume', $matches) - ) { + if (preg_match('/(?.*?)<\/CurrentVolume>/', $payload, $matches) !== 1) { throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response); } return intval($matches['volume']); } - if ( - preg_match('/(?.*?)<\/CurrentVolume>/', $body, $matches) !== 1 - || !array_key_exists('volume', $matches) - ) { + if (preg_match('/(?.*?)<\/CurrentVolume>/', $body, $matches) !== 1) { throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response); } @@ -1876,10 +1860,6 @@ private function parseGetMute( $this->session !== null && preg_match('/(?.*?)<\/X_EncResult>/', $body, $matches) === 1 ) { - if (!array_key_exists('encrypted', $matches)) { - throw new Exceptions\TelevisionApiCall('Could not parse received response', $request, $response); - } - try { $payload = Crypto::decryptPayload( $matches['encrypted'], @@ -1897,20 +1877,14 @@ private function parseGetMute( ); } - if ( - preg_match('/(?.*?)<\/CurrentMute>/', $payload, $matches) !== 1 - || !array_key_exists('mute', $matches) - ) { + if (preg_match('/(?.*?)<\/CurrentMute>/', $payload, $matches) !== 1) { throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response); } return boolval($matches['mute']); } - if ( - preg_match('/(?.*?)<\/CurrentMute>/', $body, $matches) !== 1 - || !array_key_exists('mute', $matches) - ) { + if (preg_match('/(?.*?)<\/CurrentMute>/', $body, $matches) !== 1) { throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response); } diff --git a/src/Clients/Discovery.php b/src/Clients/Discovery.php index 8823d15..f81cdb5 100644 --- a/src/Clients/Discovery.php +++ b/src/Clients/Discovery.php @@ -113,17 +113,13 @@ public function discover(): void } $this->sender->on('message', async(function (string $data): void { - if ( - preg_match(self::MATCH_DEVICE_LOCATION, $data, $matches) === 1 - && array_key_exists('location', $matches) - ) { + if (preg_match(self::MATCH_DEVICE_LOCATION, $data, $matches) === 1) { $urlParts = parse_url($matches['location']); if ( is_array($urlParts) && array_key_exists('host', $urlParts) && preg_match(self::MATCH_DEVICE_ID, $data, $matches) === 1 - && array_key_exists('usn', $matches) ) { $this->handleDiscoveredDevice( $matches['usn'], diff --git a/src/Commands/Discover.php b/src/Commands/Discover.php index 86313e7..51242a8 100644 --- a/src/Commands/Discover.php +++ b/src/Commands/Discover.php @@ -36,9 +36,7 @@ use FastyBird\Module\Devices\Models as DevicesModels; use FastyBird\Module\Devices\Types as DevicesTypes; use FastyBird\Module\Devices\Utilities as DevicesUtilities; -use IPub\DoctrineCrud\Exceptions as DoctrineCrudExceptions; use Nette\Localization; -use Nette\Utils; use Ramsey\Uuid; use Symfony\Component\Console; use Symfony\Component\Console\Input; @@ -78,10 +76,9 @@ class Discover extends Console\Command\Command public function __construct( private readonly Api\TelevisionApiFactory $televisionApiFactory, private readonly Helpers\Device $deviceHelper, + private readonly Helpers\DeviceProperty $deviceProperty, private readonly Viera\Logger $logger, private readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository, - private readonly DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository, - private readonly DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager, private readonly DevicesModels\Configuration\Connectors\Repository $connectorsConfigurationRepository, private readonly DevicesModels\Configuration\Devices\Repository $devicesConfigurationRepository, private readonly DateTimeFactory\Factory $dateTimeFactory, @@ -115,13 +112,12 @@ protected function configure(): void /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime * @throws Console\Exception\ExceptionInterface * @throws Console\Exception\InvalidArgumentException * @throws DevicesExceptions\InvalidState + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument * @throws MetadataExceptions\InvalidState @@ -336,11 +332,10 @@ function (string|int|null $answer) use ($connectors): Documents\Connectors\Conne /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime * @throws DevicesExceptions\InvalidState + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument * @throws MetadataExceptions\InvalidState @@ -432,10 +427,9 @@ private function showResults( * @param array $encryptedDevices * * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument * @throws MetadataExceptions\InvalidState @@ -595,51 +589,23 @@ private function processEncryptedDevices( $authorization = $this->askPinCode($io, $connector, $televisionApi); - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->byDeviceId($device->getId()); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::APP_ID); - - $appIdProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); - - if ($appIdProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'device' => $device, - 'identifier' => Types\DevicePropertyIdentifier::APP_ID->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getAppId(), - 'format' => null, - ])); - } else { - $this->devicesPropertiesManager->update($appIdProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getAppId(), - ])); - } - - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->byDeviceId($device->getId()); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::ENCRYPTION_KEY); - - $encryptionKeyProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getAppId(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::APP_ID, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), + ); - if ($encryptionKeyProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'device' => $device, - 'identifier' => Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - 'name' => DevicesUtilities\Name::createName( - Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - ), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getEncryptionKey(), - 'format' => null, - ])); - } else { - $this->devicesPropertiesManager->update($encryptionKeyProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getEncryptionKey(), - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getEncryptionKey(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::ENCRYPTION_KEY, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), + ); $io->success( (string) $this->translator->translate( diff --git a/src/Commands/Install.php b/src/Commands/Install.php index e0825b4..b95e09b 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -86,6 +86,8 @@ class Install extends Console\Command\Command public function __construct( private readonly API\TelevisionApiFactory $televisionApiFactory, + private readonly Helpers\DeviceProperty $deviceProperty, + private readonly Helpers\ChannelProperty $channelProperty, private readonly Viera\Logger $logger, private readonly DevicesModels\Entities\Connectors\ConnectorsRepository $connectorsRepository, private readonly DevicesModels\Entities\Connectors\ConnectorsManager $connectorsManager, @@ -96,7 +98,6 @@ public function __construct( private readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository, private readonly DevicesModels\Entities\Channels\ChannelsManager $channelsManager, private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository, - private readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager, private readonly ApplicationHelpers\Database $databaseHelper, private readonly DateTimeFactory\Factory $dateTimeFactory, private readonly Localization\Translator $translator, @@ -808,89 +809,87 @@ private function createDevice(Style\SymfonyStyle $io, Entities\Connectors\Connec ])); assert($device instanceof Entities\Devices\Device); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::IP_ADDRESS->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::IP_ADDRESS->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $ipAddress, - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $ipAddress, + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::IP_ADDRESS, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::IP_ADDRESS->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::PORT->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::PORT->value), - 'dataType' => MetadataTypes\DataType::UINT, - 'value' => Entities\Devices\Device::DEFAULT_PORT, - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + Entities\Devices\Device::DEFAULT_PORT, + MetadataTypes\DataType::UINT, + Types\DevicePropertyIdentifier::PORT, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::PORT->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MODEL->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MODEL->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => trim(sprintf('%s %s', $specs->getModelName(), $specs->getModelNumber())), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + trim(sprintf('%s %s', $specs->getModelName(), $specs->getModelNumber())), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MODEL, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MODEL->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MANUFACTURER->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MANUFACTURER->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $specs->getManufacturer(), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $specs->getManufacturer(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MANUFACTURER, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MANUFACTURER->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::SERIAL_NUMBER->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::SERIAL_NUMBER->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $specs->getSerialNumber(), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $specs->getSerialNumber(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::SERIAL_NUMBER, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::SERIAL_NUMBER->value), + ); - if ($macAddress !== null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MAC_ADDRESS->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MAC_ADDRESS->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $macAddress, - 'device' => $device, - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $macAddress, + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MAC_ADDRESS, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MAC_ADDRESS->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::ENCRYPTED->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTED->value), - 'dataType' => MetadataTypes\DataType::BOOLEAN, - 'value' => $specs->isRequiresEncryption(), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $specs->isRequiresEncryption(), + MetadataTypes\DataType::BOOLEAN, + Types\DevicePropertyIdentifier::ENCRYPTED, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTED->value), + ); if ($authorization !== null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::APP_ID->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getAppId(), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getAppId(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::APP_ID, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), + ); - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getEncryptionKey(), - 'device' => $device, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getEncryptionKey(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::ENCRYPTION_KEY, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), + ); } $channel = $this->channelsManager->create(Utils\ArrayHash::from([ @@ -899,61 +898,75 @@ private function createDevice(Style\SymfonyStyle $io, Entities\Connectors\Connec 'identifier' => Types\ChannelType::TELEVISION->value, ])); - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::STATE->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::STATE->value), - 'dataType' => MetadataTypes\DataType::BOOLEAN, - 'settable' => true, - 'queryable' => true, - 'format' => null, - 'channel' => $channel, - ])); - - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::VOLUME->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::VOLUME->value), - 'dataType' => MetadataTypes\DataType::UCHAR, - 'settable' => true, - 'queryable' => true, - 'format' => [ + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BOOLEAN, + Types\ChannelPropertyIdentifier::STATE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::STATE->value), + null, + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::UCHAR, + Types\ChannelPropertyIdentifier::VOLUME, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::VOLUME->value), + [ 0, 100, ], - 'channel' => $channel, - ])); - - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::MUTE->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::MUTE->value), - 'dataType' => MetadataTypes\DataType::BOOLEAN, - 'settable' => true, - 'queryable' => true, - 'format' => null, - 'channel' => $channel, - ])); - - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::HDMI->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::HDMI->value), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => $hdmi !== [] ? $hdmi : null, - 'channel' => $channel, - ])); - - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::APPLICATION->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::APPLICATION->value), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => $apps !== null ? array_map( + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BOOLEAN, + Types\ChannelPropertyIdentifier::MUTE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::MUTE->value), + null, + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::STRING, + Types\ChannelPropertyIdentifier::REMOTE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::REMOTE->value), + null, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::HDMI, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::HDMI->value), + $hdmi !== [] ? $hdmi : null, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::APPLICATION, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::APPLICATION->value), + $apps !== null ? array_map( static fn (API\Messages\Response\Application $item): array => [ Helpers\Name::sanitizeEnumName($item->getName()), $item->getId(), @@ -961,17 +974,17 @@ private function createDevice(Style\SymfonyStyle $io, Entities\Connectors\Connec ], $apps->getApps(), ) : null, - 'channel' => $channel, - ])); + true, + ); - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::INPUT_SOURCE->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::INPUT_SOURCE->value), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => array_merge( + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::INPUT_SOURCE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::INPUT_SOURCE->value), + array_merge( [ [ 'TV', @@ -989,8 +1002,8 @@ private function createDevice(Style\SymfonyStyle $io, Entities\Connectors\Connec $apps->getApps(), ) : [], ), - 'channel' => $channel, - ])); + true, + ); // Commit all changes into database $this->databaseHelper->commitTransaction(); @@ -1159,16 +1172,6 @@ private function editDevice(Style\SymfonyStyle $io, Entities\Connectors\Connecto } } - $appsProperty = null; - - if ($channel !== null) { - $findChannelPropertyQuery = new Queries\Entities\FindChannelProperties(); - $findChannelPropertyQuery->forChannel($channel); - $findChannelPropertyQuery->byIdentifier(Types\ChannelPropertyIdentifier::APPLICATION); - - $appsProperty = $this->channelsPropertiesRepository->findOneBy($findChannelPropertyQuery); - } - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); $findDevicePropertyQuery->forDevice($device); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::APP_ID); @@ -1181,18 +1184,6 @@ private function editDevice(Style\SymfonyStyle $io, Entities\Connectors\Connecto $encryptionKeyProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->forDevice($device); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::MODEL); - - $hardwareModelProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); - - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->forDevice($device); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::MANUFACTURER); - - $hardwareManufacturerProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); $findDevicePropertyQuery->forDevice($device); $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::MAC_ADDRESS); @@ -1440,116 +1431,78 @@ private function editDevice(Style\SymfonyStyle $io, Entities\Connectors\Connecto ])); assert($device instanceof Entities\Devices\Device); - if ($ipAddressProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::IP_ADDRESS->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::IP_ADDRESS->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $ipAddress, - 'device' => $device, - ])); - } elseif ($ipAddress !== null) { - $this->devicesPropertiesManager->update($ipAddressProperty, Utils\ArrayHash::from([ - 'value' => $ipAddress, - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $ipAddress, + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::IP_ADDRESS, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::IP_ADDRESS->value), + ); - if ($portProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::PORT->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::PORT->value), - 'dataType' => MetadataTypes\DataType::UINT, - 'value' => $port, - 'device' => $device, - ])); - } else { - $this->devicesPropertiesManager->update($portProperty, Utils\ArrayHash::from([ - 'value' => $port, - ])); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $port, + MetadataTypes\DataType::UINT, + Types\DevicePropertyIdentifier::PORT, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::PORT->value), + ); + + if ($authorization !== null && $authorization !== false) { + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getAppId(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::APP_ID, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), + ); + + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getEncryptionKey(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::ENCRYPTION_KEY, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), + ); } - if ($appIdProperty === null && $authorization !== null && $authorization !== false) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::APP_ID->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getAppId(), - 'device' => $device, - ])); - } elseif ($appIdProperty !== null && $authorization !== null && $authorization !== false) { - $this->devicesPropertiesManager->update($appIdProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getAppId(), - ])); - } elseif ($appIdProperty !== null && $authorization === false) { + if ($appIdProperty !== null && $authorization === false) { $this->devicesPropertiesManager->delete($appIdProperty); } - if ($encryptionKeyProperty === null && $authorization !== null && $authorization !== false) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getEncryptionKey(), - 'device' => $device, - ])); - } elseif ($encryptionKeyProperty !== null && $authorization !== null && $authorization !== false) { - $this->devicesPropertiesManager->update($encryptionKeyProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getEncryptionKey(), - ])); - } elseif ($encryptionKeyProperty !== null && $authorization === false) { + if ($encryptionKeyProperty !== null && $authorization === false) { $this->devicesPropertiesManager->delete($encryptionKeyProperty); } - if ($hardwareModelProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MODEL->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MODEL->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => trim(sprintf('%s %s', $specs->getModelName(), $specs->getModelNumber())), - 'device' => $device, - ])); - } else { - $this->devicesPropertiesManager->update($hardwareModelProperty, Utils\ArrayHash::from([ - 'value' => trim(sprintf('%s %s', $specs->getModelName(), $specs->getModelNumber())), - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + trim(sprintf('%s %s', $specs->getModelName(), $specs->getModelNumber())), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MODEL, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MODEL->value), + ); - if ($hardwareManufacturerProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MODEL->value, - 'name' => DevicesUtilities\Name::createName( - Types\DevicePropertyIdentifier::MANUFACTURER->value, - ), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $specs->getManufacturer(), - 'device' => $device, - ])); - } else { - $this->devicesPropertiesManager->update($hardwareManufacturerProperty, Utils\ArrayHash::from([ - 'value' => $specs->getManufacturer(), - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $specs->getManufacturer(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MANUFACTURER, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MANUFACTURER->value), + ); - if ($macAddressProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'identifier' => Types\DevicePropertyIdentifier::MAC_ADDRESS->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MAC_ADDRESS->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $macAddress, - 'device' => $device, - ])); - } elseif ($macAddress !== null) { - $this->devicesPropertiesManager->update($macAddressProperty, Utils\ArrayHash::from([ - 'value' => $macAddress, - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $macAddress, + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::MAC_ADDRESS, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MAC_ADDRESS->value), + ); if ($channel === null) { $channel = $this->channelsManager->create(Utils\ArrayHash::from([ @@ -1560,65 +1513,40 @@ private function editDevice(Style\SymfonyStyle $io, Entities\Connectors\Connecto } if ($hdmi !== null) { - if ($hdmiProperty === null) { - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::HDMI->value, - 'name' => DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::HDMI->value), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => array_map(static fn (string $name, int $index): array => [ - Helpers\Name::sanitizeEnumName($name), - $index, - $index, - ], array_values($hdmi), array_keys($hdmi)), - 'channel' => $channel, - ])); - } elseif ($macAddress !== null) { - $this->channelsPropertiesManager->update($hdmiProperty, Utils\ArrayHash::from([ - 'format' => array_map(static fn (string $name, int $index): array => [ - Helpers\Name::sanitizeEnumName($name), - $index, - $index, - ], array_values($hdmi), array_keys($hdmi)), - ])); - } + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::HDMI, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::HDMI->value), + array_map(static fn (string $name, int $index): array => [ + Helpers\Name::sanitizeEnumName($name), + $index, + $index, + ], array_values($hdmi), array_keys($hdmi)), + true, + ); } if ($apps !== null) { - if ($appsProperty === null) { - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => Types\ChannelPropertyIdentifier::APPLICATION->value, - 'name' => DevicesUtilities\Name::createName( - Types\ChannelPropertyIdentifier::APPLICATION->value, - ), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => $apps->getApps() !== [] ? array_map( - static fn (API\Messages\Response\Application $application): array => [ - Helpers\Name::sanitizeEnumName($application->getName()), - $application->getId(), - $application->getId(), - ], - $apps->getApps(), - ) : null, - 'channel' => $channel, - ])); - } elseif ($macAddress !== null) { - $this->channelsPropertiesManager->update($appsProperty, Utils\ArrayHash::from([ - 'format' => $apps->getApps() !== [] ? array_map( - static fn (API\Messages\Response\Application $application): array => [ - Helpers\Name::sanitizeEnumName($application->getName()), - $application->getId(), - $application->getId(), - ], - $apps->getApps(), - ) : null, - ])); - } + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::APPLICATION, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::APPLICATION->value), + $apps->getApps() !== [] ? array_map( + static fn (API\Messages\Response\Application $application): array => [ + Helpers\Name::sanitizeEnumName($application->getName()), + $application->getId(), + $application->getId(), + ], + $apps->getApps(), + ) : null, + true, + ); } // Commit all changes into database @@ -1763,8 +1691,10 @@ private function listDevices(Style\SymfonyStyle $io, Entities\Connectors\Connect /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime * @throws Console\Exception\ExceptionInterface * @throws DevicesExceptions\InvalidState + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException * @throws DoctrineCrudExceptions\EntityCreation * @throws DoctrineCrudExceptions\InvalidArgument @@ -1974,6 +1904,7 @@ private function askInstallAction(Style\SymfonyStyle $io): void /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime * @throws Console\Exception\ExceptionInterface * @throws DBAL\Exception * @throws DevicesExceptions\InvalidState @@ -2453,10 +2384,9 @@ function (string|int|null $answer) use ($connector, $devices): Entities\Devices\ * @param array $encryptedDevices * * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument * @throws MetadataExceptions\InvalidState @@ -2603,51 +2533,23 @@ private function processEncryptedDevices( return; } - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->byDeviceId($device->getId()); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::APP_ID); - - $appIdProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); - - if ($appIdProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'device' => $device, - 'identifier' => Types\DevicePropertyIdentifier::APP_ID->value, - 'name' => DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getAppId(), - 'format' => null, - ])); - } else { - $this->devicesPropertiesManager->update($appIdProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getAppId(), - ])); - } - - $findDevicePropertyQuery = new Queries\Entities\FindDeviceProperties(); - $findDevicePropertyQuery->byDeviceId($device->getId()); - $findDevicePropertyQuery->byIdentifier(Types\DevicePropertyIdentifier::ENCRYPTION_KEY); - - $encryptionKeyProperty = $this->devicesPropertiesRepository->findOneBy($findDevicePropertyQuery); + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getAppId(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::APP_ID, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), + ); - if ($encryptionKeyProperty === null) { - $this->devicesPropertiesManager->create(Utils\ArrayHash::from([ - 'entity' => DevicesEntities\Devices\Properties\Variable::class, - 'device' => $device, - 'identifier' => Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - 'name' => DevicesUtilities\Name::createName( - Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value, - ), - 'dataType' => MetadataTypes\DataType::STRING, - 'value' => $authorization->getEncryptionKey(), - 'format' => null, - ])); - } else { - $this->devicesPropertiesManager->update($encryptionKeyProperty, Utils\ArrayHash::from([ - 'value' => $authorization->getEncryptionKey(), - ])); - } + $this->deviceProperty->create( + DevicesEntities\Devices\Properties\Variable::class, + $device->getId(), + $authorization->getEncryptionKey(), + MetadataTypes\DataType::STRING, + Types\DevicePropertyIdentifier::ENCRYPTION_KEY, + DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTION_KEY->value), + ); $io->success( (string) $this->translator->translate( diff --git a/src/Constants.php b/src/Constants.php new file mode 100644 index 0000000..d66dc50 --- /dev/null +++ b/src/Constants.php @@ -0,0 +1,60 @@ + + * @package FastyBird:VieraConnector! + * @subpackage common + * @since 1.0.0 + * + * @date 10.08.23 + */ + +namespace FastyBird\Connector\Viera; + +/** + * Connector constants + * + * @package FastyBird:VieraConnector! + * @subpackage common + * + * @author Adam Kadlec + */ +class Constants +{ + + /** array */ + public const KEYS_PROPERTIES = [ + Types\ActionKey::TV->value => Types\ChannelPropertyIdentifier::KEY_TV, + Types\ActionKey::HOME->value => Types\ChannelPropertyIdentifier::KEY_HOME, + Types\ActionKey::CH_UP->value => Types\ChannelPropertyIdentifier::KEY_CHANNEL_UP, + Types\ActionKey::CH_DOWN->value => Types\ChannelPropertyIdentifier::KEY_CHANNEL_DOWN, + Types\ActionKey::VOLUME_UP->value => Types\ChannelPropertyIdentifier::KEY_VOLUME_UP, + Types\ActionKey::VOLUME_DOWN->value => Types\ChannelPropertyIdentifier::KEY_VOLUME_DOWN, + Types\ActionKey::UP->value => Types\ChannelPropertyIdentifier::KEY_ARROW_UP, + Types\ActionKey::DOWN->value => Types\ChannelPropertyIdentifier::KEY_ARROW_DOWN, + Types\ActionKey::LEFT->value => Types\ChannelPropertyIdentifier::KEY_ARROW_LEFT, + Types\ActionKey::RIGHT->value => Types\ChannelPropertyIdentifier::KEY_ARROW_RIGHT, + Types\ActionKey::NUM_0->value => Types\ChannelPropertyIdentifier::KEY_0, + Types\ActionKey::NUM_1->value => Types\ChannelPropertyIdentifier::KEY_1, + Types\ActionKey::NUM_2->value => Types\ChannelPropertyIdentifier::KEY_2, + Types\ActionKey::NUM_3->value => Types\ChannelPropertyIdentifier::KEY_3, + Types\ActionKey::NUM_4->value => Types\ChannelPropertyIdentifier::KEY_4, + Types\ActionKey::NUM_5->value => Types\ChannelPropertyIdentifier::KEY_5, + Types\ActionKey::NUM_6->value => Types\ChannelPropertyIdentifier::KEY_6, + Types\ActionKey::NUM_7->value => Types\ChannelPropertyIdentifier::KEY_7, + Types\ActionKey::NUM_8->value => Types\ChannelPropertyIdentifier::KEY_8, + Types\ActionKey::NUM_9->value => Types\ChannelPropertyIdentifier::KEY_9, + Types\ActionKey::RED->value => Types\ChannelPropertyIdentifier::KEY_RED, + Types\ActionKey::GREEN->value => Types\ChannelPropertyIdentifier::KEY_GREEN, + Types\ActionKey::YELLOW->value => Types\ChannelPropertyIdentifier::KEY_YELLOW, + Types\ActionKey::BLUE->value => Types\ChannelPropertyIdentifier::KEY_BLUE, + Types\ActionKey::ENTER->value => Types\ChannelPropertyIdentifier::KEY_OK, + Types\ActionKey::RETURN->value => Types\ChannelPropertyIdentifier::KEY_BACK, + Types\ActionKey::MENU->value => Types\ChannelPropertyIdentifier::KEY_MENU, + ]; + +} diff --git a/src/DI/VieraExtension.php b/src/DI/VieraExtension.php index b2dbb26..116330c 100644 --- a/src/DI/VieraExtension.php +++ b/src/DI/VieraExtension.php @@ -217,6 +217,9 @@ public function loadConfiguration(): void $builder->addDefinition($this->prefix('subscribers.controls'), new DI\Definitions\ServiceDefinition()) ->setType(Subscribers\Controls::class); + $builder->addDefinition($this->prefix('subscribers.device'), new DI\Definitions\ServiceDefinition()) + ->setType(Subscribers\Device::class); + /** * JSON-API SCHEMAS */ @@ -253,6 +256,18 @@ public function loadConfiguration(): void $builder->addDefinition($this->prefix('helpers.device'), new DI\Definitions\ServiceDefinition()) ->setType(Helpers\Device::class); + $builder->addDefinition($this->prefix('helpers.deviceProperty'), new DI\Definitions\ServiceDefinition()) + ->setType(Helpers\DeviceProperty::class) + ->setArguments([ + 'logger' => $logger, + ]); + + $builder->addDefinition($this->prefix('helpers.channelProperty'), new DI\Definitions\ServiceDefinition()) + ->setType(Helpers\ChannelProperty::class) + ->setArguments([ + 'logger' => $logger, + ]); + /** * COMMANDS */ diff --git a/src/Queue/Consumers/ChannelProperty.php b/src/Helpers/ChannelProperty.php similarity index 85% rename from src/Queue/Consumers/ChannelProperty.php rename to src/Helpers/ChannelProperty.php index 500fa5c..70913bf 100644 --- a/src/Queue/Consumers/ChannelProperty.php +++ b/src/Helpers/ChannelProperty.php @@ -7,13 +7,13 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:VieraConnector! - * @subpackage Queue + * @subpackage Helpers * @since 1.0.0 * - * @date 28.06.23 + * @date 27.08.24 */ -namespace FastyBird\Connector\Viera\Queue\Consumers; +namespace FastyBird\Connector\Viera\Helpers; use Doctrine\DBAL; use FastyBird\Connector\Viera; @@ -29,23 +29,19 @@ use Ramsey\Uuid; use function array_merge; -/** - * Channel property consumer trait - * - * @package FastyBird:VieraConnector! - * @subpackage Queue - * - * @author Adam Kadlec - * - * @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 ApplicationHelpers\Database $databaseHelper - * @property-read Viera\Logger $logger - */ -trait ChannelProperty +final readonly class ChannelProperty { + public function __construct( + private DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository, + private DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository, + private DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager, + private ApplicationHelpers\Database $databaseHelper, + private Viera\Logger $logger, + ) + { + } + /** * @param class-string $type * @param string|array|array|Utils\ArrayHash|null>|array|Utils\ArrayHash|null>>|null $format @@ -55,7 +51,7 @@ trait ChannelProperty * @throws DBAL\Exception * @throws Exceptions\InvalidArgument */ - private function setChannelProperty( + public function create( string $type, Uuid\UuidInterface $channelId, string|bool|int|null $value, @@ -96,7 +92,7 @@ function () use ($property): void { 'Stored channel property was not of valid type', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'channel-property-helper', 'channel' => [ 'id' => $channelId->toString(), ], @@ -118,7 +114,7 @@ function () use ($property): void { 'Channel was not found, property could not be configured', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'channel-property-helper', 'channel' => [ 'id' => $channelId->toString(), ], @@ -158,7 +154,7 @@ function () use ($property): void { 'Channel property was created', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'channel-property-helper', 'channel' => [ 'id' => $channelId->toString(), ], @@ -194,7 +190,7 @@ function () use ($property): void { 'Channel property was updated', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'channel-property-helper', 'channel' => [ 'id' => $channelId->toString(), ], diff --git a/src/Queue/Consumers/DeviceProperty.php b/src/Helpers/DeviceProperty.php similarity index 85% rename from src/Queue/Consumers/DeviceProperty.php rename to src/Helpers/DeviceProperty.php index 1388bc0..628a761 100644 --- a/src/Queue/Consumers/DeviceProperty.php +++ b/src/Helpers/DeviceProperty.php @@ -7,13 +7,13 @@ * @copyright https://www.fastybird.com * @author Adam Kadlec * @package FastyBird:VieraConnector! - * @subpackage Queue + * @subpackage Helpers * @since 1.0.0 * - * @date 28.06.23 + * @date 27.08.24 */ -namespace FastyBird\Connector\Viera\Queue\Consumers; +namespace FastyBird\Connector\Viera\Helpers; use Doctrine\DBAL; use FastyBird\Connector\Viera; @@ -30,33 +30,29 @@ use Ramsey\Uuid; use function array_merge; -/** - * Device property consumer trait - * - * @package FastyBird:VieraConnector! - * @subpackage Queue - * - * @author Adam Kadlec - * - * @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 ApplicationHelpers\Database $databaseHelper - * @property-read Viera\Logger $logger - */ -trait DeviceProperty +final readonly class DeviceProperty { + public function __construct( + private DevicesModels\Entities\Devices\DevicesRepository $devicesRepository, + private DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository, + private DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager, + private ApplicationHelpers\Database $databaseHelper, + private Viera\Logger $logger, + ) + { + } + /** * @param class-string $type * @param string|array|array|Utils\ArrayHash|null>|array|Utils\ArrayHash|null>>|null $format * * @throws ApplicationExceptions\InvalidState * @throws ApplicationExceptions\Runtime - * @throws Exceptions\InvalidArgument * @throws DBAL\Exception + * @throws Exceptions\InvalidArgument */ - private function setDeviceProperty( + public function create( string $type, Uuid\UuidInterface $deviceId, string|bool|int|null $value, @@ -97,7 +93,7 @@ function () use ($property): void { 'Stored device property was not of valid type', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'device-property-helper', 'device' => [ 'id' => $deviceId->toString(), ], @@ -122,7 +118,7 @@ function () use ($property): void { 'Device was not found, property could not be configured', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'device-property-helper', 'device' => [ 'id' => $deviceId->toString(), ], @@ -162,7 +158,7 @@ function () use ($property): void { 'Device property was created', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'device-property-helper', 'device' => [ 'id' => $deviceId->toString(), ], @@ -198,7 +194,7 @@ function () use ($property): void { 'Device property was updated', [ 'source' => MetadataTypes\Sources\Connector::VIERA->value, - 'type' => 'message-consumer', + 'type' => 'device-property-helper', 'device' => [ 'id' => $deviceId->toString(), ], diff --git a/src/Queue/Consumers/StoreDevice.php b/src/Queue/Consumers/StoreDevice.php index f91db43..375d01b 100644 --- a/src/Queue/Consumers/StoreDevice.php +++ b/src/Queue/Consumers/StoreDevice.php @@ -46,22 +46,18 @@ final class StoreDevice implements Queue\Consumer { - use DeviceProperty; - use ChannelProperty; use Nette\SmartObject; public function __construct( - protected readonly Viera\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 ApplicationHelpers\Database $databaseHelper, + private readonly Viera\Logger $logger, + private readonly Helpers\DeviceProperty $deviceProperty, + private readonly Helpers\ChannelProperty $channelProperty, private readonly DevicesModels\Entities\Connectors\ConnectorsRepository $connectorsRepository, + private readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository, private readonly DevicesModels\Entities\Devices\DevicesManager $devicesManager, + private readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository, private readonly DevicesModels\Entities\Channels\ChannelsManager $channelsManager, + private readonly ApplicationHelpers\Database $databaseHelper, ) { } @@ -126,7 +122,7 @@ function () use ($message, $connector): Entities\Devices\Device { ); } - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getIpAddress(), @@ -134,7 +130,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::IP_ADDRESS, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::IP_ADDRESS->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getPort(), @@ -142,7 +138,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::PORT, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::PORT->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getModel(), @@ -150,7 +146,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::MODEL, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MODEL->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getManufacturer(), @@ -158,7 +154,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::MANUFACTURER, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MANUFACTURER->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getSerialNumber(), @@ -166,7 +162,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::SERIAL_NUMBER, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::SERIAL_NUMBER->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getMacAddress(), @@ -174,7 +170,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::MAC_ADDRESS, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::MAC_ADDRESS->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->isEncrypted(), @@ -182,7 +178,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::ENCRYPTED, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::ENCRYPTED->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getAppId(), @@ -190,7 +186,7 @@ function () use ($message, $connector): Entities\Devices\Device { Types\DevicePropertyIdentifier::APP_ID, DevicesUtilities\Name::createName(Types\DevicePropertyIdentifier::APP_ID->value), ); - $this->setDeviceProperty( + $this->deviceProperty->create( DevicesEntities\Devices\Properties\Variable::class, $device->getId(), $message->getEncryptionKey(), @@ -232,7 +228,7 @@ function () use ($message, $connector): Entities\Devices\Device { ); } - $this->setChannelProperty( + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, @@ -244,7 +240,7 @@ function () use ($message, $connector): Entities\Devices\Device { true, ); - $this->setChannelProperty( + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, @@ -259,7 +255,7 @@ function () use ($message, $connector): Entities\Devices\Device { true, ); - $this->setChannelProperty( + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, @@ -271,7 +267,18 @@ function () use ($message, $connector): Entities\Devices\Device { true, ); - $this->setChannelProperty( + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::STRING, + Types\ChannelPropertyIdentifier::REMOTE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::REMOTE->value), + null, + true, + ); + + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, @@ -289,7 +296,7 @@ function () use ($message, $connector): Entities\Devices\Device { true, ); - $this->setChannelProperty( + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, @@ -307,7 +314,7 @@ function () use ($message, $connector): Entities\Devices\Device { true, ); - $this->setChannelProperty( + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, $channel->getId(), null, diff --git a/src/Subscribers/Device.php b/src/Subscribers/Device.php new file mode 100644 index 0000000..049a2b3 --- /dev/null +++ b/src/Subscribers/Device.php @@ -0,0 +1,217 @@ + + * @package FastyBird:DevicesModuleUiModuleBridge! + * @subpackage Subscribers + * @since 1.0.0 + * + * @date 27.08.24 + */ + +namespace FastyBird\Connector\Viera\Subscribers; + +use Doctrine\DBAL; +use FastyBird\Connector\Viera; +use FastyBird\Connector\Viera\Entities; +use FastyBird\Connector\Viera\Exceptions; +use FastyBird\Connector\Viera\Helpers; +use FastyBird\Connector\Viera\Queries; +use FastyBird\Connector\Viera\Types; +use FastyBird\Library\Application\Exceptions as ApplicationExceptions; +use FastyBird\Library\Metadata\Types as MetadataTypes; +use FastyBird\Module\Devices\Entities as DevicesEntities; +use FastyBird\Module\Devices\Events as DevicesEvents; +use FastyBird\Module\Devices\Models as DevicesModels; +use FastyBird\Module\Devices\Utilities as DevicesUtilities; +use Nette; +use Symfony\Component\EventDispatcher; +use TypeError; +use ValueError; + +/** + * Device events + * + * @package FastyBird:DevicesModuleUiModuleBridge! + * @subpackage Subscribers + * + * @author Adam Kadlec + */ +final class Device implements EventDispatcher\EventSubscriberInterface +{ + + use Nette\SmartObject; + + public function __construct( + private readonly Helpers\ChannelProperty $channelProperty, + private readonly DevicesModels\Entities\Channels\ChannelsRepository $channelsRepository, + private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository, + ) + { + } + + public static function getSubscribedEvents(): array + { + return [ + DevicesEvents\EntityUpdated::class => 'updated', + ]; + } + + /** + * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception + * @throws Exceptions\InvalidArgument + * @throws TypeError + * @throws ValueError + */ + public function updated(DevicesEvents\EntityUpdated $event): void + { + $entity = $event->getEntity(); + + if (!$entity instanceof Entities\Devices\Device) { + return; + } + + $this->checkChannelProperties($entity); + } + + /** + * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception + * @throws Exceptions\InvalidArgument + * @throws TypeError + * @throws ValueError + */ + private function checkChannelProperties(Entities\Devices\Device $device): void + { + $findChannelQuery = new Queries\Entities\FindChannels(); + $findChannelQuery->forDevice($device); + $findChannelQuery->byIdentifier(Types\ChannelType::TELEVISION); + + $channel = $this->channelsRepository->findOneBy($findChannelQuery, Entities\Channels\Channel::class); + + if ($channel === null) { + return; + } + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BOOLEAN, + Types\ChannelPropertyIdentifier::STATE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::STATE->value), + null, + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::UCHAR, + Types\ChannelPropertyIdentifier::VOLUME, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::VOLUME->value), + [ + 0, + 100, + ], + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BOOLEAN, + Types\ChannelPropertyIdentifier::MUTE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::MUTE->value), + null, + true, + true, + ); + + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::STRING, + Types\ChannelPropertyIdentifier::REMOTE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::REMOTE->value), + null, + true, + ); + + $findChannelProperty = new Queries\Entities\FindChannelProperties(); + $findChannelProperty->forChannel($channel); + $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::HDMI); + + $hdmiProperty = $this->channelsPropertiesRepository->findOneBy( + $findChannelProperty, + DevicesEntities\Channels\Properties\Dynamic::class, + ); + + if ($hdmiProperty === null) { + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::HDMI, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::HDMI->value), + null, + true, + ); + } + + $findChannelProperty = new Queries\Entities\FindChannelProperties(); + $findChannelProperty->forChannel($channel); + $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::APPLICATION); + + $appsProperty = $this->channelsPropertiesRepository->findOneBy( + $findChannelProperty, + DevicesEntities\Channels\Properties\Dynamic::class, + ); + + if ($appsProperty === null) { + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::APPLICATION, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::APPLICATION->value), + null, + true, + ); + } + + foreach (Viera\Constants::KEYS_PROPERTIES as $actionKey => $identifier) { + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BUTTON, + $identifier, + DevicesUtilities\Name::createName($identifier->value), + [ + [ + MetadataTypes\Payloads\Button::CLICKED->value, + Types\ActionKey::from($actionKey)->value, + Types\ActionKey::from($actionKey)->value, + ], + ], + true, + ); + } + } + +} diff --git a/src/Subscribers/Properties.php b/src/Subscribers/Properties.php index 9799702..9b34adc 100644 --- a/src/Subscribers/Properties.php +++ b/src/Subscribers/Properties.php @@ -19,8 +19,10 @@ use Doctrine\DBAL; use Doctrine\ORM; use Doctrine\Persistence; +use FastyBird\Connector\Viera; use FastyBird\Connector\Viera\Entities; use FastyBird\Connector\Viera\Exceptions; +use FastyBird\Connector\Viera\Helpers; use FastyBird\Connector\Viera\Queries; use FastyBird\Connector\Viera\Types; use FastyBird\Library\Application\Exceptions as ApplicationExceptions; @@ -52,10 +54,10 @@ final class Properties implements Common\EventSubscriber use Nette\SmartObject; public function __construct( + private readonly Helpers\ChannelProperty $channelProperty, private readonly DevicesModels\Entities\Devices\Properties\PropertiesRepository $devicesPropertiesRepository, private readonly DevicesModels\Entities\Devices\Properties\PropertiesManager $devicesPropertiesManager, private readonly DevicesModels\Entities\Channels\Properties\PropertiesRepository $channelsPropertiesRepository, - private readonly DevicesModels\Entities\Channels\Properties\PropertiesManager $channelsPropertiesManager, ) { } @@ -72,6 +74,8 @@ public function getSubscribedEvents(): array * @param Persistence\Event\LifecycleEventArgs $eventArgs * * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException * @throws DoctrineCrudExceptions\EntityCreation * @throws DoctrineCrudExceptions\InvalidArgument @@ -103,10 +107,9 @@ public function postPersist(Persistence\Event\LifecycleEventArgs $eventArgs): vo * @param Persistence\Event\LifecycleEventArgs $eventArgs * * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument @@ -119,6 +122,13 @@ public function postUpdate(Persistence\Event\LifecycleEventArgs $eventArgs): voi // onFlush was executed before, everything already initialized $entity = $eventArgs->getObject(); + if ( + $entity instanceof DevicesEntities\Channels\Channel + && $entity->getDevice() instanceof Entities\Devices\Device + ) { + $this->configureDeviceKeys($entity); + } + if ( $entity instanceof DevicesEntities\Channels\Properties\Dynamic && $entity->getChannel()->getDevice() instanceof Entities\Devices\Device @@ -188,118 +198,40 @@ private function configureDeviceState(Entities\Devices\Device $device): void /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws TypeError * @throws ValueError */ private function configureDeviceKeys(DevicesEntities\Channels\Channel $channel): void { - $keysProperties = [ - Types\ActionKey::TV->value => Types\ChannelPropertyIdentifier::KEY_TV, - Types\ActionKey::HOME->value => Types\ChannelPropertyIdentifier::KEY_HOME, - Types\ActionKey::CH_UP->value => Types\ChannelPropertyIdentifier::KEY_CHANNEL_UP, - Types\ActionKey::CH_DOWN->value => Types\ChannelPropertyIdentifier::KEY_CHANNEL_DOWN, - Types\ActionKey::VOLUME_UP->value => Types\ChannelPropertyIdentifier::KEY_VOLUME_UP, - Types\ActionKey::VOLUME_DOWN->value => Types\ChannelPropertyIdentifier::KEY_VOLUME_DOWN, - Types\ActionKey::UP->value => Types\ChannelPropertyIdentifier::KEY_ARROW_UP, - Types\ActionKey::DOWN->value => Types\ChannelPropertyIdentifier::KEY_ARROW_DOWN, - Types\ActionKey::LEFT->value => Types\ChannelPropertyIdentifier::KEY_ARROW_LEFT, - Types\ActionKey::RIGHT->value => Types\ChannelPropertyIdentifier::KEY_ARROW_RIGHT, - Types\ActionKey::NUM_0->value => Types\ChannelPropertyIdentifier::KEY_0, - Types\ActionKey::NUM_1->value => Types\ChannelPropertyIdentifier::KEY_1, - Types\ActionKey::NUM_2->value => Types\ChannelPropertyIdentifier::KEY_2, - Types\ActionKey::NUM_3->value => Types\ChannelPropertyIdentifier::KEY_3, - Types\ActionKey::NUM_4->value => Types\ChannelPropertyIdentifier::KEY_4, - Types\ActionKey::NUM_5->value => Types\ChannelPropertyIdentifier::KEY_5, - Types\ActionKey::NUM_6->value => Types\ChannelPropertyIdentifier::KEY_6, - Types\ActionKey::NUM_7->value => Types\ChannelPropertyIdentifier::KEY_7, - Types\ActionKey::NUM_8->value => Types\ChannelPropertyIdentifier::KEY_8, - Types\ActionKey::NUM_9->value => Types\ChannelPropertyIdentifier::KEY_9, - Types\ActionKey::RED->value => Types\ChannelPropertyIdentifier::KEY_RED, - Types\ActionKey::GREEN->value => Types\ChannelPropertyIdentifier::KEY_GREEN, - Types\ActionKey::YELLOW->value => Types\ChannelPropertyIdentifier::KEY_YELLOW, - Types\ActionKey::BLUE->value => Types\ChannelPropertyIdentifier::KEY_BLUE, - Types\ActionKey::ENTER->value => Types\ChannelPropertyIdentifier::KEY_OK, - Types\ActionKey::RETURN->value => Types\ChannelPropertyIdentifier::KEY_BACK, - ]; - - foreach ($keysProperties as $actionKey => $identifier) { - $this->processChannelProperty( - $channel, + foreach (Viera\Constants::KEYS_PROPERTIES as $actionKey => $identifier) { + $this->channelProperty->create( + DevicesEntities\Channels\Properties\Dynamic::class, + $channel->getId(), + null, + MetadataTypes\DataType::BUTTON, $identifier, - Types\ActionKey::from($actionKey), - ); - } - } - - /** - * @throws ApplicationExceptions\InvalidState - * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState - * @throws Exceptions\InvalidArgument - */ - private function processChannelProperty( - DevicesEntities\Channels\Channel $channel, - Types\ChannelPropertyIdentifier $identifier, - Types\ActionKey $key, - ): void - { - $findChannelPropertyQuery = new Queries\Entities\FindChannelProperties(); - $findChannelPropertyQuery->forChannel($channel); - $findChannelPropertyQuery->byIdentifier($identifier); - - $property = $this->channelsPropertiesRepository->findOneBy($findChannelPropertyQuery); - - if ($property !== null && !$property instanceof DevicesEntities\Channels\Properties\Dynamic) { - $this->channelsPropertiesManager->delete($property); - - $property = null; - } - - if ($property === null) { - $this->channelsPropertiesManager->create(Utils\ArrayHash::from([ - 'channel' => $channel, - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'identifier' => $identifier->value, - 'name' => DevicesUtilities\Name::createName($identifier->value), - 'dataType' => MetadataTypes\DataType::BUTTON, - 'unit' => null, - 'format' => [ + DevicesUtilities\Name::createName($identifier->value), + [ [ MetadataTypes\Payloads\Button::CLICKED->value, - $key->value, - $key->value, + Types\ActionKey::from($actionKey)->value, + Types\ActionKey::from($actionKey)->value, ], ], - 'settable' => true, - 'queryable' => false, - ])); - } else { - $this->channelsPropertiesManager->update($property, Utils\ArrayHash::from([ - 'name' => DevicesUtilities\Name::createName($identifier->value), - 'dataType' => MetadataTypes\DataType::BUTTON, - 'unit' => null, - 'format' => [ - $key->value, - ], - 'settable' => true, - 'queryable' => false, - ])); + true, + ); } } /** * @throws ApplicationExceptions\InvalidState + * @throws ApplicationExceptions\Runtime + * @throws DBAL\Exception * @throws DBAL\Exception\UniqueConstraintViolationException - * @throws DoctrineCrudExceptions\EntityCreation - * @throws DoctrineCrudExceptions\InvalidArgument - * @throws DoctrineCrudExceptions\InvalidState * @throws Exceptions\InvalidArgument * @throws MetadataExceptions\InvalidArgument * @throws MetadataExceptions\InvalidState @@ -310,93 +242,72 @@ private function configureDeviceInputSource(DevicesEntities\Channels\Properties\ { $channel = $property->getChannel(); - $findChannelProperty = new Queries\Entities\FindChannelProperties(); - $findChannelProperty->forChannel($channel); - $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::HDMI); + if ($property->getIdentifier() === Types\ChannelPropertyIdentifier::HDMI->value) { + $hdmiFormat = $property->getFormat(); - $hdmiProperty = $this->channelsPropertiesRepository->findOneBy( - $findChannelProperty, - DevicesEntities\Channels\Properties\Dynamic::class, - ); + $hdmiFormat = $hdmiFormat instanceof MetadataFormats\CombinedEnum + ? $hdmiFormat->toArray() + : []; + } else { + $findChannelProperty = new Queries\Entities\FindChannelProperties(); + $findChannelProperty->forChannel($channel); + $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::HDMI); + + $hdmiProperty = $this->channelsPropertiesRepository->findOneBy( + $findChannelProperty, + DevicesEntities\Channels\Properties\Dynamic::class, + ); - $hdmiFormat = $hdmiProperty?->getFormat(); + $hdmiFormat = $hdmiProperty?->getFormat(); - $hdmiFormat = $hdmiFormat instanceof MetadataFormats\CombinedEnum ? $hdmiFormat->toArray() : []; + $hdmiFormat = $hdmiFormat instanceof MetadataFormats\CombinedEnum + ? $hdmiFormat->toArray() + : []; + } - $findChannelProperty = new Queries\Entities\FindChannelProperties(); - $findChannelProperty->forChannel($channel); - $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::APPLICATION); + if ($property->getIdentifier() === Types\ChannelPropertyIdentifier::APPLICATION->value) { + $applicationFormat = $property->getFormat(); - $applicationProperty = $this->channelsPropertiesRepository->findOneBy( - $findChannelProperty, - DevicesEntities\Channels\Properties\Dynamic::class, - ); + $applicationFormat = $applicationFormat instanceof MetadataFormats\CombinedEnum + ? $applicationFormat->toArray() + : []; + } else { + $findChannelProperty = new Queries\Entities\FindChannelProperties(); + $findChannelProperty->forChannel($channel); + $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::APPLICATION); - $applicationFormat = $applicationProperty?->getFormat(); + $applicationProperty = $this->channelsPropertiesRepository->findOneBy( + $findChannelProperty, + DevicesEntities\Channels\Properties\Dynamic::class, + ); - $applicationFormat = $applicationFormat instanceof MetadataFormats\CombinedEnum - ? $applicationFormat->toArray() - : []; + $applicationFormat = $applicationProperty?->getFormat(); - $findChannelProperty = new Queries\Entities\FindChannelProperties(); - $findChannelProperty->forChannel($channel); - $findChannelProperty->byIdentifier(Types\ChannelPropertyIdentifier::INPUT_SOURCE); + $applicationFormat = $applicationFormat instanceof MetadataFormats\CombinedEnum + ? $applicationFormat->toArray() + : []; + } - $inputSourceProperty = $this->channelsPropertiesRepository->findOneBy( - $findChannelProperty, + $this->channelProperty->create( DevicesEntities\Channels\Properties\Dynamic::class, - ); - - if ($inputSourceProperty === null) { - $this->channelsPropertiesManager->create( - Utils\ArrayHash::from( + $channel->getId(), + null, + MetadataTypes\DataType::ENUM, + Types\ChannelPropertyIdentifier::INPUT_SOURCE, + DevicesUtilities\Name::createName(Types\ChannelPropertyIdentifier::INPUT_SOURCE->value), + array_merge( + [ [ - 'entity' => DevicesEntities\Channels\Properties\Dynamic::class, - 'channel' => $channel, - 'identifier' => Types\ChannelPropertyIdentifier::INPUT_SOURCE->value, - 'name' => DevicesUtilities\Name::createName( - Types\ChannelPropertyIdentifier::INPUT_SOURCE->value, - ), - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => array_merge( - [ - [ - 'TV', - 500, - 500, - ], - ], - $hdmiFormat, - $applicationFormat, - ), + 'TV', + 500, + 500, ], - ), - ); - } else { - $this->channelsPropertiesManager->update( - $inputSourceProperty, - Utils\ArrayHash::from( - [ - 'dataType' => MetadataTypes\DataType::ENUM, - 'settable' => true, - 'queryable' => false, - 'format' => array_merge( - [ - [ - 'TV', - 500, - 500, - ], - ], - $hdmiFormat, - $applicationFormat, - ), - ], - ), - ); - } + ], + $hdmiFormat, + $applicationFormat, + ), + true, + ); } } diff --git a/src/Types/ChannelPropertyIdentifier.php b/src/Types/ChannelPropertyIdentifier.php index d51f454..9248641 100644 --- a/src/Types/ChannelPropertyIdentifier.php +++ b/src/Types/ChannelPropertyIdentifier.php @@ -32,6 +32,8 @@ enum ChannelPropertyIdentifier: string case MUTE = 'mute'; + case REMOTE = 'remote'; + case INPUT_SOURCE = 'input_source'; case APPLICATION = 'application'; @@ -90,4 +92,6 @@ enum ChannelPropertyIdentifier: string case KEY_BACK = 'key_back'; + case KEY_MENU = 'key_menu'; + } diff --git a/tests/cases/unit/DI/VieraExtensionTest.php b/tests/cases/unit/DI/VieraExtensionTest.php index 37215ba..6ace4da 100644 --- a/tests/cases/unit/DI/VieraExtensionTest.php +++ b/tests/cases/unit/DI/VieraExtensionTest.php @@ -52,6 +52,7 @@ public function testServicesRegistration(): void self::assertNotNull($container->getByType(Subscribers\Properties::class, false)); self::assertNotNull($container->getByType(Subscribers\Controls::class, false)); + self::assertNotNull($container->getByType(Subscribers\Device::class, false)); self::assertNotNull($container->getByType(Schemas\Connectors\Connector::class, false)); self::assertNotNull($container->getByType(Schemas\Devices\Device::class, false)); @@ -60,6 +61,9 @@ public function testServicesRegistration(): void self::assertNotNull($container->getByType(Hydrators\Devices\Device::class, false)); self::assertNotNull($container->getByType(Helpers\MessageBuilder::class, false)); + self::assertNotNull($container->getByType(Helpers\Device::class, false)); + self::assertNotNull($container->getByType(Helpers\DeviceProperty::class, false)); + self::assertNotNull($container->getByType(Helpers\ChannelProperty::class, false)); self::assertNotNull($container->getByType(Commands\Execute::class, false)); self::assertNotNull($container->getByType(Commands\Discover::class, false));