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
gomspace_p31u_api
gomspace_p31u_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
//
// Copyright (C) 2020 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.

// Contributed by Xueliang Bai <x.bai@sydney.edu.au> on behalf of the
// ARC Training Centre for CubeSats, UAVs & Their Applications (CUAVA) team (www.cuava.com.au)
// at the University of Sydney
use crate::model::*;
use crate::objects::*;
use gomspace_p31u_api::*;
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 Queries -----//

    /// Verify service is running without communicating with the underlying subsystem

    field ping() -> FieldResult<String>{
        Ok(String::from("pong"))    
    }

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

    /// Get the last run mutation

    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

    field errors(&executor) -> FieldResult<Vec<String>>
    {
        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()])
        }
    }


    //----- EPS Queries -----//

    /// Ping the EPS system via i2c
    field testping(&executor) -> FieldResult<GenericResponse>
    {
       Ok(executor.context().subsystem().eps_ping()?)
    }

    /// Query the EPS housekeeping data
    field epshk(&executor) -> FieldResult<SchEpsHk>
    {
        Ok(executor.context().subsystem().eps_get_housekeeping()?)
    }

    /// Query EPS system configuration      
    field epssystemconfig(&executor) -> FieldResult<SchEpsSystemConfig>
    {
        Ok(executor.context().subsystem().eps_get_system_config()?)
    }

    /// Query EPS battery configuration
    field epsbattconfig(&executor) -> FieldResult<SchEpsBatteryConfig>
    {
        Ok(executor.context().subsystem().eps_get_battery_config()?)
    }

    /// Query EPS heatter
    field epsheatter(&executor) -> FieldResult<i32>
    {
        Ok(executor.context().subsystem().eps_get_heater()?)
    }

});

pub struct MutationRoot;

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

    // Each field represents functionality available
    // through the GraphQL mutations
    
    /// Reset the EPS
    field reset(&executor) -> FieldResult<GenericResponse>{
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::Reboot;
        Ok(executor.context().subsystem().eps_reset()?)
    }

    /// Reboot the EPS
    field reboot(&executor) -> FieldResult<GenericResponse>{
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::Reboot;
        Ok(executor.context().subsystem().eps_reboot()?)
    }

    /// Set system configuration
    field set_system_config(&executor,
                ppt_mode: i32,
                battheater_mode: i32,
                battheater_low:i32,
                battheater_high:i32,
                output_normal_value:Vec<i32>,
                output_safe_value:Vec<i32>,
                output_initial_on_delay:Vec<i32>,
                output_initial_off_delay:Vec<i32>,
                vboost_settings:Vec<i32>) -> FieldResult<GenericResponse>{
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SetSystemConfig;
        Ok(executor.context().subsystem().eps_set_system_config(   
                ppt_mode,
                battheater_mode,
                battheater_low,
                battheater_high,
                output_normal_value,
                output_safe_value,
                output_initial_on_delay,
                output_initial_off_delay,
                vboost_settings
        )?)
    }

    /// Set battery configuration
    field set_battery_config(&executor,
            batt_maxvoltage: i32,
            batt_safevoltage: i32,
            batt_criticalvoltage: i32,
            batt_normalvoltage: i32) -> FieldResult<GenericResponse>{
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SetBatteryConfig;
        Ok(executor.context().subsystem().eps_set_battery_config(
            batt_maxvoltage,
            batt_safevoltage,
            batt_criticalvoltage,
            batt_normalvoltage
        )?)
    }

    /// Save battery configuration
    field save_battery_config(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SaveBattConfig;
        Ok(executor.context().subsystem().eps_save_battery_config()?)
    }

    /// Reset the system configuration to user default
    field reset_system_config(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SystemConfigReset;
        Ok(executor.context().subsystem().eps_reset_system_config()?)
    }

    /// Reset the battery configuration to user default
    field reset_battery_config(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::BatteryConfigReset;        
        Ok(executor.context().subsystem().eps_reset_battery_config()?)
    }

    /// Reset all counters including watchdogs and reboot counters to 0 
    field reset_counter(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::ResetCounters;    
        Ok(executor.context().subsystem().eps_reset_counters()?)
    }

    /// Set a single EPS output
    field eps_set_single_output(&executor, channel:EpsChannels, power_state:EpsPowerState, delay:i32) -> FieldResult<(GenericResponse)>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::SetEpsChannels;
        Ok(executor.context().subsystem().eps_set_single_output(channel,power_state,delay)?)
    }

    /// Set EPS MPPT value for mode 2 (Non-Auto mode)
    field eps_set_input_value(&executor, in1_voltage: i32,in2_voltage:i32,in3_voltage:i32) -> FieldResult<(GenericResponse)>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::EpsSetMPPTLevel;
        Ok(executor.context().subsystem().eps_set_input_value(in1_voltage,in2_voltage,in3_voltage)?)
    }

    /// Set EPS MPPT mode
    field eps_set_input_mode(&executor,mode:i32) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::EpsSetMPPTmode;
        Ok(executor.context().subsystem().eps_set_input_mode(mode)?)
    }

    /// set heater on/off
    field eps_set_heater(&executor, heater:HeaterSelect, mode:EpsPowerState) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::EpsHeaterToggle;
        Ok(executor.context().subsystem().eps_set_heater(heater,mode)?)
    }

    /// EPS set output
    field eps_set_output(&executor, mask:i32) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::EpsHeaterToggle;
        Ok(executor.context().subsystem().eps_set_output(mask)?)
    }

    ///Kick watchdog
    field eps_watchdog_kick(&executor) -> FieldResult<GenericResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::EpsWatchDog;
        Ok(executor.context().subsystem().eps_watchdog_kick()?)
    }

    // 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.
    // rxLen: Number of response bytes to read
    //

    field issue_raw_command(&executor, command: String, rx_len = 0: i32) -> FieldResult<RawCommandResponse>
    {
        let mut last_cmd = executor.context().subsystem().last_cmd.write()?;
        *last_cmd = AckCommand::IssueRawCommand;
        Ok(executor.context().subsystem().passthrough(command, rx_len)?)
    }
});