calibration.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 = 0
  9. SEGMENT_SIZE = 5
  10. # SEGMENT_SIZE
  11. # n = cualquier cantidad de segundos
  12. # ceil = la duracion del audio en segmentos de 5
  13. # integer = la duracion exacto del audio
  14. def __init__(self, device_id):
  15. self.dev_ref = fbdb.reference('devices').child(device_id)
  16. self.calibrations_ref = self.dev_ref.child('calibrations')
  17. self.thread = None
  18. self.listener = None
  19. self.items = {}
  20. def get(self, station):
  21. calref = self.calibrations_ref.child(station)
  22. try:
  23. remote = calref.get()
  24. except fbdb.ApiCallError as err:
  25. log.error('[get] {}'.format(err))
  26. remote = None
  27. local = self.items.get(station, None)
  28. if not remote and not local:
  29. calibration = {
  30. 'tolerance': self.TOLERANCE_RATE,
  31. 'threshold': self.THRESHOLD,
  32. 'fallTolerance': self.FALL_TOLERANCE,
  33. 'segmentSize': self.SEGMENT_SIZE,
  34. }
  35. elif not remote and local:
  36. calibration = {
  37. 'tolerance': self.TOLERANCE_RATE,
  38. 'threshold': self.THRESHOLD,
  39. 'fallTolerance': self.FALL_TOLERANCE,
  40. 'segmentSize': self.SEGMENT_SIZE,
  41. }
  42. else:
  43. calibration = {
  44. 'tolerance': remote.get(
  45. 'tolerance',
  46. self.TOLERANCE_RATE,
  47. ),
  48. 'threshold': remote.get(
  49. 'threshold',
  50. self.THRESHOLD,
  51. ),
  52. 'fallTolerance': remote.get(
  53. 'fallTolerance',
  54. self.FALL_TOLERANCE,
  55. ),
  56. 'segmentSize': remote.get(
  57. 'segmentSize',
  58. self.SEGMENT_SIZE,
  59. )
  60. }
  61. self.items[station] = calibration
  62. return self.items[station]