-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
162 lines (128 loc) · 5.63 KB
/
Copy pathmain.py
File metadata and controls
162 lines (128 loc) · 5.63 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
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
import asyncio
import bleak
import asyncio
import time
import datetime
from bleak import BleakClient, BleakScanner
from bleak.backends.device import BLEDevice
class SupportLevel:
ECO = 1
TOUR = 2
BOOST = 3
class StatusData:
def __init__(self, raw: bytes, battery_volts: int):
self.raw = raw
self.battery_volts = battery_volts
async def initiate_listeners(client):
print("Initiating listeners")
def output(data):
with open("notifications.txt", "a") as f:
f.write(f"{datetime.datetime.now().isoformat()} - {data}\n")
f.flush()
async def notification_handler(sender, data):
hex_string = ''.join('{:02x}'.format(x) for x in data)
# This function will be called when notifications are received
output(
f"Received notification on {sender.uuid}: {data} - {hex_string}")
async def status_handler(sender, data):
battery_volts = (data[3] & 255) * 2
status_data = StatusData(data, battery_volts)
min_voltage = 315
max_voltage = 380
def calculate_battery_percentage(voltage):
voltage_range = max_voltage - min_voltage
voltage_above_min = voltage - min_voltage
battery_percentage = voltage_above_min / voltage_range * 100
return max(min(battery_percentage, 100), 0)
battery_percentage = calculate_battery_percentage(
status_data.battery_volts)
output(
f"Battery percentage: {round(battery_percentage)}, raw: {''.join([format(b, '02x') for b in data])}")
async def rocking_handler(_, data):
rocking_error = "none"
if len(data) < 4:
return None
elif (data[0] ^ 128) != 20:
rocking_error = "unknown"
else:
rocking_error = "brake not eng."
def p(b2, b3, z):
if z:
return ((b2 & 255) << 8) | (b3 & 255)
return (b2 & 255) | ((b3 & 255) << 8)
intensity = data[0] & 0xF
time_left = p(data[1], data[2], False)
set_time = p(data[3], data[4], False)
disc = bool(data[0] & 0b00010000)
output(
f"Rock! error: {rocking_error}, intensity: {intensity}, time_left: {time_left}, set_time:{set_time}, disc: {disc} - raw: {''.join([format(b, '02x') for b in data])}")
await client.start_notify("a1fc0102-78d3-40c2-9b6f-3c5f7b2797df", status_handler)
await client.start_notify(
"a1fc0103-78d3-40c2-9b6f-3c5f7b2797df", notification_handler)
await client.start_notify(
"a1fc0104-78d3-40c2-9b6f-3c5f7b2797df", rocking_handler)
while True:
await asyncio.sleep(1)
async def get_input(prompt: str) -> str:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, input, prompt)
async def initiate_interaction(client):
print("Initiating interaction")
while True:
try:
mode = int(await get_input("Mode (1: rocking, 2: spport level): "))
match mode:
case 1:
duration = 55
rocking_intensity = int(await get_input(
"Enter a value for 'rocking_intensity' (0: OFF, 1: HIGH, 2: MED, 3: LOW): "))
if rocking_intensity < 0 or rocking_intensity > 3:
raise ValueError
stop_on_disconnect = False
d = rocking_intensity
if stop_on_disconnect:
d |= 16
bArr = bytes(
[(duration & 65535), ((duration >> 8) & 65535)])
bArr2 = bytes([d, bArr[0], bArr[1]])
hex_str = ''.join([format(b, '02x') for b in bArr2])
print("Got hex, trying to rock: ", hex_str)
await client.write_gatt_char("a1fc0104-78d3-40c2-9b6f-3c5f7b2797df", bArr2)
print(f"Sent...")
print("---")
case 2:
support_level = int(
await get_input("Enter a value for 'drive_mode' (1: ECO, 2: TOUR, 3: BOOST???): "))
if support_level < 1 or support_level > 3:
raise ValueError
bArr = bytes([support_level])
hex_str = ''.join([format(b, '02x') for b in bArr])
print("Got hex, trying to blaze: ", hex_str)
await client.write_gatt_char("a1fc0103-78d3-40c2-9b6f-3c5f7b2797df", bArr)
print("Sent...")
print("---")
except ValueError:
print("Invalid input. Please enter a valid value.")
async def main():
device: BLEDevice = None
while not device:
devices = await BleakScanner.discover()
for d in devices:
if d.metadata.get("manufacturer_data", {}).get(1933, None) is None:
continue
device = d
time.sleep(1)
print(f"Found {device.name} ({device.address}), connecting...")
async with BleakClient(device.address) as client:
print("Are we connected?", client.is_connected)
while not client.is_connected:
print("Not connected, sleep")
await asyncio.sleep(0.1)
listeners = asyncio.create_task(initiate_listeners(client))
interaction = asyncio.create_task(initiate_interaction(client))
done, _ = await asyncio.wait([listeners, interaction],
return_when=asyncio.FIRST_EXCEPTION)
for task in done:
if task.exception():
print(f"Task {task} raised an exception: {task.exception()}")
asyncio.run(main())