Java223 Scripting - Script with Java on openHAB

logo

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

Alpha release, no “published” tag. Published ! Many thanks to you if you want to test.
I’m requesting for comment !

(For the why and the history, see the second post below.)

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 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

4 Likes

History

There are already three other bundles more or less available allowing you to use java in OpenHAB, so why another one ?

First, many thanks to them, especially @J-N-K for the SmartHome/J implementation, from which I took a good share of code. I kept author names in some classes to reflect that. The 3 other bundles are:

I decided to do another one, mainly for these two requirements :

  • a bundle available on the main OpenHAB marketplace, and maybe merged as an official add-on in some time
  • fully compatible JSR223

So I started by proposing small features to @weberjn bundle. But I didn’t stop at “small” and had other ideas, which was going against its preference of keeping the bundle simple. So I forked for myself. Then I learned about the SmartHome/J bundle (I wasn’t aware of it before), and took some code and ideas from it (but with a very different compiling architecture)

Then, I kept piling new features:

  • a cache for very fast execution and reuse of instance (allowing sharing value between executions)
  • a very clear separation between the way you can write your Java scripts (no dependencies at all, plain java class), the bundle, and the helper library which is “just” a library that you could have written yourself and that can evolve independently.
  • a ‘no boilerplate’ option for one liner script
  • many quality of life improvement (a smart injection of openHAB values, some effort for flexibility to propose different coding ways, some default values here and there to shorten lengthiness or complexity of code, etc.)
  • and other features… (See documentation)

Now the base code is waaayyy different from all of those, and thus, a new bundle. I’m not sure if it is better (probably not, and this bundle won’t have all the features the other ones can have, I think for example JRule has many helper functions), but the reverse is certainly true : it has its own strong arguments that others don’t have.

If this bundle doesn’t meet a public, I will of course remove it from the marketplace, as I don’t want to add useless confusion to this already many-bundle situation.

5 Likes

Nice. Just had a brief look at the code (I personally switched to JavaScript). If you want to open a PR in openhab-addons, Please ping me and I’ll try to assist with the review process.

4 Likes