-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm10_characteristic.py
67 lines (49 loc) · 2.12 KB
/
hm10_characteristic.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
from pybleno import Characteristic
import array
import struct
import sys
import traceback
import time
from thread_wrapper import periodic
class HM10_Characteristic(Characteristic):
def __init__(self, uuid):
Characteristic.__init__(self, {
'uuid': uuid,
'properties': ['read', 'write', 'notify'],
'value': None
})
self._value = array.array('B', [0] * 0)
self._updateValueCallback = None
self.data_in = {'timestamp': 0, 'data': []}
self.data_out = []
# Periodic task for sending data
self.sendThread = periodic(self.sendCommThread, 1.0, "Send")
self.sendThread.start()
def onReadRequest(self, offset, callback):
#print('HM-10 Characteristic - %s - onReadRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))
callback(Characteristic.RESULT_SUCCESS, self._value[offset:])
def onWriteRequest(self, data, offset, withoutResponse, callback):
self._value = data
self.data_in['timestamp'] = time.time()
self.data_in['data'] = [hex(c) for c in self._value]
#print('HM-10 Characteristic - %s - onWriteRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))
#print(' %s: %s' % (self['uuid'], [hex(c) for c in self._value]))
'''
# send data after write arrived instead of periodic task
if self._updateValueCallback:
self._updateValueCallback(self._value)
'''
callback(Characteristic.RESULT_SUCCESS)
def onSubscribe(self, maxValueSize, updateValueCallback):
print('HM-10 Characteristic - onSubscribe')
self._updateValueCallback = updateValueCallback
def onNotify(self):
pass
def onUnsubscribe(self):
print('HM-10 Characteristic - onUnsubscribe');
self._updateValueCallback = None
def sendCommThread(self):
if self._updateValueCallback:
#print('HM-10 Characteristic - sending data')
array_to_send = bytearray(self.data_out)
self._updateValueCallback(array_to_send)