|
| 1 | +# Opencast Camera Control |
| 2 | +# Copyright 2024 Osnabrück University, virtUOS |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import logging |
| 18 | + |
| 19 | +from prometheus_client import generate_latest, CONTENT_TYPE_LATEST |
| 20 | +from flask import Flask, Response |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | +app = Flask(__name__) |
| 25 | + |
| 26 | + |
| 27 | +@app.route('/control/<string:status>/<string:req_camera_url>') |
| 28 | +# @requires_auth |
| 29 | +def activate_camera(status, req_camera_url): |
| 30 | + """ Endpoint for switching between manual and automatic camera control. |
| 31 | + The desired camera is identified by the passed req_camera_url. |
| 32 | + """ |
| 33 | + if status == "manual": |
| 34 | + logger.info('Camera with URL "[%s]" will be controlled manually and ' + |
| 35 | + 'therefore it\'s position won\'t be updated automatically', |
| 36 | + req_camera_url) |
| 37 | + elif status == "automatic": |
| 38 | + logger.info('Camera with URL "[%s]" will switch to automatic ' + |
| 39 | + 'controlling. The camera\'s position will be ' + |
| 40 | + 'adjusted to the corresponding agent\'s status ' + |
| 41 | + 'automatically.', req_camera_url) |
| 42 | + else: |
| 43 | + return 'ERROR<br/>Given status %s is invalid.', status |
| 44 | + |
| 45 | + # Get rid of http or https prefixes to ensure reliable comparability |
| 46 | + sanitized_camera_url = str.replace(req_camera_url, 'http://', '') |
| 47 | + sanitized_camera_url = str.replace(req_camera_url, 'https://', '') |
| 48 | + cameras = app.config["cameras"] |
| 49 | + for camera in cameras: |
| 50 | + camera_url = str.replace(getattr(camera, 'url'), 'http://', '') |
| 51 | + camera_url = str.replace(getattr(camera, 'url'), 'https://', '') |
| 52 | + |
| 53 | + if sanitized_camera_url == camera_url: |
| 54 | + setattr(camera, 'control', status) |
| 55 | + # Resets the current position of the camera |
| 56 | + setattr(camera, 'position', -1) |
| 57 | + return ( |
| 58 | + f"Successfully set camera with url '{camera_url} " |
| 59 | + f"to control status <b>'{status}'</b>." |
| 60 | + ) |
| 61 | + logger.info(f"Camera with url '{req_camera_url}' could not be found.") |
| 62 | + return f"ERROR<br/>Camera with url '{req_camera_url}' could not be found." |
| 63 | + |
| 64 | + |
| 65 | +@app.route('/control_status/<string:req_camera_url>') |
| 66 | +def view_current_camera_control_status(req_camera_url): |
| 67 | + """ Endpoint for requesting the current control status |
| 68 | + (manual or automatic) for a camera. |
| 69 | + The desired camera is identified by the passed camera url. |
| 70 | + """ |
| 71 | + # Get rid of http or https prefixes to ensure reliable comparability |
| 72 | + sanitized_camera_url = str.replace(req_camera_url, 'http://', '') |
| 73 | + sanitized_camera_url = str.replace(req_camera_url, 'https://', '') |
| 74 | + |
| 75 | + cameras = app.config["cameras"] |
| 76 | + for camera in cameras: |
| 77 | + camera_url = str.replace(getattr(camera, 'url'), 'http://', '') |
| 78 | + camera_url = str.replace(getattr(camera, 'url'), 'https://', '') |
| 79 | + if sanitized_camera_url == camera_url: |
| 80 | + return ( |
| 81 | + f"STATUS<br/>The control status of the camera " |
| 82 | + f"with url '{req_camera_url}' is " |
| 83 | + f"<b>{getattr(camera, 'control')}</b>" |
| 84 | + ) |
| 85 | + logger.info(f"Camera with url '{req_camera_url}' could not be found.") |
| 86 | + return f"ERROR</br>Camera with url '{req_camera_url}' could not be found." |
| 87 | + |
| 88 | + |
| 89 | +# expose camera control metrics |
| 90 | +@app.route('/metrics') |
| 91 | +# @requires_auth |
| 92 | +def metrics(): |
| 93 | + # registry = CollectorRegistry() |
| 94 | + # multiprocess.MultiProcessCollector(registry) |
| 95 | + return Response(generate_latest(), content_type=CONTENT_TYPE_LATEST) |
| 96 | + |
| 97 | + |
| 98 | +def start_camera_control_server(cameras): |
| 99 | + """Start the flask server for managing the camera control |
| 100 | + """ |
| 101 | + logger.info('Starting camera control server') |
| 102 | + # start flask app |
| 103 | + # ToDo get host and port from config, default 127.0.0.1:8000 |
| 104 | + app.config['cameras'] = cameras |
| 105 | + app.run(host='127.0.0.1', port=8080) |
0 commit comments