Advertisement
DeaD_EyE

pumpe2.py

Mar 2nd, 2024
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. import time
  2. from enum import Enum
  3. from dataclasses import dataclass, field
  4.  
  5. from rich import get_console
  6. from rich.live import Live
  7. from rich.table import Table
  8.  
  9. from wled import set_level
  10.  
  11.  
  12. console = get_console()
  13.  
  14.  
  15. def get_status(step, height, status) -> Table:
  16.     table = Table()
  17.     table.add_row("Step", str(step))
  18.     table.add_row("Height", f"{height:.2f}")
  19.     table.add_row("Status", str(status))
  20.     return table
  21.  
  22.  
  23. class Direction(str, Enum):
  24.     a_to_b = "A to B"
  25.     b_to_a = "B to A"
  26.  
  27.  
  28. @dataclass
  29. class Height:
  30.     min : int | float
  31.     max : int | float
  32.     step : int | float = field(default=1)
  33.  
  34.     def __post_init__(self):
  35.         self._value = 50.0
  36.         console.print("Height measurement initialized")
  37.         console.print(f"Min-Height: {self.min} mm")
  38.         console.print(f"Max-Height: {self.max} mm")
  39.         console.print(f"Step: {self.step} mm")
  40.         console.print(f"Value: {self.value:.2f} mm")
  41.  
  42.     @property
  43.     def value(self) -> int | float:
  44.         return self._value
  45.  
  46.     @value.setter
  47.     def value(self, value):
  48.         console.print("Setting new height to:", value)
  49.         self._value = value
  50.  
  51.     @property
  52.     def ok(self) -> bool:
  53.         return self.min <= self.value <= self.max
  54.  
  55.     @property
  56.     def min_ok(self) -> bool:
  57.         return self.min < self.value
  58.  
  59.     @property
  60.     def max_ok(self) -> bool:
  61.         return self.max > self.value
  62.  
  63.     def update(self, direction: Direction | None) -> int | float:
  64.         if direction is Direction.a_to_b:
  65.             self._value -= self.step
  66.         elif direction is Direction.b_to_a:
  67.             self._value += self.step
  68.  
  69.  
  70. @dataclass
  71. class Pumpe:
  72.     name : str
  73.  
  74.     def __post_init__(self):
  75.          self._state = None
  76.          console.print(f"Created {self.name}.")
  77.  
  78.     @property
  79.     def state(self) -> Direction:
  80.         return self._state
  81.  
  82.     def a_to_b(self):
  83.         self._state = Direction.a_to_b
  84.         # console.print(f"{self.name}: {self._state.value}")
  85.  
  86.     def b_to_a(self):
  87.         self._state = Direction.b_to_a
  88.         # console.print(f"{self.name}: {self._state.value}")
  89.  
  90.     def stop(self):
  91.         self._state = None
  92.         # console.print(f"{self.name}: Stop")
  93.  
  94.  
  95.  
  96. def main(live):
  97.     delay = 0.005
  98.     step_size = (140 / 14) * delay
  99.     console.print(f"Simulation with step_size of {step_size:.2f} and a delay of {delay:.3f}s")
  100.  
  101.     height = Height(min=15.0, max=100.0, step=step_size)
  102.     height.value = 100.0
  103.  
  104.     pumpe = Pumpe("Pupe 1")
  105.  
  106.     console.print("Starting main loop")
  107.     step = 0
  108.     timer = 1.0
  109.  
  110.     while True:
  111.         time.sleep(delay)
  112.  
  113.         height.update(pumpe.state)
  114.         # console.print(f"Height: {height.value:.2f}")
  115.         # console.print("\nStep:", step)
  116.         live.update(get_status(step, height.value, pumpe.state))
  117.         set_level(height.value, min_value=0.0, max_value=140.0)
  118.  
  119.         #  0 [      start  ] -> Pumpe stoppen | ->  1
  120.  
  121.         #              Verzweigung
  122.         #  1 [      max_ok ] -> Von b nach a  | -> 10
  123.         #  1 [ not max_ok  ] -> Von a nach b  | -> 20
  124.  
  125.         #              von b nach a
  126.         # 10 [ not max_ok  ] -> Pumpe stoppen | -> 11
  127.         # 11 [ time 4 s    ] ->               | -> 20
  128.  
  129.         #              von a nach b
  130.         # 20 [             ] -> Von a nach b  | -> 21
  131.         # 21 [  not min_ok ] -> Pumpe stoppen | -> 30
  132.  
  133.         # 30 [             ] ->               | ->  0
  134.  
  135.         match step:
  136.             case 0:
  137.                 step = 1
  138.                 pumpe.stop()
  139.                 input("Enter to start: ")
  140.                 console.clear()
  141.  
  142.             case 1 if height.max_ok:
  143.                 step = 10
  144.                 pumpe.b_to_a()
  145.  
  146.             case 1 if not height.max_ok:
  147.                 step = 20
  148.                 pumpe.a_to_b()
  149.  
  150.             case 10 if not height.max_ok:
  151.                 step = 11
  152.                 pumpe.stop()
  153.  
  154.             case 11:
  155.                 step = 20
  156.                 # console.print(f"Delay of {timer}s")
  157.                 time.sleep(timer)
  158.  
  159.             case 20:
  160.                 step = 21
  161.                 pumpe.a_to_b()
  162.  
  163.             case 21 if not height.min_ok:
  164.                 step = 30
  165.                 pumpe.stop()
  166.  
  167.             case 30:
  168.                 step = 0
  169.  
  170.  
  171. if __name__ == "__main__":
  172.     console.print("Starting simulation")
  173.     try:
  174.         with Live(refresh_per_second=60) as live:
  175.             main(live)
  176.     except KeyboardInterrupt:
  177.         print()
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement