Browse Source

Process alarms on activity and inactivity events when device state is persisted to telemetry

pull/4104/head
Viacheslav Kukhtyn 6 years ago
committed by Andrew Shvayka
parent
commit
c4b1f9ea7a
  1. 2
      application/src/main/java/org/thingsboard/server/service/state/DefaultDeviceStateService.java
  2. 1
      common/data/src/main/java/org/thingsboard/server/common/data/DataConstants.java
  3. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/DeviceState.java

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

@ -207,6 +207,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
state.setActive(true); state.setActive(true);
save(deviceId, ACTIVITY_STATE, state.isActive()); save(deviceId, ACTIVITY_STATE, state.isActive());
stateData.getMetaData().putValue("scope", SERVER_SCOPE); stateData.getMetaData().putValue("scope", SERVER_SCOPE);
stateData.getMetaData().putValue(DataConstants.PERSIST_STATE_TO_TELEMETRY, String.valueOf(persistToTelemetry));
pushRuleEngineMessage(stateData, ACTIVITY_EVENT); pushRuleEngineMessage(stateData, ACTIVITY_EVENT);
} }
} }
@ -385,6 +386,7 @@ public class DefaultDeviceStateService implements DeviceStateService {
state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout()); state.setActive(ts < state.getLastActivityTime() + state.getInactivityTimeout());
if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime()) && stateData.getDeviceCreationTime() + state.getInactivityTimeout() < ts) { if (!state.isActive() && (state.getLastInactivityAlarmTime() == 0L || state.getLastInactivityAlarmTime() < state.getLastActivityTime()) && stateData.getDeviceCreationTime() + state.getInactivityTimeout() < ts) {
state.setLastInactivityAlarmTime(ts); state.setLastInactivityAlarmTime(ts);
stateData.getMetaData().putValue(DataConstants.PERSIST_STATE_TO_TELEMETRY, String.valueOf(persistToTelemetry));
pushRuleEngineMessage(stateData, INACTIVITY_EVENT); pushRuleEngineMessage(stateData, INACTIVITY_EVENT);
save(deviceId, INACTIVITY_ALARM_TIME, ts); save(deviceId, INACTIVITY_ALARM_TIME, ts);
save(deviceId, ACTIVITY_STATE, state.isActive()); save(deviceId, ACTIVITY_STATE, state.isActive());

1
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 CONNECT_EVENT = "CONNECT_EVENT";
public static final String DISCONNECT_EVENT = "DISCONNECT_EVENT"; public static final String DISCONNECT_EVENT = "DISCONNECT_EVENT";
public static final String ACTIVITY_EVENT = "ACTIVITY_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_CREATED = "ENTITY_CREATED";
public static final String ENTITY_UPDATED = "ENTITY_UPDATED"; public static final String ENTITY_UPDATED = "ENTITY_UPDATED";

13
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 { private boolean processDeviceActivityEvent(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException {
//TODO: Add handling a case when device state is saved in telemetry String deviceStateIsPersistedToTelemetry = msg.getMetaData().getValue(DataConstants.PERSIST_STATE_TO_TELEMETRY);
return processAttributesUpdate(ctx, msg, msg.getMetaData().getValue("scope")); 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) { private boolean processAlarmClearNotification(TbContext ctx, TbMsg msg) {
@ -192,7 +195,7 @@ class DeviceState {
if (StringUtils.isEmpty(scope)) { if (StringUtils.isEmpty(scope)) {
scope = DataConstants.CLIENT_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 { 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 { 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; boolean stateChanged = false;
Set<AttributeKvEntry> attributes = JsonConverter.convertToAttributes(new JsonParser().parse(msg.getData())); Set<AttributeKvEntry> attributes = JsonConverter.convertToAttributes(new JsonParser().parse(msg.getData()));
if (!attributes.isEmpty()) { if (!attributes.isEmpty()) {

Loading…
Cancel
Save