Skip to content

Commit

Permalink
Introduce OCR channel config change scope
Browse files Browse the repository at this point in the history
  • Loading branch information
fracz committed Aug 29, 2024
1 parent 5f55ea4 commit 6aa999e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function postUpdate(IODeviceChannel $channel, PostUpdateEventArgs $args)
if (isset($changeArray['function'])) {
$changes->add(ChannelConfigChangeScope::CHANNEL_FUNCTION);
}
$changedConfigKeys = [];
if (isset($changeArray['userConfig'])) {
$before = json_decode($changeArray['userConfig'][0] ?: '[]', true);
$after = json_decode($changeArray['userConfig'][1] ?: '[]', true);
Expand All @@ -57,6 +56,10 @@ public function postUpdate(IODeviceChannel $channel, PostUpdateEventArgs $args)
$changes->add(ChannelConfigChangeScope::ALEXA_INTEGRATION_ENABLED);
}
}
if (in_array('ocr', $changedConfigKeys)) {
$changes->add(ChannelConfigChangeScope::OCR);
unset($changedConfigKeys[array_search('ocr', $changedConfigKeys)]);
}
if ($changedConfigKeys || $relationsChanges) {
$changes->add(ChannelConfigChangeScope::JSON_BASIC);
}
Expand All @@ -65,19 +68,17 @@ public function postUpdate(IODeviceChannel $channel, PostUpdateEventArgs $args)
$changes->add(ChannelConfigChangeScope::ICON);
}
$this->suplaServer->channelConfigChanged($channel, $changes->getValue());
$this->reconnectUserIfNeeded($channel, $changes, $changedConfigKeys);
$this->reconnectUserIfNeeded($channel, $changes);
}

private function reconnectUserIfNeeded(IODeviceChannel $channel, PrzemekBitsBuilder $changes, array $changedConfigKeys): void {
$keysThatDoesNotTriggerReconnect = ['ocr'];
private function reconnectUserIfNeeded(IODeviceChannel $channel, PrzemekBitsBuilder $changes): void {
$typesThatDoesNotTriggerReconnect = [
ChannelType::HVAC,
ChannelType::GENERAL_PURPOSE_MEASUREMENT,
ChannelType::GENERAL_PURPOSE_METER,
];
$changedConfigKeys = array_diff($changedConfigKeys, $keysThatDoesNotTriggerReconnect);
$onlyConfigChanged = $changes->getValue() === ChannelConfigChangeScope::JSON_BASIC;
if (!in_array($channel->getType()->getId(), $typesThatDoesNotTriggerReconnect) && ($changedConfigKeys || !$onlyConfigChanged)) {
$onlyOcrChanged = $changes->getValue() === ChannelConfigChangeScope::OCR;
if (!in_array($channel->getType()->getId(), $typesThatDoesNotTriggerReconnect) && !$onlyOcrChanged) {
$this->suplaServer->reconnect($channel->getUser());
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/SuplaBundle/Enums/ChannelConfigChangeScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @method static ChannelConfigChangeScope JSON_ALT_WEEKLY_SCHEDULE()
* @method static ChannelConfigChangeScope RELATIONS()
* @method static ChannelConfigChangeScope ALEXA_INTEGRATION_ENABLED()
* @method static ChannelConfigChangeScope OCR()
*/
final class ChannelConfigChangeScope extends ChannelFunctionBits {
const CHANNEL_FUNCTION = 0x1;
Expand All @@ -40,4 +41,5 @@ final class ChannelConfigChangeScope extends ChannelFunctionBits {
const JSON_ALT_WEEKLY_SCHEDULE = 0x80;
const RELATIONS = 0x100;
const ALEXA_INTEGRATION_ENABLED = 0x200;
const OCR = 0x400;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testUpdatingOcrSettings() {
'USER-ON-CHANNEL-CONFIG-CHANGED:1,%d,%d,5010,330,%d',
$this->device->getId(),
$this->counter->getId(),
ChannelConfigChangeScope::JSON_BASIC
ChannelConfigChangeScope::OCR
));
}

Expand Down

0 comments on commit 6aa999e

Please sign in to comment.