-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jruby) JRuby scripting initial binding commit
Signed-off-by: Brian O'Connell <[email protected]>
- Loading branch information
Showing
10 changed files
with
552 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
This content is produced and maintained by the openHAB project. | ||
|
||
* Project home: https://www.openhab.org | ||
|
||
== Declared Project Licenses | ||
|
||
This program and the accompanying materials are made available under the terms | ||
of the Eclipse Public License 2.0 which is available at | ||
https://www.eclipse.org/legal/epl-2.0/. | ||
|
||
== Source Code | ||
|
||
https://github.com/openhab/openhab-addons |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# JRuby Scripting | ||
|
||
This add-on provides [JRuby](https://www.jruby.org/) 9.3.1 that can be used as a scripting language within automation rules and which eliminates the need to download JRuby and create `EXTRA_JAVA_OPTS` entries for `bootclasspath` and `rubylib`. | ||
|
||
## JRuby Scripting Configuration | ||
|
||
JRuby configuration parameters may be set by creating a jruby.cfg file in $OPENHAB_CONF/services/ | ||
|
||
|
||
| Parameter | Default | Description | | ||
|-------------------------------------------------------|--------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| org.openhab.automation.jrubyscripting:gem_home | $OPENHAB_CONF/automation/lib/ruby/gem_home | Location ruby gems will be installed and loaded from | | ||
| org.openhab.automation.jrubyscripting:ruby_lib | $OPENHAB_CONF/automation/lib/ruby/ | Search path for libraries | | ||
| org.openhab.automation.jrubyscripting:local_context | threadsafe | The local context holds Ruby runtime, name-value pairs for sharing variables between Java and Ruby. See [this](https://github.com/jruby/jruby/wiki/RedBridge#Context_Instance_Type) for options and details | | ||
| org.openhab.automation.jrubyscripting:local_variables | transient | Defines how variables are shared between Ruby and Java. See [this](https://github.com/jruby/jruby/wiki/RedBridge#local-variable-behavior-options) for options and details | | ||
| org.openhab.automation.jrubyscripting:gems | | Comma seperated list of [Ruby Gems](https://rubygems.org/) to install. | | ||
|
||
|
||
## Ruby Gems | ||
|
||
This binding will install and make available on the library search path user specified gems. Gem versions may be specified using the standard ruby gem_name=version format. For example this configuration will install version 4 or higher of the OpenHAB JRuby Scripting Libray. | ||
|
||
```text | ||
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` configuration directory. | ||
If you create an empty file called `test.rb`, you will see a log line with information similar to: | ||
|
||
```text | ||
... [INFO ] [.a.m.s.r.i.l.ScriptFileWatcher:150 ] - Loading script 'test.rb' | ||
``` | ||
|
||
To enable debug logging, use the [console logging]({{base}}/administration/logging.html) commands to | ||
enable debug logging for the automation functionality: | ||
|
||
```text | ||
log:set DEBUG org.openhab.core.automation | ||
log:set DEBUG org.openhab.binding.jrubyscripting | ||
``` | ||
|
||
## Imports | ||
|
||
All [ScriptExtensions]({{base}}/configuration/jsr223.html#scriptextension-objects-all-jsr223-languages) 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](https://www.slf4j.org/) library for logging. | ||
|
||
```ruby | ||
require 'java' | ||
java_import org.slf4j.LoggerFactory | ||
|
||
LoggerFactory.getLogger("org.openhab.core.automation.examples").info("Hello world!") | ||
``` | ||
|
||
JRuby can [import Java classes](https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby). | ||
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). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>org.openhab.addons.bundles</groupId> | ||
<artifactId>org.openhab.addons.reactor.bundles</artifactId> | ||
<version>3.2.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>org.openhab.automation.jrubyscripting</artifactId> | ||
|
||
<name>openHAB Add-ons :: Automation :: JRuby Scripting</name> | ||
|
||
<properties> | ||
<bnd.importpackage> | ||
com.ibm.icu.*;resolution:=optional, | ||
org.abego.treelayout.*;resolution:=optional, | ||
org.apache.ivy.*;resolution:=optional, | ||
org.stringtemplate.v4.*;resolution:=optional</bnd.importpackage> | ||
<jruby.version>9.3.1.0</jruby.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jruby</groupId> | ||
<artifactId>jruby-complete</artifactId> | ||
<version>${jruby.version}</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
9 changes: 9 additions & 0 deletions
9
bundles/org.openhab.automation.jrubyscripting/src/main/feature/feature.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<features name="org.openhab.automation.jrubyscripting-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0"> | ||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository> | ||
|
||
<feature name="openhab-automation-jrubyscripting" description="JRuby Scripting" version="${project.version}"> | ||
<feature>openhab-runtime-base</feature> | ||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.automation.jrubyscripting/${project.version}</bundle> | ||
</feature> | ||
</features> |
Oops, something went wrong.