Skip to content

Commit

Permalink
Use optimistic state update (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 30, 2024
1 parent 53798a8 commit 16227d6
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Queue/Consumers/WriteSubDeviceChannelPropertyState.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use FastyBird\Module\Devices\Exceptions as DevicesExceptions;
use FastyBird\Module\Devices\Models as DevicesModels;
use FastyBird\Module\Devices\Queries as DevicesQueries;
use FastyBird\Module\Devices\States as DevicesStates;
use Nette;
use Nette\Utils;
use stdClass;
Expand Down Expand Up @@ -310,6 +311,8 @@ public function consume(Queue\Messages\Message $message): bool
DevicesDocuments\Channels\Properties\Dynamic::class,
);

$updateCallbacks = [];

if (
preg_match(Zigbee2Mqtt\Constants::CHANNEL_IDENTIFIER_REGEX, $channel->getIdentifier(), $matches) === 1
&& Types\ExposeType::tryFrom($matches['type']) !== null
Expand All @@ -322,6 +325,16 @@ public function consume(Queue\Messages\Message $message): bool
$state->getExpectedValue(),
);

$updateCallbacks[] = function () use ($property, $state): void {
await($this->channelPropertiesStatesManager->set(
$property,
Utils\ArrayHash::from([
DevicesStates\Property::ACTUAL_VALUE_FIELD => $state->getExpectedValue(),
DevicesStates\Property::EXPECTED_VALUE_FIELD => null,
]),
MetadataTypes\Sources\Connector::SHELLY,
));
};
} else {
try {
$value = $this->stateRepository->get($property->getId());
Expand Down Expand Up @@ -356,6 +369,17 @@ public function consume(Queue\Messages\Message $message): bool
$state->getExpectedValue(),
);

$updateCallbacks[] = function () use ($property, $state): void {
await($this->channelPropertiesStatesManager->set(
$property,
Utils\ArrayHash::from([
DevicesStates\Property::ACTUAL_VALUE_FIELD => $state->getExpectedValue(),
DevicesStates\Property::EXPECTED_VALUE_FIELD => null,
]),
MetadataTypes\Sources\Connector::SHELLY,
));
};

} else {
try {
$value = $this->stateRepository->get($property->getId());
Expand Down Expand Up @@ -447,7 +471,9 @@ public function consume(Queue\Messages\Message $message): bool
),
Utils\Json::encode($payload),
)
->then(function () use ($connector, $bridge, $device, $channel, $message): void {
->then(function () use ($connector, $bridge, $device, $channel, $updateCallbacks, $message): void {
Utils\Arrays::invoke($updateCallbacks);

$this->logger->debug(
'Channel state was successfully sent to device',
[
Expand Down

0 comments on commit 16227d6

Please sign in to comment.