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
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
//
// 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 crate::model::*;
use crate::objects::*;
use juniper::FieldResult;
use kubos_service;

type Context = kubos_service::Context<Subsystem>;

pub struct QueryRoot;

// Base GraphQL query model
graphql_object!(QueryRoot: Context as "Query" |&self| {

    // Test query to verify service is running without attempting
    // to communicate with the underlying subsystem
    //
    // {
    //     ping: "pong"
    // }
    field ping() -> FieldResult<String>
    {
        Ok(String::from("pong"))
    }

    //----- Generic Queries -----//

    // Get the last run mutation
    //
    // {
    //     ack: AckCommand
    // }
    field ack(&executor) -> FieldResult<AckCommand>
    {
        let last_cmd = executor.context().subsystem().last_cmd.read()?;
        Ok(*last_cmd)
    }

    // Get all errors encountered since the last time this field was queried
    //
    // {
    //     errors: [String]
    // }
    field errors(&executor) -> FieldResult<Vec<String>>
    {
        executor.context().subsystem().get_read_health();

        match executor.context().subsystem().errors.write() {
            Ok(mut master_vec) => {
                let current = master_vec.clone();
                master_vec.clear();
                master_vec.shrink_to_fit();
                Ok(current)
            },
            _ => Ok(vec!["Error: Failed to borrow master errors vector".to_owned()])
        }
    }

    // Get the current power state and uptime of the system
    //
    // {
    //     power {
    //         state: PowerState,
    //         uptime: Int
    //     }
    // }
    field power(&executor) -> FieldResult<GetPowerResponse>
    {
        Ok(executor.context().subsystem().get_power()?)
    }

    // Get the current configuration of the system
    //
    // {
    //     config: "Not Implemented"
    // }
    field config(&executor) -> FieldResult<String>
    {
        Ok(String::from("Not Implemented"))
    }

    // Get current telemetry information for the system
    //
    // {
    //     telemetry{
    //         nominal{
    //             gpsTime: i32,
    //             timeSubsec: i32,
    //             cmdValidCntr: i32,
    //             cmdInvalidCntr: i32,
    //             cmdInvalidChksumCntr: i32,
    //             lastCommand: i32,
    //             acsMode: i32,
    //             css: [i32; 6],
    //             eclipseFlag: i32,
    //             sunVecB: [i32; 3],
    //             iBFieldMeas: [i32; 3],
    //             bd: [f64; 3],
    //             rwsSpeedCmd: [i32; 3],
    //             rwsSpeedTach: [i32; 3],
    //             rwaTorqueCmd: [f64; 3],
    //             gcRwaTorqueCmd: [i32; 3],
    //             torqueCoilCmd: [f64; 3],
    //             gcTorqueCoilCmd: [i32; 3],
    //             qboCmd: [i32; 4],
    //             qboHat: [i32; 4],
    //             angleToGo: f64,
    //             qError: [i32; 4],
    //             omegaB: [f64; 3],
    //             nb: [i32; 3],
    //             neci: [i32; 3],
    //         },
    //         debug{
    //             irehs{
    //                 thermopilesA: [i32; 4],
    //                 thermopilesB: [i32; 4],
    //                 tempA: [i32; 4],
    //                 tempB: [i32; 4],
    //                 dipAngleA: i32,
    //                 dipAngleB: i32,
    //                 solutionDegraded: [Vec<String>; 8],
    //                 thermopileStructA{
    //                     dipAngle: i32,
    //                     earthLimb: {
    //                         adc: i32,
    //    		               temp: i32,
    //    		               errors: bool,
    //    		               flags: Vec<String>,
    //     	               },
    //                     earthRef: {...},
    //                     spaceRef: {...},
    //                     wideFov: {...},
    //                 thermopileStructB{...}
    //             },
    //             rawImu{
    //                 accel: [i32; 3],
    //                 gyro: [i32; 3],
    //                 gyroTemp: i32,
    //             },
    //             rotating{
    //                 bFieldIgrf: [f64; 3],
    //                 sunVecEph: [f64; 3],
    //                 scPosEci: [f64; 3],
    //                 scVelEci: [f64; 3],
    //                 keplerElem{
    //                     semiMajorAxis: f32,
    //                     eccentricity: f32,
    //                     inclination: f32,
    //                     raan: f32,
    //                     argParigee: f32,
    //                     trueAnomoly: f32,
    //                 },
    //                 kBdot: [f64; 3],
    //                 kp: [f64; 3],
    //                 kd: [f64; 3],
    //                 kUnload: [f64; 3],
    //                 cssBias: [i32; 6],
    //                 magBias: [i32; 3],
    //                 rwsVolt: i32,
    //                 rwsPress: i32,
    //                 attDetMode: i32,
    //                 rwsResetCntr: [i32; 3],
    //                 sunMagAligned: i32,
    //                 minorVersion: i32,
    //                 maiSn: i32,
    //                 orbitPropMode: i32,
    //                 acsOpMode: i32,
    //                 procResetCntr: i32,
    //                 majorVersion: i32,
    //                 adsOpMode: i32,
    //                 cssGain: [f64; 6],
    //                 magGain: [f64; 3],
    //                 orbitEpoch: i32,
    //                 trueAnomolyEpoch: f64,
    //                 orbitEpochNext: i32,
    //                 scPosEciEpoch: [f64; 3],
    //                 scVelEciEpoch: [f64; 3],
    //                 qbXWheelSpeed: i32,
    //                 qbXFilterGain: f64,
    //                 qbXDipoleGain: f64,
    //                 dipoleGain: [f64; 3],
    //                 wheelSpeedBias: [i32; 3],
    //                 cosSunMagAlignThresh: f64,
    //                 unloadAngThresh: f64,
    //                 qSat: f64,
    //                 rwaTrqMax: f64,
    //                 rwsMotorCurrent: [i32; 3],
    //                 rwsMotorTemp: i32,
    //             }
    //         }
    //     }
    // }
    field telemetry(&executor) -> FieldResult<Telemetry>
    {
        Ok(executor.context().subsystem().get_telemetry()?)
    }

    // Get the test results of the last run test
    //
    // Note: For this service, this actually just fetches the nominal
    // and debug telemetry of the system, since there is no actual
    // built-in test
    //
    // {
    //     testResults{
    //         success,
    //         telemetryNominal{...},
    //         telemetryDebug{...}
    //     }
    // }
    field test_results(&executor) -> FieldResult<IntegrationTestResults> {
        Ok(executor.context().subsystem().get_test_results()?)
    }

    // Get the current mode of the system
    //
    // {
    //     mode: Mode
    // }
    field mode(&executor) -> FieldResult<Mode> {
        Ok(executor.context().subsystem().get_mode()?)
    }

    // Get the last reported orientation of the system
    //
    // {
    //     orientation: "Not Implemented"
    // }
    field orientation(&executor) -> FieldResult<String> {
        Ok(String::from("Not Implemented"))
    }

    // Get the last reported spin values of the system
    // Note: The spin values are automatically updated every six seconds
    //
    // {
    //     spin{
    //         x: f64,
    //         y: f64,
    //         z: f64
    //     }
    // }
    field spin(&executor) -> FieldResult<Spin> {
        Ok(executor.context().subsystem().get_spin()?)
    }
});

pub struct MutationRoot;

// Base GraphQL mutation model
graphql_object!(MutationRoot: Context as "Mutation" |&self| {

    // Get all errors encountered while processing this GraphQL request
    //
    // Note: This will only return errors thrown by fields which have
    // already been processed, so it is recommended that this field be specified last.
    //
    // mutation {
    //     errors: [String]
    // }
    field errors(&executor) -> FieldResult<Vec<String>>
    {
        match executor.context().subsystem().errors.read() {
            Ok(master_vec) => Ok(master_vec.clone()),
            _ => Ok(vec!["Error: Failed to borrow master errors vector".to_owned()])
        }
    }

    // Execute a trivial command against the system
    //
    // mutation {
    //     noop {
    //         errors: String,
    //         success: Boolean
    //    }
    // }
    field noop(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::Noop;
        Ok(executor.context().subsystem().noop()?)
    }

    // Control the power state of the system
    //
    // state: Power state the system should be changed to
    //   Note: The only valid input for this service is RESET
    //
    // mutation {
    //     controlPower(state: PowerState) {
    //         errors: String,
    //         success: Boolean,
    //         power: PowerState
    //     }
    // }
    field control_power(&executor, state: PowerState) -> FieldResult<ControlPowerResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::ControlPower;
        Ok(executor.context().subsystem().control_power(state)?)
    }

    // Configure the system
    //
    // mutation {
    //     configureHardware: "Not Implemented"
    // }
    field configure_hardware(&executor) -> FieldResult<String>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::ConfigureHardware;
        Ok(String::from("Not Implemented"))
    }

    // Run a system self-test
    //
    // test: Type of self-test to perform
    //
    // mutation {
    //     testHardware(test: TestType) {
    //         ... on IntegrationTestRsults {
    //             errors: String,
    //             success: Boolean,
    //             telemetryNominal{...},
    //             telemetryDebug{...}
    //         }
    //         ... on HardwareTestResults {
    //             errors: "Not Implemented",
    //             success: true,
    //             data: Empty
    //         }
    //    }
    // }
    field test_hardware(&executor, test: TestType) -> FieldResult<TestResults>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::TestHardware;

        match test {
            TestType::Integration => Ok(TestResults::Integration(executor.context().subsystem()
                    .get_test_results().unwrap())),
            TestType::Hardware => Ok(TestResults::Hardware(HardwareTestResults {
                        errors: "Not Implemented".to_owned(), success: true, data: "".to_owned()}))
        }
    }

    // Pass a custom command through to the system
    //
    // command: String containing the hex values to be sent (ex. "C3")
    //          It will be converted to a byte array before transfer.
    //
    // mutation {
    //     issueRawCommand(command: String) {
    //         errors: String,
    //         success: Boolean,
    //         response: String
    //     }
    // }
    field issue_raw_command(&executor, command: String) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::IssueRawCommand;
        Ok(executor.context().subsystem().passthrough(command)?)
    }

    // Set the attitude control mode
    //
    // mode: Control mode to change to
    // qbiCmd: Optional array of four values needed for Qinertial and Qtable mode
    // sunAngleEnable: Optional. Specifies whether the sun rotating angle should be updated when
    //                 using Normal-Sun or LatLong-Sun mode
    // sunRotAngle: Optional. The sun rotating angle for use in Normal-Sun and LatLong-Sun mode
    //
    // mutation {
    //     setMode(mode: Mode, qbiCmd: Vec<i32>, sunAngleEnable: bool, sunRotAngle: f64) {
    //         errors: String,
    //         success: Boolean,
    //         response: String
    //     }
    // }
    field set_mode(
        &executor,
        mode: Mode,
        qbi_cmd = {vec![0,0,0,0]}: Vec<i32>,
        sun_angle_enable = false: bool,
        sun_rot_angle = 0.0: f64)
    -> FieldResult<GenericResponse> {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SetMode;
        match mode {
            Mode::NormalSun | Mode::LatLongSun => Ok(executor.context().subsystem().set_mode_sun(
                    mode as u8, sun_angle_enable as i16, sun_rot_angle as f32)?),
            _ => Ok(executor.context().subsystem().set_mode(mode as u8, qbi_cmd)?),
        }
    }

    // Update system values
    //
    // gpsTime: Optional. If specified, updates the system's ADACS clock
    // rv: Optional. If specified, updates the orbital position and velocity at epoch.
    //     The argument has the following sub-fields:
    //         - eciPos: Vector containing the new X, Y, and Z ECI position values
    //         - eciVel: Vector containing the new X, Y, and Z ECI velocity values
    //         - timeEpoch: GPS time at which the eciPos and eciVel values will go into effect
    //
    // mutation {
    //     update(gps_time: Option<i32>,
    //         rv: Option<{eciPos: [f64; 3], eciVel: [f64; 3], timeEpoch: i32}>) {
    //         errors: String,
    //         success: Boolean,
    //     }
    // }
    field update(&executor, gps_time: Option<i32>, rv: Option<RVInput>)
    -> FieldResult<GenericResponse> {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::Update;
        Ok(executor.context().subsystem().update(gps_time, rv)?)
    }

});