Skip to content

Commit

Permalink
[viera-connector] Fixed viera device installation (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 27, 2024
1 parent e1f3dca commit d4b3fa1
Show file tree
Hide file tree
Showing 13 changed files with 750 additions and 702 deletions.
38 changes: 6 additions & 32 deletions src/API/TelevisionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1653,10 +1655,6 @@ private function parseGetApps(
$this->session !== null
&& preg_match('/<X_EncResult>(?<encrypted>.*?)<\/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'],
Expand Down Expand Up @@ -1748,10 +1746,6 @@ private function parseGetVectorInfo(
$this->session !== null
&& preg_match('/<X_EncResult>(?<encrypted>.*?)<\/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'],
Expand Down Expand Up @@ -1821,10 +1815,6 @@ private function parseGetVolume(
$this->session !== null
&& preg_match('/<X_EncResult>(?<encrypted>.*?)<\/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'],
Expand All @@ -1842,20 +1832,14 @@ private function parseGetVolume(
);
}

if (
preg_match('/<CurrentVolume>(?<volume>.*?)<\/CurrentVolume>/', $payload, $matches) !== 1
|| !array_key_exists('volume', $matches)
) {
if (preg_match('/<CurrentVolume>(?<volume>.*?)<\/CurrentVolume>/', $payload, $matches) !== 1) {
throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response);
}

return intval($matches['volume']);
}

if (
preg_match('/<CurrentVolume>(?<volume>.*?)<\/CurrentVolume>/', $body, $matches) !== 1
|| !array_key_exists('volume', $matches)
) {
if (preg_match('/<CurrentVolume>(?<volume>.*?)<\/CurrentVolume>/', $body, $matches) !== 1) {
throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response);
}

Expand All @@ -1876,10 +1860,6 @@ private function parseGetMute(
$this->session !== null
&& preg_match('/<X_EncResult>(?<encrypted>.*?)<\/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'],
Expand All @@ -1897,20 +1877,14 @@ private function parseGetMute(
);
}

if (
preg_match('/<CurrentMute>(?<mute>.*?)<\/CurrentMute>/', $payload, $matches) !== 1
|| !array_key_exists('mute', $matches)
) {
if (preg_match('/<CurrentMute>(?<mute>.*?)<\/CurrentMute>/', $payload, $matches) !== 1) {
throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response);
}

return boolval($matches['mute']);
}

if (
preg_match('/<CurrentMute>(?<mute>.*?)<\/CurrentMute>/', $body, $matches) !== 1
|| !array_key_exists('mute', $matches)
) {
if (preg_match('/<CurrentMute>(?<mute>.*?)<\/CurrentMute>/', $body, $matches) !== 1) {
throw new Exceptions\TelevisionApiCall('Received response is not valid', $request, $response);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Clients/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
80 changes: 23 additions & 57 deletions src/Commands/Discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -432,10 +427,9 @@ private function showResults(
* @param array<Documents\Devices\Device> $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
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit d4b3fa1

Please sign in to comment.