def test_client():
    global photos_list
    global videos_list
    app = create_app()
    testing_client = app.test_client()

    # Establish application context
    ctx = app.app_context()
    ctx.push()

    file_saver = FileSaver(app.user_config)

    # Start camera controller
    while app.camera_controller.is_alive() is False:
        app.camera_controller.start()
        time.sleep(1)

    # Take photos and record their filenames
    for x in range(2):
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
        thumb = file_saver.save_thumb(app.camera_controller.get_md_image(),
                                      timestamp, "photo")
        filename = file_saver.save_image(
            app.camera_controller.get_hires_image(), timestamp)
        photos_list.append(filename)
        photos_thumb_list.append(thumb)
        time.sleep(1)

    # Record videos and save their filenames.
    app.camera_controller.start_video_stream()
    for y in range(2):
        timestamp = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
        time.sleep(2)
        thumb = file_saver.save_thumb(app.camera_controller.get_md_image(),
                                      timestamp, "video")
        app.camera_controller.wait_recording(2)
        with app.camera_controller.get_video_stream().lock:
            filename = file_saver.save_video(
                app.camera_controller.get_video_stream(), timestamp)
            videos_list.append(filename)
            videos_thumb_list.append(thumb)
            time.sleep(1)

    yield testing_client

    # Teardown

    for f in photos_list:
        os.remove(app.user_config["photos_path"] + f)
    for f in videos_list:
        os.remove(app.user_config["videos_path"] + f)
    for f in photos_thumb_list:
        os.remove(app.user_config["photos_path"] + f)
    for f in videos_thumb_list:
        os.remove(app.user_config["videos_path"] + f)

    app.camera_controller.stop()

    ctx.pop()
Пример #2
0
def test_client():
    app = create_app()
    testing_client = app.test_client()

    # Establish application context
    ctx = app.app_context()
    ctx.push()

    yield testing_client

    app.camera_controller.stop()

    ctx.pop()
def run_around_tests():
    global file_saver
    global app
    app = create_app()
    file_saver = FileSaver(app.user_config)
    testing_client = app.test_client()

    while app.camera_controller.is_alive() is False:
        app.camera_controller.start()
        time.sleep(1)

    # Establish application context
    ctx = app.app_context()
    ctx.push()

    yield testing_client

    app.camera_controller.stop()

    ctx.pop()
Пример #4
0
                    help='Port number to attach to')
args = parser.parse_args()

class CameraNotFoundException(Exception):
    pass

def is_camera_enabled():
    # inspired by https://stackoverflow.com/questions/58250817/raspberry-pi-camera-module-check-if-connected#comment102874971_58250817
    vcgencmd_result = subprocess.run(['/opt/vc/bin/vcgencmd', 'get_camera'], stdout=subprocess.PIPE)
    result_text = vcgencmd_result.stdout.decode('utf-8').strip()
    properties = dict(pair.split('=') for pair in result_text.split(' '))
    return properties['supported'] == '1'

if __name__ == '__main__':
    try:
        app = create_app()
        app.camera_controller.start()
        app.change_detector.start()
    except Exception as e:
        if isinstance(e, PiCameraError) and "Camera is not enabled" in str(e):
            # This error message appears even if the camera _is_ enabled, but the camera is not found.
            # e.g. due to a connection problem.
            # We don't want to mislead users into messing with raspi-config, so check if the
            # camera interface is really disabled.
            if (is_camera_enabled()):
                e = CameraNotFoundException("Unable to access camera. Is the cable properly connected?")

        app = create_error_app(e)

    app.run(debug=False, threaded=True, port=args.port, host='0.0.0.0')