From 6391cf779c98366f50872d0108b63987d4f01d20 Mon Sep 17 00:00:00 2001 From: raoulspronck Date: Mon, 23 Oct 2023 14:36:10 +0200 Subject: [PATCH] Shutdown on request --- src-tauri/src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 6722769..afc64a7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,6 +3,7 @@ use rumqttc::{AsyncClient, Event, LastWill, MqttOptions, Packet, QoS, ConnAck, ConnectReturnCode}; use serde::{Deserialize, Serialize}; +use std::process::Command; use std::sync::atomic::{AtomicBool, Ordering}; use tauri::{Manager, State}; use tokio::sync::Mutex; @@ -195,6 +196,31 @@ async fn main() { let datapoint_key: Vec<&str> = datapoint_key_split.collect(); + let device_key = vec_topic[2]; + + if device_key == device_key_clone && datapoint_key[0] == "Shutdown" { + if cfg!(target_os = "windows") { + // Windows + Command::new("shutdown") + .args(&["/s", "/t", "0"]) + .output() + .expect("Failed to shut down the computer"); + } else if cfg!(target_os = "linux") { + // Linux + Command::new("shutdown") + .args(&["-h", "now"]) + .output() + .expect("Failed to shut down the computer"); + } else if cfg!(target_os = "macos") { + // macOS + Command::new("shutdown") + .args(&["-h", "now"]) + .output() + .expect("Failed to shut down the computer"); + } + continue + } + let s: String = format!( "{}", datapoint_key[0]