123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- from firebase_admin import db as fbdb
- from threading import Thread
- from tornado.ioloop import IOLoop
- import logging as log
- class Calibrations(object):
- DEFAULT_TOLERANCE_RATE = 0.6
- DEFAULT_THRESHOLD = 10
- 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.DEFAULT_TOLERANCE_RATE,
- 'threshold': self.DEFAULT_THRESHOLD,
- }
- elif remote and not local:
- calibration = {
- 'tolerance': remote.get(
- 'tolerance',
- self.DEFAULT_TOLERANCE_RATE,
- ),
- 'threshold': remote.get(
- 'threshold',
- self.DEFAULT_THRESHOLD,
- ),
- }
- elif not remote and local:
- calibration = {
- 'tolerance': self.DEFAULT_TOLERANCE_RATE,
- 'threshold': self.DEFAULT_THRESHOLD
- }
- self.items[station] = calibration
- return self.items[station]
|