working examples feature complete

This commit is contained in:
Øyvind Saltvik 2012-08-10 20:37:16 +02:00
parent 9073d60858
commit c5c64435cf
4 changed files with 70 additions and 88 deletions

View file

@ -10,41 +10,38 @@ allMethods = telldus.TELLSTICK_TURNON | telldus.TELLSTICK_TURNOFF | telldus.TELL
for i in xrange(devices): for i in xrange(devices):
deviceid = telldus.tdGetDeviceId(i) deviceid = telldus.tdGetDeviceId(i)
name = telldus.tdGetName(deviceid)
print "%s - %s\n" % (deviceid, name) if deviceid:
name = telldus.tdGetName(deviceid)
methods = telldus.tdMethods(deviceid, allMethods) print "%s - %s\n" % (deviceid, name)
if methods & telldus.TELLSTICK_TURNON: methods = telldus.tdMethods(deviceid, allMethods)
print " * TurnOn\n"
telldus.tdTurnOn(deviceid)
time.sleep(1)
if methods & telldus.TELLSTICK_TURNOFF: if methods & telldus.TELLSTICK_TURNON:
print " * TurnOff\n" print " * TurnOn\n"
telldus.tdTurnOff(deviceid) telldus.tdTurnOn(deviceid)
time.sleep(1) time.sleep(1)
if methods & telldus.TELLSTICK_BELL: if methods & telldus.TELLSTICK_TURNOFF:
echo " * Bell\n" print " * TurnOff\n"
telldus.tdBell(deviceid) telldus.tdTurnOff(deviceid)
time.sleep(1) time.sleep(1)
if methods & telldus.TELLSTICK_TOGGLE: if methods & telldus.TELLSTICK_BELL:
print " * Toggle\n" print " * Bell\n"
telldus.tdBell(deviceid)
time.sleep(1)
if methods & telldus.TELLSTICK_DIM: if methods & telldus.TELLSTICK_TOGGLE:
print " * Dim\n" print " * Toggle\n"
telldus.tdDim(deviceid, 128)
time.sleep(1) if methods & telldus.TELLSTICK_DIM:
print " * Dim\n"
telldus.tdDim(deviceid, 128)
time.sleep(1)
telldus.tdClose() telldus.tdClose()
import time
time.sleep(30)
for i in xrange(20):
telldus.tdTurnOn(3)
telldus.tdTurnOff(3)

View file

@ -2,10 +2,8 @@ import threading
import telldus import telldus
import time import time
telldus.tdInit() telldus.tdInit()
def turnOn(): def turnOn():
print "turning on" print "turning on"
telldus.tdTurnOn(1) telldus.tdTurnOn(1)
@ -16,21 +14,25 @@ def turnOff():
def callback(deviceId, method, value, callbackId): def callback(deviceId, method, value, callbackId):
print "callback" print "callback"
print "DeviceId: %i Method: %i Value: %s" % (deviceId, method, value)
return True return True
#function to be called when device event occurs, even for unregistered devices #function to be called when device event occurs, even for unregistered devices
def rawcallback(data, controllerId, callbackId): def rawcallback(data, controllerId, callbackId):
print "raw callback" print "raw callback"
print "Data: %s ControllerId: %i" % (data, controllerId)
return True return True
#callbackid = telldus.tdRegisterDeviceEvent(callback) callbackid = telldus.tdRegisterDeviceEvent(callback)
rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback) rawcallbackid = telldus.tdRegisterRawDeviceEvent(rawcallback)
print callbackid, rawcallbackid
try: try:
while(1): while(1):
time.sleep(0.5) #don't exit time.sleep(0.5) #don't exit
except KeyboardInterrupt: except KeyboardInterrupt:
print "Exiting" print "Exiting"
#telldus.tdUnregisterCallback(callbackid) telldus.tdUnregisterCallback(callbackid)
telldus.tdUnregisterCallback(rawcallbackid) telldus.tdUnregisterCallback(rawcallbackid)
telldus.tdClose() telldus.tdClose()

View file

@ -2,27 +2,27 @@ import telldus
telldus.tdInit() telldus.tdInit()
while(1): while True:
result = telldus.tdSensor() result = telldus.tdSensor()
if not result: if not result:
break break
else: else:
protocol, model, sensorId = result protocol, model, sensorId, dataTypes = result
print "%s,\t%s,\t%i\n" % (protocol, model, sensorId) print "Protocol: %s,\tModel: %s,\tSensorId: %i\nDataTypes: %i" % (protocol, model, sensorId, dataTypes)
# Retrieve the values the sensor supports # Retrieve the values the sensor supports
if (dataTypes & telldus.TELLSTICK_TEMPERATURE): if dataTypes & telldus.TELLSTICK_TEMPERATURE:
result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_TEMPERATURE) result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_TEMPERATURE)
if result: if result:
value, timestamp = result value, timestamp = result
print "Temperature:\t%sº\t(%s)\n" % (value, str(timestamp)) print "Temperature:\t%sC\t(%s)\n" % (value, str(timestamp))
if (dataTypes & telldus.TELLSTICK_HUMIDITY): if dataTypes & telldus.TELLSTICK_HUMIDITY:
result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_HUMIDITY) result = telldus.tdSensorValue(protocol, model, sensorId, telldus.TELLSTICK_HUMIDITY)
if result: if result:
value, timestamp = result value, timestamp = result
print "Humidity:\t%s\t(%s)\n" % (value, str(timestamp)) print "Humidity:\t%s\t(%s)\n" % (value, str(timestamp))
print "\n" print "\n"
tdClose() telldus.tdClose()

View file

@ -31,9 +31,6 @@ estrdup(char *s)
static PyObject * static PyObject *
telldus_tdInit(PyObject *self) telldus_tdInit(PyObject *self)
{ {
PyEval_InitThreads();
PyEval_ReleaseLock();
tdInit(); tdInit();
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
@ -386,7 +383,7 @@ telldus_tdRegisterDeviceEvent(PyObject *self, PyObject *args)
DeviceEventCallback = func; DeviceEventCallback = func;
tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0); result = tdRegisterDeviceEvent((TDDeviceEvent) &telldus_deviceEventCallback, 0);
return PyLong_FromLong((long) result); return PyLong_FromLong((long) result);
} }
@ -552,7 +549,7 @@ telldus_tdUnregisterCallback(PyObject *self, PyObject *args)
{ {
long id; long id;
if (!PyArg_ParseTuple(args, "l", &id)); if (!PyArg_ParseTuple(args, "l", &id))
return NULL; return NULL;
return PyLong_FromLong((long) tdUnregisterCallback(id)); return PyLong_FromLong((long) tdUnregisterCallback(id));
@ -568,7 +565,7 @@ telldus_tdSensor(PyObject *self, PyObject *args)
long result; 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) if (result == TELLSTICK_SUCCESS)
{ {
@ -576,7 +573,8 @@ telldus_tdSensor(PyObject *self, PyObject *args)
} }
else 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; long dataType = 0;
char value[DATA_LENGTH]; char value[DATA_LENGTH];
long timestamp = 0; long timestamp = 0;
PyObject *floatObj;
PyObject *timeTuple;
long result; 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; return NULL;
result = tdSensorValue(protocol, model, sensorId, dataType, &value, DATA_LENGTH, &timestamp); result = tdSensorValue(protocol, model, sensorId, dataType, &value, DATA_LENGTH, &timestamp);
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) if (result == TELLSTICK_SUCCESS)
{ {
return Py_BuildValue("sO", value, py_date); return Py_BuildValue("sl", value, timestamp);
} }
else else
{ {
return PyLong_FromLong(result); Py_INCREF(Py_None);
return Py_None;
} }
} }