|
1 | 1 | import pygatt
|
| 2 | +import sensors.sensorbase as sensorbase |
2 | 3 | from binascii import hexlify
|
3 | 4 |
|
4 | 5 | YOUR_DEVICE_ADDRESS = "E5:FB:01:09:F7:B4"
|
5 | 6 | ADDRESS_TYPE = pygatt.BLEAddressType.random
|
6 | 7 |
|
7 | 8 | adapter = pygatt.GATTToolBackend()
|
| 9 | +index = 0 |
8 | 10 |
|
9 |
| -def handle_data(handle, value): |
10 |
| - """ |
11 |
| - handle -- integer, characteristic read handle the data was received on |
12 |
| - value -- bytearray, the data returned in the notification |
13 |
| - """ |
14 |
| - print("Received data: %s" % hexlify(value)) |
15 |
| -""" |
16 |
| -def write_handle(handle, value): |
17 |
| - val = [0x55, i, value] |
18 |
| - device.char_write_handle(handle, val, True) |
19 |
| -""" |
20 |
| -try: |
21 |
| - adapter.start() |
22 |
| - device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE) |
23 |
| - device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data) |
24 |
| - |
25 |
| - device.char_write_handle(0x000e, bytearray([0x55, 0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43, 0xAA]), True) |
26 |
| - |
27 |
| - device.char_write_handle(0x000c, bytearray([0x01, 0x00]), True) |
28 |
| - |
29 |
| - device.char_write_handle(0x000e, bytearray([0x55, 0x01, 0x01, 0xAA]), True) |
30 |
| - device.char_write_handle(0x000e, bytearray([0x55, 0x02, 0x03, 0xAA]), True) |
31 |
| - device.char_write_handle(0x000e, bytearray([0x55, 0x03, 0x06, 0xAA]), True) |
32 |
| - |
33 |
| -finally: |
34 |
| - adapter.stop() |
| 11 | +class G201S(sensorbase.SensorBase): |
| 12 | + def __init__(self): |
| 13 | + super(G201S, self).__init__(update_callback = self._update_sensor_data) |
| 14 | + |
| 15 | + @property |
| 16 | + def handle_data(handle, value): |
| 17 | + """ |
| 18 | + handle -- integer, characteristic read handle the data was received on |
| 19 | + value -- bytearray, the data returned in the notification |
| 20 | + """ |
| 21 | + print("Received data: %s" % hexlify(value)) |
| 22 | + |
| 23 | + @property |
| 24 | + def write_handle(handle, value, increment=True): |
| 25 | + global index |
| 26 | + if handle == 0x000e: |
| 27 | + val = [0x55, bytes(index), value, 0xAA] |
| 28 | + else: |
| 29 | + val = [bytes(index), value] |
| 30 | + |
| 31 | + device.char_write_handle(handle, val, True) |
| 32 | + if increment: index += 1 |
| 33 | + print(val) |
| 34 | + |
| 35 | + def turn_on(self): |
| 36 | + try: |
| 37 | + adapter.start() |
| 38 | + device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE) |
| 39 | + device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data) |
| 40 | + |
| 41 | + write_handle(0x000e, bytearray([0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False) |
| 42 | + write_handle(0x000c, bytearray([0x01, 0x00])) |
| 43 | + write_handle(0x000e, bytearray([0x01])) |
| 44 | + write_handle(0x000e, bytearray([0x03])) |
| 45 | + |
| 46 | + finally: |
| 47 | + adapter.stop() |
| 48 | + |
| 49 | + def turn_off(self): |
| 50 | + try: |
| 51 | + adapter.start() |
| 52 | + device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE) |
| 53 | + device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data) |
| 54 | + |
| 55 | + write_handle(0x000e, bytearray([0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False) |
| 56 | + write_handle(0x000c, bytearray([0x01, 0x00])) |
| 57 | + write_handle(0x000e, bytearray([0x01])) |
| 58 | + write_handle(0x000e, bytearray([0x04])) |
| 59 | + |
| 60 | + finally: |
| 61 | + adapter.stop() |
| 62 | + |
| 63 | + def set_temperature(self, value): |
| 64 | + print("set temperature {}".format(value)) |
| 65 | + # try: |
| 66 | + # adapter.start() |
| 67 | + # device = adapter.connect(YOUR_DEVICE_ADDRESS, address_type=ADDRESS_TYPE) |
| 68 | + # device.subscribe("6e400003-b5a3-f393-e0a9-e50e24dcca9e", callback=handle_data) |
| 69 | + |
| 70 | + # write_handle(0x000e, bytearray([0x00, 0xFF, 0xDF, 0x24, 0x0E, 0xC6, 0x94, 0xD1, 0x97, 0x43]), False) |
| 71 | + # write_handle(0x000c, bytearray([0x01, 0x00])) |
| 72 | + # write_handle(0x000e, bytearray([0x01])) |
| 73 | + # write_handle(0x000e, bytearray([0x06])) |
| 74 | + |
| 75 | + # finally: |
| 76 | + # adapter.stop() |
| 77 | + |
| 78 | + def _update_sensor_data(self): |
| 79 | + print("update") |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + sensor = G201S(self) |
0 commit comments