Skip to content

Commit

Permalink
Use optimistics state update (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec authored Sep 29, 2024
1 parent 05ef0f5 commit 740bb67
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 2 deletions.
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

0 comments on commit 740bb67

Please # to comment.