-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJsonRule.java
More file actions
125 lines (87 loc) · 3.52 KB
/
JsonRule.java
File metadata and controls
125 lines (87 loc) · 3.52 KB
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
123
124
125
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openhab.automation.javascripting.annotations.ItemStateUpdateTrigger;
import org.openhab.automation.javascripting.annotations.Rule;
import org.openhab.automation.javascripting.scriptsupport.Script;
import org.openhab.core.automation.Action;
import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule;
import org.openhab.core.items.Item;
import org.openhab.core.library.types.DecimalType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
public class JsonRule extends Script {
private Logger logger = LoggerFactory.getLogger("org.openhab.automation.javascripting.jsonrule");
final String outsideTemperatureItem = "OutsideTemperature";
final String salonTemperatureItem = "SalonTemperature";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Rule(name = "JsonRule")
@ItemStateUpdateTrigger(id = "OutsideTemperatureTrigger", item = outsideTemperatureItem)
@ItemStateUpdateTrigger(id = "SalonTemperatureTrigger", item = salonTemperatureItem)
public SimpleRule jsonrule = new SimpleRule() {
@Override
public Object execute(Action module, Map<String, ?> inputs) {
logger.info("Java jsonrule execute: {}", inputs.toString());
try {
String json = createJson();
logger.info("json: {}", json);
Item item = itemRegistry.get("EPaper_Screen_Json");
events.sendCommand(item, json);
} catch (Exception e) {
logger.error(inputs.toString(), e);
throw e;
}
return "";
}
};
@Override
protected Object onLoad() {
logger.info("Java onLoad()");
return null;
};
private String createJson() {
Map<String, List<Map<String, Object>>> screen = new HashMap<>();
List<Map<String, Object>> ops = new ArrayList<>();
int x = 10;
int y = 0;
LocalDateTime ldt = LocalDateTime.now();
String s = ldt.format(dateTimeFormatter);
Map<String, Object> text0 = new HashMap<>();
text0.put("type", "text");
text0.put("x", x);
text0.put("y", y);
text0.put("text", String.format("@ %s", s));
ops.add(text0);
y += 30;
DecimalType dt = itemRegistry.get(outsideTemperatureItem).getStateAs(DecimalType.class);
if (dt != null) {
float f = dt.floatValue();
Map<String, Object> text1 = new HashMap<>();
text1.put("type", "text");
text1.put("x", x);
text1.put("y", y);
text1.put("text", String.format("Temp Outside: %.1f", f));
ops.add(text1);
y += 30;
}
DecimalType dts = itemRegistry.get(salonTemperatureItem).getStateAs(DecimalType.class);
if (dts != null) {
float f = dts.floatValue();
Map<String, Object> text1 = new HashMap<>();
text1.put("type", "text");
text1.put("x", x);
text1.put("y", y);
text1.put("text", String.format("Temp Salon: %.1f", f));
ops.add(text1);
y += 30;
}
screen.put("screenobjects", ops);
Gson gson = new Gson();
String output = gson.toJson(screen);
return output;
}
}