Help with a repeating timer in Indigo 5 plugin

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
User avatar
travisc
Posts: 346
Joined: Tue Sep 07, 2010 7:24 am
Location: Toronto, Canada
Contact:

Help with a repeating timer in Indigo 5 plugin

Post by travisc »

I'm about 95% done my iPhone Presence plugin that detects iPhones on the local network, but I'm getting hung up on creating a silly timer.

Basically I need a recurring timer that fires every minute. I use this to increment the iPhoneLastSeen variables.

What's the easiest way to create a recurring timer for this use? Is there an indigo.system.time.minuteElapsed event I can use? Or an indigo.system.getTime method I can watch to see the time? I've tried creating a standard recurring timer using several other examples found on the web. They work outside of the plugin, but they will not fire during runConcurrentThread.

I'm still figuring out Python so take it easy on me if the answer is obvious. :D

Thanks in advance.

Travis
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help with a repeating timer in Indigo 5 plugin

Post by jay (support) »

Can you just increment the variable yourself inside RunConcurrentThread?

Code: Select all

def runConcurrentThread(self):
	try:
		while True:
			theVarValue = int(indigo.variables[VARID].value)
			theVarValue += 1
			indigo.variable.updateValue(VARID, value=str(theVarValue))
			self.sleep(60)
	except self.StopThread:
		pass
If we had Schedules in the IOM you could also create a Schedule to do it. They aren't quite there yet but will be by the time we GM... :)

Also, just realized I haven't documented this call:

Code: Select all

indigo.server.getTime()
It returns a datetime object with the server's current time. Since your plugin is running on the same Mac as the server, it should return the same thing as the datetime.datetime.now() method.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
travisc
Posts: 346
Joined: Tue Sep 07, 2010 7:24 am
Location: Toronto, Canada
Contact:

Re: Help with a repeating timer in Indigo 5 plugin

Post by travisc »

I'm polling a socket in the runConcurrentThread so I can't do a sleep in there. The socket call times out after a second if there's no data, but when data is received it returns right away. So it's not really a consistent interval to use to keep track of time.

I do like that .getTime call though, that's what I was looking for. I'll use that and check the minutes once in a while. When it changes I know a minute will have passed.

Thanks for the help!

EDIT: Actually after you mentioned datetime it occurred to me I should just use time.time() which returns the seconds from epoch. I'll just check that in the runConcurrentThread, when it incs by 60 a minute has passed. That probably has the least overhead.
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help with a repeating timer in Indigo 5 plugin

Post by jay (support) »

Using time.time() definitely won't cause a round-trip to the server which for this purpose is good.

Note, however, that the runConcurrentThread() method, in order to end gracefully, generally needs to have one of two things happen:
  1. self.sleep() called - that gives the built-in thread stop mechanism (raise self.StopThread) a chance to happen in response to a request by the Indigo Server. You can send sub-seconds there (self.sleep(0.1) for instance) - just call it periodically so that you'll get the self.StopThread exception notification when it's time to shut down. This is the simple method to gracefully shut down your plugin.
  2. Implement stopConcurrentThread() - this is the actual method that the host process calls when the server wants it to shut down - the default implementation just sets a flag that causes the self.sleep() method to throw self.StopThread.
My point is that you need to make sure that runConcurrentThread() will exit gracefully when the server needs your plugin to shut down.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
travisc
Posts: 346
Joined: Tue Sep 07, 2010 7:24 am
Location: Toronto, Canada
Contact:

Re: Help with a repeating timer in Indigo 5 plugin

Post by travisc »

I've already implemented option 2, stopConcurrentThread(). stopConcurrentThread() sets a self.Exit variable to True. RunConcurrentThread() checks to make sure self.Exit is false before it loops again. This guarantees the plugin exits within one second of getting the signal from Indigo to shutdown.

I'll be finished the first release very soon. I'll create a thread in the forum you suggested and post it. Feel free to have a peak inside, I'm certainly open to feedback if I can improve something. Not that you don't have enough to do. :) There's not much to it, and I've followed the docs as best I could, it should be solid.
nlagaros
Posts: 1646
Joined: Mon Dec 20, 2010 2:16 pm

Re: Help with a repeating timer in Indigo 5 plugin

Post by nlagaros »

I'm having to do the same thing in a plugin. Were you successful? If so, can you share some of the concepts to I can integrate in to my plugin?

Thanks.
User avatar
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help with a repeating timer in Indigo 5 plugin

Post by jay (support) »

Look at the code in my first reply post, it seems to be what you need. Rather than increment a variable, start up the thread (I'm assuming it will stop by itself after it does some stuff). Then sleep.

If the spawned thread is long running you may need to implement some mechanism to kill them inside the except - that's where control goes when the plugin is shutting down.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
nlagaros
Posts: 1646
Joined: Mon Dec 20, 2010 2:16 pm

Re: Help with a repeating timer in Indigo 5 plugin

Post by nlagaros »

Thanks. I am going with travisc's approach. I too have a constant loop to poll a socket so can't sleep at all. Counting the seconds is working very well!
Locked

Return to “Extending Indigo with Plugins and Python”