Thomas Buck il y a 1 an
Parent
révision
6d96daf795
1 fichiers modifiés avec 36 ajouts et 13 suppressions
  1. 36
    13
      python-test/flow.py

+ 36
- 13
python-test/flow.py Voir le fichier

@@ -10,16 +10,39 @@ from poll import (
10 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 33
 async def wait_for_temp(client, temp):
14 34
     print("Setting temperature {}".format(temp))
15 35
     await set_target_temp(client, temp)
16 36
 
17 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 41
     while curr < temp:
20
-        print("Currently at {}".format(curr))
21
-        await asyncio.sleep(2.0)
42
+        await asyncio.sleep(1.0)
22 43
         curr = await get_current_temp(client)
44
+        print_bar(curr, start, temp, " degC")
45
+    print()
23 46
 
24 47
     print("Reached temperature {}".format(temp))
25 48
 
@@ -27,23 +50,23 @@ async def flow_step(client, temp, t_wait, t_pump):
27 50
     await wait_for_temp(client, temp)
28 51
 
29 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 55
     print("Pumping for {}s".format(t_pump))
33 56
     await set_state(client, (True, True)) # turn on pump
34
-    await asyncio.sleep(t_pump)
57
+    await sleep(t_pump)
35 58
     await set_state(client, (True, False)) # turn off pump
36 59
 
37 60
 async def flow(client):
38 61
     print("Turning on heater")
39 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 65
     await flow_step(client, 205.0, 10.0, 20.0)
43 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 70
         await asyncio.sleep(1.0)
48 71
         await set_state(client, (True, True)) # turn on pump
49 72
         await asyncio.sleep(1.0)
@@ -60,12 +83,12 @@ async def main(address):
60 83
         try:
61 84
             print("Starting Workflow")
62 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 88
             await set_state(client, (False, False)) # turn off heater and pump
89
+            raise
90
+
91
+        print("Disconnecting...")
69 92
 
70 93
 if __name__ == "__main__":
71 94
     if len(sys.argv) <= 1:

Chargement…
Annuler
Enregistrer