working with callbacks
This commit is contained in:
parent
517c987b6d
commit
9073d60858
3 changed files with 162 additions and 121 deletions
|
@ -1,10 +1,10 @@
|
|||
import threading
|
||||
import telldus
|
||||
import time
|
||||
from threading import Timer
|
||||
|
||||
|
||||
telldus.tdInit()
|
||||
|
||||
timers = {} #timerlist
|
||||
|
||||
def turnOn():
|
||||
print "turning on"
|
||||
|
@ -15,39 +15,15 @@ def turnOff():
|
|||
telldus.tdTurnOff(1)
|
||||
|
||||
def callback(deviceId, method, value, callbackId):
|
||||
global timers
|
||||
|
||||
print "callback!"
|
||||
|
||||
if (deviceId == 1):
|
||||
# is turning on deviceId 1 here, so just return if events for that device are picked up
|
||||
return
|
||||
|
||||
t = 0
|
||||
print "Received event for device %d" % (deviceId,)
|
||||
if (deviceId in timers):
|
||||
# a timer already exists for this device, it might be running so interrupt it
|
||||
# Many devices (for example motion detectors) resends their messages many times to ensure that they
|
||||
# are received correctly. In this example, we don't want to run the turnOn/turnOff methods every time, instead we
|
||||
# start a timer, and run the method when the timer is finished. For every incoming event on this device, the timer
|
||||
# is restarted.
|
||||
t = timers[deviceId]
|
||||
t.cancel()
|
||||
if (method == 1):
|
||||
#on
|
||||
t = Timer(0.5, turnOn) #start timer with 0.5 second delay (adjust the delay to suit your needs), then turn on
|
||||
else:
|
||||
#off
|
||||
t = Timer(0.5, turnOff) #start timer with 0.5 second delay (adjust the delay to suit your needs), then turn off
|
||||
|
||||
t.start()
|
||||
timers[deviceId] = t #put timer in list, to allow later cancellation
|
||||
print "callback"
|
||||
return True
|
||||
|
||||
#function to be called when device event occurs, even for unregistered devices
|
||||
def rawcallback(data, controllerId, callbackId):
|
||||
print str(data)
|
||||
print "raw callback"
|
||||
return True
|
||||
|
||||
callbackid = telldus.tdRegisterDeviceEvent(callback)
|
||||
#callbackid = telldus.tdRegisterDeviceEvent(callback)
|
||||
rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback)
|
||||
|
||||
try:
|
||||
|
@ -55,6 +31,6 @@ try:
|
|||
time.sleep(0.5) #don't exit
|
||||
except KeyboardInterrupt:
|
||||
print "Exiting"
|
||||
telldus.tdUnregisterCallback(callbackid)
|
||||
#telldus.tdUnregisterCallback(callbackid)
|
||||
telldus.tdUnregisterCallback(rawcallbackid)
|
||||
telldus.tdClose()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue