-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIotSensorRule.java
122 lines (98 loc) · 3.18 KB
/
IotSensorRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package kafkastreams.iot.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.function.Function;
public class IotSensorRule {
@JsonProperty("ruleName")
String ruleName;
@JsonProperty("metricName")
String metricName;
@JsonProperty("metricValue")
Integer metricValue;
Function<Integer, Boolean> conditionEval;
@JsonProperty("condition")
String condition;
@JsonProperty("controlAction")
String controlAction;
@JsonCreator
public IotSensorRule(
@JsonProperty("ruleName") String ruleName,
@JsonProperty("metricName") String metricName,
@JsonProperty("metricValue") Integer metricValue,
@JsonProperty("condition") String condition,
@JsonProperty("controlAction") String controlAction
) {
this.ruleName = ruleName;
this.metricName = metricName;
this.metricValue = metricValue;
this.condition = condition;
if (condition.equals("==")) {
conditionEval = (value) -> value == metricValue;
} else if (condition.equals(">")) {
conditionEval = (value) -> value > metricValue;
} else if (condition.equals("<")) {
conditionEval = (value) -> value < metricValue;
} else if (condition.equals(">=")) {
conditionEval = (value) -> value >= metricValue;
} else if (condition.equals("<=")) {
conditionEval = (value) -> value <= metricValue;
} else if (condition.equals("!=")) {
conditionEval = (value) -> value != metricValue;
} else {
conditionEval = (value) -> false;
}
this.controlAction = controlAction;
}
public Boolean eval(Integer value) {
return conditionEval.apply(value);
}
@JsonProperty
public String getMetricName() {
return metricName;
}
@JsonProperty
public void setMetricName(String metricName) {
this.metricName = metricName;
}
@JsonProperty
public String getRuleName() {
return ruleName;
}
@JsonProperty
public void setRuleName(String ruleName) {
this.ruleName = ruleName;
}
@JsonProperty
public Integer getMetricValue() {
return metricValue;
}
@JsonProperty
public void setMetricValue(Integer metricValue) {
this.metricValue = metricValue;
}
@JsonProperty
public String getCondition() {
return condition;
}
@JsonProperty
public void setCondition(String condition) {
this.condition = condition;
}
@JsonProperty
public String getControlAction() {
return controlAction;
}
@JsonProperty
public void setControlAction(String controlAction) {
this.controlAction = controlAction;
}
@Override
public String toString() {
return "{" + "ruleName" + ":" + ruleName + ", " +
"metricName" + ":" + metricName + ", " +
"metricValue" + ":" + metricValue + ", " +
"condition" + ":" + condition + ", " +
"controlAction" + ":" + controlAction +
"}";
}
}