-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·131 lines (111 loc) · 3.83 KB
/
main.py
File metadata and controls
executable file
·131 lines (111 loc) · 3.83 KB
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
#!/usr/bin/env python3
"""
Gluconnect Server
"""
import sys
import json
import struct
import glucolib
import uuid
import logging
import asyncio
import threading
from glucometerutils import common
from typing import Any, Dict, Union
from bless import ( # type: ignore
BlessServer,
BlessGATTCharacteristic,
GATTCharacteristicProperties,
GATTAttributePermissions,
)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(name=__name__)
trigger: Union[asyncio.Event, threading.Event]
if sys.platform in ["darwin", "win32"]:
trigger = threading.Event()
else:
trigger = asyncio.Event()
GET_READING_CHAR = ("51ff12bb-3ed8-46e5-b4f9-d64e2fec021b")
NUM_READING_CHAR = ("bfc0c92f-317d-4ba9-976b-cc11ce77b4ca")
SERVICE_UUID = ("a07498ca-ad5b-474e-940d-16f1fbe7e8cd")
glucometer = glucolib.Device("/dev/sda", "otverio2015")
print("connecting...")
glucometer.connect()
readings = glucometer.get_readings()
reading_cache = list(readings)
print("info:", glucometer.get_device_info())
def read_request(characteristic: BlessGATTCharacteristic, **kwargs) -> bytearray:
logger.debug(f"Reading {characteristic.uuid}")
if (characteristic.uuid == NUM_READING_CHAR):
logger.debug(f"CAUGHT NUM READING")
val = len(reading_cache)
print("val to encode:", val)
ret = bytearray(val.to_bytes(8, byteorder="little", signed = False))
print('returning', ret)
return ret
return (characteristic.value)
def write_request(characteristic: BlessGATTCharacteristic, value: Any, **kwargs):
characteristic.value = value
logger.debug(f"Setting char value for {characteristic.uuid}")
if (int(value[0]) < len(reading_cache)):
reading = reading_cache[int(value[0])]
out = {
"time": reading.timestamp.isoformat(),
"value": reading.get_value_as(common.Unit.MG_DL),
"meal": reading.meal.value,
"comment": reading.comment,
"measure_method": reading.measure_method.value,
"extra_data": reading.extra_data,
}
characteristic.value = bytearray(json.dumps(out), "utf-8")
else:
characteristic.value = bytearray(b"")
async def run(loop):
trigger.clear()
# Instantiate the server
gatt: Dict = {
SERVICE_UUID: {
GET_READING_CHAR: {
"Properties": (
GATTCharacteristicProperties.read
| GATTCharacteristicProperties.write
| GATTCharacteristicProperties.indicate
),
"Permissions": (
GATTAttributePermissions.readable
| GATTAttributePermissions.writeable
),
"Value": None,
},
NUM_READING_CHAR: {
"Properties": GATTCharacteristicProperties.read | GATTCharacteristicProperties.indicate,
"Permissions": GATTAttributePermissions.readable,
"Value": bytearray(b"\x69"),
}
},
}
my_service_name = "Gluconnect Service"
server = BlessServer(name=my_service_name, loop=loop)
server.read_request_func = read_request
server.write_request_func = write_request
await server.add_gatt(gatt)
await server.start()
logger.debug(server.get_characteristic(GET_READING_CHAR))
logger.debug("Advertising")
if trigger.__module__ == "threading":
trigger.wait()
else:
await trigger.wait()
#await asyncio.sleep(2)
#
#logger.debug("Updating")
#server.get_characteristic("51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B").value = bytearray(
#b"i"
#)
#server.update_value(
#"A07498CA-AD5B-474E-940D-16F1FBE7E8CD", "51FF12BB-3ED8-46E5-B4F9-D64E2FEC021B"
#)
#await asyncio.sleep(5)
await server.stop()
loop = asyncio.get_event_loop()
loop.run_until_complete(run(loop))