peach-workspace/peach-web/src/routes/settings/power/shutdown.rs

36 lines
1.1 KiB
Rust

use log::info;
use rouille::Response;
use std::{
io::Result,
process::{Command, Output},
};
use crate::utils::flash::FlashResponse;
/// Executes a system command to shutdown the device immediately.
fn shutdown() -> Result<Output> {
info!("Shutting down the device");
// ideally, we'd like to shutdown after 5 seconds to allow time for JSON
// response but this is not possible with the `shutdown` command alone.
// TODO: send "shutting down..." message to `peach-oled` for display
Command::new("sudo").arg("shutdown").arg("now").output()
}
pub fn handle_shutdown() -> Response {
let (name, msg) = match shutdown() {
Ok(_) => (
"success".to_string(),
"Shutting down the device".to_string(),
),
Err(err) => (
"error".to_string(),
format!("Failed to shutdown the device: {}", err),
),
};
let (flash_name, flash_msg) = (format!("flash_name={}", name), format!("flash_msg={}", msg));
Response::redirect_303("/power").add_flash(flash_name, flash_msg)
}