from firebase_admin import db as fbdb from threading import Thread from tornado.ioloop import IOLoop import logging as log class Calibrations(object): TOLERANCE_RATE = 0.6 THRESHOLD = 10 FALL_TOLERANCE = 0 SEGMENT_SIZE = 5 HOURLY_OFFSET = -5 # SEGMENT_SIZE # n = cualquier cantidad de segundos # ceil = la duracion del audio en segmentos de 5 # integer = la duracion exacto del audio def __init__(self, device_id, client=None): self.client = client self.dev_ref = fbdb.reference('devices').child(device_id) self.calibrations_ref = self.dev_ref.child('calibrations') self.thread = None self.listener = None self.items = {} def get(self, station): remote = self.get_remote(station) local = self.items.get(station, None) if not remote and not local: calibration = { 'tolerance': self.TOLERANCE_RATE, 'threshold': self.THRESHOLD, 'fallTolerance': self.FALL_TOLERANCE, 'segmentSize': self.SEGMENT_SIZE, 'hourlyOffset': self.HOURLY_OFFSET, } elif not remote and local: calibration = { 'tolerance': self.TOLERANCE_RATE, 'threshold': self.THRESHOLD, 'fallTolerance': self.FALL_TOLERANCE, 'segmentSize': self.SEGMENT_SIZE, 'hourlyOffset': self.HOURLY_OFFSET, } else: calibration = { 'tolerance': remote.get( 'tolerance', self.TOLERANCE_RATE, ), 'threshold': remote.get( 'threshold', self.THRESHOLD, ), 'fallTolerance': remote.get( 'fallTolerance', self.FALL_TOLERANCE, ), 'segmentSize': remote.get( 'segmentSize', self.SEGMENT_SIZE, ), 'hourlyOffset': remote.get( 'hourlyOffset', self.HOURLY_OFFSET, ) } self.items[station] = calibration return self.items[station] def get_remote(self, station): try: if self.client: return self.client.get_calibrations(station=station) else: calref = self.calibrations_ref.child(station) return calref.get() except Exception as err: log.error('[Calibrations.get_remote] {}'.format(err)) return None