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