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