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,6 +10,8 @@ allMethods = telldus.TELLSTICK_TURNON | telldus.TELLSTICK_TURNOFF | telldus.TELL
for i in xrange(devices):
deviceid = telldus.tdGetDeviceId(i)
if deviceid:
name = telldus.tdGetName(deviceid)
print "%s - %s\n" % (deviceid, name)
@ -27,7 +29,7 @@ for i in xrange(devices):
time.sleep(1)
if methods & telldus.TELLSTICK_BELL:
echo " * Bell\n"
print " * Bell\n"
telldus.tdBell(deviceid)
time.sleep(1)
@ -41,10 +43,5 @@ for i in xrange(devices):
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 time
telldus.tdInit()
def turnOn():
print "turning on"
telldus.tdTurnOn(1)
@ -16,21 +14,25 @@ def turnOff():
def callback(deviceId, method, value, callbackId):
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()

View file

@ -2,22 +2,22 @@ import telldus
telldus.tdInit()
while(1):
while True:
result = telldus.tdSensor()
if not result:
break
else:
protocol, model, sensorId = result
print "%s,\t%s,\t%i\n" % (protocol, model, sensorId)
protocol, model, sensorId, dataTypes = result
print "Protocol: %s,\tModel: %s,\tSensorId: %i\nDataTypes: %i" % (protocol, model, sensorId, dataTypes)
# 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)
if 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)
if result:
value, timestamp = result
@ -25,4 +25,4 @@ telldus.tdInit()
print "\n"
tdClose()
telldus.tdClose()

View file

@ -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;
@ -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));
@ -576,7 +573,8 @@ telldus_tdSensor(PyObject *self, PyObject *args)
}
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, &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)
{
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;
}
}