Files
adcs_api
cbor_protocol
channel_protocol
clyde_3g_eps_api
clyde_3g_eps_service
comms_service
db_test
eps_api
example_rust_c_service
example_rust_service
extern_lib
file_protocol
file_service
iobc_supervisor_service
isis_ants
isis_ants_api
isis_ants_service
isis_imtq_api
isis_iobc_supervisor
kubos_app
kubos_app_service
kubos_build_helper
kubos_file_client
kubos_service
kubos_shell_client
kubos_system
kubos_telemetry_db
large_download
large_upload
local_comms_service
mai400
mai400_api
mai400_service
monitor_service
novatel_oem6_api
novatel_oem6_service
nsl_duplex_d2
nsl_duplex_d2_comms_service
obc_hs
radio_api
rust_i2c
rust_mission_app
rust_uart
scheduler_service
serial_comms_service
shell_protocol
shell_service
telemetry_service
uart_comms_client
udp_client
utils
  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
use failure::{bail, Error};
use getopts::Options;
use kubos_app::*;
use log::*;
use std::thread;
use std::time::Duration;

fn main() -> Result<(), Error> {
    logging_setup!("rust-mission-app")?;
    let args: Vec<String> = ::std::env::args().collect();
    let mut opts = Options::new();

    // Standard app args:
    // This option will be processed by the system-api crate when a service query is run
    opts.optflagopt(
        "c",
        "config",
        "System config file which should be used",
        "CONFIG",
    );
    opts.optflag("h", "help", "Print this help menu");
    // App-specific args:
    opts.optflagopt("s", "cmd_string", "Subcommand", "CMD_STR");
    opts.optflagopt("t", "cmd_sleep", "Safe-mode sleep time", "CMD_INT");

    // Parse the command args
    let matches = match opts.parse(args) {
        Ok(r) => r,
        Err(f) => panic!(f.to_string()),
    };

    // Check for subcommand to run
    if let Some(subcommand) = matches.opt_str("s") {
        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 {
                            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)
                    }
                }
            }
        }
    } else {
        // If there's no subcommand, we'll just go ahead and collect telemetry
        let monitor_service = ServiceConfig::new("monitor-service")?;
        let telemetry_service = ServiceConfig::new("telemetry-service")?;

        // 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) => {
                error!("Monitor service query failed: {}", err);
                bail!("Monitor service query failed: {}", err);
            }
        };

        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) => {
                                error!("Failed to save value to database: {}", errors);
                                bail!("Failed to save value to database: {}", errors);
                            }
                            None => {
                                error!("Failed to save value to database");
                                bail!("Failed to save value to database");
                            }
                        };
                    }
                }
                Err(err) => {
                    error!("Telemetry service mutation failed: {}", err);
                    bail!("Telemetry service mutation failed: {}", err);
                }
            }
        }
    }

    Ok(())
}