Exemplo n.º 1
0
async def delete_camera(camera_id: int, user_id: int = Depends(get_identity)):
    try:
        CameraController.delete(camera_id)
        return {'msg': 'ok'}
    except ObjectNotFound:
        raise HTTPException(status_code=http_status.HTTP_404_NOT_FOUND,
                            detail='camera not found')
Exemplo n.º 2
0
async def delete_user_camera(user_id: int,
                             camera_id: int,
                             logged_user_id: int = Depends(get_identity)):
    if user_id != logged_user_id:
        raise HTTPException(status_code=http_status.HTTP_403_FORBIDDEN,
                            detail='FORBIDDEN')
    try:
        _ = UserController.read(user_id)
        CameraController.delete(camera_id)
        return {'msg': 'ok'}
    except ObjectNotFound:
        raise HTTPException(status_code=http_status.HTTP_404_NOT_FOUND,
                            detail='user/camera not found')
Exemplo n.º 3
0
async def read_cameras():
    try:
        cameras = CameraController.read_all()
        return cameras
    except ObjectNotFound:
        raise HTTPException(status_code=http_status.HTTP_404_NOT_FOUND,
                            detail='user not found')
Exemplo n.º 4
0
async def create_user_camera(user_id: int,
                             camera: schemas.CameraCreate,
                             logged_user_id: int = Depends(get_identity)):
    if user_id != logged_user_id:
        raise HTTPException(status_code=http_status.HTTP_403_FORBIDDEN,
                            detail='FORBIDDEN')
    try:
        created_camera = CameraController.create(camera)
        return UserController.add_cameras(user_id, created_camera.id)
    except ObjectNotFound:
        raise HTTPException(status_code=http_status.HTTP_404_NOT_FOUND,
                            detail='user not found')
    except TransactionIntegrityError:
        camera_in_db = CameraController.read_by_connection_string(
            camera.connection_string)
        return UserController.add_cameras(user_id, camera_in_db.id)
Exemplo n.º 5
0
async def stream_endpoint(user_id: int, camera_id: int, websocket: WebSocket):
    _ = UserController.read(user_id)
    camera = CameraController.read(camera_id)
    uri = camera.connection_string

    if uri.startswith('int:'):
        uri = int(uri.replace('int:', ''))

    await websocket.accept()
    await StreamService.add(uri)
    create_task(StreamService.run_x_seconds(uri))

    await sleep(1)
    while await StreamService.is_active(uri):
        resp = await StreamService.read(uri)
        if resp:
            await websocket.send_json(str(resp))
Exemplo n.º 6
0
async def update_camera(user_id: int,
                        camera_id: int,
                        camera: schemas.CameraUpdate,
                        logged_user_id: int = Depends(get_identity)):
    if user_id != logged_user_id:
        raise HTTPException(status_code=http_status.HTTP_403_FORBIDDEN,
                            detail='FORBIDDEN')
    try:
        _ = UserController.read(user_id)
        updated_camera = CameraController.update(camera_id, camera)
        return updated_camera
    except ObjectNotFound:
        raise HTTPException(status_code=http_status.HTTP_404_NOT_FOUND,
                            detail='user/camera not found')
    except IntegrityError:
        raise HTTPException(
            status_code=http_status.HTTP_422_UNPROCESSABLE_ENTITY,
            detail='camera already exists')
Exemplo n.º 7
0
async def smart_spy_endpoint(user_id: int, camera_id: int,
                             websocket: WebSocket):
    _ = UserController.read(user_id)
    camera = CameraController.read(camera_id)
    uri = camera.connection_string

    if uri.startswith('int:'):
        uri = int(uri.replace('int:', ''))

    await websocket.accept()
    await StreamService.add(uri)
    create_task(StreamService.run_x_seconds(uri, detect=True))

    await sleep(1)
    while await StreamService.is_active(uri):
        print(0)
        resp = await StreamService.read(uri)
        if resp:
            await sleep(0.1)
            print(1)
            await websocket.send_text(dumps(resp))
            print(2)
Exemplo n.º 8
0
async def smart_spy_test(user_id: int, camera_id: int):
    UserController.read(user_id)
    CameraController.read(camera_id)
    _html = html.replace("{user_id}",
                         str(user_id)).replace("{camera_id}", str(camera_id))
    return HTMLResponse(_html)