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 b1d47c9864..e780241d3f 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 @@ -207,6 +207,7 @@ public class DefaultDeviceStateService implements DeviceStateService { state.setActive(true); save(deviceId, ACTIVITY_STATE, state.isActive()); stateData.getMetaData().putValue("scope", SERVER_SCOPE); + stateData.getMetaData().putValue(DataConstants.PERSIST_STATE_TO_TELEMETRY, String.valueOf(persistToTelemetry)); pushRuleEngineMessage(stateData, ACTIVITY_EVENT); } } @@ -385,6 +386,7 @@ public class DefaultDeviceStateService implements DeviceStateService { state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout()); if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime()) && stateData.getDeviceCreationTime() + state.getInactivityTimeout() < ts) { state.setLastInactivityAlarmTime(ts); + stateData.getMetaData().putValue(DataConstants.PERSIST_STATE_TO_TELEMETRY, String.valueOf(persistToTelemetry)); pushRuleEngineMessage(stateData, INACTIVITY_EVENT); save(deviceId, INACTIVITY_ALARM_TIME, ts); save(deviceId, ACTIVITY_STATE, state.isActive()); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java b/common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java index 12cc17270c..f238ad25e7 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java @@ -51,6 +51,7 @@ public class DataConstants { public static final String CONNECT_EVENT = "CONNECT_EVENT"; public static final String DISCONNECT_EVENT = "DISCONNECT_EVENT"; public static final String ACTIVITY_EVENT = "ACTIVITY_EVENT"; + public static final String PERSIST_STATE_TO_TELEMETRY = "persistStateToTelemetry"; public static final String ENTITY_CREATED = "ENTITY_CREATED"; public static final String ENTITY_UPDATED = "ENTITY_UPDATED"; diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java index a4d2a2269f..b5f0674993 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java @@ -161,8 +161,11 @@ class DeviceState { } private boolean processDeviceActivityEvent(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException { - //TODO: Add handling a case when device state is saved in telemetry - return processAttributesUpdate(ctx, msg, msg.getMetaData().getValue("scope")); + String deviceStateIsPersistedToTelemetry = msg.getMetaData().getValue(DataConstants.PERSIST_STATE_TO_TELEMETRY); + if (Boolean.TRUE.toString().equals(deviceStateIsPersistedToTelemetry)) { + return processTelemetry(ctx, msg); + } + return processAttributes(ctx, msg, msg.getMetaData().getValue("scope")); } private boolean processAlarmClearNotification(TbContext ctx, TbMsg msg) { @@ -192,7 +195,7 @@ class DeviceState { if (StringUtils.isEmpty(scope)) { scope = DataConstants.CLIENT_SCOPE; } - return processAttributesUpdate(ctx, msg, scope); + return processAttributes(ctx, msg, scope); } private boolean processAttributesDeleteNotification(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException { @@ -217,10 +220,10 @@ class DeviceState { } protected boolean processAttributesUpdateRequest(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException { - return processAttributesUpdate(ctx, msg, DataConstants.CLIENT_SCOPE); + return processAttributes(ctx, msg, DataConstants.CLIENT_SCOPE); } - private boolean processAttributesUpdate(TbContext ctx, TbMsg msg, String scope) throws ExecutionException, InterruptedException { + private boolean processAttributes(TbContext ctx, TbMsg msg, String scope) throws ExecutionException, InterruptedException { boolean stateChanged = false; Set attributes = JsonConverter.convertToAttributes(new JsonParser().parse(msg.getData())); if (!attributes.isEmpty()) {