forked from EliasGabrielsson/tdtool-improved.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
td.py
610 lines (463 loc) · 19.3 KB
/
td.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#!/usr/bin/env python
# ******************************************
#
# Python wrapper for libtelldus on Linux
#
# Developed by David Karlsson
# (david.karlsson.80@gmail.com)
#
# Released as is without any garantees on
# functionality.
#
# *******************************************
import platform
import time
from ctypes import c_int, c_ubyte, c_void_p, c_char_p, POINTER, string_at,\
create_string_buffer, byref
debug = False
#platform specific imports and CFUNC definitions:
if (platform.system() == 'Windows'):
#Windows
from ctypes import windll, WINFUNCTYPE
tdlib = windll.LoadLibrary('TelldusCore.dll')
DEVICEFUNC = WINFUNCTYPE(None, c_int, c_int, c_char_p, c_int, c_void_p)
DEVICECHANGEFUNC = WINFUNCTYPE(None, c_int, c_int, c_int, c_int, c_void_p)
SENSORFUNC = WINFUNCTYPE(None, c_char_p, c_char_p, c_int, c_int, c_char_p, c_int, c_int, c_void_p)
RAWDEVICEFUNC = WINFUNCTYPE(None, c_char_p, c_int, c_int, c_void_p)
else:
if (platform.system() == 'Darwin'):
#Mac
from ctypes import cdll, CFUNCTYPE
tdlib = cdll.LoadLibrary('/Library/Frameworks/TelldusCore.framework/TelldusCore')
else:
#Others; if not found, try adding the directory with the file to env var LD_LIBRARY_PATH
from ctypes import cdll, CFUNCTYPE
tdlib = cdll.LoadLibrary('libtelldus-core.so.2')
# Make tdReleasString work on *BSD
tdlib.tdReleaseString.argtypes = [c_void_p]
tdlib.tdReleaseString.restype = None
DEVICEFUNC = CFUNCTYPE(None, c_int, c_int, c_char_p, c_int, c_void_p)
DEVICECHANGEFUNC = CFUNCTYPE(None, c_int, c_int, c_int, c_int, c_void_p)
SENSORFUNC = CFUNCTYPE(None, c_char_p, c_char_p, c_int, c_int, c_char_p, c_int, c_int, c_void_p)
RAWDEVICEFUNC = CFUNCTYPE(None, c_char_p, c_int, c_int, c_void_p)
#Device methods
TELLSTICK_TURNON = 1
TELLSTICK_TURNOFF = 2
TELLSTICK_BELL = 4
TELLSTICK_TOGGLE = 8
TELLSTICK_DIM = 16
TELLSTICK_LEARN = 32
TELLSTICK_EXECUTE = 64
TELLSTICK_UP = 128
TELLSTICK_DOWN = 256
TELLSTICK_STOP = 512
TELLSTICK_ALL = 0x3FF
methodsReadable = {1: 'ON',
2: 'OFF',
4: 'BELL',
8: 'TOGGLE',
16: 'DIM',
32: 'LEARN',
64: 'EXECUTE',
128: 'UP',
256: 'DOWN',
512: 'STOP'}
#Sensor value types
TELLSTICK_TEMPERATURE = 1
TELLSTICK_HUMIDITY = 2
TELLSTICK_RAINRATE = 4
TELLSTICK_RAINTOTAL = 8
TELLSTICK_WINDDIRECTION = 16
TELLSTICK_WINDAVERAGE = 32
TELLSTICK_WINDGUST = 64
sensorValueTypeReadable = {TELLSTICK_TEMPERATURE: 'Temperature',
TELLSTICK_HUMIDITY: 'Humidity',
TELLSTICK_RAINRATE: 'Rain rate',
TELLSTICK_RAINTOTAL: 'Rain total',
TELLSTICK_WINDDIRECTION: 'Wind direction',
TELLSTICK_WINDAVERAGE: 'Wind average',
TELLSTICK_WINDGUST: 'Wind gust'
}
#Error codes
TELLSTICK_SUCCESS = 0
TELLSTICK_ERROR_NOT_FOUND = -1
TELLSTICK_ERROR_PERMISSION_DENIED = -2
TELLSTICK_ERROR_DEVICE_NOT_FOUND = -3
TELLSTICK_ERROR_METHOD_NOT_SUPPORTED = -4
TELLSTICK_ERROR_COMMUNICATION = -5
TELLSTICK_ERROR_CONNECTING_SERVICE = -6
TELLSTICK_ERROR_UNKNOWN_RESPONSE = -7
TELLSTICK_ERROR_SYNTAX = -8
TELLSTICK_ERROR_BROKEN_PIPE = -9
TELLSTICK_ERROR_COMMUNICATING_SERVICE = -10
TELLSTICK_ERROR_CONFIG_SYNTAX = -11
TELLSTICK_ERROR_UNKNOWN = -99
#Controller typedef
TELLSTICK_CONTROLLER_TELLSTICK = 1
TELLSTICK_CONTROLLER_TELLSTICK_DUO = 2
TELLSTICK_CONTROLLER_TELLSTICK_NET = 3
#Device changes
TELLSTICK_DEVICE_ADDED = 1
TELLSTICK_DEVICE_CHANGED = 2
TELLSTICK_DEVICE_REMOVED = 3
TELLSTICK_DEVICE_STATE_CHANGED = 4
#Change types
TELLSTICK_CHANGE_NAME = 1
TELLSTICK_CHANGE_PROTOCOL = 2
TELLSTICK_CHANGE_MODEL = 3
TELLSTICK_CHANGE_METHOD = 4
TELLSTICK_CHANGE_AVAILABLE = 5
TELLSTICK_CHANGE_FIRMWARE = 6
methodsSupportedDefault = 0
_callbackFuncs = {}
def getNumberOfDevices():
return tdlib.tdGetNumberOfDevices()
def getDeviceId(i):
return tdlib.tdGetDeviceId(int(i))
def getDeviceIdFromStr(s):
try:
id = int(s)
devId = getDeviceId(id)
return devId, getName(devId)
except:
pass
for i in range(getNumberOfDevices()):
if s == getName(getDeviceId(i)):
return getDeviceId(i), s
return -1, 'UNKNOWN'
def getName(id):
getNameFunc = tdlib.tdGetName
getNameFunc.restype = c_void_p
vp = getNameFunc(id)
cp = c_char_p(vp)
s = cp.value
tdlib.tdReleaseString(vp)
return s
def methods(id, methodsSupported = None, readable = False):
if methodsSupported == None:
methodsSupported = methodsSupportedDefault
methods = tdlib.tdMethods(id, methodsSupported)
if readable:
l = []
for m in methodsReadable:
if methods & m:
l.append(methodsReadable[m])
return ','.join(l)
return methods
def turnOn(intDeviceId):
return tdlib.tdTurnOn(intDeviceId)
def turnOff(intDeviceId):
return tdlib.tdTurnOff(intDeviceId)
def bell(intDeviceId):
return tdlib.tdBell(intDeviceId)
def dim(intDeviceId, level):
return tdlib.tdDim(intDeviceId, level)
def up(intDeviceId):
return tdlib.tdUp(intDeviceId)
def down(intDeviceId):
return tdlib.tdDown(intDeviceId)
def stop(intDeviceId):
return tdlib.tdStop(intDeviceId)
def learn(intDeviceId):
return tdlib.tdLearn(intDeviceId)
def lastSentCommand(intDeviceId, methodsSupported = None, readable = False):
if methodsSupported == None:
methodsSupported = methodsSupportedDefault
if readable:
return methodsReadable.get(tdlib.tdLastSentCommand(intDeviceId, methodsSupported), 'UNKNOWN')
return tdlib.tdLastSentCommand(intDeviceId, methodsSupported)
def lastSentValue(id_):
func = tdlib.tdLastSentValue
func.restype = c_char_p
return func(id_)
def getErrorString(intErrorNo):
getErrorStringFunc = tdlib.tdGetErrorString
getErrorStringFunc.restype = c_void_p
vp = getErrorStringFunc(intErrorNo)
cp = c_char_p(vp)
s = cp.value
tdlib.tdReleaseString(vp)
return s
def addDevice():
return tdlib.tdAddDevice()
def removeDevice(intDeviceId):
return tdlib.tdRemoveDevice(intDeviceId)
def setName(intDeviceId, chNewName):
if not isinstance(chNewName, str):
raise ValueError('chNewName needs to be a str')
if not isinstance(intDeviceId, int):
raise ValueError('intDeviceId needs to be an integer')
return tdlib.tdSetName(intDeviceId, chNewName)
def getProtocol(intDeviceId):
if not isinstance(intDeviceId, int):
raise ValueError('intDeviceId needs to be an integer')
tdGetProtocolFunc = tdlib.tdGetProtocol
tdGetProtocolFunc.restype = c_void_p
vp = tdGetProtocolFunc(intDeviceId)
cp = c_char_p(vp)
s = cp.value
tdlib.tdReleaseString(vp)
return s
def getModel(intDeviceId):
if not isinstance(intDeviceId, int):
raise ValueError('intDeviceId needs to be an integer')
tdGetModelFunc = tdlib.tdGetModel
tdGetModelFunc.restype = c_void_p
vp = tdGetModelFunc(intDeviceId)
cp = c_char_p(vp)
s = cp.value
tdlib.tdReleaseString(vp)
return s
def getDeviceParameter(intDeviceId, strName, defaultValue):
if not isinstance(intDeviceId, int):
raise ValueError('intDeviceId needs to be an integer')
if not isinstance(strName, str):
raise ValueError('strName needs to be a str')
if not isinstance(defaultValue, str):
raise ValueError('defaultValue needs to be a str')
tdGetDeviceParameterFunc = tdlib.tdGetDeviceParameter
tdGetDeviceParameterFunc.restype = c_void_p
vp = tdGetDeviceParameterFunc(intDeviceId, strName, defaultValue)
cp = c_char_p(vp)
s = cp.value
tdlib.tdReleaseString(vp)
return s
def init(defaultMethods = 0):
#defaultMethods could be one or many from: TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_BELL | TELLSTICK_TOGGLE | TELLSTICK_DIM | TELLSTICK_LEARN | TELLSTICK_EXECUTE | TELLSTICK_UP | TELLSTICK_DOWN | TELLSTICK_STOP
global methodsSupportedDefault
methodsSupportedDefault = defaultMethods
tdlib.tdInit()
def close():
tdlib.tdClose()
callbacks = {'lastAdd': 0,
'deviceEvent': {},
'deviceChangeEvent': {},
'sensorEvent': {},
'rawDeviceEvent': {}
}
def deviceEvent(deviceId, method, data, callbackId, context):
if debug:
print 'DeviceEvent'
print 'deviceId:', deviceId
print 'method:', method
print 'data:', data
print 'callbackId:', callbackId
print 'context:', context
for key in callbacks['deviceEvent']:
f = callbacks['deviceEvent'][key]
try:
f(deviceId, method, data, callbackId)
except:
print 'Error calling registered callback for deviceEvent'
if debug:
raise
def deviceChangeEvent(deviceId, changeEvent, changeType, callbackId, context):
if debug:
print 'DeviceChangeEvent'
print 'deviceId:', deviceId
print 'changeEvent:', changeEvent
print 'changeType:', changeType
print 'callbackId:', callbackId
for key in callbacks['deviceChangeEvent']:
f = callbacks['deviceChangeEvent'][key]
try:
f(deviceId, changeEvent, changeType, callbackId)
except:
print 'Error calling registered callback for deviceChangeEvent'
if debug:
raise
def sensorEvent(protocol, model, id, dataType, value, timestamp, callbackId, context):
if debug:
print 'SensorEvent'
print 'protocol:', protocol
print 'model:', model
print 'id:', id
print 'datatype:', dataType
print 'value:', value
print 'timestamp:', timestamp
print 'callbackId:', callbackId
print 'context:', context
for key in callbacks['sensorEvent']:
f = callbacks['sensorEvent'][key]
try:
f(protocol, model, id, dataType, value, timestamp, callbackId)
except:
print 'Error calling registered callback for sensorEvent'
if debug:
raise
def rawDeviceEvent(data, controllerId, callbackId, context):
if debug:
print 'RawDeviceEvent'
print 'data:', data
print 'controllerId:', controllerId
print 'callbackId:', callbackId
print 'context:', context
for key in callbacks['rawDeviceEvent']:
f = callbacks['rawDeviceEvent'][key]
try:
f(data, controllerId, callbackId)
except:
print 'Error calling registered callback for rawDeviceEvent'
if debug:
raise
device_func = DEVICEFUNC(deviceEvent)
_callbackFuncs['device'] = device_func
deviceChange_func = DEVICECHANGEFUNC(deviceChangeEvent)
_callbackFuncs['deviceChange'] = deviceChange_func
sensor_func = SENSORFUNC(sensorEvent)
_callbackFuncs['sensor'] = sensor_func
rawDevice_func = RAWDEVICEFUNC(rawDeviceEvent)
_callbackFuncs['raw'] = rawDevice_func
def registerEvent(func, eventType):
global callbacks
if len(callbacks[eventType]) == 0:
#if first registration of this type of callback
# register the handler
if eventType == 'deviceEvent':
_callbackFuncs['deviceCallbackId'] = tdlib.tdRegisterDeviceEvent(_callbackFuncs['device'], 0)
elif eventType == 'deviceChangeEvent':
_callbackFuncs['deviceChangeCallbackId'] = tdlib.tdRegisterDeviceChangeEvent(_callbackFuncs['deviceChange'], 0)
elif eventType == 'sensorEvent':
_callbackFuncs['sensorCallbackId'] = tdlib.tdRegisterSensorEvent(_callbackFuncs['sensor'], 0)
elif eventType == 'rawDeviceEvent':
_callbackFuncs['rawCallbackId'] = tdlib.tdRegisterRawDeviceEvent(_callbackFuncs['raw'], 0)
else:
print 'Unknown event type', eventType
callbacks[eventType][callbacks['lastAdd']] = func
id = callbacks['lastAdd']
callbacks['lastAdd'] += 1
return id
def registerDeviceEvent(func):
return registerEvent(func, 'deviceEvent')
def registerDeviceChangedEvent(func):
return registerEvent(func, 'deviceChangeEvent')
def registerSensorEvent(func):
return registerEvent(func, 'sensorEvent')
def registerRawDeviceEvent(func):
return registerEvent(func, 'rawDeviceEvent')
def unregisterCallback(callbackId):
global callbacks
if callbackId in callbacks['deviceEvent']:
del callbacks['deviceEvent'][callbackId]
if len(callbacks['deviceEvent']) == 0:
tdlib.tdUnregisterCallback(_callbackFuncs['deviceCallbackId'])
del _callbackFuncs['deviceCallbackId']
elif callbackId in callbacks['deviceChangeEvent']:
del callbacks['deviceChangeEvent'][callbackId]
if len(callbacks['deviceChangeEvent']) == 0:
tdlib.tdUnregisterCallback(_callbackFuncs['deviceChangeCallbackId'])
del _callbackFuncs['deviceChangeCallbackId']
elif callbackId in callbacks['sensorEvent']:
del callbacks['sensorEvent'][callbackId]
if len(callbacks['sensorEvent']) == 0:
tdlib.tdUnregisterCallback(_callbackFuncs['sensorCallbackId'])
del _callbackFuncs['sensorCallbackId']
elif callbackId in callbacks['rawDeviceEvent']:
del callbacks['rawDeviceEvent'][callbackId]
if len(callbacks['rawDeviceEvent']) == 0:
tdlib.tdUnregisterCallback(_callbackFuncs['rawCallbackId'])
del _callbackFuncs['rawCallbackId']
def setProtocol(intDeviceId, strProtocol):
return tdlib.tdSetProtocol(intDeviceId, strProtocol)
def setModel(intDeviceId, strModel):
return tdlib.tdSetModel(intDeviceId, strModel)
def setDeviceParameter(intDeviceId, strName, strValue):
return tdlib.tdSetDeviceParameter(intDeviceId, strName, strValue)
#Completly untested calls
def connectTellStickController(vid, pid, serial):
tdlib.tdConnectTellStickController(vid, pid, serial)
def disconnectTellStickController(vid, pid, serial):
tdlib.tdDisConnectTellStickController(vid, pid, serial)
#Missing support for these API calls:
#
#int tdRegisterControllerEvent( TDControllerEvent eventFunction, void *context);
#int tdSendRawCommand(const char *command, int reserved);
# TELLSTICK_API void WINAPI tdConnectTellStickController(int vid, int pid, const char *serial);
# TELLSTICK_API void WINAPI tdDisconnectTellStickController(int vid, int pid, const char *serial);
#
# TELLSTICK_API int WINAPI tdController(int *controllerId, int *controllerType, char *name, int nameLen, int *available);
# TELLSTICK_API int WINAPI tdControllerValue(int controllerId, const char *name, char *value, int valueLen);
# TELLSTICK_API int WINAPI tdSetControllerValue(int controllerId, const char *name, const char *value);
# TELLSTICK_API int WINAPI tdRemoveController(int controllerId);
class Sensor(object):
def __init__(self, protocol, model, id, dataType, value, timestamp):
self.protocol = protocol
self.model = model
self.id = id
self.dataType = dataType
self.value = value
self.timestamp = timestamp
def __repr__(self):
return "Sensor: %s.%s.%s %s value: %s %s" % (self.protocol, self.model, self.id, self.dataType, self.value, self.timestamp)
# TELLSTICK_API int WINAPI tdSensor(char *protocol, int protocolLen, char *model, int modelLen, int *id, int *dataTypes);
# TELLSTICK_API int WINAPI tdSensorValue(const char *protocol, const char *model, int id, int dataType, char *value, int len, int *timestamp);
def getSensors():
""" returns all sensors in an array """
sensors = []
LEN = 256
protocol = create_string_buffer(LEN)
model = create_string_buffer(LEN)
id_ = c_int()
dataTypes = c_int()
while 0 == tdlib.tdSensor(protocol, LEN, model, LEN, byref(id_), byref(dataTypes)):
for i in range(0,32):
dataType = 1 << i
if dataTypes.value & dataType:
valuelen = c_int(256)
value = create_string_buffer(256)
timestamp = c_int()
tdlib.tdSensorValue(protocol, model, id_, dataType, value, valuelen, byref(timestamp))
sensors.append(Sensor(protocol.value, model.value, id_.value, dataType, value.value, timestamp.value))
return sensors
if __name__ == '__main__':
import time
init(defaultMethods = TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_BELL | TELLSTICK_TOGGLE | TELLSTICK_DIM | TELLSTICK_LEARN)
print 'getNumberOfDevices', getNumberOfDevices()
print 'Id\tName'
for i in range(getNumberOfDevices()):
devId = getDeviceId(i)
print devId, getName(devId), methods(devId)
if 0:
print 'Methods(1)', methods(1)
print 'methods(1, readable=True)', methods(1, readable = True)
print 'methods(3124, readable=True)', methods(3124, readable = True)
print 'TurnOn(1)', turnOn(1)
time.sleep(1)
print 'TurnOff(1)', turnOff(1)
time.sleep(1)
print 'Dim (1, 121)', dim(1, 121)
print 'LastSentCommand(1)', lastSentCommand(1)
print 'LastSentValue(1)', lastSentValue(1)
print 'GetErrorString(-2)', getErrorString(-2)
print 'getDeviceIdFromStr', getDeviceIdFromStr('2')
print 'getDeviceIdFromStr', getDeviceIdFromStr('Vardagsrum')
print 'getDeviceIdFromStr', getDeviceIdFromStr('234')
devId = addDevice()
if devId > 0:
print 'AddDevice', devId
print 'setName', repr(setName(devId, 'Test'))
print 'getName', repr(getName(devId))
print 'getProtocol', getProtocol(devId)
print 'setProtocol', setProtocol(devId, 'arctech')
print 'getProtocol', getProtocol(devId)
print 'getModel', getModel(devId)
print 'setModel', setModel(devId, 'selflearning-switch')
print 'getModel', getModel(devId)
print 'getDeviceParameter (unit)', repr(getDeviceParameter(devId, "unit", ""))
print 'setDeviceParameter (unit)', repr(setDeviceParameter(devId, 'unit', '123'))
print 'getDeviceParameter (unit)', repr(getDeviceParameter(devId, "unit", ""))
print 'getDeviceParameter (house)', repr(getDeviceParameter(devId, "house", ""))
print 'setDeviceParameter (house)', repr(setDeviceParameter(devId, "house", "321"))
print 'getDeviceParameter (house)', repr(getDeviceParameter(devId, "house", ""))
print '\n\nId\tName'
for i in range(getNumberOfDevices()):
devId = getDeviceId(i)
print devId, getName(devId), methods(devId)
print 'Remove Device', removeDevice(devId)
else:
print 'addDevice returned error', getErrorString(devId)
print '\n\nId\tName'
for i in range(getNumberOfDevices()):
devId = getDeviceId(i)
print devId, getName(devId), methods(devId)
print 'Done with unit test'