Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[shelly-connector][redisdb-plugin] Use optimistic state update #314

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function create(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\
}

/**
* @throws RedisDbExceptions\InvalidArgument
* @throws RedisDbExceptions\InvalidState
*/
public function update(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\ChannelProperty|false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function create(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\
}

/**
* @throws RedisDbExceptions\InvalidArgument
* @throws RedisDbExceptions\InvalidState
*/
public function update(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\ConnectorProperty|false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function create(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\
}

/**
* @throws RedisDbExceptions\InvalidArgument
* @throws RedisDbExceptions\InvalidState
*/
public function update(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\DeviceProperty|false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function create(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\
}

/**
* @throws RedisDbExceptions\InvalidArgument
* @throws RedisDbExceptions\InvalidState
*/
public function update(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\Action|false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function create(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\
}

/**
* @throws RedisDbExceptions\InvalidArgument
* @throws RedisDbExceptions\InvalidState
*/
public function update(Uuid\UuidInterface $id, Utils\ArrayHash $values): States\Condition|false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
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 FastyBird\Module\Devices\Types as DevicesTypes;
use Fig\Http\Message\StatusCodeInterface;
use Nette;
use Nette\Utils;
use RuntimeException;
use Throwable;
use TypeError;
Expand Down Expand Up @@ -419,7 +421,17 @@ public function consume(Queue\Messages\Message $message): bool
}

$result->then(
function () use ($message, $connector, $device, $channel, $property): void {
function () use ($message, $connector, $device, $channel, $property, $state): void {
await($this->channelPropertiesStatesManager->set(
$property,
Utils\ArrayHash::from([
DevicesStates\Property::ACTUAL_VALUE_FIELD => $state->getExpectedValue(),
DevicesStates\Property::EXPECTED_VALUE_FIELD => null,
DevicesStates\Property::PENDING_FIELD => false,
]),
MetadataTypes\Sources\Connector::SHELLY,
));

$this->logger->debug(
'Channel state was successfully sent to device',
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function create(
/**
* @phpstan-return T
*
* @throws Exceptions\InvalidArgument
* @throws Exceptions\InvalidState
*/
public function update(
Expand All @@ -125,6 +126,8 @@ public function update(

} catch (Exceptions\NotUpdated) {
return false;
} catch (Exceptions\NotFound) {
$raw = $this->createKey($id, $values, $this->entity::getCreateFields(), $database);
}

try {
Expand Down Expand Up @@ -253,7 +256,7 @@ private function updateKey(
$raw = $this->client->get($id->toString());

if (!is_string($raw)) {
throw new Exceptions\InvalidState('Stored record could not be loaded from database');
throw new Exceptions\NotFound('Stored record could not be loaded from database');
}

$data = Utils\Json::decode($raw);
Expand Down
Loading