Exemplo n.º 1
0
def camera_img_latest_timelapse(camera_unique_id, max_age):
    """Capture an image and/or return a filename"""
    _, _, tl_ts, tl_filename, _ = utils_general.get_camera_image_info()

    if camera_unique_id in tl_filename and tl_filename[camera_unique_id]:
        camera_path = os.path.join(PATH_CAMERAS,
                                   '{uid}'.format(uid=camera_unique_id))
        camera = Camera.query.filter(
            Camera.unique_id == camera_unique_id).first()
        if camera.path_timelapse:
            tl_path = camera.path_timelapse
        else:
            tl_path = os.path.join(camera_path, 'timelapse')
        full_img_path = os.path.join(tl_path, tl_filename[camera_unique_id])

        try:
            timestamp = os.path.getctime(full_img_path)
            time_max_age = datetime.datetime.now() - datetime.timedelta(
                seconds=int(max_age))
            if datetime.datetime.fromtimestamp(timestamp) > time_max_age:
                return_values = '["{}","{}"]'.format(
                    tl_filename[camera_unique_id], tl_ts[camera_unique_id])
            else:
                return_values = '["max_age_exceeded"]'
        except OSError:
            return_values = '["file_not_found"]'
    else:
        return_values = '["file_not_found"]'
    return Response(return_values, mimetype='text/json')
Exemplo n.º 2
0
    def get(self, unique_id, img_type):
        """get last camera image."""
        if not utils_general.user_has_permission('view_camera'):
            abort(403)

        camera = Camera.query.filter(Camera.unique_id == unique_id).first()

        if not camera:
            abort(422, custom='No camera with ID found')
        if img_type not in ["still", "timelapse"]:
            abort(422, custom='Type not "still" or "timelapse"')

        (latest_img_still_ts, latest_img_still_size, latest_img_still,
         latest_img_tl_ts, latest_img_tl_size, latest_img_tl,
         time_lapse_imgs) = utils_general.get_camera_image_info()

        camera_path = assure_path_exists(os.path.join(PATH_CAMERAS, unique_id))

        path = ""
        filename = ""
        if img_type == 'still':
            filename = latest_img_still[unique_id]
            if camera.path_still:
                path = camera.path_still
            else:
                path = os.path.join(camera_path, img_type)
        elif img_type == 'timelapse':
            filename = latest_img_tl[unique_id]
            if camera.path_timelapse:
                path = camera.path_timelapse
            else:
                path = os.path.join(camera_path, img_type)
        else:
            abort(422, custom=f'Unknown image type: {img_type}')

        if path and os.path.isdir(path):
            files = (files for files in os.listdir(path)
                     if os.path.isfile(os.path.join(path, files)))
        else:
            files = []

        try:
            if filename and filename in files:
                path_file = os.path.join(path, filename)
                if os.path.abspath(path_file).startswith(path):
                    return send_file(path_file, mimetype='image/jpeg')

            return abort(500)
        except Exception:
            abort(500,
                  message='An exception occurred',
                  error=traceback.format_exc())
Exemplo n.º 3
0
def camera_img_latest_timelapse(camera_unique_id, max_age):
    """Capture an image and/or return a filename"""
    _, _, tl_ts, tl_path, _ = utils_general.get_camera_image_info()
    if camera_unique_id in tl_path and tl_path[camera_unique_id]:
        camera_path = assure_path_exists(
            os.path.join(PATH_CAMERAS, '{uid}/timelapse'.format(
                uid=camera_unique_id)))
        image_path_full = os.path.join(camera_path, tl_path[camera_unique_id])
        try:
            timestamp = os.path.getctime(image_path_full)
            time_max_age = datetime.datetime.now() - datetime.timedelta(seconds=int(max_age))
            if datetime.datetime.fromtimestamp(timestamp) > time_max_age:
                return_values = '["{}","{}"]'.format(tl_path[camera_unique_id],
                                                     tl_ts[camera_unique_id])
            else:
                return_values = '["max_age_exceeded"]'
        except OSError:
            return_values = '["file_not_found"]'
    else:
        return_values = '["file_not_found"]'
    return Response(return_values, mimetype='text/json')
Exemplo n.º 4
0
def camera_img_latest_timelapse(camera_unique_id, max_age):
    """Capture an image and resturn the filename"""
    _, _, tl_ts, tl_path = utils_general.get_camera_image_info()
    if camera_unique_id in tl_path and tl_path[camera_unique_id]:
        camera_path = assure_path_exists(
            os.path.join(PATH_CAMERAS, '{uid}/timelapse'.format(
                uid=camera_unique_id)))
        image_path_full = os.path.join(camera_path, tl_path[camera_unique_id])
        try:
            timestamp = os.path.getctime(image_path_full)
            time_max_age = datetime.datetime.now() - datetime.timedelta(seconds=int(max_age))
            if datetime.datetime.fromtimestamp(timestamp) > time_max_age:
                return_values = '["{}","{}"]'.format(tl_path[camera_unique_id],
                                                     tl_ts[camera_unique_id])
            else:
                return_values = '["max_age_exceeded"]'
        except OSError:
            return_values = '["file_not_found"]'
    else:
        return_values = '["file_not_found"]'
    return Response(return_values, mimetype='text/json')