- Platform information:
- Hardware: Raspberry Pi 3 B+, 120gb mSATA SSD in a USB enclosure
- OS: Raspbian Stretch
- Java Runtime Environment: 1.8.0_212
- openHAB version: 2.4.0 release build. Jython 2.7.0
Hi there!
Short version: The script execution context of a JSR223 Jython script is not terminated when a script is updated and reloaded, so you end up with old recurring timers or threads running simultaneously, fighting each other.
I’m a new to openhab (and linux) but I have 20 years of C/C++ experience as a Windows developer. I’m trying to migrate from fibaro (lua) before my system grows any bigger as it’s already showing serious cracks. Experiencing some serious learning curve issues with openHAB but working through them – openHAB is incredibly powerful if you know how to use it, so it really looks like the best answer. I’ve spent over a week watching tutorials, reading documentation, following guide, and experimenting, but here’s where I’m stuck and could use a few pointers if possible.
I have exterior lights all around my property on 3 separate circuits in a round-robin fashion (1-2-3-1-2-3), controlled by a web relay. Controlling them is as simple as sending an HTTP request or an UDP packet.
I’m trying to make lights either flash or chase. Chase mode is strictly for showing off, so long-term performance is not an issue (I never leave them in chase mode for very long) but I do need to loop at 3 Hz or so (interval ~300ms).
Flashing is for the door bell. If I’m out in the garden far from the house, it would be great if the lights flash a couple of times if someone happens to ring the door bell when I’m outside… and if it’s currently in chase mode it should flash in a way that is noticeable and then go back to chase. It’s those kind of features that make it necessary to use a recurring timer of some kind.
At first, I was going to try to do it in the Rules DSL (after all I managed to do it in LUA in a fibaro HC2 scene so how hard could it be!). But, then I saw an answer from a highly skilled volunteer in another thread about looping timers, that anything below 750ms should not be implemented in a rule DSL for performance reasons. He did say he didn’t think it should be a JSR223 rule either but for reasons not applicable to my particular case (not performance related). Here is that thread: Design Pattern: Looping Timers
So, I’m trying to implement this recurring loop in Jython. It really doesn’t feel like this should be difficult, but this is where I’m stuck. In other platforms I know, using a recurring timer is the easiest thing in the world. Hopefully I’m just doing it the wrong way.
So, the problem:
I can create a thread with a while loop…
from threading import Thread
import time
def thread_function(name):
for i in xrange(10) :
LoggerFactory.getLogger("org.eclipse.smarthome.automation.examples").info("looping in thread")
time.sleep(2)
LoggerFactory.getLogger("org.eclipse.smarthome.automation.examples").info("thread done")
x=Thread(target=thread_function,args=(1,))
x.start()
…or I can create a timer which reinitializes itself on the callback.
from threading import Timer
def timerhandler():
LoggerFactory.getLogger(“org.eclipse.smarthome.automation.examples”).info(“tick tock”)
t=Timer(3.0,timerhandler)
t.start()
timerhandler()
The problem is:
When the script reloads (because i made a change), the thread or recurring timer from the previous version never stops running. So then there’s two of them running, fighting. Another time and there’s three. sudo service openhab2 restart is the only way out.
Surely there must be a better way. CRON won’t do it because I need higher granularity than 1 second for this particular purpose.
So, what is the best way of having something recurring (at an interval of let’s say 3 times per second) running as a script inside OpenHAB?
It doesn’t have to be Jython. I’m still just starting out with OpenHAB and I have no previous python or java experience so I can totally use another form of scripting if there’s a better way.
Thanks very much in advance!
///Leif