-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileWriteRule.java
More file actions
54 lines (41 loc) · 1.73 KB
/
FileWriteRule.java
File metadata and controls
54 lines (41 loc) · 1.73 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileWriteRule extends Script {
private Logger logger = LoggerFactory.getLogger("org.openhab.automation.javascripting.writefile");
@Rule(name = "TemperatureToFileRule")
@ItemStateUpdateTrigger(id = "TemperatureTrigger", item = "Morning_Temperature")
public SimpleRule temperatureToFileRule = new SimpleRule() {
@Override
public Object execute(Action module, Map<String, ?> inputs) {
logger.info("@ItemStateUpdateTrigger Morning_Temperature");
Item item = itemRegistry.get("Morning_Temperature");
Number state = (Number) item.getState();
Path path = Paths.get("/tmp/Morning_Temperature.txt");
try {
Files.writeString(path, String.format("%f%n", state.floatValue()), StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
} catch (IOException e) {
logger.error("", e);
throw new RuntimeException(e);
}
return "";
}
};
@Override
protected Object onLoad() {
logger.info("Java onLoad()");
return null;
}
}