Skip to content

Commit

Permalink
Preparing connector service, next step
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jun 19, 2022
1 parent a1bf4f2 commit c241f99
Show file tree
Hide file tree
Showing 47 changed files with 850 additions and 146 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"require" : {
"php": ">=7.4.0|>=8.0.0",
"ext-bcmath": "*",
"ext-pcntl": "*",
"contributte/flysystem": "^0.3.0",
"cweagans/composer-patches": "^1.7",
"fastybird/exchange": "^0.44",
Expand Down
30 changes: 15 additions & 15 deletions fastybird_devices_module/connectors/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,63 +575,63 @@ async def __write_control_command( # pylint: disable=too-many-return-statements
if self.__connector is None:
return

if item.routing_key == RoutingKey.DEVICE_ACTION and ControlAction.has_value(str(item.data.get("name"))):
if item.routing_key == RoutingKey.CONNECTOR_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
device_control = self.__connectors_control_repository.get_by_name(
connector_control = self.__connectors_control_repository.get_by_name(
connector_id=uuid.UUID(item.data.get("connector"), version=4),
control_name=str(item.data.get("name")),
)

except ValueError:
return

if device_control is None:
self.__logger.warning("Device control was not found in database")
if connector_control is None:
self.__logger.warning("Connector control was not found in database")

return

await self.__connector.write_control(
control_item=device_control,
control_item=connector_control,
data=item.data,
action=ControlAction(item.data.get("name")),
)

if item.routing_key == RoutingKey.CHANNEL_ACTION and ControlAction.has_value(str(item.data.get("name"))):
if item.routing_key == RoutingKey.DEVICE_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
channel_control = self.__devices_control_repository.get_by_name(
device_control = self.__devices_control_repository.get_by_name(
device_id=uuid.UUID(item.data.get("device"), version=4), control_name=str(item.data.get("name"))
)

except ValueError:
return

if channel_control is None:
self.__logger.warning("Channel control was not found in database")
if device_control is None:
self.__logger.warning("Device control was not found in database")

return

await self.__connector.write_control(
control_item=channel_control,
control_item=device_control,
data=item.data,
action=ControlAction(item.data.get("name")),
)

if item.routing_key == RoutingKey.CONNECTOR_ACTION and ControlAction.has_value(str(item.data.get("name"))):
if item.routing_key == RoutingKey.CHANNEL_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
connector_control = self.__channels_control_repository.get_by_name(
channel_control = self.__channels_control_repository.get_by_name(
channel_id=uuid.UUID(item.data.get("channel"), version=4), control_name=str(item.data.get("name"))
)

except ValueError:
return

if connector_control is None:
self.__logger.warning("Connector control was not found in database")
if channel_control is None:
self.__logger.warning("Channel control was not found in database")

return

await self.__connector.write_control(
control_item=connector_control,
control_item=channel_control,
data=item.data,
action=ControlAction(item.data.get("name")),
)
Expand Down
39 changes: 38 additions & 1 deletion src/Commands/ConnectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

namespace FastyBird\DevicesModule\Commands;

use FastyBird\DevicesModule\Connectors;
use FastyBird\DevicesModule\Exceptions\TerminateException;
use FastyBird\DevicesModule\Models;
use Nette\Localization;
use Psr\Log;
use Ramsey\Uuid;
Expand All @@ -35,6 +38,12 @@
class ConnectorCommand extends Console\Command\Command
{

/** @var Connectors\Connector */
private Connectors\Connector $connector;

/** @var Models\DataStorage\IConnectorsRepository */
private Models\DataStorage\IConnectorsRepository $connectorsRepository;

/** @var Localization\Translator */
private Localization\Translator $translator;

Expand All @@ -45,13 +54,18 @@ class ConnectorCommand extends Console\Command\Command
private EventLoop\LoopInterface $eventLoop;

public function __construct(
Connectors\Connector $connector,
Models\DataStorage\IConnectorsRepository $connectorsRepository,
Localization\Translator $translator,
EventLoop\LoopInterface $eventLoop,
?Log\LoggerInterface $logger = null,
?string $name = null
) {
parent::__construct($name);

$this->connector = $connector;
$this->connectorsRepository = $connectorsRepository;

$this->translator = $translator;
$this->eventLoop = $eventLoop;

Expand Down Expand Up @@ -101,7 +115,30 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
return 1;
}

$this->eventLoop->run();
$connector = $this->connectorsRepository->findById(Uuid\Uuid::fromString($connectorId));

if ($connector === null) {
$this->logger->alert('Connector was not found', [
'source' => 'devices-module',
'type' => 'connector',
]);

return 0;
}

try {
$this->eventLoop->futureTick(function () use ($connector): void {
$this->connector->execute($connector);
});

$this->eventLoop->addSignal(SIGINT, function (int $signal) {
$this->connector->terminate();
});

$this->eventLoop->run();
} catch (TerminateException $ex) {
$this->eventLoop->stop();
}

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/InitializeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
} catch (Throwable $ex) {
// Log caught exception
$this->logger->error('An unhandled error occurred', [
'source' => 'devices-module-initialize',
'type' => 'run',
'source' => 'devices-module',
'type' => 'initialize-cmd',
'exception' => [
'message' => $ex->getMessage(),
'code' => $ex->getCode(),
Expand Down
Loading

0 comments on commit c241f99

Please sign in to comment.