forked from openhab/openhab-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
429 lines (407 loc) · 18 KB
/
actions.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* eslint-disable no-irregular-whitespace */
/**
* Actions namespace.
*
* This namespace provides access to openHAB actions. {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/package-summary.html All available actions} can be accessed as direct properties of this
* object (via their simple class name).
*
* Additional actions provided by user installed addons can be accessed using their common name on the actions name space if the addon exports them in a proper way.
*
* @namespace actions
* @example <caption>Sends a broadcast notification</caption>
* const { actions } = require('openhab');
* actions.NotificationAction.sendBroadcastNotification("Hello World!")
*/
/** @typedef {import('@js-joda/core').ZonedDateTime} time.ZonedDateTime */
const typeOfArguments = require('./typeOfArguments');
const osgi = require('./osgi');
// See https://github.com/openhab/openhab-core/blob/main/bundles/org.openhab.core.automation.module.script/src/main/java/org/openhab/core/automation/module/script/internal/defaultscope/ScriptThingActionsImpl.java
const { actions } = require('@runtime/Defaults');
const log = require('./log')('actions');
const Things = Java.type('org.openhab.core.model.script.actions.Things');
const actionServices = osgi.findServices('org.openhab.core.model.script.engine.action.ActionService', null) || [];
const JavaScriptExecution = Java.type('org.openhab.core.model.script.actions.ScriptExecution');
const JavaTransformation = Java.type('org.openhab.core.transform.actions.Transformation');
// Dynamically export all found actions
const dynamicExports = {};
actionServices.forEach(function (item) {
try {
// if an action fails to activate, then warn and continue so that other actions are available
dynamicExports[item.getActionClass().getSimpleName()] = item.getActionClass().static;
log.debug('Successfully activated action {} as {}', item, item.getActionClass().getSimpleName());
} catch (e) {
log.warn('Failed to activate action {} due to {}', item, e);
}
});
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/audio Audio} Actions
*
* The static methods of this class are made available as functions in the scripts. This allows a script to use audio features.
*
* @example
* Audio.decreaseMasterVolume(float percent)
* Audio.getMasterVolume()
* Audio.increaseMasterVolume(float percent)
* Audio.playSound(String filename)
* Audio.playSound(String sink, String filename)
* Audio.playSound(String sink, String filename, PercentType volume)
* Audio.playSound(String filename, PercentType volume)
* Audio.playStream(String url)
* Audio.playStream(String sink, String url)
* Audio.setMasterVolume(float volume)
* Audio.setMasterVolume(PercentType percent)
* @example
* Audio.decreaseMasterVolume(1.0)
*
* @name Audio
* @memberof actions
*/
const Audio = Java.type('org.openhab.core.model.script.actions.Audio');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/busevent BusEvent} Actions
*
* The static methods of this class are made available as functions in the scripts. This gives direct write access to the openHAB event bus from within scripts. Items should not be updated directly (setting the state property), but updates should be sent to the bus, so that all interested bundles are notified.
* @example
* BusEvent.postUpdate(String itemName, String stateString)
* BusEvent.postUpdate(Item item, Number state)
* BusEvent.postUpdate(Item item, String stateAsString)
* BusEvent.postUpdate(Item item, State state)
* BusEvent.restoreStates(Map<Item,State> statesMap)
* BusEvent.sendCommand(String itemName, String commandString)
* BusEvent.sendCommand(Item item, Number number)
* BusEvent.sendCommand(Item item, String commandString)
* BusEvent.sendCommand(Item item, Command command)
* BusEvent.storeStates(Item... items)
*
* @name BusEvent
* @memberof actions
*/
const BusEvent = Java.type('org.openhab.core.model.script.actions.BusEvent');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/ephemeris Ephemeris} Actions
*
* The static methods of this class are made available as functions in the scripts. This allows a script to use ephemeris features.
* @example
* Ephemeris.getBankHolidayName()
* Ephemeris.getBankHolidayName(int offset)
* Ephemeris.getBankHolidayName(int offset, String filename)
* Ephemeris.getBankHolidayName(String filename)
* Ephemeris.getBankHolidayName(ZonedDateTime day)
* Ephemeris.getBankHolidayName(ZonedDateTime day, String filename)
* Ephemeris.getDaysUntil(String searchedHoliday)
* Ephemeris.getDaysUntil(String searchedHoliday, String filename)
* Ephemeris.getDaysUntil(ZonedDateTime day, String searchedHoliday)
* Ephemeris.getDaysUntil(ZonedDateTime day, String searchedHoliday, String filename)
* Ephemeris.getHolidayDescription(@Nullable String holiday)
* Ephemeris.getNextBankHoliday()
* Ephemeris.getNextBankHoliday(int offset)
* Ephemeris.getNextBankHoliday(int offset, String filename)
* Ephemeris.getNextBankHoliday(String filename)
* Ephemeris.getNextBankHoliday(ZonedDateTime day)
* Ephemeris.getNextBankHoliday(ZonedDateTime day, String filename)
* Ephemeris.isBankHoliday()
* Ephemeris.isBankHoliday(int offset)
* Ephemeris.isBankHoliday(int offset, String filename)
* Ephemeris.isBankHoliday(String filename)
* Ephemeris.isBankHoliday(ZonedDateTime day)
* Ephemeris.isBankHoliday(ZonedDateTime day, String filename)
* Ephemeris.isInDayset(String daysetName)
* Ephemeris.isInDayset(String daysetName, int offset)
* Ephemeris.isInDayset(String daysetName, ZonedDateTime day)
* Ephemeris.isWeekend()
* Ephemeris.isWeekend(int offset)
* Ephemeris.isWeekend(ZonedDateTime day)
*
* @name Ephemeris
* @memberof actions
*/
const Ephemeris = Java.type('org.openhab.core.model.script.actions.Ephemeris');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/exec Exec} Actions
*
* This class provides static methods that can be used in automation rules for executing commands on command line.
*
* @example
* Exec.executeCommandLine(String... commandLine)
* Exec.executeCommandLine(Duration timeout, String... commandLine)
*
* @name Exec
* @memberof actions
*/
const Exec = Java.type('org.openhab.core.model.script.actions.Exec');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/HTTP.html HTTP} Actions
*
* This class provides static methods that can be used in automation rules for sending HTTP requests
*
* @example
* HTTP.sendHttpDeleteRequest(String url)
* HTTP.sendHttpDeleteRequest(String url, int timeout)
* HTTP.sendHttpDeleteRequest(String url, Map<String,String> headers, int timeout)
* HTTP.sendHttpGetRequest(String url)
* HTTP.sendHttpGetRequest(String url, int timeout)
* HTTP.sendHttpGetRequest(String url, Map<String,String> headers, int timeout)
* HTTP.sendHttpPostRequest(String url)
* HTTP.sendHttpPostRequest(String url, int timeout)
* HTTP.sendHttpPostRequest(String url, String contentType, String content)
* HTTP.sendHttpPostRequest(String url, String contentType, String content, int timeout)
* HTTP.sendHttpPostRequest(String url, String contentType, String content, Map<String,String> headers, int timeout)
* HTTP.sendHttpPutRequest(String url)
* HTTP.sendHttpPutRequest(String url, int timeout)
* HTTP.sendHttpPutRequest(String url, String contentType, String content)
* HTTP.sendHttpPutRequest(String url, String contentType, String content, int timeout)
* HTTP.sendHttpPutRequest(String url, String contentType, String content, Map<String,String> headers, int timeout)
*
* @name HTTP
* @memberof actions
*/
const HTTP = Java.type('org.openhab.core.model.script.actions.HTTP');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/Log.html Log} Actions
*
* The static methods of this class are made available as functions in the scripts. This allows a script to log to the SLF4J-Log.
*
* @example
* Log.logDebug(String loggerName, String format, Object... args)
* Log.logError(String loggerName, String format, Object... args)
* Log.logInfo(String loggerName, String format, Object... args)
* Log.logWarn(String loggerName, String format, Object... args)
*
* @name Log
* @memberof actions
*/
const LogAction = Java.type('org.openhab.core.model.script.actions.Log');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/Ping.html Ping} Actions
*
* This Action checks the vitality of the given host.
*
* @example
* Ping.checkVitality(String host, int port, int timeout)
*
* @name Ping
* @memberof actions
*/
const Ping = Java.type('org.openhab.core.model.script.actions.Ping');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/scriptexecution ScriptExecution} Actions
*
* The static methods of this class are made available as functions in the scripts.
*
* @example
* ScriptExecution.callScript(string scriptName)
* ScriptExecution.createTimer(time.ZonedDateTime instant, function callbackFunction)
* ScriptExecution.createTimer(string identifier, time.ZonedDateTime instant, function callbackFunction)
*
* @memberof actions
* @hideconstructor
*/
class ScriptExecution {
/**
* Calls a script which must be located in the configurations/scripts folder.
*
* @param {string} scriptName the name of the script (if the name does not end with the .script file extension it is added)
*/
static callScript (scriptName) {
JavaScriptExecution.callScript(scriptName);
}
/**
* Schedules a function for later execution.
*
* @param {string} identifier an optional identifier
* @param {time.ZonedDateTime} instant the point in time when the code should be executed
* @param {function} closure the code block to execute
* @returns {*} a native openHAB Timer
*/
static createTimer (identifier, instant, closure) {
// Support method overloading as identifier is optional
if (typeof identifier === 'string' && closure != null) {
// Try to access the createTimer method of ThreadsafeTimers
try {
return ThreadsafeTimers.createTimer(identifier, instant, closure); // eslint-disable-line no-undef
} catch {
return JavaScriptExecution.createTimer(identifier, instant, closure);
}
} else {
// Try to access the createTimer method of ThreadsafeTimers
try {
return ThreadsafeTimers.createTimer(identifier, instant); // eslint-disable-line no-undef
} catch {
return JavaScriptExecution.createTimer(identifier, instant);
}
}
}
/**
* Schedules a function (with argument) for later execution
*
* @deprecated
* @param string [identifier] an optional identifier
* @param {time.ZonedDateTime} instant the point in time when the code should be executed
* @param {*} arg1 the argument to pass to the code block
* @param {function} closure the code block to execute
* @returns {*} a native openHAB Timer
*/
static createTimerWithArgument (identifier, instant, arg1, closure) {
console.warn('"createTimerWithArgument" has been deprecated and will be removed in a future release. Use "createTimer" or "setTimeout" instead.');
// Support method overloading as identifier is optional
if (typeof identifier === 'string' && closure != null) {
return JavaScriptExecution.createTimerWithArgument(identifier, instant, arg1, closure);
} else {
return JavaScriptExecution.createTimerWithArgument(identifier, instant, arg1);
}
}
}
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/Semantics.html Semantics} Actions
*
* The static methods of this class are made available as functions in the scripts. This allows a script to use Semantics features.
*
* @example
* Semantics.getEquipment(Item item)
* Semantics.getEquipmentType(Item item)
* Semantics.getLocation(Item item)
* Semantics.getLocationType(Item item)
* Semantics.getPointType(Item item)
* Semantics.getPropertyType(Item item)
* Semantics.getSemanticType(Item item)
* Semantics.isEquipment(Item item)
* Semantics.isLocation(Item item)
* Semantics.isPoint(Item item)
*
* @name Semantics
* @memberof actions
*/
const Semantics = Java.type('org.openhab.core.model.script.actions.Semantics');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/Things.html Things} Actions
*
* This class provides static methods that can be used in automation rules for getting thing's status info.
*
* @example
* Things.getActions(String bindingId, String thingUid)
* Things.getThingStatusInfo(String thingUid)
*
* @name Things
* @memberof actions
*/
const ThingsAction = Java.type('org.openhab.core.model.script.actions.Things');
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/transform/actions/transformation Transformation} Actions
*
* The static methods of this class allow rules to execute transformations using one of the various {@link https://www.openhab.org/addons/#transform data transformation services}.
*
* @example
* actions.Transformation.transform('MAP', 'en.map', 'OPEN'); // returns "open"
* actions.Transformation.transform('MAP', 'de.map', 'OPEN'); // returns "offen"
* @memberof actions
* @hideconstructor
*/
class Transformation {
/**
* Applies a transformation of a given type with some function to a value.
*
* @param {string} type the transformation type, e.g. REGEX or MAP
* @param {string} fn the function to call, this value depends on the transformation type
* @param {string} value the value to apply the transformation to
* @returns {string} the transformed value or the original one, if there was no service registered for the given type or a transformation exception occurred
*/
static transform (type, fn, value) {
typeOfArguments([type, fn, value], ['string', 'string', 'string']);
return JavaTransformation.transform(type, fn, value).toString();
}
/**
* Applies a transformation of a given type with some function to a value.
*
* @param {string} type the transformation type, e.g. REGEX or MAP
* @param {string} fn the function to call, this value depends on the transformation type
* @param {string} value the value to apply the transformation to
* @returns {string} the transformed value
* @throws Java {@link https://www.openhab.org/javadoc/latest/org/openhab/core/transform/TransformationException.html TransformationException}
*/
static transformRaw (type, fn, value) {
typeOfArguments([type, fn, value], ['string', 'string', 'string']);
// Wrap exception to enable JS stack traces
try {
return JavaTransformation.transformRaw(type, fn, value).toString();
} catch (error) {
throw new Error(error);
}
}
}
/**
* {@link https://www.openhab.org/javadoc/latest/org/openhab/core/model/script/actions/Voice.html Voice} Actions
*
* The static methods of this class are made available as functions in the scripts. This allows a script to use voice features.
*
* @example
* Voice.interpret(Object text)
* Voice.interpret(Object text, String interpreter)
* Voice.interpret(Object text, String interpreter, String sink)
* Voice.say(Object text)
* Voice.say(Object text, String voice)
* Voice.say(Object text, String voice, String sink)
* Voice.say(Object text, String voice, String sink, PercentType volume)
* Voice.say(Object text, String voice, PercentType volume)
* Voice.say(Object text, PercentType volume)
*
* @name Voice
* @memberof actions
*/
const Voice = Java.type('org.openhab.core.model.script.actions.Voice');
/**
* Cloud Notification Actions
*
* If the {@link https://www.openhab.org/addons/integrations/openhabcloud/ openHAB Cloud Connector} add-on is installed, notifications can be sent to registered users/devices.
*
* The static methods of this class are made available as functions in the scripts. This allows a script to send notifications using the openHAB Cloud Connector add-on.
* See {@link https://www.openhab.org/docs/configuration/actions.html#cloud-notification-actions Cloud Notification Action Docs} for full documentation.
*
* @example
* NotificationAction.sendNotification('<email>', '<message>'); // to a single myopenHAB user identified by e-mail
* NotificationAction.sendBroadcastNotification('<message>'); // to all myopenHAB users
* NotificationAction.sendLogNotification('<message>'); // only listed in the notification log
*
* @name NotificationAction
* @memberof actions
*/
let NotificationAction;
try {
NotificationAction = Java.type('org.openhab.io.openhabcloud.NotificationAction');
} catch (error) {
if (error.name !== 'TypeError') throw Error(error);
}
module.exports = Object.assign(dynamicExports, {
Audio,
BusEvent,
Ephemeris,
Exec,
HTTP,
Log: LogAction,
Ping,
ScriptExecution,
Semantics,
Things: ThingsAction,
Transformation,
Voice,
NotificationAction,
/**
* Get the ThingActions of a given Thing.
* Duplicate of {@link actions.Things actions.Things.getActions()}.
*
* @memberof actions
* @param {string} bindingId binding ID
* @param {string} thingUid Thing UID
* @returns {*} Native Java {@link https://www.openhab.org/javadoc/latest/org/openhab/core/thing/binding/thingactions ThingActions}
*/
get: (bindingId, thingUid) => actions.get(bindingId, thingUid),
/**
* Get the ThingActions of a given Thing.
* Duplicate of {@link actions.get actions.get()} and {@link actions.Things actions.Things.getActions()}.
*
* @memberof actions
* @param {string} bindingId binding ID
* @param {string} thingUid Thing UID
* @returns {*} Native Java {@link https://www.openhab.org/javadoc/latest/org/openhab/core/thing/binding/thingactions ThingActions}
*/
thingActions: (bindingId, thingUid) => Things.getActions(bindingId, thingUid)
});