Browse Source

refactored DefaultDeviceStateService, added the field useTelemetry(need for saving attributes, if it true, attributes will be save like a telemetry)

pull/2188/head
YevhenBondarenko 7 years ago
committed by Andrew Shvayka
parent
commit
50b89d774c
  1. 54
      application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java
  2. 1
      application/src/main/resources/thingsboard.yml

54
application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java

@ -30,7 +30,6 @@ 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.thingsboard.rule.engine.api.RpcError;
import org.thingsboard.server.actors.service.ActorService;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.Device;
@ -38,8 +37,10 @@ import org.thingsboard.server.common.data.Tenant;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.BooleanDataEntry;
import org.thingsboard.server.common.data.kv.LongDataEntry;
import org.thingsboard.server.common.data.page.TextPageLink;
import org.thingsboard.server.common.data.plugin.ComponentLifecycleState;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData;
@ -52,7 +53,6 @@ import org.thingsboard.server.dao.tenant.TenantService;
import org.thingsboard.server.gen.cluster.ClusterAPIProtos;
import org.thingsboard.server.service.cluster.routing.ClusterRoutingService;
import org.thingsboard.server.service.cluster.rpc.ClusterRpcService;
import org.thingsboard.server.service.rpc.FromDeviceRpcResponse;
import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService;
import javax.annotation.Nullable;
@ -60,6 +60,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
@ -124,6 +125,10 @@ public class DefaultDeviceStateService implements DeviceStateService {
@Getter
private long defaultStateCheckIntervalInSec;
@Value("${state.useTelemetry}")
@Getter
private boolean useTelemetry;
// TODO in v2.1
// @Value("${state.defaultStatePersistenceIntervalInSec}")
// @Getter
@ -277,8 +282,8 @@ public class DefaultDeviceStateService implements DeviceStateService {
if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime())) {
state.setLastInactivityAlarmTime(ts);
pushRuleEngineMessage(stateData, INACTIVITY_EVENT);
saveAttribute(deviceId, INACTIVITY_ALARM_TIME, ts);
saveAttribute(deviceId, ACTIVITY_STATE, state.isActive());
save(deviceId, INACTIVITY_ALARM_TIME, ts);
save(deviceId, ACTIVITY_STATE, state.isActive());
}
}
}
@ -289,7 +294,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
long ts = System.currentTimeMillis();
stateData.getState().setLastConnectTime(ts);
pushRuleEngineMessage(stateData, CONNECT_EVENT);
saveAttribute(deviceId, LAST_CONNECT_TIME, ts);
save(deviceId, LAST_CONNECT_TIME, ts);
}
}
@ -299,7 +304,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
long ts = System.currentTimeMillis();
stateData.getState().setLastDisconnectTime(ts);
pushRuleEngineMessage(stateData, DISCONNECT_EVENT);
saveAttribute(deviceId, LAST_DISCONNECT_TIME, ts);
save(deviceId, LAST_DISCONNECT_TIME, ts);
}
}
@ -308,11 +313,14 @@ public class DefaultDeviceStateService implements DeviceStateService {
if (stateData != null) {
DeviceState state = stateData.getState();
long ts = System.currentTimeMillis();
state.setActive(true);
stateData.getState().setLastActivityTime(ts);
pushRuleEngineMessage(stateData, ACTIVITY_EVENT);
saveAttribute(deviceId, LAST_ACTIVITY_TIME, ts);
saveAttribute(deviceId, ACTIVITY_STATE, state.isActive());
save(deviceId, LAST_ACTIVITY_TIME, ts);
if (!state.isActive()) {
state.setActive(true);
save(deviceId, ACTIVITY_STATE, state.isActive());
}
}
}
@ -345,7 +353,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
boolean oldActive = state.isActive();
state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout());
if (!oldActive && state.isActive() || oldActive && !state.isActive()) {
saveAttribute(deviceId, ACTIVITY_STATE, state.isActive());
save(deviceId, ACTIVITY_STATE, state.isActive());
}
}
}
@ -464,12 +472,28 @@ public class DefaultDeviceStateService implements DeviceStateService {
}
}
private void saveAttribute(DeviceId deviceId, String key, long value) {
tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback(deviceId, key, value));
private void save(DeviceId deviceId, String key, long value) {
if (useTelemetry) {
tsSubService.saveAndNotify(
TenantId.SYS_TENANT_ID, deviceId,
DataConstants.SERVER_SCOPE,
Collections.singletonList(new BaseAttributeKvEntry(new LongDataEntry(key, value), System.currentTimeMillis())),
new AttributeSaveCallback(deviceId, key, value));
} else {
tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback(deviceId, key, value));
}
}
private void saveAttribute(DeviceId deviceId, String key, boolean value) {
tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback(deviceId, key, value));
private void save(DeviceId deviceId, String key, boolean value) {
if (useTelemetry) {
tsSubService.saveAndNotify(
TenantId.SYS_TENANT_ID, deviceId,
DataConstants.SERVER_SCOPE,
Collections.singletonList(new BaseAttributeKvEntry(new BooleanDataEntry(key, value), System.currentTimeMillis())),
new AttributeSaveCallback(deviceId, key, value));
} else {
tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback(deviceId, key, value));
}
}
private class AttributeSaveCallback implements FutureCallback<Void> {

1
application/src/main/resources/thingsboard.yml

@ -403,6 +403,7 @@ audit-log:
state:
defaultInactivityTimeoutInSec: "${DEFAULT_INACTIVITY_TIMEOUT:10}"
defaultStateCheckIntervalInSec: "${DEFAULT_STATE_CHECK_INTERVAL:10}"
useTelemetry: "${USE_TELEMETRY:true}"
kafka:
enabled: true

Loading…
Cancel
Save