Java223 Scripting - Script with Java on openHAB [4.3.0.0,5.0.0.0)

This automation bundle is openHAB-JSSR223 compliant and allows you to write your script in Java.

IMPORTANT : THIS TOPIC / BUNDLE IS A LEGACY VERSION, KEPT FOR COMPATIBILITY WITH OPENHAB 4. No support. Please update to openHAB5.

Alpha release, no “published” tag. Published ! Many thanks to you if you want to test.
Comments welcome.

Main feature:

  • full JSR 223 support (use in files, in GUI, transformations, inline rule action, etc…)
  • auto injection of OpenHAB variable/preset for simplicity
  • library support for sharing code (.jar and .java)
  • rule annotations available in the helper library for creating rules the easiest way
  • helper library files auto generation for items, things, and actions, with strong typing and ease of use
  • cache compiled scripts in memory for blazingly fast executions after the first one (sub millisecond overload)
  • no boilerplate code for simple script: you can do a one liner script, as declaring a class and a method is optional.
  • optional reuse of instances script to share values between execution occurrences
  • designed to be easily used with your favorite IDE, with very little configuration

How to use it ? See documentation on the last link of this post.

Changelog

Version BETA2

  • fix: remove generic parameters in generated getMethod() reflection call (thanks Ardanedh)
  • fix: Wrong ChannelEvent parameter type
  • new parameter (startupGuardTime) for delaying helper generation at startup if the file already exists

Version BETA1

  • makes action initialization more reliable
  • allow custom rule uid
  • accomodate around the new openHAB functionnality “compile once” by still allowing cache invalidation when a lib changes

Version ALPHA1

  • initial release

Resources

JAR Bundle
Source and documentation

2 Likes

Hi, @dalgwen.

I find it great that someone is working with Java. I would really like to see the day when it is merged with the core and fully supported. For years, I’ve wondered why Java is not one of the supported programming languages, keeping in mind that OH has been developed in Java :confused:

I want to test it, but I’m wondering if it’s possible to pre-compile the rules into a JAR and upload it to a folder where the binding would pick it up from. I didn’t see that in the documentation.

Hello,

Thanks for you support !

This is not really how the JSR223 bundles works in openHAB (I use plural, because it’s not related to this java bundle in particular).
The “standard” way to do it is by putting a script file in the automation/jsr223/<langage>
This script is then passed to the relevant bundle, which may, or may not, use additional files in the automation/lib directory
Without a “trigger” like a new script file or a modification, the bundle isn’t called and shouldn’t do anything. We can imagine a special case triggered by adding a library of rules, and some meddling with the rule manager indirectly, but I’d rather stick to the JSR223 openHAB reference for the sake of consistency.

HOWEVER, I think a workaround is totally doable.

You just have to put your jar in the lib directory, and create a script that use your lib.

For example, If you have in your JAR a class containing rules named “MyLibInJar”, you can use it by dropping the following script in automation/jsr223/java :


import org.openhab.automation.java223.common.InjectBinding;
import org.openhab.automation.java223.common.RunScript;
import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedAutomationManager;

import helper.rules.RuleAnnotationParser;
import helper.rules.RuleParserException;
import myscript.MyLibInJar;

public class RuleLibraryLoader extends Java223Script {
	
    @RunScript
    public void loadMyRulesInJARWithHelperFacilities() throws IllegalArgumentException, RuleParserException {
        RuleAnnotationParser.parse(new MyLibInJar(), automationManager);
    }
	
}

(By the way, this is more or less how every scripts load their rules if you extend the Java223Script class, except Java223Script do it on itself rather than on another class)

Then, when you modify your JAR in the lib directory, just do not forget to touch this script (just adding a whitespace somewhere and save, or the ‘touch’ command line), to trigger another parsing of your JAR.

I would really like to see the day when it is merged with the core and fully supported.

I think I will make a PR for the next version, but I have some other things to finish first.