Is it possible to use own build JAR-Files (pojo) in rules

I would like to move some logic from my rule files to standard java code, which can be unit tested.

Is it possible to put a simple pojo jar file into openhab2 and use that code in rules - as you would use for example the DateTime object.

So far, I have no idea to achieve this. “bundle:install” didn’t work either.

My vision:

import mycode.OpenHabAirControl

rule testJar
when
    Time cron ...
then
    var OpenHabAirControl obj = new OpenHabAirControl()    
    obj.setArg1("1")
    obj.setArg2("2")
    var String newState = obj.compute
    
    itemAirControl.sendCommand(newState)
end

Thanks.

I don’t know the direct answer to your question but suspect it is potentially possible. I would look to see how to build an add-on, which is essentially what you would be writing, for how to deploy it. In particular, look to see how to write an Action.

Alternatively, I would recommend looking at the JSR233 binding which is going to be included in the snapshot version of OH 2 any day now. This would allow you to write rules in Jython, JavaScript, or Groovy, all three of which allow unit testing of rules. This would be my first recommendation.

Another alternative would be to use the Exec binding and/or executeCommandLine, to call your external code. For example, your rule above would become:

then
    val newState = executeCommandLine("java OpenHabAirControl  1 2", 5000)
    itemAirControl.sendCommand(newState)
end

Assumptions:

  • correct paths to java and OpenHabAirControl.class need to be right
  • the script returns a String that can be easily parsed into a state itemAirControl supports

3 ways to reach my goal - very good. Thanks you.

JSR233 were indeed a cool solution. But so long I could help myself with the exec workaround.

I looked also into developing a virtual binding, but that would mean a lot of overhead. And all this stuff within Eclipse knocks me out … ;-(