Timed events in plugins

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
jay (support)
Site Admin
Posts: 19232
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Timed events in plugins

Post by jay (support) »

berkinet wrote:The plugin can be used to create/manage devices, but with an essentially empty plugin.py. Then, the actual work, in this case creating and running an irrigation schedule, could be in a script run by a SCHEDULE. The net effect is the same, but, leaving a plugin running and doing nothing seems somehow wasteful - but, probably doesn't really matter.
But the plugin.py handles all the device management stuff you're talking about - creating/editing/deleting devices. Starting/stopping them, etc. You can't define devices without the appropriate methods.

Now - a plugin may not implement the runConcurrentThread(), and that's fine too (the Action Collection didn't for at first). But I've yet to see an example of a plugin that needs to stop/start to do it's work. My guess is if you have simple enough needs then you'd just distribute a python script file that people could run from the execute script action.
Jay (Indigo Support)
Twitter | Facebook | LinkedIn
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Timed events in plugins

Post by berkinet »

jay wrote:...Now - a plugin may not implement the runConcurrentThread(), and that's fine too (the Action Collection didn't at first). But I've yet to see an example of a plugin that needs to stop/start to do it's work. My guess is if you have simple enough needs then you'd just distribute a python script file that people could run from the execute script action.
That's exactly what I want to do - you just said it better :wink:

The idea is to create a series of custom devices, each which contains its own operational requirements. So, that requires a plugin. But, in fact, the devices never do anything expect represent a real world action (Eg. water the front lawn for 15 minutes every 4th day). Of course, a device may also maintain some properties (the physical device and zone that actually does the watering, etc.) and state data, like last watering duration, days to next watering, etc. But, the device itself does not communicate or really do anything.

So, as @johnpolasek noted, the (scheduled) script would loop over the devices, create a schedule, and then turn on the physical devices as needed.
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Timed events in plugins

Post by berkinet »

I wrote:...So, as @johnpolasek noted, the (scheduled) script would loop over the devices, create a schedule, and then turn on the physical devices as needed.
Of course, the plugin could do this too. And that is what the first couple of questions were about. How to get a plugin to wake up at 6:00, do some work, and then wait until 6:00 the next day.
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Timed events in plugins

Post by berkinet »

It just dawned on me. Like the Action Collection plugin, the irrigation plugin could define an action called start watering. This could then be called from a schedule event. Duh.
Last edited by berkinet on Sat Dec 17, 2011 6:34 pm, edited 1 time in total.
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Timed events in plugins

Post by matt (support) »

berkinet wrote:It just dawned on me. Like the Action Collectoon plugin, the irrigation plugin could define an action called start watering. This could then be called from a schedule event. Duh.
Yep, that is most definitely what I would recommend. :-)
Image
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Timed events in plugins

Post by berkinet »

Here's what I ended up with (for now). This example calls a function to start something (presumably an Indigo device); loops every .5 seconds until the run time has elapsed; and then calls another function to stop whatever had been started. The actual time in the loop is managed by comparing the current time to a pre-determined stop time.

Just to demonstrate that this loop could be interrupted (at least every 1/2 second) I used a Timer to cause the value of "shutdown" to be set to True while the loop was running. I could have also used a timer to manage the stop function, but I still would have had to do something (like looping) to kill time while waiting for the Timer to fire. But, using the Timer to cause the loop to exit was a good way to test the code, and also learn a bit about Timers.

Code: Select all

import time
from datetime import datetime
from datetime import timedelta
from threading import Timer	# For testing only

shutdown = False

def shutdownMethod():		# For testing only
	print("Shutting Down")
	global shutdown
	shutdown = True
	
def startSomeIndigoThing():
	print("something started at %s" % datetime.now())

def stopSomeIndigoThing():
	print("something stopped at %s" % datetime.now())

shutOff = Timer(5.0, shutdownMethod)	# For testing only
shutOff.start() 			# For testing only

runTime = 10.5                # Minutes

startSomeIndigoThing()

now = datetime.now()			# The time we started the loop
then = now + timedelta(minutes=runTime)	# The time we will end the loop
shutdown = False
while now <= then and not shutdown:
	now = datetime.now()
	time.sleep(.5)
Thanks Matt and Jay for your help.
Locked

Return to “Extending Indigo with Plugins and Python”