Skip to content

Commit

Permalink
Fixing handling states (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Nov 19, 2023
1 parent f6d4fb3 commit faebba0
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,39 @@ class Property extends RedisDbStates\State implements DevicesStates\Property
public function __construct(
Uuid\UuidInterface $id,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
new ObjectMapper\Rules\IntValue(),
new ObjectMapper\Rules\FloatValue(),
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\ButtonPayload::class),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\SwitchPayload::class),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\CoverPayload::class),
new ObjectMapper\Rules\ObjectValue(),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
])]
#[ObjectMapper\Modifiers\FieldName(self::ACTUAL_VALUE_FIELD)]
// phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
private readonly bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayload|MetadataTypes\SwitchPayload|MetadataTypes\CoverPayload|null $actualValue = null,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
new ObjectMapper\Rules\IntValue(),
new ObjectMapper\Rules\FloatValue(),
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\ButtonPayload::class),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\SwitchPayload::class),
new BootstrapObjectMapper\Rules\ConsistenceEnumValue(class: MetadataTypes\CoverPayload::class),
new ObjectMapper\Rules\ObjectValue(),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
])]
#[ObjectMapper\Modifiers\FieldName(self::EXPECTED_VALUE_FIELD)]
// phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
private readonly bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayload|MetadataTypes\SwitchPayload|MetadataTypes\CoverPayload|null $expectedValue = null,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\ObjectValue(),
new ObjectMapper\Rules\BoolValue(),
])]
#[ObjectMapper\Modifiers\FieldName(self::PENDING_FIELD)]
private readonly bool|DateTimeInterface $pending = false,
Expand All @@ -78,12 +81,14 @@ public function __construct(
private readonly bool $valid = false,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
new ObjectMapper\Rules\ObjectValue(),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
])]
#[ObjectMapper\Modifiers\FieldName(self::CREATED_AT_FIELD)]
private readonly DateTimeInterface|null $createdAt = null,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
new ObjectMapper\Rules\ObjectValue(),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
])]
#[ObjectMapper\Modifiers\FieldName(self::UPDATED_AT_FIELD)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\ArrayOf(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
private readonly float $power,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\FloatValue(),
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
])]
private readonly float|bool $overpower,
#[ObjectMapper\Rules\BoolValue(castBoolLike: true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\ArrayOf(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\ArrayOf(
new ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\NullValue(castEmptyString: true),
]),
Expand Down
14 changes: 11 additions & 3 deletions src/FastyBird/Connector/Shelly/src/Writers/Periodic.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract class Periodic
/** @var array<string, MetadataDocuments\DevicesModule\Device> */
private array $devices = [];

/** @var array<string, MetadataDocuments\DevicesModule\ChannelDynamicProperty> */
/** @var array<string, array<string, MetadataDocuments\DevicesModule\ChannelDynamicProperty>> */
private array $properties = [];

/** @var array<string> */
Expand Down Expand Up @@ -100,6 +100,10 @@ public function connect(): void
foreach ($this->devicesRepository->findAllBy($findDevicesQuery) as $device) {
$this->devices[$device->getId()->toString()] = $device;

if (!array_key_exists($device->getId()->toString(), $this->properties)) {
$this->properties[$device->getId()->toString()] = [];
}

$findChannelsQuery = new DevicesQueries\Configuration\FindChannels();
$findChannelsQuery->forDevice($device);

Expand All @@ -116,7 +120,7 @@ public function connect(): void

foreach ($properties as $property) {
if ($property->isSettable()) {
$this->properties[$property->getId()->toString()] = $property;
$this->properties[$device->getId()->toString()][$property->getId()->toString()] = $property;
}
}
}
Expand Down Expand Up @@ -176,7 +180,11 @@ private function writeChannelsProperty(MetadataDocuments\DevicesModule\Device $d
{
$now = $this->dateTimeFactory->getNow();

foreach ($this->properties as $property) {
if (!array_key_exists($device->getId()->toString(), $this->properties)) {
return false;
}

foreach ($this->properties[$device->getId()->toString()] as $property) {
$debounce = array_key_exists($property->getId()->toString(), $this->processedProperties)
? $this->processedProperties[$property->getId()->toString()]
: false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class: Types\PropertyType::class,
new ObjectMapper\Rules\DateTimeValue(format: DateTimeInterface::ATOM),
])]
private readonly bool|DateTimeInterface $pending = false,
#[ObjectMapper\Rules\BoolValue(castBoolLike: true)]
#[ObjectMapper\Rules\BoolValue()]
private readonly bool $valid = false,
#[ObjectMapper\Rules\AnyOf([
new ObjectMapper\Rules\BoolValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function __construct(
new ObjectMapper\Rules\StringValue(notEmpty: true),
new ObjectMapper\Rules\IntValue(),
new ObjectMapper\Rules\FloatValue(),
new ObjectMapper\Rules\BoolValue(castBoolLike: true),
new ObjectMapper\Rules\BoolValue(),
]),
key: new ObjectMapper\Rules\IntValue(unsigned: true),
minItems: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Nette;
use Nette\Utils;
use Psr\EventDispatcher as PsrEventDispatcher;
use function array_diff;
use function property_exists;

/**
Expand Down Expand Up @@ -105,7 +106,26 @@ public function update(
);
}

$this->dispatcher?->dispatch(new Events\ChannelPropertyStateEntityUpdated($property, $state, $updatedState));
if (
array_diff(
[
$state->getActualValue(),
$state->getExpectedValue(),
$state->getPending(),
$state->isValid(),
],
[
$updatedState->getActualValue(),
$updatedState->getExpectedValue(),
$updatedState->getPending(),
$updatedState->isValid(),
],
) !== []
) {
$this->dispatcher?->dispatch(
new Events\ChannelPropertyStateEntityUpdated($property, $state, $updatedState),
);
}

return $updatedState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Nette;
use Nette\Utils;
use Psr\EventDispatcher as PsrEventDispatcher;
use function array_diff;
use function property_exists;

/**
Expand Down Expand Up @@ -105,7 +106,26 @@ public function update(
);
}

$this->dispatcher?->dispatch(new Events\ConnectorPropertyStateEntityUpdated($property, $state, $updatedState));
if (
array_diff(
[
$state->getActualValue(),
$state->getExpectedValue(),
$state->getPending(),
$state->isValid(),
],
[
$updatedState->getActualValue(),
$updatedState->getExpectedValue(),
$updatedState->getPending(),
$updatedState->isValid(),
],
) !== []
) {
$this->dispatcher?->dispatch(
new Events\ConnectorPropertyStateEntityUpdated($property, $state, $updatedState),
);
}

return $updatedState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Nette;
use Nette\Utils;
use Psr\EventDispatcher as PsrEventDispatcher;
use function array_diff;
use function property_exists;

/**
Expand Down Expand Up @@ -105,7 +106,24 @@ public function update(
);
}

$this->dispatcher?->dispatch(new Events\DevicePropertyStateEntityUpdated($property, $state, $updatedState));
if (
array_diff(
[
$state->getActualValue(),
$state->getExpectedValue(),
$state->getPending(),
$state->isValid(),
],
[
$updatedState->getActualValue(),
$updatedState->getExpectedValue(),
$updatedState->getPending(),
$updatedState->isValid(),
],
) !== []
) {
$this->dispatcher?->dispatch(new Events\DevicePropertyStateEntityUpdated($property, $state, $updatedState));
}

return $updatedState;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ private function updateState(

return $this->stateMapper->process(
array_merge(
$state->toArray(),
[
$state::ACTUAL_VALUE_FIELD => $state->getActualValue(),
$state::EXPECTED_VALUE_FIELD => $state->getExpectedValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ private function updateState(

return $this->stateMapper->process(
array_merge(
$state->toArray(),
[
$state::ACTUAL_VALUE_FIELD => $state->getActualValue(),
$state::EXPECTED_VALUE_FIELD => $state->getExpectedValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ private function updateState(

return $this->stateMapper->process(
array_merge(
$state->toArray(),
[
$state::ACTUAL_VALUE_FIELD => $state->getActualValue(),
$state::EXPECTED_VALUE_FIELD => $state->getExpectedValue(),
Expand Down
4 changes: 2 additions & 2 deletions src/FastyBird/Module/Devices/src/Utilities/ValueHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ static function (array $item) use ($value): bool {
return self::flattenValue($value);
}

public static function transformValueFromMappedParent(
public static function transformValueToMappedParent(
MetadataTypes\DataType $dataType,
MetadataTypes\DataType $parentDataType,
bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayload|MetadataTypes\SwitchPayload|MetadataTypes\CoverPayload|null $value,
Expand Down Expand Up @@ -717,7 +717,7 @@ public static function transformValueFromMappedParent(
return $value;
}

public static function transformValueToMappedParent(
public static function transformValueFromMappedParent(
MetadataTypes\DataType $dataType,
MetadataTypes\DataType $parentDataType,
bool|float|int|string|DateTimeInterface|MetadataTypes\ButtonPayload|MetadataTypes\SwitchPayload|MetadataTypes\CoverPayload|null $value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function createKey(
// Initialize structure
$data = new stdClass();

$values->offsetSet('id', $id->toString());
$values->offsetSet(States\State::ID_FIELD, $id->toString());

foreach ($fields as $field => $default) {
$value = $default;
Expand Down
4 changes: 3 additions & 1 deletion src/FastyBird/Plugin/RedisDb/src/States/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
class State implements ObjectMapper\MappedObject
{

public const ID_FIELD = 'id';

public const CREATED_AT_FIELD = 'createdAt';

public const UPDATED_AT_FIELD = 'updatedAt';
Expand All @@ -47,7 +49,7 @@ public function __construct(
public static function getCreateFields(): array
{
return [
'id',
self::ID_FIELD,
];
}

Expand Down

0 comments on commit faebba0

Please sign in to comment.