forked from PunchThrough/espresso-ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
342 lines (246 loc) · 9.64 KB
/
app.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
#!/usr/bin/env python3
import logging
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service
from ble import (
Advertisement,
Characteristic,
Service,
Application,
find_adapter,
Descriptor,
Agent,
)
import struct
import requests
import array
from enum import Enum
import sys
MainLoop = None
try:
from gi.repository import GLib
MainLoop = GLib.MainLoop
except ImportError:
import gobject as GObject
MainLoop = GObject.MainLoop
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logHandler = logging.StreamHandler()
filelogHandler = logging.FileHandler("logs.log")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logHandler.setFormatter(formatter)
filelogHandler.setFormatter(formatter)
logger.addHandler(filelogHandler)
logger.addHandler(logHandler)
VivaldiBaseUrl = "XXXXXXXXXXXX"
mainloop = None
BLUEZ_SERVICE_NAME = "org.bluez"
GATT_MANAGER_IFACE = "org.bluez.GattManager1"
LE_ADVERTISEMENT_IFACE = "org.bluez.LEAdvertisement1"
LE_ADVERTISING_MANAGER_IFACE = "org.bluez.LEAdvertisingManager1"
class InvalidArgsException(dbus.exceptions.DBusException):
_dbus_error_name = "org.freedesktop.DBus.Error.InvalidArgs"
class NotSupportedException(dbus.exceptions.DBusException):
_dbus_error_name = "org.bluez.Error.NotSupported"
class NotPermittedException(dbus.exceptions.DBusException):
_dbus_error_name = "org.bluez.Error.NotPermitted"
class InvalidValueLengthException(dbus.exceptions.DBusException):
_dbus_error_name = "org.bluez.Error.InvalidValueLength"
class FailedException(dbus.exceptions.DBusException):
_dbus_error_name = "org.bluez.Error.Failed"
def register_app_cb():
logger.info("GATT application registered")
def register_app_error_cb(error):
logger.critical("Failed to register application: " + str(error))
mainloop.quit()
class VivaldiS1Service(Service):
"""
Dummy test service that provides characteristics and descriptors that
exercise various API functionality.
"""
ESPRESSO_SVC_UUID = "12634d89-d598-4874-8e86-7d042ee07ba7"
def __init__(self, bus, index):
Service.__init__(self, bus, index, self.ESPRESSO_SVC_UUID, True)
self.add_characteristic(PowerControlCharacteristic(bus, 0, self))
self.add_characteristic(BoilerControlCharacteristic(bus, 1, self))
self.add_characteristic(AutoOffCharacteristic(bus, 2, self))
class PowerControlCharacteristic(Characteristic):
uuid = "4116f8d2-9f66-4f58-a53d-fc7440e7c14e"
description = b"Get/set machine power state {'ON', 'OFF', 'UNKNOWN'}"
class State(Enum):
on = "ON"
off = "OFF"
unknown = "UNKNOWN"
@classmethod
def has_value(cls, value):
return value in cls._value2member_map_
power_options = {"ON", "OFF", "UNKNOWN"}
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index, self.uuid, ["encrypt-read", "encrypt-write"], service,
)
self.value = [0xFF]
self.add_descriptor(CharacteristicUserDescriptionDescriptor(bus, 1, self))
def ReadValue(self, options):
logger.debug("power Read: " + repr(self.value))
res = None
try:
res = requests.get(VivaldiBaseUrl + "/vivaldi")
self.value = bytearray(res.json()["machine"], encoding="utf8")
except Exception as e:
logger.error(f"Error getting status {e}")
self.value = bytearray(self.State.unknown, encoding="utf8")
return self.value
def WriteValue(self, value, options):
logger.debug("power Write: " + repr(value))
cmd = bytes(value).decode("utf-8")
if self.State.has_value(cmd):
# write it to machine
logger.info("writing {cmd} to machine")
data = {"cmd": cmd.lower()}
try:
res = requests.post(VivaldiBaseUrl + "/vivaldi/cmds", json=data)
except Exceptions as e:
logger.error(f"Error updating machine state: {e}")
else:
logger.info(f"invalid state written {cmd}")
raise NotPermittedException
self.value = value
class BoilerControlCharacteristic(Characteristic):
uuid = "322e774f-c909-49c4-bd7b-48a4003a967f"
description = b"Get/set boiler power state can be `on` or `off`"
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index, self.uuid, ["encrypt-read", "encrypt-write"], service,
)
self.value = []
self.add_descriptor(CharacteristicUserDescriptionDescriptor(bus, 1, self))
def ReadValue(self, options):
logger.info("boiler read: " + repr(self.value))
res = None
try:
res = requests.get(VivaldiBaseUrl + "/vivaldi")
self.value = bytearray(res.json()["boiler"], encoding="utf8")
except Exception as e:
logger.error(f"Error getting status {e}")
return self.value
def WriteValue(self, value, options):
logger.info("boiler state Write: " + repr(value))
cmd = bytes(value).decode("utf-8")
# write it to machine
logger.info("writing {cmd} to machine")
data = {"cmd": "setboiler", "state": cmd.lower()}
try:
res = requests.post(VivaldiBaseUrl + "/vivaldi/cmds", json=data)
logger.info(res)
except Exceptions as e:
logger.error(f"Error updating machine state: {e}")
raise
class AutoOffCharacteristic(Characteristic):
uuid = "9c7dbce8-de5f-4168-89dd-74f04f4e5842"
description = b"Get/set autoff time in minutes"
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index, self.uuid, ["secure-read", "secure-write"], service,
)
self.value = []
self.add_descriptor(CharacteristicUserDescriptionDescriptor(bus, 1, self))
def ReadValue(self, options):
logger.info("auto off read: " + repr(self.value))
res = None
try:
res = requests.get(VivaldiBaseUrl + "/vivaldi")
self.value = bytearray(struct.pack("i", int(res.json()["autoOffMinutes"])))
except Exception as e:
logger.error(f"Error getting status {e}")
return self.value
def WriteValue(self, value, options):
logger.info("auto off write: " + repr(value))
cmd = bytes(value)
# write it to machine
logger.info("writing {cmd} to machine")
data = {"cmd": "autoOffMinutes", "time": struct.unpack("i", cmd)[0]}
try:
res = requests.post(VivaldiBaseUrl + "/vivaldi/cmds", json=data)
logger.info(res)
except Exceptions as e:
logger.error(f"Error updating machine state: {e}")
raise
class CharacteristicUserDescriptionDescriptor(Descriptor):
"""
Writable CUD descriptor.
"""
CUD_UUID = "2901"
def __init__(
self, bus, index, characteristic,
):
self.value = array.array("B", characteristic.description)
self.value = self.value.tolist()
Descriptor.__init__(self, bus, index, self.CUD_UUID, ["read"], characteristic)
def ReadValue(self, options):
return self.value
def WriteValue(self, value, options):
if not self.writable:
raise NotPermittedException()
self.value = value
class VivaldiAdvertisement(Advertisement):
def __init__(self, bus, index):
Advertisement.__init__(self, bus, index, "peripheral")
self.add_manufacturer_data(
0xFFFF, [0x70, 0x74],
)
self.add_service_uuid(VivaldiS1Service.ESPRESSO_SVC_UUID)
self.add_local_name("Vivaldi")
self.include_tx_power = True
def register_ad_cb():
logger.info("Advertisement registered")
def register_ad_error_cb(error):
logger.critical("Failed to register advertisement: " + str(error))
mainloop.quit()
AGENT_PATH = "/com/punchthrough/agent"
def main():
global mainloop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
# get the system bus
bus = dbus.SystemBus()
# get the ble controller
adapter = find_adapter(bus)
if not adapter:
logger.critical("GattManager1 interface not found")
return
adapter_obj = bus.get_object(BLUEZ_SERVICE_NAME, adapter)
adapter_props = dbus.Interface(adapter_obj, "org.freedesktop.DBus.Properties")
# powered property on the controller to on
adapter_props.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1))
# Get manager objs
service_manager = dbus.Interface(adapter_obj, GATT_MANAGER_IFACE)
ad_manager = dbus.Interface(adapter_obj, LE_ADVERTISING_MANAGER_IFACE)
advertisement = VivaldiAdvertisement(bus, 0)
obj = bus.get_object(BLUEZ_SERVICE_NAME, "/org/bluez")
agent = Agent(bus, AGENT_PATH)
app = Application(bus)
app.add_service(VivaldiS1Service(bus, 2))
mainloop = MainLoop()
agent_manager = dbus.Interface(obj, "org.bluez.AgentManager1")
agent_manager.RegisterAgent(AGENT_PATH, "NoInputNoOutput")
ad_manager.RegisterAdvertisement(
advertisement.get_path(),
{},
reply_handler=register_ad_cb,
error_handler=register_ad_error_cb,
)
logger.info("Registering GATT application...")
service_manager.RegisterApplication(
app.get_path(),
{},
reply_handler=register_app_cb,
error_handler=[register_app_error_cb],
)
agent_manager.RequestDefaultAgent(AGENT_PATH)
mainloop.run()
# ad_manager.UnregisterAdvertisement(advertisement)
# dbus.service.Object.remove_from_connection(advertisement)
if __name__ == "__main__":
main()