Thomas Buck 1 year ago
parent
commit
6d96daf795
1 changed files with 36 additions and 13 deletions
  1. 36
    13
      python-test/flow.py

+ 36
- 13
python-test/flow.py View File

10
     get_state, set_state
10
     get_state, set_state
11
 )
11
 )
12
 
12
 
13
+terminal_width = 80 + 20 # TODO detect?
14
+
15
+def print_bar(value, start, end, unit):
16
+    width = terminal_width
17
+    s = "\r"
18
+    s += "#" * int((value - start) / (end - start) * width)
19
+    s += "-" * (width - int((value - start) / (end - start) * width))
20
+    s += " {}{}".format(value, unit)
21
+    print(s, end="", flush=True)
22
+
23
+async def sleep(t):
24
+    w = terminal_width
25
+    if t < w:
26
+        w = int(t)
27
+    print_bar(0, 0, w, "s")
28
+    for i in range(0, w):
29
+        await asyncio.sleep(t / w)
30
+        print_bar(i, 0, w, "s")
31
+    print()
32
+
13
 async def wait_for_temp(client, temp):
33
 async def wait_for_temp(client, temp):
14
     print("Setting temperature {}".format(temp))
34
     print("Setting temperature {}".format(temp))
15
     await set_target_temp(client, temp)
35
     await set_target_temp(client, temp)
16
 
36
 
17
     print("Waiting for temperature to rise...")
37
     print("Waiting for temperature to rise...")
18
-    curr = await get_current_temp(client)
38
+    start = await get_current_temp(client)
39
+    curr = start
40
+    print_bar(curr, start, temp, " degC")
19
     while curr < temp:
41
     while curr < temp:
20
-        print("Currently at {}".format(curr))
21
-        await asyncio.sleep(2.0)
42
+        await asyncio.sleep(1.0)
22
         curr = await get_current_temp(client)
43
         curr = await get_current_temp(client)
44
+        print_bar(curr, start, temp, " degC")
45
+    print()
23
 
46
 
24
     print("Reached temperature {}".format(temp))
47
     print("Reached temperature {}".format(temp))
25
 
48
 
27
     await wait_for_temp(client, temp)
50
     await wait_for_temp(client, temp)
28
 
51
 
29
     print("Waiting {}s for heat to settle...".format(t_wait))
52
     print("Waiting {}s for heat to settle...".format(t_wait))
30
-    await asyncio.sleep(t_wait)
53
+    await sleep(t_wait)
31
 
54
 
32
     print("Pumping for {}s".format(t_pump))
55
     print("Pumping for {}s".format(t_pump))
33
     await set_state(client, (True, True)) # turn on pump
56
     await set_state(client, (True, True)) # turn on pump
34
-    await asyncio.sleep(t_pump)
57
+    await sleep(t_pump)
35
     await set_state(client, (True, False)) # turn off pump
58
     await set_state(client, (True, False)) # turn off pump
36
 
59
 
37
 async def flow(client):
60
 async def flow(client):
38
     print("Turning on heater")
61
     print("Turning on heater")
39
     await set_state(client, (True, False))
62
     await set_state(client, (True, False))
40
 
63
 
41
-    await flow_step(client, 190.0, 25.0, 5.0)
64
+    await flow_step(client, 190.0, 20.0, 5.0)
42
     await flow_step(client, 205.0, 10.0, 20.0)
65
     await flow_step(client, 205.0, 10.0, 20.0)
43
     await flow_step(client, 220.0, 10.0, 20.0)
66
     await flow_step(client, 220.0, 10.0, 20.0)
44
 
67
 
45
-    print("Notification by pumping four times...")
46
-    for i in range(0, 4):
68
+    print("Notification by pumping three times...")
69
+    for i in range(0, 3):
47
         await asyncio.sleep(1.0)
70
         await asyncio.sleep(1.0)
48
         await set_state(client, (True, True)) # turn on pump
71
         await set_state(client, (True, True)) # turn on pump
49
         await asyncio.sleep(1.0)
72
         await asyncio.sleep(1.0)
60
         try:
83
         try:
61
             print("Starting Workflow")
84
             print("Starting Workflow")
62
             await flow(client)
85
             await flow(client)
63
-        except asyncio.exceptions.CancelledError:
64
-            print("Turning heater off")
65
-            await set_state(client, (False, False)) # turn off heater and pump
66
-        except KeyboardInterrupt:
67
-            print("Turning heater off")
86
+        except:
87
+            print("\nTurning heater off")
68
             await set_state(client, (False, False)) # turn off heater and pump
88
             await set_state(client, (False, False)) # turn off heater and pump
89
+            raise
90
+
91
+        print("Disconnecting...")
69
 
92
 
70
 if __name__ == "__main__":
93
 if __name__ == "__main__":
71
     if len(sys.argv) <= 1:
94
     if len(sys.argv) <= 1:

Loading…
Cancel
Save