@@ -236,23 +236,26 @@ Note that prior to openHAB 3, script ordering was performed alphanumerically bas
236236
237237To facilitate JSR223 scripting, several openHAB-related variables are automatically predefined within ` ScriptExtension ` presets.
238238They can be loaded into the script context using ` scriptExtension.importPreset(String preset) ` , e.g. ` scriptExtension.importPreset("RuleSimple") ` .
239- The ` default ` preset is preloaded, so it does not require importing.
239+ The ` default ` , ` lifecycle ` and ` media ` presets are preloaded, so they do not require importing.
240240With ` scriptExtension.get("automationManager") ` the ` automationManager ` can be made available without loading a preset.
241241
242242- [ Overview] ( #overview )
243243 - [ Example rules for a first impression] ( #example-rules-for-a-first-impression )
244244 - [ Script Locations] ( #script-locations )
245245 - [ ` ScriptExtension ` Objects (all JSR223 languages)] ( #scriptextension-objects-all-jsr223-languages )
246- - [ Default Preset (` importPreset ` not required)] ( #default-preset-importpreset-not-required )
246+ - [ ` default ` Preset (` importPreset ` not required)] ( #default-preset-importpreset-not-required )
247247 - [ ` events ` operations] ( #events-operations )
248+ - [ ` lifecycle ` Preset (` importPreset ` not required)] ( #lifecycle-preset-importpreset-not-required )
249+ - [ ` media ` Preset (` importPreset ` not required)] ( #media-preset-importpreset-not-required )
248250 - [ ` RuleSimple ` Preset] ( #rulesimple-preset )
249251 - [ ` RuleSupport ` Preset] ( #rulesupport-preset )
250252 - [ ` RuleFactories ` Preset] ( #rulefactories-preset )
251253 - [ ` ScriptAction ` Preset] ( #scriptaction-preset )
252254 - [ ` cache ` Preset] ( #cache-preset )
253255 - [ ` TriggerType ` Objects (all JSR223 languages)] ( #triggertype-objects-all-jsr223-languages )
256+ - [ The ` scriptLoaded ` and ` scriptUnloaded ` functions] ( #the-scriptloaded-and-scriptunloaded-functions )
254257
255- #### Default Preset (` importPreset ` not required)
258+ #### ` default ` Preset (` importPreset ` not required)
256259
257260| Variable | Description |
258261| -------------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
@@ -335,6 +338,24 @@ With `scriptExtension.get("automationManager")` the `automationManager` can be m
335338- ` events.storeStates(Item...) `
336339- ` events.restoreStates(Map<Item, State>) `
337340
341+ #### ` lifecycle ` Preset (` importPreset ` not required)
342+
343+ It provides a mechanism to execute code, when the script is deleted.
344+ Modifying a script deletes it and creates it again.
345+
346+ | Variable | Description |
347+ | --------------------| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
348+ | ` lifecycleTracker ` | [ ` org.openhab.core.automation.module.script.LifecycleScriptExtensionProvider.LifecycleTracker ` ] ( https://www.openhab.org/javadoc/latest/org/openhab/core/automation/module/script/lifecyclescriptextensionprovider.lifecycletracker ) |
349+
350+ This is demonstrated in [ The ` scriptLoaded ` and ` scriptUnloaded ` functions] ( #the-scriptloaded-and-scriptunloaded-functions ) section.
351+
352+ #### ` media ` Preset (` importPreset ` not required)
353+
354+ | Variable | Description |
355+ | ----------| ---------------------------------------------------------------------------------------------------------------------|
356+ | ` audio ` | [ ` org.openhab.core.audio.AudioManager ` ] ( https://www.openhab.org/javadoc/latest/org/openhab/core/audio/audiomanager ) |
357+ | ` voice ` | [ ` org.openhab.core.voice.VoiceManager ` ] ( https://www.openhab.org/javadoc/latest/org/openhab/core/voice/voicemanager ) |
358+
338359#### ` RuleSimple ` Preset
339360
340361These variables and classes are loaded using:
@@ -666,3 +687,129 @@ Read the JSR223 language specific documentation for examples of using these `Tri
666687| ` startlevel ` | The system ` StartLevel ` |
667688
668689:::
690+
691+ ### The ` scriptLoaded ` and ` scriptUnloaded ` functions
692+
693+ When a JSR223 script is created or changed and it contains the ` scriptLoaded(String) ` function, OpenHAB executes it, and passes as parameter the path of the loaded script.
694+ Similarly, when a script is changed or deleted, and the script contained the ` scriptUnloaded() ` function, it is invoked.
695+ The [ ` lifecycleTracker ` ] ( #lifecycle-preset-importpreset-not-required ) object and the ` scriptUnloaded() ` function serve the same purpose.
696+
697+ :::: tabs
698+
699+ ::: tab Groovy
700+
701+ ``` groovy
702+ static @groovy.transform.Field logger = org.slf4j.LoggerFactory.getLogger('org.openhab.automation.example')
703+
704+ lifecycleTracker.addDisposeHook(new org.openhab.core.automation.module.script.LifecycleScriptExtensionProvider.Disposable() {
705+ public void dispose() {
706+ logger.info("I am logged fourth.")
707+ }
708+ }
709+ )
710+
711+ lifecycleTracker.addDisposeHook{ logger.info("I am logged fifth. Bye!") }
712+
713+ void scriptLoaded(String filename) { // can be static
714+ logger.error("I am logged second and I am from ${filename}.")
715+ }
716+
717+ static void scriptUnloaded() { // can be non-static
718+ logger.error("I am logged third.")
719+ }
720+ logger.error("I am logged first.")
721+ ```
722+
723+ :::
724+
725+ ::: tab JS  ; Scripting
726+
727+ ``` js
728+ require (' @runtime' ).lifecycleTracker .addDisposeHook ( () => console .log (" I am logged fourth. Bye!" ) )
729+
730+ const scriptUnloaded = () => console .log (" I am logged third." )
731+
732+ function scriptLoaded (script ) {
733+ console .log (` I am logged second and I am from ${ script} .` )
734+ }
735+
736+ console .log (" I am logged first." )
737+ ```
738+
739+ :::
740+
741+ ::: tab JS  ; Nashorn
742+
743+ ``` js
744+ var logger = Java .type (' org.slf4j.LoggerFactory' ).getLogger (" org.openhab.core.automation.examples" )
745+ lifecycleTracker .addDisposeHook (function () {
746+ logger .error (" I am logged fourth. Bye!" )
747+ })
748+ function scriptLoaded (filename ) {
749+ logger .error (" I am logged second and I am from " + filename + " ." )
750+ }
751+ function scriptUnloaded () {
752+ logger .error (" I am logged third." )
753+ }
754+ logger .error (" I am logged first." )
755+ ```
756+
757+ :::
758+
759+ ::: tab Jython
760+
761+ ``` python
762+ from org.slf4j import LoggerFactory
763+
764+ logger = LoggerFactory.getLogger(" org.openhab.core.automation.examples" )
765+
766+ def scriptUnloaded ():
767+ logger.error(" I am logged third." )
768+
769+ def scriptLoaded (filename ):
770+ logger.error(" I am logged second and I am from " + filename + " ." )
771+
772+ lifecycleTracker.addDisposeHook(lambda : logger.error(" I am logged fourth. Bye!" ))
773+ logger.error(" I am logged first." )
774+ ```
775+
776+ :::
777+
778+ ::: tab Python
779+
780+ When the ` scope ` module is enabled, the setting “Use scope and import wrapper” is on.
781+
782+ ``` python
783+ import sys
784+ import scope
785+
786+ def scriptUnloaded ():
787+ print (" I am logged third." )
788+
789+ def scriptLoaded (filename ):
790+ print (" I am logged second and I am from " + filename + " ." )
791+
792+ scope.lifecycleTracker.addDisposeHook(lambda : print (" I am logged fourth. Bye!" ))
793+
794+ print (" I am logged first." )
795+ ```
796+
797+ When the ` scope ` module is disabled.
798+
799+ ``` python
800+ import sys
801+
802+ def scriptUnloaded ():
803+ print (" I am logged third." )
804+
805+ def scriptLoaded (filename ):
806+ print (" I am logged second and I am from " + filename + " ." )
807+
808+ lifecycleTracker.addDisposeHook(lambda : print (" I am logged fourth. Bye!" ))
809+
810+ print (" I am logged first." )
811+ ```
812+
813+ :::
814+
815+ ::::
0 commit comments