def register_capture(dt_stamp: datetime, cam_id: str): log.warning('Adding capture to redis db') rcfg = partial(get_config_value, app_config, 'db_redis') redis_host = rcfg('host', 'DB_REDIS_HOST') redis_port = rcfg('port', 'DB_REDIS_PORT') redis_db = rcfg('db', 'DB_REDIS_DB') redis_password = rcfg('password', 'DB_REDIS_PASSWORD') timezone = get_config_value(app_config, 'global', 'timezone', 'TIMEZONE', 'US/Central') redis_prefix = rcfg('db_prefix', 'DB_REDIS_PREFIX') rsrv = redis.Redis(redis_host, redis_port, redis_db, redis_password) fmt = '%Y%m%d/%H/%M/%Y.%m.%d.%H.%M.%S.%f' key = '{}/{}/{}'.format(redis_prefix, cam_id, dt_stamp.strftime(fmt)) tz = pytz.timezone(timezone) local_time = tz.localize(dt_stamp) val = { 'utc_time': dt_stamp.isoformat(timespec='microseconds'), 'local_time': local_time.isoformat(timespec='microseconds'), 'camera_id': cam_id } val = json.dumps(val) ret = rsrv.set(key, val) log.warn('Redis updated with {}'.format(dt_stamp.isoformat()))
def local_start(config, shared_obj, storage_queues: dict): storage_q = storage_queues.get('local') if storage_q is None: log.error('No queue found for local storage') sleep(1) return camid = get_config_value(config, 'camera', 'camera_id', 'CAMERA_ID', 'unknown/unknown') local_storage = LocalStorage(config) while True: # blocks until a message is ready dt_pic = storage_q.get() with shared_obj['lock']: curdt: datetime = shared_obj['img_captured'] imgbuf = shared_obj['imgbuf'] # if the current image datetime doesn't match the message # then this message is stale. We can skip it and pull the next. if curdt != dt_pic['dt']: continue log.debug('Storing image {}'.format(curdt.isoformat())) local_storage.upload(imgbuf, curdt, camid)
def minio_start(config, shared_obj, storage_queues: dict): pri_storage_queue = storage_queues.get('minio') if pri_storage_queue is None: log.error('No message queue for minio') sleep(1) return mio = MinioStorage(config) if not mio.check(): log.error('Failed to initialize minio storage') return camid = get_config_value(config, 'camera', 'camera_id', 'CAMERA_ID', 'unknown/unknown') while True: # blocks until a message is ready dt_pic = pri_storage_queue.get() with shared_obj['lock']: curdt: datetime = shared_obj['img_captured'] imgbuf = shared_obj['imgbuf'] # if the current image datetime doesn't match the message # then this message is stale. We can skip it and pull the next. if curdt != dt_pic['dt']: continue log.debug('Storing image on minio: {}'.format(curdt.isoformat())) mio.upload(imgbuf, curdt, camid) sleep(.01)
def __init__(self, config): self.cfg = partial(get_config_value, config, 'celery') self.host = self.cfg('host', 'CELERY_REDIS_HOST') self.port = int(self.cfg('port', 'CELERY_REDIS_PORT', '6379')) self.db = int(self.cfg('db', 'CELERY_REDIS_DB', '0')) self.camid = get_config_value(config, 'camera', 'camera_id', 'CAMERA_ID', 'unknown/unknown') self.redis_url = f'redis://{self.host}:{self.port}/{self.db}' celery_app.conf.broker_url = self.redis_url celery_app.conf.result_backend = self.redis_url lvl = get_config_value(config, 'global', 'loglevel', 'LOG_LEVEL') fmt = get_config_value(config, 'global', 'log_format', 'LOG_FORMAT') enable_log(fmt=fmt) set_loglevel(lvl)
def camera_start(config, shared_obj, shared_lock, msg_queues: dict): """ :param config: The global config :param shared_obj: Where to store the pics and metadata :param msg_queues: Queues to notify when a picture is ready :return: """ lvl = get_config_value(config, 'global', 'loglevel', 'LOG_LEVEL') fmt = get_config_value(config, 'global', 'log_format', 'LOG_FORMAT') enable_log(fmt=fmt) set_loglevel(lvl) log.info('Starting camera') camera = None cam_config = config.get('camera', None) if cam_config is None: log.error('No [camera] config section found') sleep(1) return time_delay = cam_config.get('time_delay', '0') time_delay = int(time_delay) try: camera = UsbCamera(config) ok = camera.init_camera() log.info('Starting camera loop') while ok: log.debug('capturing {}'.format(datetime.utcnow())) img = camera.get_image() if img is not None: dtsrt = datetime.now() # with shared_obj['lock']: log.debug('captured {}'.format(dtsrt)) if log.level == logging.DEBUG: # so it stands out print('\ncaptured {}\n'.format(dtsrt)) with shared_lock: shared_obj['imgbuf'] = img.copy() shared_obj['img_captured'] = dtsrt for val in msg_queues.values(): try: val.put({'dt': dtsrt}, False) except Exception as e: log.warning('Skipping frame for queue') continue log.info('captured image at {}'.format(dtsrt)) else: log.error('Failed to get image') if time_delay > 0: sleep(time_delay) finally: if camera is not None: camera.close_camera()
def is_enabled(self, key_name, env_name): return get_config_value( self.config, 'multi', key_name, env_name, False) \ in [True, 'True']