I have been using Jython for some time now and it is great!
The problem I am struggling though are the error messages.
The messages on the normal console are not useful because they relate to java and not to Jython.
The workaround I am using is the “try:” construct. e.g.
I don’t think there is a better way. Ultimately the Jython is running on Java so if you don’t catch the exceptions yourself like the above it will fall into Java code and print the stack trace accordingly.
You could possibly create a class or a function and annotations to automatically add a try/except to your rules. I am by no means an expert, but you can see an example of how I adopted this approach to handle signals (e.g. CTRL-c) in one of my scripts. See the check function in sensorReporter.py for the annotation and signalProc.py for the handling code.
Hi Rich,
Thanks for the reply and also the link to your python code which for sure will serve as a reference for quality coding and which I will start to study.
I use a class decorator which automatically wraps the execute method with a method decorator (which can also be used independently for other functions) that does the traceback logging. The class decorator also creates a class-specific logger, adds a method for notification support (only supports Pushover, for now) and causes the rule class to inherit from the openHAB Rule class. The class decorator is used like in the following snippet…
import openhab
@openhab.rule
class MyRule(object):
# the rule definition ...
Looks interesting. But I guess my jython understanding is not good enough to know how to integrate this code into my jython rules.
For the time being I would require a kind of step by step explaination where to put this code and also what remains to be done in my jython rules.
If you find time to describe this a bit further, this would be great.
Did you take a look at the associated wiki (linked to in the README.md)? If so, I can try to answer some specific questions about those instructions (and/or update them to be more clear).
I have read the wiki and tried to follow the instructions.
I copied the openhab directory into configuration directory containing the 7 python files
I have added the “import openhab” line and the “@openhab.rule”. But when doing so, I receive a jython script error.
Additionally I have restarted openhab.
I think the import statement cannot find the openhab directory in the configuration folder
Update:
I have checked my startup file and discovered that -Dpython.path = was set to "-Dpython.path=“configurations/scripts/lib” and that the directory “lib” did not exist.
I have no clue why this was done this way. When removing “lib”, the script error is done.
the only change is the Info stating:
EngineName: jython
compiler: discarding redundant interface: org.openhab.core.jsr223.internal.shared.Rule
But It is working now. I am receiving helpfull error messages.
Thanks for the great support!
I’m guessing that when you used the class decorator (@openhab.rule) you didn’t change the Rule base to object from your rule class. I can update the decorator to detect this situation, but if you change…
@openhab.rule
class MyRule(Rule):
# rule defn
to
@openhab.rule
class MyRule(object):
# rule defn
that should eliminate the duplicate interface warning. The warning is given because the decorator adds the Rule interface automatically already.