JavaScript (node.js) or Java

Hello,

is possible to use any methods from external library of JavaScript in OH? Eg.

const http = require('http');

or is possible to use Java in rules without installation add component like “JRule - openHAB Rules using Java” (btw: still in beta), OH is coded in Java, so is it possible to use Java in any way in rule?

Yes with some warnings. The JS Scripting add-on provides a node.js like environment but it’s not fully compatible with the full node.js environment. Most libraries should work but any that depend on the event loop or stuff like that will likely not work.

You must first install the library using npm to $OH_CONF/automation/js (e.g. cd $OH_CONF/automation/js && npm install openhab_rules_tools). The bottom of the docs for the JS Scripting add-on has more details about installing and writing your own libraries.

Ues, you can import and use Java classes into your JS Scripting rule. A bunch of OH related classes (see JSR223 Scripting | openHAB) can be imported using const runtime = require("@runtime"); and then accessed using, for example, runtime.OnOffType.ON.

Any other Java class from the JRE and from openHAB itself can be imported using const HTTP = Java.type("java.net.http.HttpClient");. I don’t know if there is a way to import and use a third party Java library though.

Note, when working with Java Classes you must be aware of types. For example, a Java List<?> is not the same as a JS Array and cannot be used interchangable. If you have a Java Object or class, you must only use Java Objects with it except for ZonedDateTime (I think), primitives, and Strings.

jRuby has a similar mechanism I beleive and supports Ruby gems. Rules DSL can use import to import any of the core and openHAB Java classes also. Groovy is mostly working with Java as I understand it.

best for me is to use Java Script, in details I would like do decrypt data from wm-bus.

The script is ready, everything works fine (I can share community of course), finnaly need to be tested in OH, but this code in JS is using:

const crypto = require('crypto');

so how to do that as easily as possible?

Install the crypto library using npm (see above) and then you can require it and use it as normal.

Thank you very much for your reply.

This thread is starting to outgrow my modest skills :frowning:

Crypto JS is no longer maintained and recomendation is to use Node.js

The topic is further complicated by the fact that I work in a Windows environment, the alternative to npm throws errors, and thus more problems arise…

I guess I have to use/implement AES decryption somehow differently :frowning:

I need to dive into the literature…