From 7471ada0c541c174e7f5597fe50b4bfb00366df1 Mon Sep 17 00:00:00 2001 From: Andrew Shvayka Date: Tue, 26 Nov 2019 13:54:55 +0200 Subject: [PATCH] Fix device state fuction --- .../state/DefaultDeviceStateService.java | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 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 7f8ebf5057..1a1dc33bbc 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 @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import org.thingsboard.server.actors.service.ActorService; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Device; @@ -456,34 +457,40 @@ public class DefaultDeviceStateService implements DeviceStateService { @Nullable @Override public DeviceStateData apply(@Nullable List data) { - long lastActivityTime = getAttributeValue(data, LAST_ACTIVITY_TIME, 0L); - long inactivityAlarmTime = getAttributeValue(data, INACTIVITY_ALARM_TIME, 0L); - long inactivityTimeout = getAttributeValue(data, INACTIVITY_TIMEOUT, TimeUnit.SECONDS.toMillis(defaultInactivityTimeoutInSec)); - boolean active = System.currentTimeMillis() < lastActivityTime + inactivityTimeout; - DeviceState deviceState = DeviceState.builder() - .active(active) - .lastConnectTime(getAttributeValue(data, LAST_CONNECT_TIME, 0L)) - .lastDisconnectTime(getAttributeValue(data, LAST_DISCONNECT_TIME, 0L)) - .lastActivityTime(lastActivityTime) - .lastInactivityAlarmTime(inactivityAlarmTime) - .inactivityTimeout(inactivityTimeout) - .build(); - TbMsgMetaData md = new TbMsgMetaData(); - md.putValue("deviceName", device.getName()); - md.putValue("deviceType", device.getType()); - return DeviceStateData.builder() - .tenantId(device.getTenantId()) - .deviceId(device.getId()) - .metaData(md) - .state(deviceState).build(); + try { + long lastActivityTime = getEntryValue(data, LAST_ACTIVITY_TIME, 0L); + long inactivityAlarmTime = getEntryValue(data, INACTIVITY_ALARM_TIME, 0L); + long inactivityTimeout = getEntryValue(data, INACTIVITY_TIMEOUT, TimeUnit.SECONDS.toMillis(defaultInactivityTimeoutInSec)); + boolean active = System.currentTimeMillis() < lastActivityTime + inactivityTimeout; + DeviceState deviceState = DeviceState.builder() + .active(active) + .lastConnectTime(getEntryValue(data, LAST_CONNECT_TIME, 0L)) + .lastDisconnectTime(getEntryValue(data, LAST_DISCONNECT_TIME, 0L)) + .lastActivityTime(lastActivityTime) + .lastInactivityAlarmTime(inactivityAlarmTime) + .inactivityTimeout(inactivityTimeout) + .build(); + TbMsgMetaData md = new TbMsgMetaData(); + md.putValue("deviceName", device.getName()); + md.putValue("deviceType", device.getType()); + return DeviceStateData.builder() + .tenantId(device.getTenantId()) + .deviceId(device.getId()) + .metaData(md) + .state(deviceState).build(); + } catch (Exception e) { + log.warn("[{}] Failed to fetch device state data", device.getId(), e); + } } }; } - private long getAttributeValue(List attributes, String attributeName, long defaultValue) { - for (KvEntry attribute : attributes) { - if (attribute.getKey().equals(attributeName)) { - return attribute.getLongValue().orElse(defaultValue); + private long getEntryValue(List kvEntries, String attributeName, long defaultValue) { + if (kvEntries != null) { + for (KvEntry entry : kvEntries) { + if (entry != null && !StringUtils.isEmpty(entry.getKey()) && entry.getKey().equals(attributeName)) { + return entry.getLongValue().orElse(defaultValue); + } } } return defaultValue;