This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
triggers.js
152 lines (137 loc) · 5.1 KB
/
triggers.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Triggers namespace.
* This namespace allows creation of Openhab rule triggers.
*
* @namespace triggers
*/
const utils = require('./utils');
const ModuleBuilder = utils.typeWithFallback(
"org.eclipse.smarthome.automation.core.util.ModuleBuilder",
"org.openhab.core.automation.util.ModuleBuilder");
const Configuration = Java.type("org.openhab.core.config.core.Configuration");
/**
* Creates a trigger. Internal function, instead use predefined trigger types.
*
* @memberof triggers
* @private
* @param {String} typeString the type of trigger to create
* @param {String} [name] the name of the trigger
* @param {Configuration} config the trigger configuration
*/
let createTrigger = function(typeString, name, config) {
if(typeof name === 'undefined' || name === null) {
name = utils.randomUUID().toString();
}
return ModuleBuilder.createTrigger()
.withId(name)
.withTypeUID(typeString)
.withConfiguration(new Configuration(config))
.build();
}
module.exports = {
/**
* Creates a trigger that fires upon specific events in a channel.
*
* @example
* ChannelEventTrigger('astro:sun:local:rise#event', 'START')
*
* @name ChannelEventTrigger
* @memberof triggers
* @param {String} channel the name of the channel
* @param {String} event the name of the event to listen for
* @param {String} [triggerName] the name of the trigger to create
*
*/
ChannelEventTrigger: (channel, event, triggerName) => createTrigger("core.ChannelEventTrigger", triggerName, {
"channelUID": channel,
"event": event
}),
/**
* Creates a trigger that fires upon an item changing state.
*
* @example
* ItemStateChangeTrigger('my_item', 'OFF', 'ON')
*
* @name ItemStateChangeTrigger
* @memberof triggers
* @param {String} itemName the name of the item to monitor for change
* @param {String} [oldState] the previous state of the item
* @param {String} [newState] the new state of the item
* @param {String} [triggerName] the name of the trigger to create
*/
ItemStateChangeTrigger: (itemName, oldState, newState, triggerName) => createTrigger("core.ItemStateChangeTrigger", triggerName, {
"itemName": itemName,
"state": newState,
"oldState": oldState
}),
/**
* Creates a trigger that fires upon an item receiving a state update. Note that the item does not need to change state.
*
* @example
* ItemStateUpdateTrigger('my_item', 'OFF')
*
* @name ItemStateUpdateTrigger
* @memberof triggers
* @param {String} itemName the name of the item to monitor for change
* @param {String} [state] the new state of the item
* @param {String} [triggerName] the name of the trigger to create
*/
ItemStateUpdateTrigger: (itemName, state, triggerName) => createTrigger("core.ItemStateUpdateTrigger", triggerName, {
"itemName": itemName,
"state": state
}),
/**
* Creates a trigger that fires upon an item receiving a command. Note that the item does not need to change state.
*
* @example
* ItemCommandTrigger('my_item', 'OFF')
*
* @name ItemCommandTrigger
* @memberof triggers
* @param {String} itemName the name of the item to monitor for change
* @param {String} [command] the command received
* @param {String} [triggerName] the name of the trigger to create
*/
ItemCommandTrigger: (itemName, command, triggerName) => createTrigger("core.ItemCommandTrigger", triggerName, {
"itemName": itemName,
"command": command
}),
/**
* Creates a trigger that fires on a cron schedule. The supplied cron expression defines when the trigger will fire.
*
* @example
* GenericCronTrigger('0 30 16 * * ? *')
*
* @name GenericCronTrigger
* @memberof triggers
* @param {String} expression the cron expression defining the triggering schedule
*/
GenericCronTrigger: (expression, triggerName) => createTrigger("timer.GenericCronTrigger", triggerName, {
"cronExpression": expression
}),
/**
* Creates a trigger that fires daily at a specific time. The supplied time defines when the trigger will fire.
*
* @example
* TimeOfDayTrigger('19:00')
*
* @name TimeOfDayTrigger
* @memberof triggers
* @param {String} time the time expression defining the triggering schedule
*/
TimeOfDayTrigger: (time, triggerName) => createTrigger("timer.TimeOfDayTrigger", triggerName, {
"time": time
}),
/* not yet tested
ItemStateCondition: (itemName, state, triggerName) => createTrigger("core.ItemStateCondition", triggerName, {
"itemName": itemName,
"operator": "=",
"state": state
}),
GenericCompareCondition: (itemName, state, operator, triggerName) => createTrigger("core.GenericCompareCondition", triggerName, {
"itemName": itemName,
"operator": operator,// matches, ==, <, >, =<, =>
"state": state
})
*/
}