calibration.py 2.6 KB

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