calibration.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. TOLERANCE_RATE = 0.6
  7. THRESHOLD = 10
  8. FALL_TOLERANCE = 2
  9. def __init__(self, device_id):
  10. self.dev_ref = fbdb.reference('devices').child(device_id)
  11. self.calibrations_ref = self.dev_ref.child('calibrations')
  12. self.thread = None
  13. self.listener = None
  14. self.items = {}
  15. def get(self, station):
  16. calref = self.calibrations_ref.child(station)
  17. try:
  18. remote = calref.get()
  19. except fbdb.ApiCallError as err:
  20. log.error(err)
  21. remote = None
  22. local = self.items.get(station, None)
  23. if not remote and not local:
  24. calibration = {
  25. 'tolerance': self.TOLERANCE_RATE,
  26. 'threshold': self.THRESHOLD,
  27. 'fallTolerance': self.FALL_TOLERANCE,
  28. }
  29. elif remote and not local:
  30. calibration = {
  31. 'tolerance': remote.get(
  32. 'tolerance',
  33. self.TOLERANCE_RATE,
  34. ),
  35. 'threshold': remote.get(
  36. 'threshold',
  37. self.THRESHOLD,
  38. ),
  39. 'fallTolerance': remote.get(
  40. 'fallTolerance',
  41. self.FALL_TOLERANCE,
  42. )
  43. }
  44. elif not remote and local:
  45. calibration = {
  46. 'tolerance': self.TOLERANCE_RATE,
  47. 'threshold': self.THRESHOLD,
  48. 'fallTolerance': self.FALL_TOLERANCE,
  49. }
  50. self.items[station] = calibration
  51. return self.items[station]