Fix device state fuction
This commit is contained in:
parent
e18582f912
commit
7471ada0c5
@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.thingsboard.server.actors.service.ActorService;
|
import org.thingsboard.server.actors.service.ActorService;
|
||||||
import org.thingsboard.server.common.data.DataConstants;
|
import org.thingsboard.server.common.data.DataConstants;
|
||||||
import org.thingsboard.server.common.data.Device;
|
import org.thingsboard.server.common.data.Device;
|
||||||
@ -456,34 +457,40 @@ public class DefaultDeviceStateService implements DeviceStateService {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public DeviceStateData apply(@Nullable List<T> data) {
|
public DeviceStateData apply(@Nullable List<T> data) {
|
||||||
long lastActivityTime = getAttributeValue(data, LAST_ACTIVITY_TIME, 0L);
|
try {
|
||||||
long inactivityAlarmTime = getAttributeValue(data, INACTIVITY_ALARM_TIME, 0L);
|
long lastActivityTime = getEntryValue(data, LAST_ACTIVITY_TIME, 0L);
|
||||||
long inactivityTimeout = getAttributeValue(data, INACTIVITY_TIMEOUT, TimeUnit.SECONDS.toMillis(defaultInactivityTimeoutInSec));
|
long inactivityAlarmTime = getEntryValue(data, INACTIVITY_ALARM_TIME, 0L);
|
||||||
boolean active = System.currentTimeMillis() < lastActivityTime + inactivityTimeout;
|
long inactivityTimeout = getEntryValue(data, INACTIVITY_TIMEOUT, TimeUnit.SECONDS.toMillis(defaultInactivityTimeoutInSec));
|
||||||
DeviceState deviceState = DeviceState.builder()
|
boolean active = System.currentTimeMillis() < lastActivityTime + inactivityTimeout;
|
||||||
.active(active)
|
DeviceState deviceState = DeviceState.builder()
|
||||||
.lastConnectTime(getAttributeValue(data, LAST_CONNECT_TIME, 0L))
|
.active(active)
|
||||||
.lastDisconnectTime(getAttributeValue(data, LAST_DISCONNECT_TIME, 0L))
|
.lastConnectTime(getEntryValue(data, LAST_CONNECT_TIME, 0L))
|
||||||
.lastActivityTime(lastActivityTime)
|
.lastDisconnectTime(getEntryValue(data, LAST_DISCONNECT_TIME, 0L))
|
||||||
.lastInactivityAlarmTime(inactivityAlarmTime)
|
.lastActivityTime(lastActivityTime)
|
||||||
.inactivityTimeout(inactivityTimeout)
|
.lastInactivityAlarmTime(inactivityAlarmTime)
|
||||||
.build();
|
.inactivityTimeout(inactivityTimeout)
|
||||||
TbMsgMetaData md = new TbMsgMetaData();
|
.build();
|
||||||
md.putValue("deviceName", device.getName());
|
TbMsgMetaData md = new TbMsgMetaData();
|
||||||
md.putValue("deviceType", device.getType());
|
md.putValue("deviceName", device.getName());
|
||||||
return DeviceStateData.builder()
|
md.putValue("deviceType", device.getType());
|
||||||
.tenantId(device.getTenantId())
|
return DeviceStateData.builder()
|
||||||
.deviceId(device.getId())
|
.tenantId(device.getTenantId())
|
||||||
.metaData(md)
|
.deviceId(device.getId())
|
||||||
.state(deviceState).build();
|
.metaData(md)
|
||||||
|
.state(deviceState).build();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("[{}] Failed to fetch device state data", device.getId(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private long getAttributeValue(List<? extends KvEntry> attributes, String attributeName, long defaultValue) {
|
private long getEntryValue(List<? extends KvEntry> kvEntries, String attributeName, long defaultValue) {
|
||||||
for (KvEntry attribute : attributes) {
|
if (kvEntries != null) {
|
||||||
if (attribute.getKey().equals(attributeName)) {
|
for (KvEntry entry : kvEntries) {
|
||||||
return attribute.getLongValue().orElse(defaultValue);
|
if (entry != null && !StringUtils.isEmpty(entry.getKey()) && entry.getKey().equals(attributeName)) {
|
||||||
|
return entry.getLongValue().orElse(defaultValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user