diff --git a/bindings/python/native/example/basic.py b/bindings/python/native/example/basic.py index dd260231..ad5e37a0 100644 --- a/bindings/python/native/example/basic.py +++ b/bindings/python/native/example/basic.py @@ -10,41 +10,38 @@ allMethods = telldus.TELLSTICK_TURNON | telldus.TELLSTICK_TURNOFF | telldus.TELL 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.TELLSTICK_TURNON: - print " * TurnOn\n" - telldus.tdTurnOn(deviceid) - time.sleep(1) + + if deviceid: + name = telldus.tdGetName(deviceid) + + print "%s - %s\n" % (deviceid, name) - if methods & telldus.TELLSTICK_TURNOFF: - print " * TurnOff\n" - telldus.tdTurnOff(deviceid) - time.sleep(1) + methods = telldus.tdMethods(deviceid, allMethods) - if methods & telldus.TELLSTICK_BELL: - echo " * Bell\n" - telldus.tdBell(deviceid) - time.sleep(1) - - if methods & telldus.TELLSTICK_TOGGLE: - print " * Toggle\n" - - if methods & telldus.TELLSTICK_DIM: - print " * Dim\n" - telldus.tdDim(deviceid, 128) - time.sleep(1) + if methods & telldus.TELLSTICK_TURNON: + print " * TurnOn\n" + telldus.tdTurnOn(deviceid) + time.sleep(1) + + if methods & telldus.TELLSTICK_TURNOFF: + print " * TurnOff\n" + telldus.tdTurnOff(deviceid) + time.sleep(1) + + if methods & telldus.TELLSTICK_BELL: + print " * Bell\n" + telldus.tdBell(deviceid) + time.sleep(1) + + if methods & telldus.TELLSTICK_TOGGLE: + print " * Toggle\n" + + if methods & telldus.TELLSTICK_DIM: + print " * Dim\n" + telldus.tdDim(deviceid, 128) + time.sleep(1) telldus.tdClose() -import time -time.sleep(30) -for i in xrange(20): - telldus.tdTurnOn(3) - telldus.tdTurnOff(3) diff --git a/bindings/python/native/example/callback.py b/bindings/python/native/example/callback.py index 43774674..a6242a6f 100644 --- a/bindings/python/native/example/callback.py +++ b/bindings/python/native/example/callback.py @@ -2,10 +2,8 @@ import threading import telldus import time - telldus.tdInit() - def turnOn(): print "turning on" telldus.tdTurnOn(1) @@ -15,22 +13,26 @@ def turnOff(): telldus.tdTurnOff(1) def callback(deviceId, method, value, callbackId): - print "callback" + print "callback" + print "DeviceId: %i Method: %i Value: %s" % (deviceId, method, value) return True #function to be called when device event occurs, even for unregistered devices def rawcallback(data, controllerId, callbackId): print "raw callback" + print "Data: %s ControllerId: %i" % (data, controllerId) return True -#callbackid = telldus.tdRegisterDeviceEvent(callback) +callbackid = telldus.tdRegisterDeviceEvent(callback) rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback) +print callbackid, rawcallbackid + try: while(1): time.sleep(0.5) #don't exit except KeyboardInterrupt: print "Exiting" - #telldus.tdUnregisterCallback(callbackid) + telldus.tdUnregisterCallback(callbackid) telldus.tdUnregisterCallback(rawcallbackid) telldus.tdClose() diff --git a/bindings/python/native/example/poll.py b/bindings/python/native/example/poll.py index 7061b5a8..71cba2c6 100644 --- a/bindings/python/native/example/poll.py +++ b/bindings/python/native/example/poll.py @@ -2,27 +2,27 @@ import telldus telldus.tdInit() - while(1): - result = telldus.tdSensor() - if not result: - break - else: - protocol, model, sensorId = result - print "%s,\t%s,\t%i\n" % (protocol, model, sensorId) - - # Retrieve the values the sensor supports - if (dataTypes & telldus.TELLSTICK_TEMPERATURE): - result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_TEMPERATURE) - if result: - value, timestamp = result - print "Temperature:\t%sÂș\t(%s)\n" % (value, str(timestamp)) - - if (dataTypes & telldus.TELLSTICK_HUMIDITY): - result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_HUMIDITY) - if result: - value, timestamp = result - print "Humidity:\t%s\t(%s)\n" % (value, str(timestamp)) - - print "\n" +while True: + result = telldus.tdSensor() + if not result: + break + else: + protocol, model, sensorId, dataTypes = result + print "Protocol: %s,\tModel: %s,\tSensorId: %i\nDataTypes: %i" % (protocol, model, sensorId, dataTypes) -tdClose() + # Retrieve the values the sensor supports + if dataTypes & telldus.TELLSTICK_TEMPERATURE: + result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_TEMPERATURE) + if result: + value, timestamp = result + print "Temperature:\t%sC\t(%s)\n" % (value, str(timestamp)) + + if dataTypes & telldus.TELLSTICK_HUMIDITY: + result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_HUMIDITY) + if result: + value, timestamp = result + print "Humidity:\t%s\t(%s)\n" % (value, str(timestamp)) + + print "\n" + +telldus.tdClose() diff --git a/bindings/python/native/telldus.c b/bindings/python/native/telldus.c index 68cd74b4..4570bbd3 100644 --- a/bindings/python/native/telldus.c +++ b/bindings/python/native/telldus.c @@ -31,9 +31,6 @@ estrdup(char *s) static PyObject * telldus_tdInit(PyObject *self) { - PyEval_InitThreads(); - PyEval_ReleaseLock(); - tdInit(); Py_INCREF(Py_None); return Py_None; @@ -157,10 +154,10 @@ static PyObject * telldus_tdGetDeviceId(PyObject *self, PyObject *args) { long index; - + if (!PyArg_ParseTuple(args, "l", &index)) return NULL; - + return PyLong_FromLong((long) tdGetDeviceId(index)); } @@ -386,7 +383,7 @@ telldus_tdRegisterDeviceEvent(PyObject *self, PyObject *args) DeviceEventCallback = func; - tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0); + result = tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0); return PyLong_FromLong((long) result); } @@ -552,7 +549,7 @@ telldus_tdUnregisterCallback(PyObject *self, PyObject *args) { long id; - if (!PyArg_ParseTuple(args, "l", &id)); + if (!PyArg_ParseTuple(args, "l", &id)) return NULL; return PyLong_FromLong((long) tdUnregisterCallback(id)); @@ -568,15 +565,16 @@ telldus_tdSensor(PyObject *self, PyObject *args) long result; - result = tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes); - + result = tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes); + if (result == TELLSTICK_SUCCESS) { return Py_BuildValue("ssll", protocol, model, sensorId, dataTypes); } else { - return PyLong_FromLong(result); + Py_INCREF(Py_None); + return Py_None; } } @@ -590,36 +588,21 @@ telldus_tdSensorValue(PyObject *self, PyObject *args) long dataType = 0; char value[DATA_LENGTH]; long timestamp = 0; - - PyObject *floatObj; - PyObject *timeTuple; - long result; - PyObject *py_date; - - if (!PyArg_ParseTuple(args, "ssll", &protocol, &model, &sensorId, &dataType)); + if (!PyArg_ParseTuple(args, "ssll", &protocol, &model, &sensorId, &dataType)) return NULL; result = tdSensorValue(protocol, model, sensorId, dataType, &value, DATA_LENGTH, ×tamp); - floatObj = PyFloat_FromDouble(timestamp); - timeTuple = Py_BuildValue("(O)", floatObj); - Py_DECREF(floatObj); - - py_date = PyDateTime_FromTimestamp(timeTuple); - - Py_DECREF(timeTuple); - - Py_INCREF(py_date); - if (result == TELLSTICK_SUCCESS) { - return Py_BuildValue("sO", value, py_date); + return Py_BuildValue("sl", value, timestamp); } else { - return PyLong_FromLong(result); + Py_INCREF(Py_None); + return Py_None; } }