def __init__(self, port="5550"): self.__bind_addr = "tcp://*:%s" % port app.logger.info("Bind address: " + self.__bind_addr) self.__relay_lock = threading.Lock() self.__db = GarageDb(app.instance_path, app.resource_path) # Get initial reed state and subscribe to events GPIO.setup(app.config['REED_PIN'], GPIO.IN) GPIO.add_event_detect(app.config['REED_PIN'], GPIO.BOTH, callback=self.door_opened_or_closed) self.__door_state = None # 1 for open, 0 for closed, None for uninitialized self.door_opened_or_closed(app.config['REED_PIN']) # force update # Set up warning timer if there's a setting if app.config['DOOR_OPEN_WARNING_TIME']: app.logger.info('Starting schedule to check door at {0}...'.format( app.config['DOOR_OPEN_WARNING_TIME'])) schedule.every().day.at(app.config['DOOR_OPEN_WARNING_TIME']).do( self.check_door_open_for_warning) t = Thread(target=self.run_schedule) t.start() else: app.logger.info('No schedule to run.')
def get_db() -> GarageDb: """ Creates a new database connection if there is none yet for the current application context. """ if not hasattr(g, 'sqlite_db'): g.sqlite_db = GarageDb(app.instance_path, resource_path) return g.sqlite_db