-
Notifications
You must be signed in to change notification settings - Fork 5
/
SimpleRuleExamples.js
125 lines (104 loc) · 4.32 KB
/
SimpleRuleExamples.js
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
/**
* Copyright (c) 2018 by Helmut Lehmeyer.
*
* @author Helmut Lehmeyer
*/
'use strict';
var OPENHAB_CONF = Java.type("java.lang.System").getenv("OPENHAB_CONF"); // most this is /etc/openhab2
load(OPENHAB_CONF+'/automation/jsr223/jslib/JSRule.js');
logInfo("################# SimpleRuleExamples.js ##################", TimerTrigger);
//### Example 1: Default spelling, not simplified
var xRule = new SimpleRule(){
execute: function( module, input){
logInfo(" ################ xRule Line: "+__LINE__+" #################");
logInfo(" xRule::execute "+__LINE__, " input "+ input, " module "+ module, " uuid "+ uuid.randomUUID());
for( var i in input){
var ai = input[i];
logInfo(" -- input "+i +" = "+ ai);
}
}
};
xRule.setTriggers([
TimerTrigger("0/15 * * * * ?")
]);
//Enable/Disable Rule:
automationManager.addRule(xRule);
//logInfo(" -- getUID "+xRule.getUID());
//logInfo(" -- getUID "+automationManager.addRule(xRule).getUID());
//### Example 2: More backward compatible spelling
JSRule({
getEventTrigger: function(){
return [
new TimerTrigger("0/5 * * * * ?")//Enable/Disable Rule
]
},
execute: function( module, input){
logInfo(" ################ yRule Line: "+__LINE__+" #################");
logInfo(" yRule::execute "+__LINE__, " input "+ input, " module "+ module);
}
});
//### Example 3: Simplest spelling
JSRule({
triggers: [
TimerTrigger("0/5 * * * * ?")//Enable/Disable Rule
],
execute: function( module, input){
logInfo(" ################ zRule Line: "+__LINE__+" #################");
}
});
//### Example 4: Most simple spelling live.
JSRule({
name: "Example 4",
description: "Most simple spelling live",
triggers: [ //Enable/Disable Rule
//NOT Working: ShutDown()
//NOT Working: StartupTrigger()
//stateCondition("testItemSwitch", "ON", "cond1") //Error: Can not create new object with constructor org.eclipse.smarthome.automation.Condition with the passed arguments; they do not match any of its method signatures.
//IS WORKING: TimerTrigger("0/15 * * * * ?")
//IS WORKING: new TimerTrigger("0/15 * * * * ?")
//TimerTrigger("0/15 * * * * ?")
//StartupTrigger()
//IS WORKING: ChangedEventTrigger("testItemSwitch", "ON", "OFF")
//IS WORKING: UpdatedEventTrigger("testItemSwitch"),
//IS WORKING: CommandEventTrigger("testItemSwitch")
],
execute: function( module, input){
logInfo(" ################ zRule Line: "+__LINE__+" #################");
logInfo(" zRule::execute "+__LINE__, " input "+ input, " module "+ module);
logInfo("uuid "+__LINE__, uuid.randomUUID());
//Logging
logInfo("log "+__LINE__, "logInfo");
logWarn("log "+__LINE__, "logWarn");
logDebug("log "+__LINE__, "logDebug");
logTrace("log "+__LINE__, "logTrace");
var act = getActions();
for( var i in act){
var ai = act[i];
logInfo(" -- service "+i +" = "+ ai);
}
var testItemSwitch = updateIfUninitialized('testItemSwitch', OFF);
logInfo("testItemSwitch "+__LINE__, "testItemSwitch.state = " + testItemSwitch.state);
sendCommand("testItemSwitch", ON);
postUpdate("testItemSwitch", OFF);
//Java 8
logInfo(" -#### LocalDateTime.now().withMinute(0) "+__LINE__, LocalDateTime.now().withMinute(0));
//Java 7
logInfo(" -#### DateTime.now().withMinute(0) "+__LINE__, DateTime.now().withMinuteOfHour(0));
// Run Timer
var runme = function(){logInfo(" runme ", "Timer has been executed at "+DateTime.now());};
createTimer(now().plusSeconds(5), runme);
//Action Examples
//getAction("XMPP").static.sendXMPP("[email protected]", "automation XMPP :-)");
//getAction("Mail").static.sendMail("[email protected]", "automation Mail :-)", "It works!");
//using helper.js:
//sendXMPP("[email protected]", "automation XMPP :-)");
//sendMail("[email protected]", "automation Mail :-)", "It works!");
logInfo(" -- getTriggers ", xRule.getTriggers());
logInfo(" -- getConditions ", xRule.getConditions());
logInfo(" -- getActions ", xRule.getActions());
logInfo(" -- getConfigurationDescriptions ", xRule.getConfigurationDescriptions());
logInfo(" -- getConfiguration ", xRule.getConfiguration());
logInfo(" -- getTemplateUID ", xRule.getTemplateUID());
logInfo(" -- getVisibility ", xRule.getVisibility());
}
});