main.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from inputs.snap7_server import SiemensServer
  2. from inputs.snap7_connect import SiemensCPU
  3. from inputs.balluff_html import Balluff
  4. from inputs.allen_bradley_connect import AllenBradleyCPU
  5. from database import *
  6. import logging
  7. import time
  8. logging.basicConfig(level=logging.WARNING)
  9. logging.warning("starting")
  10. sources = [
  11. #SiemensCPU("192.168.0.10"),
  12. SiemensServer(),
  13. #Balluff(),
  14. AllenBradleyCPU("192.168.1.15"),
  15. ]
  16. sinks = [
  17. InfluxDB("http://influxdb:8086"),
  18. CSVStorage("logs"),
  19. ]
  20. for source in sources:
  21. source.start()
  22. logging.warning("started sources")
  23. startTime = 0
  24. def printStats(values):
  25. global startTime
  26. counts = {}
  27. dt = time.monotonic() - startTime
  28. startTime = time.monotonic()
  29. text = ""
  30. for meas in values:
  31. id = "{} {}".format(meas.series, meas.source)
  32. if id in counts:
  33. counts[id] += 1
  34. else:
  35. counts[id] = 1
  36. if counts:
  37. ids = list(counts.keys())
  38. ids.sort()
  39. for id in ids:
  40. text += "{}: {:4d} in {:.03f}s, {:.1f}/s ".format(id, counts[id], dt, counts[id] / dt)
  41. else:
  42. text = "0 Messungen in {:.03f}s ".format(dt)
  43. if not counts or len(ids) < 3:
  44. logging.warning(text)
  45. else:
  46. logging.info(text)
  47. while True:
  48. values = []
  49. for source in sources:
  50. values.extend(source.read())
  51. for sink in sinks:
  52. sink.write(values)
  53. printStats(values)
  54. time.sleep(1.9)