common.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. from threading import Thread
  2. from queue import Queue
  3. import time
  4. import struct
  5. import logging
  6. from structures.measurement import *
  7. class Input:
  8. _t = None
  9. _stop = False
  10. _q = Queue()
  11. interval = 1
  12. _read_cb = None
  13. def __init__(self, read_cb) -> None:
  14. self._read_cb = read_cb
  15. def start(self):
  16. if not self._t:
  17. self._stop = False
  18. self._t = Thread(target = self._main)
  19. self._t.setDaemon(True)
  20. self._t.start()
  21. def stop(self):
  22. if self._t:
  23. self._stop = True
  24. self._t.join()
  25. self._t = None
  26. def read(self):
  27. while not self._q.empty():
  28. yield self._q.get()
  29. def _main(self):
  30. start_time = time.monotonic()
  31. while not self._stop:
  32. try:
  33. self._read_cb()
  34. except Exception as e:
  35. logging.exception(F"An exception occured while reading from {type(self)}!")
  36. time.sleep(1)
  37. end_time = time.monotonic()
  38. remaining = self.interval + start_time - end_time
  39. start_time = end_time
  40. if remaining > 0:
  41. time.sleep(remaining)
  42. def queue_ifm_from_bytes(self, source, timestamp, raw, channels = 16):
  43. data = struct.unpack(">" + "B" * 16 + "HHHHHBxH", raw)
  44. current = tuple([x / 10 for x in data[0:channels]])
  45. status = tuple([data[16] & (1 << i) > 0 for i in range(channels)])
  46. overload = tuple([data[17] & (1 << i) > 0 for i in range(channels)])
  47. short_circuit = tuple([data[18] & (1 << i) > 0 for i in range(channels)])
  48. limit = tuple([data[19] & (1 << i) > 0 for i in range(channels)])
  49. pushbutton = tuple([data[20] & (1 << i) > 0 for i in range(channels)])
  50. voltage = data[22] / 100
  51. self._q.put(Measurement24v(timestamp, source, current, status, overload, short_circuit, limit, pushbutton, voltage))
  52. def queue_energy_meter_from_bytes(self, source, timestamp, raw):
  53. data = struct.unpack(">" + "f" * 9, raw)
  54. voltage = tuple(data[0:3])
  55. current = tuple(data[3:6])
  56. phase = tuple(data[6:9])
  57. self._q.put(Measurement480v(timestamp, source, voltage, current, phase))