Cancel timer in jsr223 Jython

Hi!

I use a timer under Jython like:

oh.createTimer(DateTime.now().plusMinutes(1), TimerInternetPowerOff)

But I dont know, how can I cancel the timer.
Is there something like oh.cancelTimer?

Thanks for a hint!

Martin

It might work different in the jsr223, but in the rules createTimer() returns a reference to the created timer. If you store that in a variable you can then call myTimer.cancel.

I’m not sure what createTimer returns in jsr223 though.

In the “normal rule” that works. But in Jython, there is no variable to save the timer object. Or?

Perhaps I don’t understand Jython or the jsr223 plugin, but my understanding was that you should be able to declare and assign variables the same way you would in any normal Jython script when using jsr223.

For example, in normal Python (besides the running on the JVM I don’t know the difference between Jython and Python) I would do something like:

timer = oh.createTimer(DateTime.now().plusMinutes(1), TimerInternetPowerOff)

# stuff

timer.cancel()

Not having used jsr223 I can’t say if this will certainly work or not, but it seems like it should.

Not empirically sure Rick, but I know some Python/Jython and I would expect your example to be “close enough for jazz”.

You can save the timer instance in a module-level (“global”) attribute, a class attribute or even in a closure.

Hi!

Its working!

import datetime

timer = None

class TestRule(Rule):
    def getEventTrigger(self):
        return [
            StartupTrigger(),
            TimerTrigger("*/5 * * * * ?")
        ]

    def execute(self, event):
		global timer

		def TimerFunc():
			oh.logInfo("TestRule", "TimerFunc")
		
		if(timer != None):
			oh.logInfo("TestRule", "cancel timer")
			timer.cancel()
			timer = None
			
		oh.logInfo("TestRule", "start timer")		
		timer = oh.createTimer(DateTime.now().plusSeconds(10), TimerFunc)
					
def getRules():
    return RuleSet([
        TestRule()
    ])

The funny thing is: I write this lines allready, but I dont tried it because i thought it doesnt work! :unamused:

thx for the help

It’s better to store timer as a rule member instead global variable.

class TestRule(Rule):
    def __init__(self):
        self.timer = None
    
    def getEventTrigger(self):
        return [
            StartupTrigger(),
            TimerTrigger("*/5 * * * * ?")
        ]
 
    def execute(self, event):
	    def TimerFunc():
			oh.logInfo("TestRule", "TimerFunc")
		
		if not self.timer is None:
			oh.logInfo("TestRule", "cancel timer")
			self.timer.cancel()
			
		oh.logInfo("TestRule", "start timer")		
		self.timer = oh.createTimer(DateTime.now().plusSeconds(10), TimerFunc)

Also you can use python’s timer from threading package and don’t mix spaces and tab in python.

1 Like

thanks for the hints!

Just for further reference:
Creating a timer with 1 … n arguments is really easy, too:

#define in the code somewhere
def NotificationTimerWaitHandler(name):
    #do something with parameter

#timer call somethere else:
    name = "functionname"
    oh.createTimer(DateTime.now().plusSeconds(90), lambda : NotificationTimerWaitHandler(name))