Skip to content

Commit

Permalink
After testing updates & fixes (#4)
Browse files Browse the repository at this point in the history
* After testing updates & fixes

* QA fixes

* QA fixes
  • Loading branch information
akadlec committed Jul 9, 2022
1 parent 1cebf05 commit f5249cf
Show file tree
Hide file tree
Showing 60 changed files with 829 additions and 1,081 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"ext-pcntl": "*",
"contributte/flysystem": "^0.3.0",
"cweagans/composer-patches": "^1.7",
"fastybird/exchange" : "^0.50",
"fastybird/exchange" : "^0.51",
"fastybird/json-api" : "^0.11",
"fastybird/metadata" : "^0.68",
"fastybird/metadata" : "^0.69",
"fastybird/simple-auth" : "^0.4",
"ipub/doctrine-dynamic-discriminator-map": "^1.4",
"ipub/doctrine-timestampable": "^1.5",
Expand Down
6 changes: 3 additions & 3 deletions fastybird_devices_module/connectors/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements
if self.__connector is None:
return

if item.routing_key == RoutingKey.CONNECTOR_ACTION and ControlAction.has_value(str(item.data.get("name"))):
if item.routing_key == RoutingKey.CONNECTOR_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
connector_control = self.__connectors_control_repository.get_by_name(
connector_id=uuid.UUID(item.data.get("connector"), version=4),
Expand All @@ -596,7 +596,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements
action=ControlAction(item.data.get("name")),
)

if item.routing_key == RoutingKey.DEVICE_ACTION and ControlAction.has_value(str(item.data.get("name"))):
if item.routing_key == RoutingKey.DEVICE_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
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"))
Expand All @@ -616,7 +616,7 @@ async def __write_control_command( # pylint: disable=too-many-return-statements
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.CHANNEL_CONTROL_ACTION and ControlAction.has_value(str(item.data.get("name"))):
try:
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"))
Expand Down
6 changes: 3 additions & 3 deletions fastybird_devices_module/connectors/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class ConnectorConsumer(IConsumer): # pylint: disable=too-few-public-methods
]

__CONTROLS_ACTIONS_ROUTING_KEYS: List[RoutingKey] = [
RoutingKey.CONNECTOR_ACTION,
RoutingKey.DEVICE_ACTION,
RoutingKey.CHANNEL_ACTION,
RoutingKey.CONNECTOR_CONTROL_ACTION,
RoutingKey.DEVICE_CONTROL_ACTION,
RoutingKey.CHANNEL_CONTROL_ACTION,
]

__queue: ConnectorQueue
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fastnumbers>=3.2
fastybird-exchange>=0.50
fastybird-metadata>=0.68
fastybird-exchange>=0.51
fastybird-metadata>=0.69
inflection>=0.5
kink>=0.6
setuptools>=57.4
Expand Down
48 changes: 21 additions & 27 deletions src/Commands/ConnectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,26 @@ class ConnectorCommand extends Console\Command\Command

private const SHUTDOWN_WAITING_DELAY = 3;

/** @var Connectors\ConnectorFactory */
private Connectors\ConnectorFactory $factory;

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

/** @var Models\Connectors\Properties\IPropertiesRepository */
private Models\Connectors\Properties\IPropertiesRepository $connectorPropertiesRepository;

/** @var Models\Connectors\Properties\IPropertiesManager */
private Models\Connectors\Properties\IPropertiesManager $connectorPropertiesManager;

/** @var Models\States\ConnectorPropertiesRepository */
private Models\States\ConnectorPropertiesRepository $connectorPropertiesStateRepository;

/** @var Models\States\ConnectorPropertiesManager */
private Models\States\ConnectorPropertiesManager $connectorPropertiesStateManager;

/** @var DateTimeFactory\DateTimeFactory */
private DateTimeFactory\DateTimeFactory $dateTimeFactory;

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

/** @var Log\LoggerInterface */
private Log\LoggerInterface $logger;

/** @var PsrEventDispatcher\EventDispatcherInterface|null */
private ?PsrEventDispatcher\EventDispatcherInterface $dispatcher;

/** @var EventLoop\LoopInterface */
private EventLoop\LoopInterface $eventLoop;

public function __construct(
Expand Down Expand Up @@ -114,9 +103,6 @@ public function __construct(
parent::__construct($name);
}

/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
Expand All @@ -126,9 +112,6 @@ protected function configure(): void
->setDescription('Run connector service.');
}

/**
* {@inheritDoc}
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int
{
$symfonyApp = $this->getApplication();
Expand Down Expand Up @@ -174,7 +157,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
$this->eventLoop->futureTick(function () use ($connector, $service): void {
$this->dispatcher?->dispatch(new Events\BeforeConnectorStartEvent($connector));

$this->logger->debug('Starting connector...', [
$this->logger->info('Starting connector...', [
'source' => 'devices-module',
'type' => 'connector',
]);
Expand All @@ -195,7 +178,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
});

$this->eventLoop->addSignal(SIGINT, function (int $signal) use ($connector, $service): void {
$this->logger->debug('Stopping connector...', [
$this->logger->info('Stopping connector...', [
'source' => 'devices-module',
'type' => 'connector',
]);
Expand Down Expand Up @@ -236,23 +219,34 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
'code' => $ex->getCode(),
],
]);

throw new Exceptions\TerminateException(
'Error during connector termination process',
$ex->getCode(),
$ex
);
}
});

$this->eventLoop->run();
} catch (Exceptions\TerminateException $ex) {
} catch (Throwable $ex) {
if (!$ex instanceof Exceptions\TerminateException) {
$this->logger->error('An unhandled error occurred', [
'source' => 'devices-module',
'type' => 'connector',
'exception' => [
'message' => $ex->getMessage(),
'code' => $ex->getCode(),
],
]);
}

$this->eventLoop->stop();
}

return 0;
}

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
* @param MetadataTypes\ConnectionStateType $state
*
* @return void
*/
private function setConnectorState(
MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector,
MetadataTypes\ConnectionStateType $state
Expand All @@ -268,7 +262,7 @@ private function setConnectorState(
'connector' => $connector->getId(),
'entity' => Entities\Connectors\Properties\DynamicProperty::class,
'identifier' => MetadataTypes\ConnectorPropertyNameType::NAME_STATE,
'data_type' => MetadataTypes\DataTypeType::get(MetadataTypes\DataTypeType::DATA_TYPE_ENUM),
'dataType' => MetadataTypes\DataTypeType::get(MetadataTypes\DataTypeType::DATA_TYPE_ENUM),
'unit' => null,
'format' => [
MetadataTypes\ConnectionStateType::STATE_RUNNING,
Expand Down
113 changes: 113 additions & 0 deletions src/Commands/DataStorageCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php declare(strict_types = 1);

/**
* DataStorageCommand.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Commands
* @since 0.71.0
*
* @date 08.07.22
*/

namespace FastyBird\DevicesModule\Commands;

use FastyBird\DevicesModule\DataStorage;
use Psr\Log;
use RuntimeException;
use Symfony\Component\Console;
use Symfony\Component\Console\Input;
use Symfony\Component\Console\Output;
use Symfony\Component\Console\Style;
use Throwable;

/**
* Data storage initialize command
*
* @package FastyBird:DevicesModule!
* @subpackage Commands
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class DataStorageCommand extends Console\Command\Command
{

private DataStorage\Writer $writer;

private Log\LoggerInterface $logger;

public function __construct(
DataStorage\Writer $writer,
?Log\LoggerInterface $logger = null,
?string $name = null
) {
$this->writer = $writer;

$this->logger = $logger ?? new Log\NullLogger();

parent::__construct($name);
}

protected function configure(): void
{
$this
->setName('fb:devices-module:data-storage')
->addOption('noconfirm', null, Input\InputOption::VALUE_NONE, 'do not ask for any confirmation')
->setDescription('Initialize module data storage.');
}

protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int
{
$symfonyApp = $this->getApplication();

if ($symfonyApp === null) {
return 1;
}

$io = new Style\SymfonyStyle($input, $output);

$io->title('FB devices module - data storage initialization');

$io->note('This action will create|update module data storage configuration.');

/** @var bool $continue */
$continue = $io->ask('Would you like to continue?', 'n', function ($answer): bool {
if (!in_array($answer, ['y', 'Y', 'n', 'N'], true)) {
throw new RuntimeException('You must type Y or N');
}

return in_array($answer, ['y', 'Y'], true);
});

if (!$continue) {
return 0;
}

try {
$this->writer->write();

$io->success('Devices module data storage has been successfully initialized.');

return 0;

} catch (Throwable $ex) {
// Log caught exception
$this->logger->error('An unhandled error occurred', [
'source' => 'devices-module',
'type' => 'data-storage-cmd',
'exception' => [
'message' => $ex->getMessage(),
'code' => $ex->getCode(),
],
]);

$io->error('Something went wrong, initialization could not be finished. Error was logged.');

return 1;
}
}

}
7 changes: 0 additions & 7 deletions src/Commands/InitializeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
class InitializeCommand extends Console\Command\Command
{

/** @var Log\LoggerInterface */
private Log\LoggerInterface $logger;

public function __construct(
Expand All @@ -46,9 +45,6 @@ public function __construct(
parent::__construct($name);
}

/**
* {@inheritDoc}
*/
protected function configure(): void
{
$this
Expand All @@ -57,9 +53,6 @@ protected function configure(): void
->setDescription('Initialize module.');
}

/**
* {@inheritDoc}
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int
{
$symfonyApp = $this->getApplication();
Expand Down
5 changes: 0 additions & 5 deletions src/Connectors/ConnectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ public function __construct(
}
}

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
*
* @return IConnector
*/
public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector
{
/** @var IConnectorFactory $factory */
Expand Down
9 changes: 0 additions & 9 deletions src/Connectors/IConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,10 @@
interface IConnector
{

/**
* @return void
*/
public function execute(): void;

/**
* @return void
*/
public function terminate(): void;

/**
* @return bool
*/
public function hasUnfinishedTasks(): bool;

}
8 changes: 0 additions & 8 deletions src/Connectors/IConnectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@
interface IConnectorFactory
{

/**
* @return string
*/
public function getType(): string;

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
*
* @return IConnector
*/
public function create(MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector): IConnector;

}
6 changes: 3 additions & 3 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ final class Constants
];

public const CONTROLS_ACTIONS_ROUTING_KEYS = [
MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_ACTION,
MetadataTypes\RoutingKeyType::ROUTE_DEVICE_ACTION,
MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_ACTION,
MetadataTypes\RoutingKeyType::ROUTE_CONNECTOR_CONTROL_ACTION,
MetadataTypes\RoutingKeyType::ROUTE_DEVICE_CONTROL_ACTION,
MetadataTypes\RoutingKeyType::ROUTE_CHANNEL_CONTROL_ACTION,
];

}
Loading

0 comments on commit f5249cf

Please sign in to comment.