|
@@ -0,0 +1,106 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+
|
|
3
|
+import sys
|
|
4
|
+import os
|
|
5
|
+import asyncio
|
|
6
|
+from poll import (
|
|
7
|
+ ble_conn,
|
|
8
|
+ get_current_temp,
|
|
9
|
+ get_target_temp, set_target_temp,
|
|
10
|
+ get_unit_is_fahrenheit,
|
|
11
|
+ get_state, set_state
|
|
12
|
+)
|
|
13
|
+
|
|
14
|
+terminal_width = os.get_terminal_size().columns - 15
|
|
15
|
+
|
|
16
|
+def print_bar(value, start, end, unit):
|
|
17
|
+ width = terminal_width
|
|
18
|
+ s = "\r"
|
|
19
|
+ s += "#" * int((value - start) / (end - start) * width)
|
|
20
|
+ s += "-" * (width - int((value - start) / (end - start) * width))
|
|
21
|
+ s += " {}{}".format(value, unit)
|
|
22
|
+ print(s, end="", flush=True)
|
|
23
|
+
|
|
24
|
+async def sleep(t):
|
|
25
|
+ w = terminal_width
|
|
26
|
+ if t < w:
|
|
27
|
+ w = int(t)
|
|
28
|
+ print_bar(0, 0, w, "s")
|
|
29
|
+ for i in range(0, w):
|
|
30
|
+ await asyncio.sleep(t / w)
|
|
31
|
+ print_bar(i + 1, 0, w, "s")
|
|
32
|
+ print()
|
|
33
|
+
|
|
34
|
+async def wait_for_temp(client, temp):
|
|
35
|
+ print("Setting temperature {}".format(temp))
|
|
36
|
+ await set_target_temp(client, temp)
|
|
37
|
+
|
|
38
|
+ print("Waiting for temperature to rise...")
|
|
39
|
+ start = await get_current_temp(client)
|
|
40
|
+ curr = start
|
|
41
|
+ print_bar(curr, start, temp, " degC")
|
|
42
|
+ while curr < temp:
|
|
43
|
+ await asyncio.sleep(1.0)
|
|
44
|
+ curr = await get_current_temp(client)
|
|
45
|
+ print_bar(curr, start, temp, " degC")
|
|
46
|
+ print()
|
|
47
|
+
|
|
48
|
+ print("Reached temperature {}".format(temp))
|
|
49
|
+
|
|
50
|
+async def flow_step(client, temp, t_wait, t_pump):
|
|
51
|
+ await wait_for_temp(client, temp)
|
|
52
|
+
|
|
53
|
+ print("Waiting {}s for heat to settle...".format(t_wait))
|
|
54
|
+ await sleep(t_wait)
|
|
55
|
+
|
|
56
|
+ print("Pumping for {}s".format(t_pump))
|
|
57
|
+ await set_state(client, (True, True)) # turn on pump
|
|
58
|
+ await sleep(t_pump)
|
|
59
|
+ await set_state(client, (True, False)) # turn off pump
|
|
60
|
+
|
|
61
|
+async def flow(client):
|
|
62
|
+ print("Turning on heater")
|
|
63
|
+ await set_state(client, (True, False))
|
|
64
|
+
|
|
65
|
+ await flow_step(client, 185.0, 10.0, 5.0)
|
|
66
|
+ await flow_step(client, 195.0, 5.0, 20.0)
|
|
67
|
+ await flow_step(client, 205.0, 5.0, 20.0)
|
|
68
|
+
|
|
69
|
+ print("Notification by pumping three times...")
|
|
70
|
+ for i in range(0, 3):
|
|
71
|
+ await asyncio.sleep(1.0)
|
|
72
|
+ await set_state(client, (True, True)) # turn on pump
|
|
73
|
+ await asyncio.sleep(1.0)
|
|
74
|
+ await set_state(client, (True, False)) # turn off pump
|
|
75
|
+
|
|
76
|
+ print("Turning heater off")
|
|
77
|
+ await set_state(client, (False, False)) # turn off heater and pump
|
|
78
|
+
|
|
79
|
+ print("Resetting temperature")
|
|
80
|
+ await set_target_temp(client, 190.0)
|
|
81
|
+
|
|
82
|
+async def main(address):
|
|
83
|
+ device = await ble_conn(address)
|
|
84
|
+
|
|
85
|
+ print("Connecting...")
|
|
86
|
+ async with device as client:
|
|
87
|
+ try:
|
|
88
|
+ if await get_unit_is_fahrenheit(client):
|
|
89
|
+ print("Imperial American scum is currently not supported :P")
|
|
90
|
+ sys.exit(42)
|
|
91
|
+
|
|
92
|
+ print("Starting Workflow")
|
|
93
|
+ await flow(client)
|
|
94
|
+ except:
|
|
95
|
+ print("\nTurning heater and pump off")
|
|
96
|
+ await set_state(client, (False, False)) # turn off heater and pump
|
|
97
|
+ raise
|
|
98
|
+
|
|
99
|
+ print("Disconnecting...")
|
|
100
|
+
|
|
101
|
+if __name__ == "__main__":
|
|
102
|
+ if len(sys.argv) <= 1:
|
|
103
|
+ print("Please pass MAC address of device")
|
|
104
|
+ sys.exit(1)
|
|
105
|
+
|
|
106
|
+ asyncio.run(main(sys.argv[1]))
|