From 6aa999e2ea6d144239fde7064b578dc1d42210cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Fr=C4=85cz?= Date: Thu, 29 Aug 2024 22:45:57 +0200 Subject: [PATCH] Introduce OCR channel config change scope --- .../Listeners/IODeviceChannelEntityListener.php | 15 ++++++++------- .../Enums/ChannelConfigChangeScope.php | 2 ++ .../Integration/Controller/OcrIntegrationTest.php | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/SuplaBundle/Entity/Main/Listeners/IODeviceChannelEntityListener.php b/src/SuplaBundle/Entity/Main/Listeners/IODeviceChannelEntityListener.php index 464835fe2..1af11b876 100644 --- a/src/SuplaBundle/Entity/Main/Listeners/IODeviceChannelEntityListener.php +++ b/src/SuplaBundle/Entity/Main/Listeners/IODeviceChannelEntityListener.php @@ -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); @@ -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); } @@ -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()); } } diff --git a/src/SuplaBundle/Enums/ChannelConfigChangeScope.php b/src/SuplaBundle/Enums/ChannelConfigChangeScope.php index d5bd9d5fc..c527a266d 100644 --- a/src/SuplaBundle/Enums/ChannelConfigChangeScope.php +++ b/src/SuplaBundle/Enums/ChannelConfigChangeScope.php @@ -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; @@ -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; } diff --git a/src/SuplaBundle/Tests/Integration/Controller/OcrIntegrationTest.php b/src/SuplaBundle/Tests/Integration/Controller/OcrIntegrationTest.php index ada21248c..add9b1ed3 100644 --- a/src/SuplaBundle/Tests/Integration/Controller/OcrIntegrationTest.php +++ b/src/SuplaBundle/Tests/Integration/Controller/OcrIntegrationTest.php @@ -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 )); }