openhab-addons/bundles/org.openhab.automation.jrubyscripting
jimtng 267a8aac7c
[jrubyscripting] convert presets from java class to ruby class (#12047)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
2022-02-06 21:50:56 +01:00
..
src/main [jrubyscripting] convert presets from java class to ruby class (#12047) 2022-02-06 21:50:56 +01:00
NOTICE [jrubyscripting] JRuby Scripting initial contribution (#11538) 2021-11-15 14:21:29 +01:00
pom.xml [jrubyscripting] Update jruby version to 9.3.3.0 (#12143) 2022-01-28 07:47:38 +01:00
README.md [jrubyscripting] Update version to 9.3.2 in README.md (#11823) 2021-12-20 14:51:51 +01:00

JRuby Scripting

This add-on provides JRuby 9.3.2 that can be used as a scripting language within automation rules.

JRuby Scripting Configuration

After installing this add-on, you will find configuration options in the openHAB portal under Settings -> Other Services -> JRuby Scripting.

Alternatively, JRuby configuration parameters may be set by creating a jruby.cfg file in conf/services/

Parameter Default Description
org.openhab.automation.jrubyscripting:gem_home $OPENHAB_CONF/scripts/lib/ruby/gem_home Location ruby gems will be installed and loaded, directory will be created if missing and gem installs are specified
org.openhab.automation.jrubyscripting:rubylib $OPENHAB_CONF/automation/lib/ruby/ Search path for user libraries. Separate each path with a colon (semicolon in Windows).
org.openhab.automation.jrubyscripting:local_context singlethread The local context holds Ruby runtime, name-value pairs for sharing variables between Java and Ruby. See this for options and details
org.openhab.automation.jrubyscripting:local_variables transient Defines how variables are shared between Ruby and Java. See this for options and details
org.openhab.automation.jrubyscripting:gems Comma separated list of Ruby Gems to install.

Ruby Gems

This automation add-on will install user specified gems and make them available on the library search path. Gem versions may be specified using the standard ruby gem_name=version format. The version number follows the pessimistic version constraint syntax.

For example this configuration will install version 4 or higher of the openHAB JRuby Scripting Library.

org.openhab.automation.jrubyscripting:gems=openhab-scripting=~>4.0

Creating JRuby Scripts

When this add-on is installed, you can select JRuby as a scripting language when creating a script action within the rule editor of the UI.

Alternatively, you can create scripts in the automation/jsr223/ruby/personal configuration directory. If you create an empty file called test.rb, you will see a log line with information similar to:

    ... [INFO ] [.a.m.s.r.i.l.ScriptFileWatcher:150  ] - Loading script 'test.rb'

To enable debug logging, use the console logging commands to enable debug logging for the automation functionality:

log:set DEBUG org.openhab.core.automation
log:set DEBUG org.openhab.automation.jrubyscripting

Imports

All ScriptExtensions are available in JRuby with the following exceptions/modifications:

  • The File variable, referencing java.io.File is not available as it conflicts with Ruby's File class preventing Ruby from initializing
  • Globals scriptExtension, automationManager, ruleRegistry, items, voice, rules, things, events, itemRegistry, ir, actions, se, audio, lifecycleTracker are prepended with a $ (e.g. $automationManager) making them available as a global objects in Ruby.

Script Examples

JRuby scripts provide access to almost all the functionality in an openHAB runtime environment. As a simple example, the following script logs "Hello, World!". Note that puts will usually not work since the output has no terminal to display the text. The openHAB server uses the SLF4J library for logging.

require 'java'
java_import org.slf4j.LoggerFactory

LoggerFactory.getLogger("org.openhab.core.automation.examples").info("Hello world!")

JRuby can import Java classes. Depending on the openHAB logging configuration, you may need to prefix logger names with org.openhab.core.automation for them to show up in the log file (or you modify the logging configuration).