calibration.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from firebase_admin import db as fbdb
  2. from threading import Thread
  3. from tornado.ioloop import IOLoop
  4. import logging as log
  5. class Calibrations(object):
  6. DEFAULT_TOLERANCE_RATE = 0.6
  7. DEFAULT_THRESHOLD = 10
  8. def __init__(self, device_id):
  9. self.dev_ref = fbdb.reference('devices').child(device_id)
  10. self.calibrations_ref = self.dev_ref.child('calibrations')
  11. self.thread = None
  12. self.listener = None
  13. self.items = {}
  14. def get(self, station):
  15. calref = self.calibrations_ref.child(station)
  16. try:
  17. remote = calref.get()
  18. except fbdb.ApiCallError as err:
  19. log.error(err)
  20. remote = None
  21. local = self.items.get(station, None)
  22. if not remote and not local:
  23. calibration = {
  24. 'tolerance': self.DEFAULT_TOLERANCE_RATE,
  25. 'threshold': self.DEFAULT_THRESHOLD,
  26. }
  27. elif remote and not local:
  28. calibration = {
  29. 'tolerance': remote.get(
  30. 'tolerance',
  31. self.DEFAULT_TOLERANCE_RATE,
  32. ),
  33. 'threshold': remote.get(
  34. 'threshold',
  35. self.DEFAULT_THRESHOLD,
  36. ),
  37. }
  38. elif not remote and local:
  39. calibration = {
  40. 'tolerance': self.DEFAULT_TOLERANCE_RATE,
  41. 'threshold': self.DEFAULT_THRESHOLD
  42. }
  43. self.items[station] = calibration
  44. return self.items[station]