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
456
457
458
459
460
461
462
463
464
465
466
//
// 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::{AntsTelemetry, DeployStatus, IAntS, KANTSAnt};
use juniper::FieldResult;
use kubos_service::{process_errors, push_err, run};
use std::sync::{Arc, Mutex, RwLock};

/// Common response fields structure for requests
/// which don't return any specific data
#[derive(GraphQLObject)]
pub struct GenericResponse {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
}

/// Return field for 'ack' query
///
/// Indicates last mutation executed by the service
#[derive(GraphQLEnum, Clone, Copy)]
pub enum AckCommand {
    /// No mutations have been executed
    None,
    /// No-Op
    Noop,
    /// System power state was changed
    ControlPower,
    /// System configuration was updated
    ConfigureHardware,
    /// A hardware test was performed
    TestHardware,
    /// A raw command was passed through to the system
    IssueRawCommand,
    /// Arm state was changed
    Arm,
    /// Antenna/s were deployed
    Deploy,
}

/// Return field for 'armStatus' query
#[derive(GraphQLEnum, Eq, PartialEq, Debug)]
pub enum ArmStatus {
    /// System is armed
    Armed,
    /// System is disarmed
    Disarmed,
}

/// Input field for 'arm' mutation
#[derive(GraphQLEnum)]
pub enum ArmState {
    /// Arm the system
    Arm,
    /// Disarm the system
    Disarm,
}

/// Response fields for 'arm' mutation
pub type ArmResponse = GenericResponse;

/// Input field for 'configureHardware' mutation
///
/// Sets which AntS microcontroller will be used to issue
/// commands to the antennas
#[derive(GraphQLEnum, Clone, Copy, Eq, PartialEq, Debug)]
pub enum ConfigureController {
    /// Primary controller should be used
    Primary,
    /// Secondary controller should be used
    Secondary,
}

/// Response fields for 'configureHardware' mutation
#[derive(GraphQLObject)]
pub struct ConfigureHardwareResponse {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
    /// Controller which is being used to issue commands to the antennas
    pub config: ConfigureController,
}

/// Input field for 'controlPower' mutation and
/// response field for 'power' query
#[derive(GraphQLEnum, Clone, Eq, PartialEq, Debug)]
pub enum PowerState {
    /// System is on
    On,
    /// System is off or unavailable
    Off,
    /// System will be reset
    Reset,
}

/// Response fields for 'power' query
pub struct GetPowerResponse {
    /// Current power status
    pub state: PowerState,
    /// System uptime, in seconds
    pub uptime: u32,
}

graphql_object!(GetPowerResponse: () |&self| {
    field state() -> FieldResult<PowerState> {
        Ok(self.state.clone())
    }

    field uptime() -> FieldResult<i32> {
        Ok(self.uptime as i32)
    }
});

/// Response fields for 'controlPower' mutation
#[derive(GraphQLObject)]
pub struct ControlPowerResponse {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
    /// Current power status
    pub power: PowerState,
}

/// Enum for 'deployStatus' response field of 'deploymentStatus' query
#[derive(GraphQLEnum, Clone, Debug, PartialEq)]
pub enum DeploymentStatus {
    /// All antennas have been successfully deployed
    Deployed,
    /// At least one antenna is currently being deployed
    InProgress,
    /// Some, but not all, antennas have been deployed.
    /// No errors are present
    Partial,
    /// All antennas are stowed (not deployed)
    Stowed,
    /// At least one antenna encountered an error during its last
    /// deployment attempt
    Error,
}

/// Response fields for 'deploymentStatus' query
pub struct GetDeployResponse {
    /// Overall deployment status
    pub status: DeploymentStatus,
    /// Full deployment status
    pub details: DeployStatus,
}

graphql_object!(GetDeployResponse: () |&self| {
    field status() -> FieldResult<DeploymentStatus> {
        Ok(self.status.clone())
    }

    field sys_burn_active() -> FieldResult<bool> {
        Ok(self.details.sys_burn_active)
    }

    field sys_ignore_deploy() -> FieldResult<bool> {
        Ok(self.details.sys_ignore_deploy)
    }

    field sys_armed() -> FieldResult<bool> {
        Ok(self.details.sys_armed)
    }

    field ant_1_not_deployed() -> FieldResult<bool> {
        Ok(self.details.ant_1_not_deployed)
    }

    field ant_1_stopped_time() -> FieldResult<bool> {
        Ok(self.details.ant_1_stopped_time)
    }

    field ant_1_active() -> FieldResult<bool> {
        Ok(self.details.ant_1_active)
    }

    field ant_2_not_deployed() -> FieldResult<bool> {
        Ok(self.details.ant_2_not_deployed)
    }

    field ant_2_stopped_time() -> FieldResult<bool> {
        Ok(self.details.ant_2_stopped_time)
    }

    field ant_2_active() -> FieldResult<bool> {
        Ok(self.details.ant_2_active)
    }

    field ant_3_not_deployed() -> FieldResult<bool> {
        Ok(self.details.ant_3_not_deployed)
    }

    field ant_3_stopped_time() -> FieldResult<bool> {
        Ok(self.details.ant_3_stopped_time)
    }

    field ant_3_active() -> FieldResult<bool> {
        Ok(self.details.ant_3_active)
    }

    field ant_4_not_deployed() -> FieldResult<bool> {
        Ok(self.details.ant_4_not_deployed)
    }

    field ant_4_stopped_time() -> FieldResult<bool> {
        Ok(self.details.ant_4_stopped_time)
    }

    field ant_4_active() -> FieldResult<bool> {
        Ok(self.details.ant_4_active)
    }

});

/// Input field for 'deploy' mutation
#[derive(GraphQLEnum)]
pub enum DeployType {
    /// Deploy all antennas sequentially
    All,
    /// Deploy antenna 1
    Antenna1,
    /// Deploy antenna 2
    Antenna2,
    /// Deploy antenna 3
    Antenna3,
    /// Deploy antenna 4
    Antenna4,
}

/// Response fields for 'deploy' mutation
pub type DeployResponse = GenericResponse;

/// Response fields for 'noop' mutation
pub type NoopResponse = GenericResponse;

/// Input field for 'testHardware' mutation
#[derive(GraphQLEnum)]
pub enum TestType {
    /// Integration (non-invasive) test
    Integration,
    /// Hardware (invasive) test
    Hardware,
}

/// Enum for the 'testHardware' mutation response union
pub enum TestResults {
    /// Integration test results
    Integration(IntegrationTestResults),
    /// Hardware test results
    Hardware(HardwareTestResults),
}

// Response union for 'testHardware' mutation
graphql_union!(TestResults: () where Scalar = <S> |&self| {
    instance_resolvers: |&_| {
        &IntegrationTestResults => match *self { TestResults::Integration(ref i) => Some(i), _ => None},
        &HardwareTestResults => match *self { TestResults::Hardware(ref h) => Some(h), _ => None},
    }
});

/// Response fields for 'testHardware(test: INTEGRATION)' mutation
#[derive(GraphQLObject)]
pub struct IntegrationTestResults {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
    /// Nominal telemetry
    pub telemetry_nominal: TelemetryNominal,
    /// Debug telemetry
    pub telemetry_debug: TelemetryDebug,
}

/// Response fields for 'testHardware(test: HARDWARE)' mutation
#[derive(GraphQLObject)]
pub struct HardwareTestResults {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
    /// Test results
    pub data: String,
}

/// Response fields for 'issueRawCommand' mutation
#[derive(GraphQLObject)]
pub struct RawCommandResponse {
    /// Any errors encountered by the request
    pub errors: String,
    /// Request completion success or failure
    pub success: bool,
    /// Command response from system
    pub response: String,
}

/// Response fields for 'telemetry' query
#[derive(GraphQLObject)]
pub struct Telemetry {
    /// Nominal telemetry
    pub nominal: TelemetryNominal,
    /// Debug telemetry
    pub debug: TelemetryDebug,
}

/// Nominal telemetry data
#[derive(Debug, Default, PartialEq)]
pub struct TelemetryNominal(pub AntsTelemetry);

graphql_object!(TelemetryNominal: () where Scalar = <S> |&self| {
    field raw_temp() -> i32 {
        i32::from(self.0.raw_temp)
    }

    field uptime() -> i32 {
        self.0.uptime as i32
    }

    field sys_burn_active() -> bool {
        self.0.deploy_status.sys_burn_active
    }

    field sys_ignore_deploy() -> bool {
        self.0.deploy_status.sys_ignore_deploy
    }

    field sys_armed() -> bool {
        self.0.deploy_status.sys_armed
    }

    field ant_1_not_deployed() -> bool {
        self.0.deploy_status.ant_1_not_deployed
    }

    field ant_1_stopped_time() -> bool {
        self.0.deploy_status.ant_1_stopped_time
    }

    field ant_1_active() -> bool {
        self.0.deploy_status.ant_1_active
    }

    field ant_2_not_deployed() -> bool {
        self.0.deploy_status.ant_2_not_deployed
    }

    field ant_2_stopped_time() -> bool {
        self.0.deploy_status.ant_2_stopped_time
    }

    field ant_2_active() -> bool {
        self.0.deploy_status.ant_2_active
    }

    field ant_3_not_deployed() -> bool {
        self.0.deploy_status.ant_3_not_deployed
    }

    field ant_3_stopped_time() -> bool {
        self.0.deploy_status.ant_3_stopped_time
    }

    field ant_3_active() -> bool {
        self.0.deploy_status.ant_3_active
    }

    field ant_4_not_deployed() -> bool {
        self.0.deploy_status.ant_4_not_deployed
    }

    field ant_4_stopped_time() -> bool {
        self.0.deploy_status.ant_4_stopped_time
    }

    field ant_4_active() -> bool {
        self.0.deploy_status.ant_4_active
    }

});

/// Debug telemetry data
#[derive(Debug, Default, PartialEq)]
pub struct TelemetryDebug {
    /// Antenna 1 status
    pub ant1: AntennaStats,
    /// Antenna 2 status
    pub ant2: AntennaStats,
    /// Antenna 3 status
    pub ant3: AntennaStats,
    /// Antenna 4 status
    pub ant4: AntennaStats,
}

/// Antenna status data
#[derive(Debug, Default, PartialEq)]
pub struct AntennaStats {
    /// Number of times deployment has been attempted without success
    pub act_count: u8,
    /// Cummulative amount of time, in 50ms steps, which has been spent deploying the antenna
    pub act_time: u16,
}

/// Get status data for a particular antenna
pub fn get_stats(
    ants_ref: &Arc<Mutex<Box<dyn IAntS>>>,
    errors: &Arc<RwLock<Vec<String>>>,
    antenna: KANTSAnt,
) -> AntennaStats {
    let ants = ants_ref.lock().unwrap();

    AntennaStats {
        act_count: run!(ants.get_activation_count(antenna.clone()), errors).unwrap_or_default(),
        act_time: run!(ants.get_activation_time(antenna), errors).unwrap_or_default(),
    }
}

graphql_object!(TelemetryDebug: () where Scalar = <S> |&self| {
    field ant_1_activation_count() -> i32 {
        i32::from(self.ant1.act_count)
    }

    field ant_1_activation_time() -> i32 {
        i32::from(self.ant1.act_time)
    }

    field ant_2_activation_count() -> i32 {
        i32::from(self.ant2.act_count)
    }

    field ant_2_activation_time() -> i32 {
        i32::from(self.ant2.act_time)
    }

    field ant_3_activation_count() -> i32 {
        i32::from(self.ant3.act_count)
    }

    field ant_3_activation_time() -> i32 {
        i32::from(self.ant3.act_time)
    }

    field ant_4_activation_count() -> i32 {
        i32::from(self.ant4.act_count)
    }

    field ant_4_activation_time() -> i32 {
        i32::from(self.ant4.act_time)
    }
});