Skip to content

Commit

Permalink
[general] Upgraded date time factory dueto testing issues (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 29, 2024
1 parent 7ac9d43 commit 321f1ea
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 74 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"fastybird/tools-library": "dev-main",
"guzzlehttp/guzzle": "^7.5",
"nette/di": "^3.0",
"orisai/nette-object-mapper": "^0.1",
"orisai/object-mapper": "^0.2",
"psr/log": "^3.0",
"ratchet/pawl": "^0.4",
"react/async": "^4.0",
Expand Down
6 changes: 3 additions & 3 deletions src/API/CloudApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function __construct(
private readonly Helpers\MessageBuilder $entityHelper,
private readonly Sonoff\Logger $logger,
private readonly MetadataSchemas\Validator $schemaValidator,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
Types\Region|null $region = null,
)
{
Expand All @@ -137,7 +137,7 @@ public function connect(): void
$this->accessToken = $result->getAccessToken();
$this->refreshToken = $result->getRefreshToken();
$this->user = $result->getUser();
$this->tokensAcquired = $this->dateTimeFactory->getNow();
$this->tokensAcquired = $this->clock->getNow();

$this->region = $result->getRegion();
}
Expand Down Expand Up @@ -1144,7 +1144,7 @@ private function callRequest(

if (
str_contains(strval($request->getUri()), self::USER_REFRESH_API_ENDPOINT)
&& $this->tokensAcquired?->diff($this->dateTimeFactory->getNow())->s >= self::ACCESS_TOKEN_VALID_TIME
&& $this->tokensAcquired?->diff($this->clock->getNow())->s >= self::ACCESS_TOKEN_VALID_TIME
&& $this->refreshToken !== null
) {
try {
Expand Down
16 changes: 8 additions & 8 deletions src/API/CloudWs.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function __construct(
private readonly Helpers\MessageBuilder $entityHelper,
private readonly Sonoff\Logger $logger,
private readonly MetadataSchemas\Validator $schemaValidator,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly ObjectMapper\Processing\Processor $objectMapper,
private readonly EventLoop\LoopInterface $eventLoop,
)
Expand All @@ -150,7 +150,7 @@ public function connect(): Promise\PromiseInterface
$this->connecting = true;
$this->connected = false;

$this->lastConnectAttempt = $this->dateTimeFactory->getNow();
$this->lastConnectAttempt = $this->clock->getNow();
$this->lost = null;
$this->disconnected = null;

Expand Down Expand Up @@ -286,7 +286,7 @@ public function disconnect(): void
$this->connecting = false;
$this->connected = false;

$this->disconnected = $this->dateTimeFactory->getNow();
$this->disconnected = $this->clock->getNow();

if ($this->pingTimer !== null) {
$this->eventLoop->cancelTimer($this->pingTimer);
Expand Down Expand Up @@ -333,7 +333,7 @@ public function readStates(string $id, string $apiKey): Promise\PromiseInterface
$message->selfApikey = $this->apiKey;
$message->deviceid = $id;
$message->userAgent = 'app';
$message->sequence = strval(intval($this->dateTimeFactory->getNow()->format('Uv')));
$message->sequence = strval(intval($this->clock->getNow()->format('Uv')));
$message->params = [];

$this->sendRequest($message, $message->action, $message->sequence, $deferred);
Expand Down Expand Up @@ -376,7 +376,7 @@ public function writeState(
$message->selfApikey = $this->apiKey;
$message->deviceid = $id;
$message->userAgent = 'app';
$message->sequence = strval(intval($this->dateTimeFactory->getNow()->format('Uv')));
$message->sequence = strval(intval($this->clock->getNow()->format('Uv')));
$message->params = $params;

$this->sendRequest($message, $message->action, $message->sequence, $deferred);
Expand Down Expand Up @@ -430,7 +430,7 @@ private function doWsHandshake(): Promise\PromiseInterface
);
}

$timestamp = $this->dateTimeFactory->getNow()->getTimestamp();
$timestamp = $this->clock->getNow()->getTimestamp();

$message = new stdClass();
$message->action = self::USER_ONLINE_ACTION;
Expand All @@ -440,7 +440,7 @@ private function doWsHandshake(): Promise\PromiseInterface
$message->nonce = strval(intval($timestamp / 100));
$message->ts = $timestamp;
$message->userAgent = 'app';
$message->sequence = strval(intval($this->dateTimeFactory->getNow()->format('Uv')));
$message->sequence = strval(intval($this->clock->getNow()->format('Uv')));
$message->version = 8;

$this->sendRequest($message, $message->action, $message->sequence, $deferred);
Expand All @@ -450,7 +450,7 @@ private function doWsHandshake(): Promise\PromiseInterface

private function lost(): void
{
$this->lost = $this->dateTimeFactory->getNow();
$this->lost = $this->clock->getNow();

Utils\Arrays::invoke($this->onLost);

Expand Down
6 changes: 3 additions & 3 deletions src/API/LanApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function __construct(
private readonly Services\MulticastFactory $multicastFactory,
private readonly Helpers\MessageBuilder $entityHelper,
private readonly Sonoff\Logger $logger,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly EventLoop\LoopInterface $eventLoop,
private readonly MetadataSchemas\Validator $schemaValidator,
)
Expand Down Expand Up @@ -311,7 +311,7 @@ public function getDeviceInfo(
$deferred = new Promise\Deferred();

$payload = new stdClass();
$payload->sequence = strval(intval($this->dateTimeFactory->getNow()->format('Uv')));
$payload->sequence = strval(intval($this->clock->getNow()->format('Uv')));
$payload->deviceid = $id;
$payload->selfApikey = '123';
$payload->data = new stdClass();
Expand Down Expand Up @@ -418,7 +418,7 @@ public function setDeviceState(
}

$payload = new stdClass();
$payload->sequence = strval(intval($this->dateTimeFactory->getNow()->format('Uv')));
$payload->sequence = strval(intval($this->clock->getNow()->format('Uv')));
$payload->deviceid = $id;
$payload->selfApikey = '123';
$payload->data = $params;
Expand Down
4 changes: 2 additions & 2 deletions src/Clients/Auto.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class Auto extends ClientProcess implements Client
public function __construct(
Helpers\Device $deviceHelper,
DevicesUtilities\DeviceConnection $deviceConnectionManager,
DateTimeFactory\Factory $dateTimeFactory,
DateTimeFactory\Clock $clock,
EventLoop\LoopInterface $eventLoop,
private readonly Documents\Connectors\Connector $connector,
private readonly LanFactory $lanClientFactory,
Expand All @@ -64,7 +64,7 @@ public function __construct(
parent::__construct(
$deviceHelper,
$deviceConnectionManager,
$dateTimeFactory,
$clock,
$eventLoop,
);
}
Expand Down
14 changes: 7 additions & 7 deletions src/Clients/ClientProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ abstract class ClientProcess
public function __construct(
protected readonly Helpers\Device $deviceHelper,
protected readonly DevicesUtilities\DeviceConnection $deviceConnectionManager,
protected readonly DateTimeFactory\Factory $dateTimeFactory,
protected readonly DateTimeFactory\Clock $clock,
protected readonly EventLoop\LoopInterface $eventLoop,
)
{
Expand Down Expand Up @@ -154,15 +154,15 @@ protected function readDeviceInformation(Documents\Devices\Device $device): bool
if (
$cmdResult instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $cmdResult->getTimestamp()
$this->clock->getNow()->getTimestamp() - $cmdResult->getTimestamp()
< $this->deviceHelper->getHeartbeatDelay($device)
)
) {
return false;
}
}

$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow();
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_HEARTBEAT] = $this->clock->getNow();

$deviceState = $this->deviceConnectionManager->getState($device);

Expand All @@ -174,7 +174,7 @@ protected function readDeviceInformation(Documents\Devices\Device $device): bool

$this->readInformation($device)
->then(function () use ($device): void {
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_HEARTBEAT] = $this->dateTimeFactory->getNow();
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_HEARTBEAT] = $this->clock->getNow();
});

return true;
Expand Down Expand Up @@ -204,15 +204,15 @@ protected function readDeviceState(Documents\Devices\Device $device): bool
if (
$cmdResult instanceof DateTimeInterface
&& (
$this->dateTimeFactory->getNow()->getTimestamp() - $cmdResult->getTimestamp()
$this->clock->getNow()->getTimestamp() - $cmdResult->getTimestamp()
< $this->deviceHelper->getStateReadingDelay($device)
)
) {
return false;
}
}

$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_STATE] = $this->dateTimeFactory->getNow();
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_STATE] = $this->clock->getNow();

$deviceState = $this->deviceConnectionManager->getState($device);

Expand All @@ -224,7 +224,7 @@ protected function readDeviceState(Documents\Devices\Device $device): bool

$this->readState($device)
->then(function () use ($device): void {
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_STATE] = $this->dateTimeFactory->getNow();
$this->processedDevicesCommands[$device->getId()->toString()][self::CMD_STATE] = $this->clock->getNow();
});

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/Clients/Cloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ final class Cloud extends ClientProcess implements Client
public function __construct(
Helpers\Device $deviceHelper,
DevicesUtilities\DeviceConnection $deviceConnectionManager,
DateTimeFactory\Factory $dateTimeFactory,
DateTimeFactory\Clock $clock,
EventLoop\LoopInterface $eventLoop,
private readonly Documents\Connectors\Connector $connector,
private readonly bool $autoMode,
Expand All @@ -70,7 +70,7 @@ public function __construct(
parent::__construct(
$deviceHelper,
$deviceConnectionManager,
$dateTimeFactory,
$clock,
$eventLoop,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Clients/Lan.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class Lan extends ClientProcess implements Client
public function __construct(
Helpers\Device $deviceHelper,
DevicesUtilities\DeviceConnection $deviceConnectionManager,
DateTimeFactory\Factory $dateTimeFactory,
DateTimeFactory\Clock $clock,
EventLoop\LoopInterface $eventLoop,
private readonly Documents\Connectors\Connector $connector,
private readonly bool $autoMode,
Expand All @@ -73,7 +73,7 @@ public function __construct(
parent::__construct(
$deviceHelper,
$deviceConnectionManager,
$dateTimeFactory,
$clock,
$eventLoop,
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(
private readonly Helpers\Device $deviceHelper,
private readonly DevicesModels\Configuration\Connectors\Repository $connectorsConfigurationRepository,
private readonly DevicesModels\Configuration\Devices\Repository $devicesConfigurationRepository,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly Localization\Translator $translator,
string|null $name = null,
)
Expand Down Expand Up @@ -287,7 +287,7 @@ function (string|int|null $answer) use ($connectors): Documents\Connectors\Conne

$io->info((string) $this->translator->translate('//sonoff-connector.cmd.discover.messages.starting'));

$this->executedTime = $this->dateTimeFactory->getNow();
$this->executedTime = $this->clock->getNow();

$serviceCmd = $symfonyApp->find(DevicesCommands\Connector::NAME);

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct(
private readonly DevicesModels\Entities\Devices\DevicesRepository $devicesRepository,
private readonly DevicesModels\Entities\Devices\DevicesManager $devicesManager,
private readonly ApplicationHelpers\Database $databaseHelper,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly Localization\Translator $translator,
string|null $name = null,
)
Expand Down Expand Up @@ -823,7 +823,7 @@ private function discoverDevices(Style\SymfonyStyle $io, Entities\Connectors\Con
throw new Exceptions\InvalidState('Something went wrong, console output is not configured');
}

$executedTime = $this->dateTimeFactory->getNow();
$executedTime = $this->clock->getNow();

$symfonyApp = $this->getApplication();

Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers/WriteChannelPropertyState.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(
private readonly DevicesModels\Configuration\Channels\Repository $channelsConfigurationRepository,
private readonly DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
private readonly DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
)
{
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public function consume(Queue\Messages\Message $message): bool
return true;
}

$now = $this->dateTimeFactory->getNow();
$now = $this->clock->getNow();
$pending = $state->getPending();

if (
Expand Down
4 changes: 2 additions & 2 deletions src/Queue/Consumers/WriteDevicePropertyState.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct(
private readonly DevicesModels\Configuration\Devices\Repository $devicesConfigurationRepository,
private readonly DevicesModels\Configuration\Devices\Properties\Repository $devicesPropertiesConfigurationRepository,
private readonly DevicesModels\States\Async\DevicePropertiesManager $devicePropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
)
{
}
Expand Down Expand Up @@ -224,7 +224,7 @@ public function consume(Queue\Messages\Message $message): bool
return true;
}

$now = $this->dateTimeFactory->getNow();
$now = $this->clock->getNow();
$pending = $state->getPending();

if (
Expand Down
4 changes: 2 additions & 2 deletions src/Writers/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(
DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
DevicesModels\States\Async\DevicePropertiesManager $devicePropertiesStatesManager,
DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
DateTimeFactory\Factory $dateTimeFactory,
DateTimeFactory\Clock $clock,
EventLoop\LoopInterface $eventLoop,
private readonly ExchangeConsumers\Container $consumer,
)
Expand All @@ -71,7 +71,7 @@ public function __construct(
$channelsPropertiesConfigurationRepository,
$devicePropertiesStatesManager,
$channelPropertiesStatesManager,
$dateTimeFactory,
$clock,
$eventLoop,
);

Expand Down
4 changes: 2 additions & 2 deletions src/Writers/Periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(
protected readonly DevicesModels\Configuration\Channels\Properties\Repository $channelsPropertiesConfigurationRepository,
private readonly DevicesModels\States\Async\DevicePropertiesManager $devicePropertiesStatesManager,
private readonly DevicesModels\States\Async\ChannelPropertiesManager $channelPropertiesStatesManager,
private readonly DateTimeFactory\Factory $dateTimeFactory,
private readonly DateTimeFactory\Clock $clock,
private readonly EventLoop\LoopInterface $eventLoop,
)
{
Expand Down Expand Up @@ -209,7 +209,7 @@ private function handleCommunication(): void
*/
private function writeProperty(Documents\Devices\Device $device): bool
{
$now = $this->dateTimeFactory->getNow();
$now = $this->clock->getNow();

foreach ($this->properties[$device->getId()->toString()] as $property) {
$debounce = array_key_exists($property->getId()->toString(), $this->processedProperties)
Expand Down
Loading

0 comments on commit 321f1ea

Please sign in to comment.