diff --git a/backend_py/src/blueprints/__init__.py b/backend_py/src/blueprints/__init__.py index ebfce82b..1797063d 100644 --- a/backend_py/src/blueprints/__init__.py +++ b/backend_py/src/blueprints/__init__.py @@ -3,3 +3,4 @@ from .logs import * from .preferences import * from .wifi import * +from .system import * diff --git a/backend_py/src/blueprints/system.py b/backend_py/src/blueprints/system.py new file mode 100644 index 00000000..fe7c116a --- /dev/null +++ b/backend_py/src/blueprints/system.py @@ -0,0 +1,17 @@ +from flask import Blueprint, request, jsonify, current_app +from ..services import SystemManager +import logging + +system_bp = Blueprint('system', __name__) + +@system_bp.route('/system/restart', methods=['POST']) +def restart(): + system_manager: SystemManager = current_app.config['system_manager'] + system_manager.restart_system() + return jsonify({}) + +@system_bp.route('/system/restart', methods=['SHUTDOWN']) +def shutdown(): + system_manager: SystemManager = current_app.config['system_manager'] + system_manager.shutdown_system() + return jsonify({}) diff --git a/backend_py/src/server.py b/backend_py/src/server.py index 5d4d7860..1d339562 100644 --- a/backend_py/src/server.py +++ b/backend_py/src/server.py @@ -9,7 +9,7 @@ from .websockets.broadcast_server import BroadcastServer from .services import * -from .blueprints import cameras_bp, lights_bp, logs_bp, preferences_bp, wifi_bp +from .blueprints import cameras_bp, lights_bp, logs_bp, preferences_bp, wifi_bp, system_bp from .logging import LogHandler import logging @@ -36,6 +36,7 @@ def __init__(self, port=8080) -> None: self.light_manager = LightManager(create_pwm_controllers()) self.preferences_manager = PreferencesManager() self.wifi_manager = WiFiManager() + self.system_manager = SystemManager() # Set the app configs self.app.config['device_manager'] = self.device_manager @@ -43,6 +44,7 @@ def __init__(self, port=8080) -> None: self.app.config['preferences_manager'] = self.preferences_manager self.app.config['log_handler'] = self.log_handler self.app.config['wifi_manager'] = self.wifi_manager + self.app.config['system_manager'] = self.system_manager # Register the blueprints self.app.register_blueprint(cameras_bp) @@ -50,6 +52,7 @@ def __init__(self, port=8080) -> None: self.app.register_blueprint(logs_bp) self.app.register_blueprint(preferences_bp) self.app.register_blueprint(wifi_bp) + self.app.register_blueprint(system_bp) # create the server and run everything self.http_server = WSGIServer(('0.0.0.0', port), self.app, log=None) diff --git a/backend_py/src/services/__init__.py b/backend_py/src/services/__init__.py index fec71ebe..7883cbae 100644 --- a/backend_py/src/services/__init__.py +++ b/backend_py/src/services/__init__.py @@ -2,3 +2,4 @@ from .lights import * from .preferences import * from .wifi import * +from .system import * diff --git a/backend_py/src/services/system/__init__.py b/backend_py/src/services/system/__init__.py new file mode 100644 index 00000000..878319ce --- /dev/null +++ b/backend_py/src/services/system/__init__.py @@ -0,0 +1 @@ +from .system_manager import * \ No newline at end of file diff --git a/backend_py/src/services/system/system_manager.py b/backend_py/src/services/system/system_manager.py new file mode 100644 index 00000000..20e5d49f --- /dev/null +++ b/backend_py/src/services/system/system_manager.py @@ -0,0 +1,21 @@ +import os +import logging + +class SystemManager: + ''' + Simple class to manage the restarting and shutting down of the system + ''' + + REBOOT_COMMAND = 'reboot now' + SHUTDOWN_COMMAND = 'shutdown now' + + def __init__(self) -> None: + pass + + def restart_system(self): + logging.info('Restarting system') + os.system(self.REBOOT_COMMAND) + + def shutdown_system(self): + logging.info('Shutting down system') + os.system(self.SHUTDOWN_COMMAND) diff --git a/frontend/package.json b/frontend/package.json index 4a767b8a..a861b45d 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "@deepwaterexploration/dwe-os-2", - "version": "v0.2.3-alpha", + "version": "v0.2.2-alpha", "description": "Web-based driver and software interface for DWE.ai cameras", "main": "lib/main.tsx", "keywords": [ diff --git a/frontend/src/components/NavigationBar.tsx b/frontend/src/components/NavigationBar.tsx index c1e83582..bf184306 100644 --- a/frontend/src/components/NavigationBar.tsx +++ b/frontend/src/components/NavigationBar.tsx @@ -34,6 +34,7 @@ import NavigationRoutes from "../utils/getRoutes"; import dweTheme from "../utils/themes"; import { version } from "../../package.json"; +import { restartMachine, shutdownMachine } from "../layouts/system/api"; const drawerWidth = 240; @@ -203,7 +204,7 @@ const NavigationBar = () => { { handleClose(); - // TODO: Shut down machine + shutdownMachine(); }} sx={{ color: "white", @@ -214,7 +215,7 @@ const NavigationBar = () => { { handleClose(); - // TODO: Restart machine + restartMachine(); }} sx={{ color: "white", diff --git a/frontend/src/layouts/system/api.tsx b/frontend/src/layouts/system/api.tsx new file mode 100644 index 00000000..e2815e2e --- /dev/null +++ b/frontend/src/layouts/system/api.tsx @@ -0,0 +1,13 @@ +import { BACKEND_API_URL, postRequest } from "../../utils/utils"; + +export async function restartMachine() { + const url = `${BACKEND_API_URL}/system/restart`; + const response = await postRequest(url); + return await response.json(); +} + +export async function shutdownMachine() { + const url = `${BACKEND_API_URL}/system/shutdown`; + const response = await postRequest(url); + return await response.json(); +}