1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use failure::{bail, Error};
use getopts::Options;
use kubos_app::*;
use log::info;
use std::thread;
use std::time::Duration;

struct MyApp;

impl AppHandler for MyApp {
    fn on_boot(&self, _args: Vec<String>) -> Result<(), Error> {
        let monitor_service = ServiceConfig::new("monitor-service");
        let telemetry_service = ServiceConfig::new("telemetry-service");

        loop {
            thread::sleep(Duration::from_secs(3));
            info!("OnBoot logic called");

            // Get the amount of memory currently available on the OBC
            let request = "{memInfo{available}}";
            let response = match query(&monitor_service, request, Some(Duration::from_secs(1))) {
                Ok(msg) => msg,
                Err(err) => {
                    info!("Monitor service query failed: {}", err);
                    continue;
                }
            };

            let memory = response.get("memInfo").and_then(|msg| msg.get("available"));

            // Save the amount to the telemetry database
            if let Some(mem) = memory {
                let request = format!(
                    r#"
                    mutation {{
                        insert(subsystem: "OBC", parameter: "available_mem", value: "{}") {{
                            success,
                            errors
                        }}
                    }}
                "#,
                    mem
                );

                match query(&telemetry_service, &request, Some(Duration::from_secs(1))) {
                    Ok(msg) => {
                        let success = msg
                            .get("insert")
                            .and_then(|data| data.get("success").and_then(|val| val.as_bool()));

                        if success == Some(true) {
                            info!("Current memory value saved to database");
                        } else {
                            match msg.get("errors") {
                                Some(errors) => {
                                    info!("Failed to save value to database: {}", errors)
                                }
                                None => info!("Failed to save value to database"),
                            };
                        }
                    }
                    Err(err) => {
                        info!("Monitor service mutation failed: {}", err);
                        continue;
                    }
                }
            }
        }
    }

    fn on_command(&self, args: Vec<String>) -> Result<(), Error> {
        let mut opts = Options::new();

        opts.optflagopt(
            "r",
            "run",
            "Run level which should be executed",
            "RUN_LEVEL",
        );
        opts.optflagopt("s", "cmd_string", "Subcommand", "CMD_STR");
        opts.optflagopt("t", "cmd_sleep", "Safe-mode sleep time", "CMD_INT");

        // Parse the command args (skip the first arg with the application name)
        let matches = match opts.parse(&args[1..]) {
            Ok(r) => r,
            Err(f) => panic!(f.to_string()),
        };

        info!("OnCommand logic called");

        let subcommand = matches.opt_str("s").unwrap_or("".to_owned());

        match subcommand.as_ref() {
            "safemode" => {
                let time: u64 = match matches.opt_get("t") {
                    Ok(Some(val)) => val,
                    _ => {
                        info!("Command Integer must be positive and non-zero");
                        bail!("Command Integer must be positive and non-zero");
                    }
                };

                info!("Going into safemode for {} seconds", time);
                thread::sleep(Duration::from_secs(time));
                info!("Resuming normal operations");
            }
            _ => {
                // Get a list of all the currently registered applications
                info!("Querying for active applications");

                let request = r#"{
                    apps {
                        active,
                        app {
                            uuid,
                            name,
                            version,
                            author
                        }
                    }
                }"#;

                match query(
                    &ServiceConfig::new("app-service"),
                    request,
                    Some(Duration::from_secs(1)),
                ) {
                    Ok(msg) => info!("App query result: {:?}", msg),
                    Err(err) => {
                        info!("App service query failed: {}", err);
                        bail!("App service query failed: {}", err)
                    }
                }
            }
        }

        Ok(())
    }
}

fn main() -> Result<(), Error> {
    let app = MyApp;
    app_main!(&app)?;

    Ok(())
}