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 54bc8517f1..808065865c 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 @@ -278,17 +278,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; + } + } + }