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 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(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, } elif not remote and local: calibration = { 'tolerance': self.TOLERANCE_RATE, 'threshold': self.THRESHOLD, 'fallTolerance': self.FALL_TOLERANCE, } else: calibration = { 'tolerance': remote.get( 'tolerance', self.TOLERANCE_RATE, ), 'threshold': remote.get( 'threshold', self.THRESHOLD, ), 'fallTolerance': remote.get( 'fallTolerance', self.FALL_TOLERANCE, ) } self.items[station] = calibration return self.items[station]