It seems python is the most popular programming language among OH-users, and I have read several who use Jython to write rules. However, being more of a rubyist myself, I thought I’d try to see if I can use JRuby the same way. I used the instructions for installing Jython, but substituted it for the Jruby .jar-file. When I restarted OH i got a log entry
2017-11-19 21:06:06.482 [INFO ] [.internal.GenericScriptEngineFactory] - Activated scripting support for ruby
2017-11-19 21:06:06.484 [DEBUG] [.internal.GenericScriptEngineFactory] - Activated scripting support with engine JSR 223 JRuby Engine(9.1.13.0) for ruby(jruby 9.1.13.0) with mimetypes [application/x-ruby] and file extensions [rb]
If I create a .rb-file in the automation/jsr223
-folder the logs says:
2017-11-19 21:30:01.662 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {}
2017-11-19 21:30:01.672 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'testrule.rb'
2017-11-19 21:30:02.000 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: testrule.rb
2017-11-19 21:30:02.003 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {}
2017-11-19 21:30:02.018 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'testrule.rb'
2017-11-19 21:30:02.147 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: testrule.rb
But I can’t make the scripts do anything. If I just make a simple script like the Jython example in the docs (http://docs.openhab.org/configuration/jsr223-jython.html)
include_class org.slf4j.LoggerFactory
logger = LoggerFactory.getLogger("org.eclipse.smarthome.automation.examples")
logger.info("Test")
Nothing happens. And if I try to use
scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")
I just get an error in the logs
2017-11-19 21:51:32.967 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - in queue: {}
2017-11-19 21:51:32.987 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'testrule.rb'
2017-11-19 21:51:33.044 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/testrule.rb': org.jruby.embed.EvalFailedException: (NameError) undefined local variable or method `scriptExtension' for main:Object
2017-11-19 21:51:33.046 [DEBUG] [rt.internal.loader.ScriptFileWatcher] - Script loaded: testrule.rb
Which at least means that the file gets loaded and parsed by JRuby. But it seems that the ESH/OH scope doesn’t get extended to JRuby.
I’m currently running OH 2.1.0 stable, would moving to a snapshot build solve anything? (Has any changes been made to the jsr223 implementation that could make it work better?)
Is it possible to work around this by for example make my own instance of whatever class scriptExtension
is an instance of?
Is this something that has to be added explicitly for each scripting language in ESH? If that’s the case it’s probably too big of an undertaking for me as I’m not very familiar with java development.
I know I’m pretty much on my own here because not many users are interested in getting this to work besides me, but any pointers in the right direction would be much appreciated.