Skip to content

Commit

Permalink
[viera-connector] Refactoring to new specs (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 13, 2023
1 parent a5a29b1 commit 9c9696d
Show file tree
Hide file tree
Showing 27 changed files with 139 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/API/LanApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ final class LanApi
public function __construct(
private readonly string $identifier,
private readonly HttpClientFactory $httpClientFactory,
private readonly MetadataSchemas\Validator $schemaValidator,
private readonly Helpers\Entity $entityHelper,
private readonly NsPanel\Logger $logger,
private readonly MetadataSchemas\Validator $schemaValidator,
)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
)
{
try {
parent::__construct($method, $uri, $headers, $body);
parent::__construct($method, $uri, $headers ?? [], $body);
} catch (InvalidArgumentException $ex) {
throw new Exceptions\InvalidArgument('Request could not be created', $ex->getCode(), $ex);
}
Expand Down
22 changes: 11 additions & 11 deletions src/Clients/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ final class Device implements Client

use Nette\SmartObject;

private API\LanApi $lanApiApi;
private API\LanApi $lanApi;

public function __construct(
private readonly Entities\NsPanelConnector $connector,
API\LanApiFactory $lanApiFactory,
private readonly Queue\Queue $queue,
private readonly Helpers\Loader $loader,
private readonly Helpers\Entity $entityHelper,
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
API\LanApiFactory $lanApiApiFactory,
)
{
$this->lanApiApi = $lanApiApiFactory->create($this->connector->getIdentifier());
$this->lanApi = $lanApiFactory->create($this->connector->getIdentifier());
}

/**
Expand Down Expand Up @@ -233,7 +233,7 @@ public function connect(): void

try {
if ($syncDevices !== []) {
$this->lanApiApi->synchroniseDevices(
$this->lanApi->synchroniseDevices(
$syncDevices,
$ipAddress,
$accessToken,
Expand Down Expand Up @@ -322,7 +322,7 @@ public function connect(): void
'body' => $ex->getRequest()?->getBody()->getContents(),
],
'response' => [
'body' => $ex->getRequest()?->getBody()->getContents(),
'body' => $ex->getResponse()?->getBody()->getContents(),
],
];

Expand Down Expand Up @@ -375,7 +375,7 @@ public function connect(): void
}

$promise->then(function () use ($gateway, $ipAddress, $accessToken): void {
$this->lanApiApi->getSubDevices($ipAddress, $accessToken)
$this->lanApi->getSubDevices($ipAddress, $accessToken)
->then(
function (Entities\API\Response\GetSubDevices $response) use ($gateway, $ipAddress, $accessToken): void {
foreach ($response->getData()->getDevicesList() as $subDevice) {
Expand All @@ -396,7 +396,7 @@ function (Entities\API\Response\GetSubDevices $response) use ($gateway, $ipAddre
continue;
}

$this->lanApiApi->removeDevice(
$this->lanApi->removeDevice(
$subDevice->getSerialNumber(),
$ipAddress,
$accessToken,
Expand Down Expand Up @@ -432,7 +432,7 @@ function (Entities\API\Response\GetSubDevices $response) use ($gateway, $ipAddre
'body' => $ex->getRequest()?->getBody()->getContents(),
],
'response' => [
'body' => $ex->getRequest()?->getBody()->getContents(),
'body' => $ex->getResponse()?->getBody()->getContents(),
],
];

Expand Down Expand Up @@ -497,7 +497,7 @@ function (Entities\API\Response\GetSubDevices $response) use ($gateway, $ipAddre
'body' => $ex->getRequest()?->getBody()->getContents(),
],
'response' => [
'body' => $ex->getRequest()?->getBody()->getContents(),
'body' => $ex->getResponse()?->getBody()->getContents(),
],
];

Expand Down Expand Up @@ -624,7 +624,7 @@ public function disconnect(): void
}

try {
$this->lanApiApi->reportDeviceOnline(
$this->lanApi->reportDeviceOnline(
$serialNumber,
false,
$ipAddress,
Expand Down Expand Up @@ -661,7 +661,7 @@ public function disconnect(): void
'body' => $ex->getRequest()?->getBody()->getContents(),
],
'response' => [
'body' => $ex->getRequest()?->getBody()->getContents(),
'body' => $ex->getResponse()?->getBody()->getContents(),
],
];
}
Expand Down
15 changes: 4 additions & 11 deletions src/Clients/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use Nette;
use React\EventLoop;
use React\Promise;
use Throwable;
use function array_key_exists;
Expand All @@ -51,16 +50,13 @@ final class Discovery implements Evenement\EventEmitterInterface
use Nette\SmartObject;
use Evenement\EventEmitterTrait;

private EventLoop\TimerInterface|null $handlerTimer = null;

public function __construct(
private readonly Entities\NsPanelConnector $connector,
private readonly API\LanApiFactory $lanApiApiFactory,
private readonly API\LanApiFactory $lanApiFactory,
private readonly Queue\Queue $queue,
private readonly Helpers\Entity $entityHelper,
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
private readonly EventLoop\LoopInterface $eventLoop,
)
{
}
Expand Down Expand Up @@ -141,10 +137,7 @@ public function discover(Entities\Devices\Gateway|null $onlyGateway = null): voi

public function disconnect(): void
{
if ($this->handlerTimer !== null) {
$this->eventLoop->cancelTimer($this->handlerTimer);
$this->handlerTimer = null;
}
// Nothing to do here
}

/**
Expand All @@ -158,7 +151,7 @@ private function discoverSubDevices(
{
$deferred = new Promise\Deferred();

$lanApiApi = $this->lanApiApiFactory->create(
$lanApi = $this->lanApiFactory->create(
$this->connector->getIdentifier(),
);

Expand All @@ -167,7 +160,7 @@ private function discoverSubDevices(
}

try {
$lanApiApi->getSubDevices(
$lanApi->getSubDevices(
$gateway->getIpAddress(),
$gateway->getAccessToken(),
)
Expand Down
10 changes: 5 additions & 5 deletions src/Clients/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ final class Gateway implements Client
/** @var array<string, array<string, DateTimeInterface|bool>> */
private array $processedDevicesCommands = [];

private API\LanApi $lanApiApi;
private API\LanApi $lanApi;

private EventLoop\TimerInterface|null $handlerTimer = null;

public function __construct(
private readonly Entities\NsPanelConnector $connector,
API\LanApiFactory $lanApiFactory,
private readonly Queue\Queue $queue,
private readonly Helpers\Entity $entityHelper,
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
private readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly EventLoop\LoopInterface $eventLoop,
API\LanApiFactory $lanApiApiFactory,
)
{
$this->lanApiApi = $lanApiApiFactory->create(
$this->lanApi = $lanApiFactory->create(
$this->connector->getIdentifier(),
);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ private function readDeviceInformation(Entities\Devices\Gateway $gateway): bool
$this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow();

try {
$this->lanApiApi->getGatewayInfo($gateway->getIpAddress())
$this->lanApi->getGatewayInfo($gateway->getIpAddress())
->then(function () use ($gateway): void {
$this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow();

Expand Down Expand Up @@ -355,7 +355,7 @@ private function readDeviceState(Entities\Devices\Gateway $gateway): bool
$this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_STATE] = $this->dateTimeFactory->getNow();

try {
$this->lanApiApi->getSubDevices($gateway->getIpAddress(), $gateway->getAccessToken())
$this->lanApi->getSubDevices($gateway->getIpAddress(), $gateway->getAccessToken())
->then(function (Entities\API\Response\GetSubDevices $subDevices) use ($gateway): void {
$this->processedDevicesCommands[$gateway->getIdentifier()][self::CMD_STATE] = $this->dateTimeFactory->getNow();

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
private readonly API\LanApiFactory $lanApiFactory,
private readonly Helpers\Loader $loader,
private readonly Helpers\Entity $entityHelper,
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Connectors\ConnectorsRepository $connectorsRepository,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
private readonly DevicesModels\Devices\DevicesManager $devicesManager,
Expand All @@ -103,7 +104,6 @@ public function __construct(
private readonly DevicesModels\Channels\Properties\PropertiesManager $channelsPropertiesManager,
private readonly Persistence\ManagerRegistry $managerRegistry,
private readonly Localization\Translator $translator,
private readonly NsPanel\Logger $logger,
string|null $name = null,
)
{
Expand Down Expand Up @@ -3940,7 +3940,7 @@ private function getOrmConnection(): DBAL\Connection
return $connection;
}

throw new Exceptions\Runtime('Transformer manager could not be loaded');
throw new Exceptions\Runtime('Database connection could not be established');
}

}
2 changes: 1 addition & 1 deletion src/Commands/Discovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function (string|int|null $answer) use ($connectors): Entities\NsPanelConnector

$result = $serviceCmd->run(new Input\ArrayInput([
'--connector' => $connector->getId()->toString(),
'--mode' => 'discover',
'--mode' => DevicesCommands\Connector::MODE_DISCOVER,
'--no-interaction' => true,
'--quiet' => true,
]), $output);
Expand Down
6 changes: 2 additions & 4 deletions src/Commands/Initialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class Initialize extends Console\Command\Command
public const NAME = 'fb:ns-panel-connector:initialize';

public function __construct(
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Connectors\ConnectorsRepository $connectorsRepository,
private readonly DevicesModels\Connectors\ConnectorsManager $connectorsManager,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
private readonly DevicesModels\Connectors\Properties\PropertiesRepository $connectorsPropertiesRepository,
private readonly DevicesModels\Connectors\Properties\PropertiesManager $connectorsPropertiesManager,
private readonly Persistence\ManagerRegistry $managerRegistry,
private readonly Localization\Translator $translator,
private readonly NsPanel\Logger $logger,
string|null $name = null,
)
{
Expand Down Expand Up @@ -425,10 +425,8 @@ private function deleteConfiguration(Style\SymfonyStyle $io): void
*/
private function listConfigurations(Style\SymfonyStyle $io): void
{
/** @var DevicesQueries\FindConnectors<Entities\NsPanelConnector> $findConnectorsQuery */
$findConnectorsQuery = new Queries\FindConnectors();

/** @var array<Entities\NsPanelConnector> $connectors */
$connectors = $this->connectorsRepository->findAllBy($findConnectorsQuery, Entities\NsPanelConnector::class);
usort(
$connectors,
Expand Down Expand Up @@ -713,7 +711,7 @@ private function getOrmConnection(): DBAL\Connection
return $connection;
}

throw new Exceptions\Runtime('Transformer manager could not be loaded');
throw new Exceptions\Runtime('Database connection could not be established');
}

}
6 changes: 2 additions & 4 deletions src/Connector/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public function __construct(
private readonly DevicesEntities\Connectors\Connector $connector,
private readonly array $clientsFactories,
private readonly Clients\DiscoveryFactory $discoveryClientFactory,
private readonly Servers\ServerFactory $serverFactory,
private readonly Writers\WriterFactory $writerFactory,
private readonly Helpers\Entity $entityHelper,
private readonly Queue\Queue $queue,
private readonly Queue\Consumers $consumers,
private readonly Servers\ServerFactory $serverFactory,
private readonly Writers\WriterFactory $writerFactory,
private readonly NsPanel\Logger $logger,
private readonly DevicesModels\Devices\DevicesRepository $devicesRepository,
private readonly DevicesModels\Channels\ChannelsRepository $channelsRepository,
Expand Down Expand Up @@ -237,8 +237,6 @@ public function discover(): void
],
);

$this->server?->disconnect();

$client = $this->discoveryClientFactory->create($this->connector);

$client->on('finished', function (): void {
Expand Down
4 changes: 3 additions & 1 deletion src/DI/NsPanelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use FastyBird\Connector\NsPanel\Subscribers;
use FastyBird\Connector\NsPanel\Writers;
use FastyBird\Library\Bootstrap\Boot as BootstrapBoot;
use FastyBird\Library\Exchange\DI as ExchangeDI;
use FastyBird\Module\Devices\DI as DevicesDI;
use Nette;
use Nette\DI;
Expand Down Expand Up @@ -104,7 +105,8 @@ public function loadConfiguration(): void
$builder->addFactoryDefinition($this->prefix('writers.exchange'))
->setImplement(Writers\ExchangeFactory::class)
->getResultDefinition()
->setType(Writers\Exchange::class);
->setType(Writers\Exchange::class)
->addTag(ExchangeDI\ExchangeExtension::CONSUMER_STATE, false);
} elseif ($configuration->writer === Writers\Periodic::NAME) {
$builder->addFactoryDefinition($this->prefix('writers.periodic'))
->setImplement(Writers\PeriodicFactory::class)
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Messages/WriteDeviceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Ramsey\Uuid;

/**
* Write update device state to hardware message entity
* Write updated device state to hardware message entity
*
* @package FastyBird:NsPanelConnector!
* @subpackage Entities
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Messages/WriteSubDeviceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace FastyBird\Connector\NsPanel\Entities\Messages;

/**
* Write update sub-device state to hardware message entity
* Write updated sub-device state to hardware message entity
*
* @package FastyBird:NsPanelConnector!
* @subpackage Entities
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/Messages/WriteThirdPartyDeviceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace FastyBird\Connector\NsPanel\Entities\Messages;

/**
* Write update third-party device state to hardware message entity
* Write updated third-party device state to hardware message entity
*
* @package FastyBird:NsPanelConnector!
* @subpackage Entities
Expand Down
4 changes: 3 additions & 1 deletion src/Events/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
class Request extends EventDispatcher\Event
{

public function __construct(private readonly ServerRequestInterface $request)
public function __construct(
private readonly ServerRequestInterface $request,
)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ final class Router
private SlimRouterHttp\ResponseFactory $responseFactory;

public function __construct(
private readonly SlimRouterRouting\IRouter $router,
private readonly NsPanel\Logger $logger,
private readonly SlimRouterRouting\IRouter $router,
private readonly EventDispatcher\EventDispatcherInterface|null $dispatcher = null,
)
{
Expand Down
Loading

0 comments on commit 9c9696d

Please sign in to comment.