
use crate::common::define::{NATS_ACCOUNT, NATS_URL, NATS_PWD, TOPIC_RESTART,SLAVE_ID};
use crate::common::define::*;
fn _cmd_restart() -> anyhow::Result<()> {
    let nc = nats::ConnectionOptions::new()
        .with_name("remote restart")
        .with_user_pass(&NATS_ACCOUNT, &NATS_PWD)
        .connect(&NATS_URL)?;

    let sub = nc.subscribe(&TOPIC_RESTART)?;
    for msg in sub.messages() {
        let str = std::str::from_utf8(&msg.data)?;
        let ord :Order= serde_json::from_str(str)?;
        if ord.slave.clone() == SLAVE_ID.to_string() {
            system_shutdown::reboot()?;
        }
    }
    Ok(())
}
pub fn cmd_restart() {
    loop {
        match _cmd_restart() {
            Ok(()) => print!("success one restart\n"),
            Err(e) => {
                let res = format!("fail restart{}\n", e);
                dbg!(res);
            }
        }
    }
}
