diff --git a/src/main.rs b/src/main.rs index 492e7a7..3ade9f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,46 +30,59 @@ struct DbData{ //fn main() { fn main () { - // read and parse configuration file - let config_file = File::open("config.json") - .expect("file should open read only"); - let config: Config = serde_json::from_reader(config_file) - .expect("file should be proper JSON"); + let mut config_file_path = "~/.isolated-switches/config.json"; // read and parse command line parameters let args: Vec = env::args().collect(); - if args.len() >= 2 { - let action: &str = args[1].as_str(); - match action { - "on" => pin_switch_on(), - "off" => pin_switch_off(), + for n in 1..args.len() { + match args[n].as_str() { + "on" => { + pin_switch_on(); + return; + }, + "off" => { + pin_switch_off(); + return; + }, "status" => println!("Inverter status: {}" , if pin_status() == 0 { "off" } else { "on" }), - "test" => pin_test(&config), - _ => println!("So far only the parameters 'on', 'off', status and 'test' are supported.") + "test" => pin_test(config_file_path), + "config" => { + if args.len() > n + 1 { config_file_path = args[n + 1].as_str(); } + }, + _ => () } } - else { // no command line parameter set; check for voltages and switch the inverter accordingly - let pin_status_cur = pin_status(); - let voltage = get_current_voltage(&config); - if voltage < config.voltage_off && pin_status_cur > 0 { - println!("Low voltage!"); - pin_switch_off(); - } - else if voltage >= config.voltage_recover && pin_status_cur == 0 { - println!("Voltage recovered!"); - pin_switch_on(); - } - else { - // no change in status needed - let pin_status_text = if pin_status_cur == 0 { "off" } else { "on" }; - println!("No change in inverter status. Current status: {}", pin_status_text); - } + let config: Config = read_config(&config_file_path); + let pin_status_cur = pin_status(); + let voltage = get_current_voltage(&config); + + if voltage < config.voltage_off && pin_status_cur > 0 { + println!("Low voltage!"); + pin_switch_off(); + } + else if voltage >= config.voltage_recover && pin_status_cur == 0 { + println!("Voltage recovered!"); + pin_switch_on(); + } + else { + // no change in status needed + let pin_status_text = if pin_status_cur == 0 { "off" } else { "on" }; + println!("No change in inverter status. Current status: {}", pin_status_text); } //end program with OK status } +// read and parse configuration file +fn read_config(config_file_path: &str) -> Config { + let config_file = File::open(&config_file_path) + .expect("Could not read config file"); + let config: Config = serde_json::from_reader(config_file) + .expect("file should be proper JSON"); + config +} + fn pin_status () -> u8 { let pin_level: u8 = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().read() as u8; return pin_level; @@ -89,7 +102,8 @@ fn pin_switch_off () { println!("Inverter switched off."); } -fn pin_test (config: &Config) { +fn pin_test (config_path: &str) { + let config: Config = read_config(&config_path); pin_switch_on(); thread::sleep(Duration::from_millis(config.test_duration_seconds * 1000)); pin_switch_off();