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 # 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): 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): calref = self.calibrations_ref.child(station) try: remote = calref.get() except fbdb.ApiCallError as err: log.error('[get] {}'.format(err)) remote = None 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, } elif not remote and local: calibration = { 'tolerance': self.TOLERANCE_RATE, 'threshold': self.THRESHOLD, 'fallTolerance': self.FALL_TOLERANCE, 'segmentSize': self.SEGMENT_SIZE, } 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, ) } self.items[station] = calibration return self.items[station]