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

This commit is contained in:
Viacheslav Kukhtyn 2021-02-12 12:56:16 +02:00 committed by Andrew Shvayka
parent d34d38613b
commit c4b1f9ea7a
3 changed files with 11 additions and 5 deletions

View File

@ -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());

View File

@ -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";

View File

@ -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()) {