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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use failure::Error;
use isis_ants_api::*;
use kubos_service::{process_errors, push_err, run};
use log::info;
use std::str;
use std::sync::{Arc, Mutex, RwLock};

use crate::objects::*;
#[cfg(feature = "nos3")]
#[derive(Clone)]
pub struct AntSShare(Arc<Mutex<Box<IAntS + Send>>>);
// Abstractions so Subsystem is agnostic of `Arc<Mutex<Box<IAntS + Send>>>` (nos3)
// or `Box<IAntS>` in its AntS api calls
#[cfg(feature = "nos3")]
impl AntSShare {
    pub fn get_deploy(&self) -> AntSResult<DeployStatus> {
        self.0.lock().unwrap().get_deploy()
    }
    pub fn get_uptime(&self) -> AntSResult<u32> {
        self.0.lock().unwrap().get_uptime()
    }
    pub fn get_system_telemetry(&self) -> AntSResult<AntsTelemetry> {
        self.0.lock().unwrap().get_system_telemetry()
    }
    pub fn get_activation_time(&self, antenna: KANTSAnt) -> AntSResult<u16> {
        self.0.lock().unwrap().get_activation_time(antenna)
    }
    pub fn get_activation_count(&self, antenna: KANTSAnt) -> AntSResult<u8> {
        self.0.lock().unwrap().get_activation_count(antenna)
    }
    pub fn watchdog_kick(&self) -> AntSResult<()> {
        self.0.lock().unwrap().watchdog_kick()
    }
    pub fn deploy(&self, antenna: KANTSAnt, force: bool, timeout: u8) -> AntSResult<()> {
        self.0.lock().unwrap().deploy(antenna, force, timeout)
    }
    pub fn configure(&self, config: KANTSController) -> AntSResult<()> {
        self.0.lock().unwrap().configure(config)
    }
    pub fn disarm(&self) -> AntSResult<()> {
        self.0.lock().unwrap().disarm()
    }
    pub fn reset(&self) -> AntSResult<()> {
        self.0.lock().unwrap().reset()
    }
    pub fn auto_deploy(&self, timeout: u8) -> AntSResult<()> {
        self.0.lock().unwrap().auto_deploy(timeout)
    }
    fn passthrough(&self, tx: &[u8], rx_in: &mut [u8]) -> AntSResult<()> {
        self.0.lock().unwrap().passthrough(tx, rx_in)
    }
    pub fn arm(&self) -> AntSResult<()> {
        self.0.lock().unwrap().arm()
    }
}

#[derive(Clone)]
pub struct Subsystem {
    #[cfg(not(feature = "nos3"))]
    pub ants: Arc<Mutex<Box<dyn IAntS>>>,
    #[cfg(feature = "nos3")]
    pub ants: AntSShare,
    pub count: u8,
    pub controller: Arc<RwLock<ConfigureController>>,
    pub errors: Arc<RwLock<Vec<String>>>,
    pub last_cmd: Arc<RwLock<AckCommand>>,
}

impl Subsystem {
    pub fn new(
        bus: &str,
        primary: u8,
        secondary: u8,
        count: u8,
        timeout: u32,
    ) -> AntSResult<Subsystem> {
        #[cfg(not(feature = "nos3"))]
        let ants: Arc<Mutex<Box<dyn IAntS>>> = Arc::new(Mutex::new(Box::new(AntS::new(
            bus, primary, secondary, count, timeout,
        )?)));
        #[cfg(feature = "nos3")]
        let ants = AntSShare(Arc::new(Mutex::new({
            let mut _ants = AntS::new(bus, primary, secondary, count, timeout)?;
            let ants_box: Box<IAntS + Send> = Box::new(_ants);
            ants_box
        })));

        info!("Kubos antenna systems service started");

        Ok(Subsystem {
            ants,
            count,
            controller: Arc::new(RwLock::new(ConfigureController::Primary)),
            errors: Arc::new(RwLock::new(vec![])),
            last_cmd: Arc::new(RwLock::new(AckCommand::None)),
        })
    }

    // Queries

    #[allow(clippy::clone_on_copy)]
    pub fn get_config(&self) -> AntSResult<ConfigureController> {
        let controller = self
            .controller
            .read()
            .map_err(|_| AntsError::GenericError)?
            .clone();
        Ok(controller)
    }

    pub fn get_arm_status(&self) -> AntSResult<ArmStatus> {
        let result = run!(self.ants.lock().unwrap().get_deploy(), self.errors);
        let armed = result.unwrap_or_default().sys_armed;

        Ok(if armed {
            ArmStatus::Armed
        } else {
            ArmStatus::Disarmed
        })
    }

    pub fn get_deploy_status(&self) -> AntSResult<GetDeployResponse> {
        let result = run!(self.ants.lock().unwrap().get_deploy(), self.errors);

        let mut status = DeploymentStatus::Error;

        let deploy = result.clone().unwrap_or_default();

        // If our API call threw any errors, just go ahead and quit now
        if result.is_err() {
            return Ok(GetDeployResponse {
                status,
                details: deploy,
            });
        }

        let mut deployed = !deploy.ant_1_not_deployed && !deploy.ant_2_not_deployed;
        if self.count > 2 {
            deployed = deployed && !deploy.ant_3_not_deployed;
        }
        if self.count > 3 {
            deployed = deployed && !deploy.ant_4_not_deployed;
        }

        if deployed {
            // If all antennas are not-not-deployed, then the system is fully deployed
            status = DeploymentStatus::Deployed;
        } else if deploy.ant_1_stopped_time
            || deploy.ant_2_stopped_time
            || deploy.ant_3_stopped_time
            || deploy.ant_4_stopped_time
        {
            // If any antennas failed to deploy, mark the system as in an error state
            // Note: A successful deployment should clear this flag for an antenna
            status = DeploymentStatus::Error;
        } else if !deploy.ant_1_not_deployed
            || !deploy.ant_2_not_deployed
            || (!deploy.ant_3_not_deployed && self.count > 2)
            || (!deploy.ant_4_not_deployed && self.count > 3)
        {
            // If there aren't any errors, but some of the antannas have been deployed,
            // mark it as a partial deployment
            // Note: This should be overridden by "InProgress". The only real way
            // a partial deployment is possible (without any errors) is if someone
            // manually deploys a single antenna
            status = DeploymentStatus::Partial
        } else if deploy.ant_1_not_deployed
            && deploy.ant_2_not_deployed
            && (deploy.ant_3_not_deployed || self.count < 3)
            && (deploy.ant_4_not_deployed || self.count < 4)
        {
            // Otherwise, if they're all marked as not-deployed, then we can
            // assume that the system is currently safely stowed
            status = DeploymentStatus::Stowed;
        }

        // An antenna can be deployed while in any of the other states, so go
        // ahead and override the status if there's an active deployment happening
        if deploy.ant_1_active || deploy.ant_2_active || deploy.ant_3_active || deploy.ant_4_active
        {
            status = DeploymentStatus::InProgress;
        }

        Ok(GetDeployResponse {
            status,
            details: deploy,
        })
    }

    pub fn get_power(&self) -> AntSResult<GetPowerResponse> {
        let result = run!(self.ants.lock().unwrap().get_uptime(), self.errors);
        let uptime = result.unwrap_or_default();

        let state = match uptime {
            0 => PowerState::Off,
            _ => PowerState::On,
        };

        Ok(GetPowerResponse { state, uptime })
    }

    pub fn get_telemetry(&self) -> AntSResult<Telemetry> {
        let nominal = run!(
            self.ants.lock().unwrap().get_system_telemetry(),
            self.errors
        )
        .unwrap_or_default();

        let debug = TelemetryDebug {
            ant1: get_stats(&self.ants, &self.errors, KANTSAnt::Ant1),
            ant2: get_stats(&self.ants, &self.errors, KANTSAnt::Ant2),
            ant3: get_stats(&self.ants, &self.errors, KANTSAnt::Ant3),
            ant4: get_stats(&self.ants, &self.errors, KANTSAnt::Ant4),
        };

        Ok(Telemetry {
            nominal: TelemetryNominal(nominal),
            debug,
        })
    }

    pub fn get_test_results(&self) -> AntSResult<IntegrationTestResults> {
        self.integration_test()
    }

    // Mutations

    pub fn arm(&self, state: ArmState) -> AntSResult<ArmResponse> {
        let result = match state {
            ArmState::Arm => run!(self.ants.lock().unwrap().arm(), self.errors),
            ArmState::Disarm => run!(self.ants.lock().unwrap().disarm(), self.errors),
        };

        Ok(ArmResponse {
            success: result.is_ok(),
            errors: match result {
                Ok(_) => "".to_owned(),
                Err(err) => err,
            },
        })
    }

    pub fn configure_hardware(
        &self,
        controller: ConfigureController,
    ) -> AntSResult<ConfigureHardwareResponse> {
        let conv = match controller {
            ConfigureController::Primary => KANTSController::Primary,
            ConfigureController::Secondary => KANTSController::Secondary,
        };

        let result = run!(self.ants.lock().unwrap().configure(conv), self.errors);

        if result.is_ok() {
            let mut curr_controller = self
                .controller
                .write()
                .map_err(|_| AntsError::GenericError)?;
            *curr_controller = controller;
        }

        Ok(ConfigureHardwareResponse {
            config: controller,
            success: result.is_ok(),
            errors: match result {
                Ok(_) => "".to_owned(),
                Err(err) => err,
            },
        })
    }

    pub fn control_power(&self, state: PowerState) -> AntSResult<ControlPowerResponse> {
        match state {
            PowerState::Reset => {
                let result = run!(self.ants.lock().unwrap().reset(), self.errors);

                Ok(ControlPowerResponse {
                    power: state,
                    success: result.is_ok(),
                    errors: match result {
                        Ok(_) => "".to_owned(),
                        Err(err) => err,
                    },
                })
            }
            _ => {
                push_err!(self.errors, "controlPower: Invalid power state".to_owned());

                Ok(ControlPowerResponse {
                    power: state,
                    errors: String::from("Invalid power state"),
                    success: false,
                })
            }
        }
    }

    pub fn deploy(&self, ant: DeployType, force: bool, time: i32) -> AntSResult<DeployResponse> {
        let conv = if time > 255 { 255 } else { time as u8 };

        let result = match ant {
            DeployType::All => run!(self.ants.lock().unwrap().auto_deploy(conv), self.errors),
            DeployType::Antenna1 => run!(
                self.ants
                    .lock()
                    .unwrap()
                    .deploy(KANTSAnt::Ant1, force, conv),
                self.errors
            ),
            DeployType::Antenna2 => run!(
                self.ants
                    .lock()
                    .unwrap()
                    .deploy(KANTSAnt::Ant2, force, conv),
                self.errors
            ),
            DeployType::Antenna3 => run!(
                self.ants
                    .lock()
                    .unwrap()
                    .deploy(KANTSAnt::Ant3, force, conv),
                self.errors
            ),
            DeployType::Antenna4 => run!(
                self.ants
                    .lock()
                    .unwrap()
                    .deploy(KANTSAnt::Ant4, force, conv),
                self.errors
            ),
        };

        Ok(DeployResponse {
            success: result.is_ok(),
            errors: match result {
                Ok(_) => "".to_owned(),
                Err(err) => err,
            },
        })
    }

    pub fn integration_test(&self) -> AntSResult<IntegrationTestResults> {
        let nom_result = run!(
            self.ants.lock().unwrap().get_system_telemetry(),
            self.errors
        );

        let debug_errors: Arc<RwLock<Vec<String>>> = Arc::new(RwLock::new(vec![]));

        let debug = TelemetryDebug {
            ant1: get_stats(&self.ants, &debug_errors, KANTSAnt::Ant1),
            ant2: get_stats(&self.ants, &debug_errors, KANTSAnt::Ant2),
            ant3: get_stats(&self.ants, &debug_errors, KANTSAnt::Ant3),
            ant4: get_stats(&self.ants, &debug_errors, KANTSAnt::Ant4),
        };

        let debug_errors = Arc::try_unwrap(debug_errors)
            .map_err(|_| AntsError::GenericError)?
            .into_inner()
            .map_err(|_| AntsError::GenericError)?;

        let success = nom_result.is_ok() && debug_errors.is_empty();
        let mut errors = String::new();

        let telemetry_nominal = match nom_result {
            Ok(data) => TelemetryNominal(data),
            Err(err) => {
                errors.push_str(&format!("Nominal: {}", err));
                TelemetryNominal::default()
            }
        };

        if !debug_errors.is_empty() {
            if !errors.is_empty() {
                errors.push_str("; ");
            }
            let concat = debug_errors.join(", ");
            errors.push_str(&format!("Debug: {}", concat));
            push_err!(self.errors, format!("get_test_results(debug): {}", concat));
        }

        Ok(IntegrationTestResults {
            errors,
            success,
            telemetry_nominal,
            telemetry_debug: debug,
        })
    }

    pub fn noop(&self) -> AntSResult<NoopResponse> {
        let result = run!(self.ants.lock().unwrap().watchdog_kick(), self.errors);

        Ok(NoopResponse {
            success: result.is_ok(),
            errors: match result {
                Ok(_) => "".to_owned(),
                Err(err) => err,
            },
        })
    }

    pub fn passthrough(&self, command: String, rx_len: i32) -> AntSResult<RawCommandResponse> {
        // Convert the hex values in the string into actual hex values
        // Ex. "c3c2" -> [0xc3, 0xc2]
        let tx: Vec<u8> = command
            .as_bytes()
            .chunks(2)
            .map(|chunk| u8::from_str_radix(str::from_utf8(chunk).unwrap(), 16).unwrap())
            .collect();

        let mut rx: Vec<u8> = vec![0; rx_len as usize];

        let result = run!(
            self.ants
                .lock()
                .unwrap()
                .passthrough(tx.as_slice(), rx.as_mut_slice()),
            self.errors
        );

        // Convert the response hex values into a String for the GraphQL output
        // Note: This is in BIG ENDIAN format
        Ok(match result {
            Ok(_) => RawCommandResponse {
                success: true,
                errors: "".to_owned(),
                response: rx
                    .iter()
                    .map(|byte| format!("{:02x}", byte))
                    .collect::<String>(),
            },
            Err(err) => RawCommandResponse {
                success: false,
                errors: err,
                response: "".to_owned(),
            },
        })
    }
}