Skip to content

Commit

Permalink
Refactoring event systems
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jun 25, 2022
1 parent 6dbe413 commit 2ec24ba
Show file tree
Hide file tree
Showing 18 changed files with 795 additions and 299 deletions.
2 changes: 1 addition & 1 deletion fastybird_devices_module/__init__.py
Expand Up @@ -18,4 +18,4 @@
Devices module
"""

__version__ = "0.64.0"
__version__ = "0.65.0"
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@fastybird/devices-module",
"version": "0.64.0",
"version": "0.65.0",
"description": "Devices module data model plugin",
"keywords": [
"devices",
Expand Down
17 changes: 15 additions & 2 deletions src/Commands/ConnectorCommand.php
Expand Up @@ -16,9 +16,12 @@
namespace FastyBird\DevicesModule\Commands;

use FastyBird\DevicesModule\Connectors;
use FastyBird\DevicesModule\Exceptions\TerminateException;
use FastyBird\DevicesModule\DataStorage;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\DevicesModule\Models;
use League\Flysystem;
use Nette\Localization;
use Nette\Utils;
use Psr\Log;
use Ramsey\Uuid;
use React\EventLoop;
Expand All @@ -44,6 +47,9 @@ class ConnectorCommand extends Console\Command\Command
/** @var Models\DataStorage\IConnectorsRepository */
private Models\DataStorage\IConnectorsRepository $connectorsRepository;

/** @var DataStorage\Reader */
private DataStorage\Reader $reader;

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

Expand All @@ -56,6 +62,7 @@ class ConnectorCommand extends Console\Command\Command
public function __construct(
Connectors\Connector $connector,
Models\DataStorage\IConnectorsRepository $connectorsRepository,
DataStorage\Reader $reader,
Localization\Translator $translator,
EventLoop\LoopInterface $eventLoop,
?Log\LoggerInterface $logger = null,
Expand All @@ -65,6 +72,7 @@ public function __construct(

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

$this->translator = $translator;
$this->eventLoop = $eventLoop;
Expand All @@ -86,6 +94,9 @@ protected function configure(): void

/**
* {@inheritDoc}
*
* @throws Flysystem\FilesystemException
* @throws Utils\JsonException
*/
protected function execute(Input\InputInterface $input, Output\OutputInterface $output): int
{
Expand Down Expand Up @@ -115,6 +126,8 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
return 1;
}

$this->reader->read();

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

if ($connector === null) {
Expand All @@ -136,7 +149,7 @@ protected function execute(Input\InputInterface $input, Output\OutputInterface $
});

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

Expand Down
23 changes: 23 additions & 0 deletions src/Connectors/Connector.php
Expand Up @@ -19,6 +19,7 @@
use FastyBird\DevicesModule;
use FastyBird\DevicesModule\Connectors;
use FastyBird\DevicesModule\Entities;
use FastyBird\DevicesModule\Events;
use FastyBird\DevicesModule\Exceptions;
use FastyBird\DevicesModule\Models;
use FastyBird\DevicesModule\Queries;
Expand All @@ -28,6 +29,7 @@
use FastyBird\Metadata\Types\DataTypeType;
use Nette;
use Nette\Utils;
use Psr\EventDispatcher as PsrEventDispatcher;
use Psr\Log;
use React\EventLoop;
use SplObjectStorage;
Expand Down Expand Up @@ -82,6 +84,9 @@ final class Connector
/** @var EventLoop\LoopInterface */
private EventLoop\LoopInterface $eventLoop;

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

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

Expand All @@ -92,6 +97,7 @@ public function __construct(
Models\States\ConnectorPropertiesManager $connectorPropertiesStateManager,
DateTimeFactory\DateTimeFactory $dateTimeFactory,
EventLoop\LoopInterface $eventLoop,
?PsrEventDispatcher\EventDispatcherInterface $dispatcher,
?Log\LoggerInterface $logger = null
) {
$this->connectorPropertiesRepository = $connectorPropertiesRepository;
Expand All @@ -101,6 +107,7 @@ public function __construct(

$this->dateTimeFactory = $dateTimeFactory;
$this->eventLoop = $eventLoop;
$this->dispatcher = $dispatcher;

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

Expand All @@ -119,6 +126,10 @@ public function execute(MetadataEntities\Modules\DevicesModule\IConnectorEntity
{
$this->connectors->rewind();

if ($this->dispatcher !== null) {
$this->dispatcher->dispatch(new Events\BeforeConnectorStartEvent($connector));
}

$this->connector = $connector;

foreach ($this->connectors as $service) {
Expand Down Expand Up @@ -146,6 +157,10 @@ public function execute(MetadataEntities\Modules\DevicesModule\IConnectorEntity
throw new Exceptions\TerminateException('Connector can\'t be started');
}

if ($this->dispatcher !== null) {
$this->dispatcher->dispatch(new Events\AfterConnectorStartEvent($connector));
}

return;
}
}
Expand All @@ -167,6 +182,10 @@ public function terminate(): void

try {
if ($this->service !== null) {
if ($this->dispatcher !== null && $this->connector !== null) {
$this->dispatcher->dispatch(new Events\BeforeConnectorTerminateEvent($this->connector));
}

$this->service->terminate();

$now = $this->dateTimeFactory->getNow();
Expand All @@ -185,6 +204,10 @@ public function terminate(): void
}

$this->setConnectorState(ConnectionStateType::get(ConnectionStateType::STATE_STOPPED));

if ($this->dispatcher !== null && $this->connector !== null) {
$this->dispatcher->dispatch(new Events\AfterConnectorTerminateEvent($this->connector));
}
} catch (Throwable $ex) {
$this->logger->error('Connector couldn\'t be stopped. An unexpected error occurred', [
'source' => 'devices-module',
Expand Down
3 changes: 3 additions & 0 deletions src/DI/DevicesModuleExtension.php
Expand Up @@ -178,6 +178,9 @@ public function loadConfiguration(): void
$builder->addDefinition($this->prefix('subscribers.entities'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\EntitiesSubscriber::class);

$builder->addDefinition($this->prefix('subscribers.states'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\StatesSubscriber::class);

$builder->addDefinition($this->prefix('subscribers.exchange'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\ExchangeSubscriber::class);

Expand Down
43 changes: 40 additions & 3 deletions src/DataStorage/Reader.php
Expand Up @@ -64,6 +64,15 @@ final class Reader
/** @var Models\DataStorage\IChannelControlsRepository */
private Models\DataStorage\IChannelControlsRepository $channelControlsRepository;

/** @var Models\States\ChannelPropertiesRepository */
private Models\States\ChannelPropertiesRepository $channelPropertiesStatesRepository;

/** @var Models\States\DevicePropertiesRepository */
private Models\States\DevicePropertiesRepository $devicePropertiesStatesRepository;

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

/** @var MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory */
private MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $connectorEntityFactory;

Expand Down Expand Up @@ -108,6 +117,9 @@ public function __construct(
Models\DataStorage\IChannelsRepository $channelsRepository,
Models\DataStorage\IChannelPropertiesRepository $channelPropertiesRepository,
Models\DataStorage\IChannelControlsRepository $channelControlsRepository,
Models\States\ChannelPropertiesRepository $channelPropertiesStatesRepository,
Models\States\DevicePropertiesRepository $devicePropertiesStatesRepository,
Models\States\ConnectorPropertiesRepository $connectorPropertiesStatesRepository,
MetadataEntities\Modules\DevicesModule\ConnectorEntityFactory $connectorEntityFactory,
MetadataEntities\Modules\DevicesModule\ConnectorPropertyEntityFactory $connectorPropertyEntityFactory,
MetadataEntities\Modules\DevicesModule\ConnectorControlEntityFactory $connectorControlEntityFactory,
Expand All @@ -131,6 +143,10 @@ public function __construct(
$this->channelPropertiesRepository = $channelPropertiesRepository;
$this->channelControlsRepository = $channelControlsRepository;

$this->channelPropertiesStatesRepository = $channelPropertiesStatesRepository;
$this->devicePropertiesStatesRepository = $devicePropertiesStatesRepository;
$this->connectorPropertiesStatesRepository = $connectorPropertiesStatesRepository;

$this->connectorEntityFactory = $connectorEntityFactory;
$this->connectorPropertyEntityFactory = $connectorPropertyEntityFactory;
$this->connectorControlEntityFactory = $connectorControlEntityFactory;
Expand Down Expand Up @@ -240,7 +256,14 @@ public function read(): void
}

try {
$entity = $this->channelPropertyEntityFactory->create(Utils\Json::encode($propertyData));
$propertyState = $this->channelPropertiesStatesRepository->findOneById(Uuid::fromString($propertyId));
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (DevicesModule\Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

try {
$entity = $this->channelPropertyEntityFactory->create(Utils\Json::encode(array_merge($propertyData, $propertyState)));

if (
$entity instanceof MetadataEntities\Modules\DevicesModule\IChannelStaticPropertyEntity
Expand Down Expand Up @@ -291,7 +314,14 @@ public function read(): void
}

try {
$entity = $this->devicePropertyEntityFactory->create(Utils\Json::encode($propertyData));
$propertyState = $this->devicePropertiesStatesRepository->findOneById(Uuid::fromString($propertyId));
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (DevicesModule\Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

try {
$entity = $this->devicePropertyEntityFactory->create(Utils\Json::encode(array_merge($propertyData, $propertyState)));

if (
$entity instanceof MetadataEntities\Modules\DevicesModule\IDeviceStaticPropertyEntity
Expand Down Expand Up @@ -360,7 +390,14 @@ public function read(): void
}

try {
$entity = $this->connectorPropertyEntityFactory->create(Utils\Json::encode($propertyData));
$propertyState = $this->connectorPropertiesStatesRepository->findOneById(Uuid::fromString($propertyId));
$propertyState = $propertyState !== null ? $propertyState->toArray() : [];
} catch (DevicesModule\Exceptions\NotImplementedException $ex) {
$propertyState = [];
}

try {
$entity = $this->connectorPropertyEntityFactory->create(Utils\Json::encode(array_merge($propertyData, $propertyState)));

if (
$entity instanceof MetadataEntities\Modules\DevicesModule\IConnectorStaticPropertyEntity
Expand Down
52 changes: 52 additions & 0 deletions src/Events/AfterConnectorStartEvent.php
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

/**
* AfterConnectorStartEvent.php
*
* @license More in license.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Events
* @since 0.65.0
*
* @date 22.06.22
*/

namespace FastyBird\DevicesModule\Events;

use FastyBird\Metadata\Entities as MetadataEntities;
use Symfony\Contracts\EventDispatcher;

/**
* Event fired after connector has been started
*
* @package FastyBird:DevicesModule!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class AfterConnectorStartEvent extends EventDispatcher\Event
{

/** @var MetadataEntities\Modules\DevicesModule\IConnectorEntity */
private MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector;

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
*/
public function __construct(
MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
) {
$this->connector = $connector;
}

/**
* @return MetadataEntities\Modules\DevicesModule\IConnectorEntity
*/
public function getConnector(): MetadataEntities\Modules\DevicesModule\IConnectorEntity
{
return $this->connector;
}

}
52 changes: 52 additions & 0 deletions src/Events/AfterConnectorTerminateEvent.php
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

/**
* AfterConnectorTerminateEvent.php
*
* @license More in license.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:DevicesModule!
* @subpackage Events
* @since 0.65.0
*
* @date 22.06.22
*/

namespace FastyBird\DevicesModule\Events;

use FastyBird\Metadata\Entities as MetadataEntities;
use Symfony\Contracts\EventDispatcher;

/**
* Event fired after connector has been terminated
*
* @package FastyBird:DevicesModule!
* @subpackage Events
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class AfterConnectorTerminateEvent extends EventDispatcher\Event
{

/** @var MetadataEntities\Modules\DevicesModule\IConnectorEntity */
private MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector;

/**
* @param MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
*/
public function __construct(
MetadataEntities\Modules\DevicesModule\IConnectorEntity $connector
) {
$this->connector = $connector;
}

/**
* @return MetadataEntities\Modules\DevicesModule\IConnectorEntity
*/
public function getConnector(): MetadataEntities\Modules\DevicesModule\IConnectorEntity
{
return $this->connector;
}

}

0 comments on commit 2ec24ba

Please sign in to comment.