Openhab 2 jython support

Thanks so much for the fast answer! I’ll test it this evening …

Hi all,
I started to play with the Jython rule support: I tryed lo load 2 simple rules, and seems that the second loaded script overwrite the first one:

12:54:11.097 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'jsr223_Script01.py'
12:54:13.065 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'scripted_000000000000001F' has been added.
12:54:13.068 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: INITIALIZING
12:54:13.120 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'jsr223_test.py'
12:54:13.130 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:13.146 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'scripted_000000000000001F' has been added.
12:54:13.156 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: INITIALIZING
12:54:13.161 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:14.022 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: RUNNING
12:54:14.025 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE
12:54:15.011 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: RUNNING
12:54:15.014 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - scripted_000000000000001F updated: IDLE

and I wondering from where the name

'scripted_000000000000001F'

come from?

The scripts are:

1° script

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")


class myRule(SimpleRule):
    def execute(self, modules, inputs):
        print "Hello World from Jython"

sRule = myRule()
sRule.setTriggers([Trigger("aTimerTrigger", "timer.GenericCronTrigger", Configuration({"cronExpression": "0/15 * * * * ?"}))])

automationManager.addRule(sRule)

2° script

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")


class myRule01(SimpleRule):
    def execute(self, modules, inputs):
        print "BYE BYE"

sRule = myRule01()
sRule.setTriggers([Trigger("aTimerTrigger", "timer.GenericCronTrigger", Configuration({"cronExpression": "* * * * * ?"}))])

automationManager.addRule(sRule)

Thanks in advance.


Anyway, for want to use Jython support on windows, the following line should be added to the file “openhab2 folder\runtime\bin\karaf.bat”

set JYTHON_HOME=C:\jython2.7.0

set EXTRA_JAVA_OPTS=-Dpython.home=%JYTHON_HOME% -Dpython.path=conf\automation\jsr223 
set CLASSPATH="C:\jython2.7.0\jython.jar"

did you enable any special logging? I don’t get any such log messages when I paste your code to a .py file on my OH2 instance:

my actual log level:

openhab> log:get
Logger | Level
--------------
ROOT   | WARN
openhab> 

Scripts are in

openhab2\conf\automation\jsr223

I just found out I have the messages as well, but only in log:display, not in the openhab.log file on disk.

I’m not quite sure because I can not test.
But must Trigger have a clear name too?
aTimerTrigger01 and aTimerTrigger02 etc.?

In the first test triggers had same name.

I modified the 2 script in order to have unique triggers name, but same behavior.

Furthermore, if I modify the script, the log shows that the script is loaded again, but seems that no changes are loaded. I need to reboot openhab to load the correct modified script.
Also, if the file is deleted from the automation\jsr223 folder openhab does not realize it, and the script continues to run.

I’m trying this too with a recent snapshot build and seeing similar behavior. That’s unfortunate because it was working quite well a few months before the PR was merged but I know there were a lot of last minute changes. Has anybody submitted the related bug reports to the SmartHome issue tracker? I’m seeing two issues. Script being loaded multiple times and no scriptUnloaded callback when script is removed (and rule objects are not being removed during unload).

Hello @steve1,
is it this?

Thanks.That definitely looks like it could be the cause of the problem. If I can get a clean build with the latest code I’ll try to do some experimentation.

Not sure I understand you right.
Do you know this mini wiki for automation? https://github.com/eclipse/smarthome/wiki/Scripted-Rule-Support

I’ll send you the newest bundles, but these can be found in nightly builds too.

I meant I was going to try to step through code in the IDE debugger, but I need an Eclipse project that will build cleanly. Do you know how to force code generation for the xtext files? I’m seeing some build errors that look like code might need to be regenerated.

The name “scripted_XXXXX” error comes from this change:

That has to be changed, but I’m currently not at my development PC. I will open an issue for that

Thanks @smerschjo, ftr: https://github.com/eclipse/smarthome/issues/3528

Issue is fixed and I have just built a new distro (#930) - it works smoothly with at least a js example:

09:21:24.401 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'example.js'
09:21:25.059 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_1' has been added.
09:21:25.062 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_1 updated: INITIALIZING
09:21:25.096 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'example2.js'
09:21:25.098 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_1 updated: IDLE
09:21:25.175 [INFO ] [smarthome.event.RuleAddedEvent      ] - Rule 'rule_2' has been added.
09:21:25.175 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_2 updated: INITIALIZING
09:21:25.181 [INFO ] [smarthome.event.RuleStatusInfoEvent ] - rule_2 updated: IDLE

So please go ahead with testing and writing docs for this feature!

1 Like

It looks like the file loading (file watcher?) issues are still there. I see my test script loaded once but it is not reloaded when I update it. Helmut thought it might be related to the issue he referenced earlier in this thread.

I’ve done some more testing and it seems like the script reloading is actually working so I’ll start generating some examples and related documentation. Thanks, Kai!

2 Likes

@smerschjo It seems like the scriptUnload functionality is not working correctly. The scriptUnloaded callback is not invoked when a script is removed. Also, when an existing script is modified and saved, the scriptLoaded callback is invoked but the scriptUnloaded callback is not. This is the behavior reported by other users earlier in this topic.

AFAICT, the ScriptEngineManager.removeEngine is trying to look up the scriptIdentifier in the loadedScriptEngineInstances. However, the key in loadedScriptEngineInstances map is a simple file name (‘test.py’, in my case) and the scriptIdentifier is a URI with the absolute path of the file. Since the rule engine is not found, it is not removed or unloaded.

@smerschjo I’ve fixed the ScriptEngineManager issue in my dev workspace but I’ve run into another problem. The SimpleRule class uses the default Rule constructor which does not set the UID. When a rule is being removed during script unloading, the null UID is used to build an event topic and this causes an exception. I modified my SimpleRule code to generate a UID and pass it to the Rule base class constructor. This has fixed the issue but I don’t know if that’s the preferred solution.

Thanks for opening issue tickets for it. I will have a look at them. I think it would not be a good idea to generate the uid in the SimpleRule class. I will figure something out.