Skip to content

Commit

Permalink
Add machine control
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonhs committed Oct 3, 2024
1 parent 1f05271 commit f728ea9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend_py/src/blueprints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .logs import *
from .preferences import *
from .wifi import *
from .system import *
17 changes: 17 additions & 0 deletions backend_py/src/blueprints/system.py
Original file line number Diff line number Diff line change
@@ -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({})
5 changes: 4 additions & 1 deletion backend_py/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,20 +36,23 @@ 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
self.app.config['light_manager'] = self.light_manager
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)
self.app.register_blueprint(lights_bp)
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)
Expand Down
1 change: 1 addition & 0 deletions backend_py/src/services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .lights import *
from .preferences import *
from .wifi import *
from .system import *
1 change: 1 addition & 0 deletions backend_py/src/services/system/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .system_manager import *
21 changes: 21 additions & 0 deletions backend_py/src/services/system/system_manager.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions frontend/src/components/NavigationBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -203,7 +204,7 @@ const NavigationBar = () => {
<MenuItem
onClick={() => {
handleClose();
// TODO: Shut down machine
shutdownMachine();
}}
sx={{
color: "white",
Expand All @@ -214,7 +215,7 @@ const NavigationBar = () => {
<MenuItem
onClick={() => {
handleClose();
// TODO: Restart machine
restartMachine();
}}
sx={{
color: "white",
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/layouts/system/api.tsx
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit f728ea9

Please sign in to comment.