diff --git a/bindings/python/native/example/__init__.py b/bindings/python/native/example/__init__.py index 5e1f823c..e69de29b 100644 --- a/bindings/python/native/example/__init__.py +++ b/bindings/python/native/example/__init__.py @@ -1,42 +0,0 @@ - -import telldus -import time - -telldus.tdInit() -devices = telldus.tdGetNumberOfDevices() -print "Devices: %d\n" % devices - -allMethods = telldus.TELLDUS_TURNON | telldus.TELLDUS_TURNOFF | telldus.TELLDUS_BELL | telldus.TELLDUS_DIM - -for i in xrange(devices): - deviceid = telldus.tdGetDeviceId(i) - name = telldus.tdGetName(deviceid) - - print "%s - %s\n" % (deviceid, name) - - methods = telldus.tdMethods(deviceid, allMethods) - - if methods & telldus.TELLDUS_TURNON: - print " * TurnOn\n" - telldus.tdTurnOn(deviceid) - time.sleep(1) - - if methods & telldus.TELLDUS_TURNOFF: - print " * TurnOff\n" - telldus.tdTurnOff(deviceid) - time.sleep(1) - - if methods & telldus.TELLDUS_BELL: - echo " * Bell\n" - telldus.tdBell(deviceid) - time.sleep(1) - - if methods & telldus.TELLDUS_TOGGLE: - print " * Toggle\n" - - if methods & telldus.TELLDUS_DIM: - print " * Dim\n" - telldus.tdDim(deviceid, 128) - time.sleep(1) - -telldus.tdClose() \ No newline at end of file diff --git a/bindings/python/native/example/basic.py b/bindings/python/native/example/basic.py new file mode 100644 index 00000000..5e1f823c --- /dev/null +++ b/bindings/python/native/example/basic.py @@ -0,0 +1,42 @@ + +import telldus +import time + +telldus.tdInit() +devices = telldus.tdGetNumberOfDevices() +print "Devices: %d\n" % devices + +allMethods = telldus.TELLDUS_TURNON | telldus.TELLDUS_TURNOFF | telldus.TELLDUS_BELL | telldus.TELLDUS_DIM + +for i in xrange(devices): + deviceid = telldus.tdGetDeviceId(i) + name = telldus.tdGetName(deviceid) + + print "%s - %s\n" % (deviceid, name) + + methods = telldus.tdMethods(deviceid, allMethods) + + if methods & telldus.TELLDUS_TURNON: + print " * TurnOn\n" + telldus.tdTurnOn(deviceid) + time.sleep(1) + + if methods & telldus.TELLDUS_TURNOFF: + print " * TurnOff\n" + telldus.tdTurnOff(deviceid) + time.sleep(1) + + if methods & telldus.TELLDUS_BELL: + echo " * Bell\n" + telldus.tdBell(deviceid) + time.sleep(1) + + if methods & telldus.TELLDUS_TOGGLE: + print " * Toggle\n" + + if methods & telldus.TELLDUS_DIM: + print " * Dim\n" + telldus.tdDim(deviceid, 128) + time.sleep(1) + +telldus.tdClose() \ No newline at end of file diff --git a/bindings/python/native/example/callback.py b/bindings/python/native/example/callback.py new file mode 100644 index 00000000..99289eb0 --- /dev/null +++ b/bindings/python/native/example/callback.py @@ -0,0 +1,60 @@ +import telldus +import time +from threading import Timer + +telldus.tdInit() + +timers = {} #timerlist + +def turnOn(): + print "turning on" + telldus.tdTurnOn(1) + +def turnOff(): + print "turning off" + 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 + +#function to be called when device event occurs, even for unregistered devices +def rawcallback(data, controllerId, callbackId): + print str(data) + +callbackid = telldus.tdRegisterDeviceEvent(callback) +rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback) + +try: + while(1): + time.sleep(0.5) #don't exit +except KeyboardInterrupt: + print "Exiting" + telldus.tdUnregisterCallback(callbackid) + telldus.tdUnregisterCallback(rawcallbackid) + telldus.tdClose() diff --git a/bindings/python/native/telldus.c b/bindings/python/native/telldus.c index 5103bd45..9b1fd947 100644 --- a/bindings/python/native/telldus.c +++ b/bindings/python/native/telldus.c @@ -490,13 +490,20 @@ telldus_tdRegisterSensorEvent(PyObject *self, PyObject *args) return PyLong_FromLong((long) tdRegisterSensorEvent((TDSensorEvent) telldus_sensorEventCallback, &func)); } + +static PyObject * +telldus_tdUnregisterCallback(PyObject *self, PyObject *args) +{ + long id; + + if (!PyArg_ParseTuple(args, "l", &id)); + return NULL; + + return PyBool_FromLong((long) tdUnregisterCallback(id)); +} */ static PyMethodDef telldus_methods[] = { - /* The cast of the function is necessary since PyCFunction values - * only take two PyObject* parameters, and keywdarg_parrot() takes - * three. - */ {"tdInit", (PyCFunction) telldus_tdInit, METH_NOARGS, "Initiate telldus."}, {"tdClose", (PyCFunction) telldus_tdClose, METH_NOARGS, "Close telldus."}, {"tdTurnOn", (PyCFunction) telldus_tdTurnOn, METH_VARARGS, "Turn on device."}, @@ -527,6 +534,7 @@ static PyMethodDef telldus_methods[] = { {"tdRegisterDeviceChangeEvent", (PyCFunction) telldus_tdRegisterDeviceChangeEvent, METH_VARARGS, "RegisterDeviceChangeEvent comment."}, {"tdRegisterRawDeviceEvent", (PyCFunction) telldus_tdRegisterRawDeviceEvent, METH_VARARGS, "RegisterRawDeviceEvent comment."}, {"tdRegisterSensorEvent", (PyCFunction) telldus_tdRegisterSensorEvent, METH_VARARGS, "RegisterSensorEvent comment."}, + {"tdUnregisterCallback", (PyCFunction) telldus_tdUnregisterCallback, METH_VARARGS, "UnregisterCallback comment."}, */ {NULL, NULL, 0, NULL} /* sentinel */ @@ -552,7 +560,9 @@ inittelldus(void) PyObject *TELLSTICK_ERROR_UNKNOWN_GLUE; PyObject *TELLSTICK_TYPE_DEVICE_GLUE; PyObject *TELLSTICK_TYPE_GROUP_GLUE; - + PyObject *TELLSTICK_TEMPERATURE_GLUE; + PyObject *TELLSTICK_HUMIDITY_GLUE; + /* Create the module and add the functions */ /* PyEval_InitThreads(); */ @@ -617,5 +627,14 @@ inittelldus(void) TELLSTICK_TYPE_GROUP_GLUE = PyLong_FromLong((long) TELLSTICK_TYPE_GROUP); PyObject_SetAttrString(module, "TELLSTICK_TYPE_GROUP", TELLSTICK_TYPE_GROUP_GLUE); - Py_DECREF(TELLSTICK_TYPE_GROUP_GLUE); + Py_DECREF(TELLSTICK_TYPE_GROUP_GLUE); + + PyObject *TELLSTICK_TEMPERATURE_GLUE = PyLong_FromLong((long) TELLSTICK_TEMPERATURE); + PyObject_SetAttrString(module, "TELLSTICK_TEMPERATURE", TELLSTICK_TEMPERATURE_GLUE); + Py_DECREF(TELLSTICK_TEMPERATURE_GLUE); + + PyObject *TELLSTICK_HUMIDITY_GLUE = PyLong_FromLong((long) TELLSTICK_HUMIDITY); + PyObject_SetAttrString(module, "TELLSTICK_HUMIDITY", TELLSTICK_HUMIDITY_GLUE); + Py_DECREF(TELLSTICK_HUMIDITY_GLUE); + }