View unanswered posts | View active topics It is currently Mon May 20, 2013 12:38 am



Reply to topic  [ 21 posts ]  Go to page: Previous  1, 2
 Timed events in plugins 
Author Message
Site Admin
User avatar

Joined: Mar 19, 2008
Posts: 6642
Location: Austin, Texas
Post Re: Timed events in plugins
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)
Image


Fri Dec 16, 2011 6:09 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1720
Location: Berkeley, CA
Post Re: Timed events in plugins
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.


Fri Dec 16, 2011 6:29 pm
Profile
User avatar

Joined: Nov 18, 2008
Posts: 1720
Location: Berkeley, CA
Post Re: Timed events in plugins
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.


Fri Dec 16, 2011 6:34 pm
Profile
User avatar

Joined: Nov 18, 2008
Posts: 1720
Location: Berkeley, CA
Post Re: Timed events in plugins
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.



Fri Dec 16, 2011 6:40 pm
Profile
Site Admin
User avatar

Joined: Jan 27, 2003
Posts: 11683
Location: Texas
Post Re: Timed events in plugins
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


Fri Dec 16, 2011 6:47 pm
Profile WWW
User avatar

Joined: Nov 18, 2008
Posts: 1720
Location: Berkeley, CA
Post Re: Timed events in plugins
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.


Sat Dec 17, 2011 6:42 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 21 posts ]  Go to page: Previous  1, 2

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.   Template designed by STSoftware.