Browse Source

Refactoring for alarm rule schedule

add getDynamicValue method to AlarmSchedule interface, refactor main isActive processing methods for work with schedules and dynamic values
pull/5818/head
desoliture 5 years ago
parent
commit
46e301b2db
  1. 3
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AlarmSchedule.java
  2. 7
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AnyTimeSchedule.java
  3. 1
      common/data/src/main/java/org/thingsboard/server/common/data/device/profile/SpecificTimeSchedule.java
  4. 47
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java
  5. 15
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/ProfileState.java

3
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AlarmSchedule.java

@ -18,6 +18,7 @@ package org.thingsboard.server.common.data.device.profile;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.thingsboard.server.common.data.query.DynamicValue;
import java.io.Serializable;
@ -34,4 +35,6 @@ public interface AlarmSchedule extends Serializable {
AlarmScheduleType getType();
DynamicValue<String> getDynamicValue();
}

7
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/AnyTimeSchedule.java

@ -15,6 +15,8 @@
*/
package org.thingsboard.server.common.data.device.profile;
import org.thingsboard.server.common.data.query.DynamicValue;
public class AnyTimeSchedule implements AlarmSchedule {
@Override
@ -22,4 +24,9 @@ public class AnyTimeSchedule implements AlarmSchedule {
return AlarmScheduleType.ANY_TIME;
}
@Override
public DynamicValue<String> getDynamicValue() {
return null;
}
}

1
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/SpecificTimeSchedule.java

@ -18,7 +18,6 @@ package org.thingsboard.server.common.data.device.profile;
import lombok.Data;
import org.thingsboard.server.common.data.query.DynamicValue;
import java.util.List;
import java.util.Set;
@Data

47
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/AlarmRuleState.java

@ -27,6 +27,7 @@ import org.thingsboard.server.common.data.device.profile.AlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.AlarmConditionSpecType;
import org.thingsboard.server.common.data.device.profile.AlarmRule;
import org.thingsboard.server.common.data.device.profile.CustomTimeSchedule;
import org.thingsboard.server.common.data.device.profile.AlarmSchedule;
import org.thingsboard.server.common.data.device.profile.CustomTimeScheduleItem;
import org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec;
@ -139,35 +140,27 @@ class AlarmRuleState {
switch (alarmRule.getSchedule().getType()) {
case ANY_TIME:
return true;
case SPECIFIC_TIME: {
SpecificTimeSchedule defaultSchedule = (SpecificTimeSchedule) alarmRule.getSchedule();
EntityKeyValue dynamicValue = getDynamicValue(data, defaultSchedule.getDynamicValue());
SpecificTimeSchedule schedule;
try {
schedule = JsonConverter.parse(dynamicValue.getJsonValue(), SpecificTimeSchedule.class);
} catch (Exception e) {
schedule = defaultSchedule;
}
return isActiveSpecific(schedule, eventTs);
}
case CUSTOM: {
CustomTimeSchedule defaultSchedule = (CustomTimeSchedule) alarmRule.getSchedule();
EntityKeyValue dynamicValue = getDynamicValue(data, defaultSchedule.getDynamicValue());
case SPECIFIC_TIME:
return isActiveSpecific((SpecificTimeSchedule) getSchedule(data, alarmRule), eventTs);
case CUSTOM:
return isActiveCustom((CustomTimeSchedule) getSchedule(data, alarmRule), eventTs);
default:
throw new RuntimeException("Unsupported schedule type: " + alarmRule.getSchedule().getType());
}
}
CustomTimeSchedule schedule;
try {
schedule = JsonConverter.parse(dynamicValue.getJsonValue(), CustomTimeSchedule.class);
} catch (Exception e) {
schedule = defaultSchedule;
}
private AlarmSchedule getSchedule(DataSnapshot data, AlarmRule alarmRule) {
AlarmSchedule schedule = alarmRule.getSchedule();
EntityKeyValue dynamicValue = getDynamicValue(data, schedule.getDynamicValue());
return isActiveCustom(schedule, eventTs);
if (dynamicValue != null) {
try {
return JsonConverter.parse(dynamicValue.getJsonValue(), alarmRule.getSchedule().getClass());
} catch (Exception e) {
log.trace("Failed to parse AlarmSchedule from dynamicValue: {}", dynamicValue.getJsonValue(), e);
}
default:
throw new RuntimeException("Unsupported schedule type: " + alarmRule.getSchedule().getType());
}
return schedule;
}
private boolean isActiveSpecific(SpecificTimeSchedule schedule, long eventTs) {
@ -252,7 +245,7 @@ class AlarmRuleState {
long repeatingTimes = 0;
AlarmConditionSpec alarmConditionSpec = getSpec();
AlarmConditionSpecType specType = alarmConditionSpec.getType();
if(specType.equals(AlarmConditionSpecType.REPEATING)) {
if (specType.equals(AlarmConditionSpecType.REPEATING)) {
RepeatingAlarmConditionSpec repeating = (RepeatingAlarmConditionSpec) spec;
repeatingTimes = repeating.getPredicate().getDefaultValue();
@ -272,7 +265,7 @@ class AlarmRuleState {
long durationTimeInMs = 0;
AlarmConditionSpec alarmConditionSpec = getSpec();
AlarmConditionSpecType specType = alarmConditionSpec.getType();
if(specType.equals(AlarmConditionSpecType.DURATION)) {
if (specType.equals(AlarmConditionSpecType.DURATION)) {
DurationAlarmConditionSpec duration = (DurationAlarmConditionSpec) spec;
TimeUnit timeUnit = duration.getUnit();

15
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/profile/ProfileState.java

@ -27,8 +27,6 @@ import org.thingsboard.server.common.data.device.profile.AlarmRule;
import org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm;
import org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.CustomTimeSchedule;
import org.thingsboard.server.common.data.device.profile.SpecificTimeSchedule;
import org.thingsboard.server.common.data.device.profile.AlarmSchedule;
import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.query.ComplexFilterPredicate;
@ -99,18 +97,7 @@ class ProfileState {
}
private void addScheduleDynamicValues(AlarmSchedule schedule) {
DynamicValue<String> dynamicValue = null;
switch (schedule.getType()) {
case SPECIFIC_TIME:
SpecificTimeSchedule specificTimeSchedule = (SpecificTimeSchedule) schedule;
dynamicValue = specificTimeSchedule.getDynamicValue();
break;
case CUSTOM:
CustomTimeSchedule customTimeSchedule = (CustomTimeSchedule) schedule;
dynamicValue = customTimeSchedule.getDynamicValue();
break;
}
DynamicValue<String> dynamicValue = schedule.getDynamicValue();
if (dynamicValue != null) {
entityKeys.add(
new AlarmConditionFilterKey(AlarmConditionKeyType.ATTRIBUTE,

Loading…
Cancel
Save