Home
New action for jsr223 script:
- Openhab.getJsr223Action()
- action: reloadTriggers(MyRuleInstance)
- You can make UtilityScript
- action: getUtilityScript(name_of_script_class)
###1 - Openhab.getJsr223Action()
This is only to get the action class. I try to add it to the regular action class(getAction()
), but it dit not work.
###2 - reloadTriggers(MyRuleInstance) You can make the engine reload your trigger. This can be useful if you need to change trigger on the fly.
test.groovy
import org.openhab.core.jsr223.internal.shared.*
import org.openhab.core.items.ItemRegistry
import org.openhab.core.library.types.*
//reloadTrigger test
class TestImpl implements Rule {
static ItemRegistry itemRegistry
static Class pe
def logger = Openhab.getLogger('Test')
def jsr = Openhab.getJsr223Action()
def instance = this
boolean isStop = false
TestImpl() {
logger.info('Test init')
}
java.util.List<EventTrigger> getEventTrigger() {
def myTrigger = []
myTrigger << new StartupTrigger()
if(isStop){
myTrigger << new ShutdownTrigger()
}
return myTrigger
}
void execute(Event event) {
logger.info('test executed!')
//you can change your logic to have different eventTrigger...
isStop = true
jsr.reloadTriggers(instance)
//your new trigger are now operational.
}
}
RuleSet getRules() {
return new RuleSet(new TestImpl())
}
TestImpl.itemRegistry = this.ItemRegistry
TestImpl.pe = this.PersistenceExtensions
###3 - You can make UtilityScript
Utility Script are script that are not executed by trigger, but used by other script. You need to implement UtilityScript and put getUtilityScripts() outside any class.
Example:
utilitytest.groovy
import org.openhab.core.jsr223.internal.shared.*
import org.openhab.core.items.ItemRegistry
import org.openhab.core.library.types.*
class UtilityImpl implements UtilityScript {
static ItemRegistry itemRegistry
static Class pe
static boolean isLoaded = false
def logger = Openhab.getLogger('UtilityImpl')
def jsr = Openhab.getJsr223Action() //needed to get new jrs223 action
def instance = this
def stringTest = ""
UtilityImpl() {
logger.info('TEST SCRIPT UTILITY')
}
void testMe() {
logger.info("Testing me is working!!! " + stringTest)
}
void giveMe(String aTest) {
stringTest = aTest
}
}
UtilitySet getUtilityScripts() {
return new UtilitySet(new UtilityImpl())
}
UtilityImpl.itemRegistry = this.ItemRegistry
UtilityImpl.pe = this.PersistenceExtensions
###4 - getUtilityScript(name_of_script_class)
To use utility script inside another script, you need to get it with the action getUtilityScript.
Example: ( you need the example in #3 for this to work )
test.groovy
import org.openhab.core.jsr223.internal.shared.*
import org.openhab.core.items.ItemRegistry
import org.openhab.core.library.types.*
//application test
class TestImpl implements Rule {
static ItemRegistry itemRegistry
static Class pe
def logger = Openhab.getLogger('TestImpl')
def jsr = Openhab.getJsr223Action()
def instance = this
TestImpl() {
logger.info('Test init')
}
java.util.List<EventTrigger> getEventTrigger() {
def myTrigger = []
myTrigger << new StartupTrigger()
myTrigger << new UpdatedEventTrigger("Light_To_Test")
return myTrigger
}
void execute(Event event) {
logger.info('test executed!')
jsr.reloadTriggers(instance) //to dynamically reload your tiggers
def test = jsr.getUtilityScript("UtilityImpl") //this will give you your Utility Script
test.testMe()
test.giveMe("abcd")
}
}
RuleSet getRules() {
return new RuleSet(new TestImpl())
}
TestImpl.itemRegistry = this.ItemRegistry
TestImpl.pe = this.PersistenceExtensions