From 2cd27418b004506f6c36d2b89d11ffb4aa36e34c Mon Sep 17 00:00:00 2001 From: Oleg Andreych Date: Thu, 30 May 2024 20:26:34 +0500 Subject: [PATCH] Added an example of the `ScriptAction` preset (#2228) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added an example of the `ScriptAction` preset Signed-off-by: Oleg Andreych * Changed brackets for some newly added anchors Signed-off-by: Oleg Andreych * add JS and JRuby Signed-off-by: Stefan Höhn * fix fences Signed-off-by: Stefan Höhn * fix fences Signed-off-by: Stefan Höhn * js indentation Signed-off-by: Stefan Höhn * jruby indentation Signed-off-by: Stefan Höhn --------- Signed-off-by: Oleg Andreych Signed-off-by: Stefan Höhn Co-authored-by: Stefan Höhn --- configuration/jsr223.md | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/configuration/jsr223.md b/configuration/jsr223.md index 781f93d03..62ec404ed 100644 --- a/configuration/jsr223.md +++ b/configuration/jsr223.md @@ -238,6 +238,8 @@ The `default` preset is preloaded, so it does not require importing. - [`RuleSimple`](#rulesimple-preset) - [`RuleSupport`](#rulesupport-preset) - [`RuleFactories`](#rulefactories-preset) +- [`ScriptAction`](#scriptaction-preset) +- [`cache`](#cache-preset) #### Default Preset (`importPreset` not required) @@ -375,6 +377,60 @@ scriptExtension.importPreset("RuleFactories") | `ConditionHandlerFactory` | `org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedConditionHandlerFactory` | | `TriggerHandlerFactory` | `org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory` | +#### `ScriptAction` Preset + +This preset can be useful for scheduling asynchronous code execution with `org.openhab.core.automation.module.script.action.Timer`. + +| Variable | Description | +|-------------------|--------------------------------------------------------------------| +| `scriptExecution` | `org.openhab.core.automation.module.script.action.ScriptExecution` | + +:::: tabs + +::: tab Groovy + +```groovy +scriptExtension.importPreset("ScriptAction") +scriptExtension.importPreset("RuleSupport") +scriptExtension.importPreset("RuleSimple") + +scriptExecution.createTimer(ZonedDateTime.now(), () -> { + org.slf4j.LoggerFactory.getLogger('Test logger').warn('Timer ran') +}) +``` + +::: + +::: tab JSScripting + +```javascript +actions.scriptExecution.createTimer(time.toZDT(1000), () => { + console.info('Timer ran'); +}) +``` + +::: + +::: tab DSL + +```java +createTimer(now.plusSeconds(1), [ | + logInfo('Test logger', 'Timer ran') +]) +``` + +::: + +::: tab JRuby + +```ruby +after(1.second) do + logger.info("Timer ran") +end +``` + +::: + #### `cache` Preset The `cache` preset does not provide a default import and needs to be imported explicitly.