From 0858d43759a7f92e8e36cc602856f65c58d9f432 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Thu, 4 Aug 2022 10:48:20 +0200 Subject: [PATCH] fixed update with different kv types or remove inactivityTimeout --- .../state/DefaultDeviceStateService.java | 7 ++-- .../DefaultSubscriptionManagerService.java | 39 ++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java b/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java index 55c5770d9b..7d955c99ab 100644 --- a/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java +++ b/application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java @@ -271,17 +271,16 @@ public class DefaultDeviceStateService extends AbstractPartitionBasedService kvEntries) { for (KvEntry kvEntry : kvEntries) { if (kvEntry.getKey().equals(DefaultDeviceStateService.INACTIVITY_TIMEOUT)) { - deviceStateService.onDeviceInactivityTimeoutUpdate(tenantId, new DeviceId(entityId.getId()), kvEntry.getLongValue().orElse(0L)); + deviceStateService.onDeviceInactivityTimeoutUpdate(tenantId, new DeviceId(entityId.getId()), getLongValue(kvEntry)); + } + } + } + + private void deleteDeviceInactivityTimeout(TenantId tenantId, EntityId entityId, List keys) { + for (String key : keys) { + if (key.equals(DefaultDeviceStateService.INACTIVITY_TIMEOUT)) { + deviceStateService.onDeviceInactivityTimeoutUpdate(tenantId, new DeviceId(entityId.getId()), 0); } } } @@ -340,6 +348,9 @@ public class DefaultSubscriptionManagerService extends TbApplicationEventListene } return subscriptionUpdate; }, false); + if (entityId.getEntityType() == EntityType.DEVICE) { + deleteDeviceInactivityTimeout(tenantId, entityId, keys); + } callback.onSuccess(); } @@ -364,6 +375,9 @@ public class DefaultSubscriptionManagerService extends TbApplicationEventListene } return subscriptionUpdate; }, false); + if (entityId.getEntityType() == EntityType.DEVICE) { + deleteDeviceInactivityTimeout(tenantId, entityId, keys); + } callback.onSuccess(); } @@ -557,4 +571,27 @@ public class DefaultSubscriptionManagerService extends TbApplicationEventListene return new TbProtoQueueMsg<>(subscription.getEntityId().getId(), toCoreMsg); } + private static long getLongValue(KvEntry kve) { + switch (kve.getDataType()) { + case LONG: + return kve.getLongValue().orElse(0L); + case DOUBLE: + return kve.getDoubleValue().orElse(0.0).longValue(); + case STRING: + try { + return Long.parseLong(kve.getStrValue().orElse("0")); + } catch (NumberFormatException e) { + return 0L; + } + case JSON: + try { + return Long.parseLong(kve.getJsonValue().orElse("0")); + } catch (NumberFormatException e) { + return 0L; + } + default: + return 0L; + } + } + }