added command line option to customize the configuration file path

This commit is contained in:
Aaska Black Wolf
2026-07-09 10:02:14 +02:00
parent 0bdaa532e4
commit 1d329a6e50
+43 -29
View File
@@ -30,46 +30,59 @@ struct DbData{
//fn main() { //fn main() {
fn main () { fn main () {
// read and parse configuration file let mut config_file_path = "~/.isolated-switches/config.json";
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");
// read and parse command line parameters // read and parse command line parameters
let args: Vec<String> = env::args().collect(); let args: Vec<String> = env::args().collect();
if args.len() >= 2 { for n in 1..args.len() {
let action: &str = args[1].as_str(); match args[n].as_str() {
match action { "on" => {
"on" => pin_switch_on(), pin_switch_on();
"off" => pin_switch_off(), return;
},
"off" => {
pin_switch_off();
return;
},
"status" => println!("Inverter status: {}" , if pin_status() == 0 { "off" } else { "on" }), "status" => println!("Inverter status: {}" , if pin_status() == 0 { "off" } else { "on" }),
"test" => pin_test(&config), "test" => pin_test(config_file_path),
_ => println!("So far only the parameters 'on', 'off', status and 'test' are supported.") "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 { let config: Config = read_config(&config_file_path);
println!("Low voltage!"); let pin_status_cur = pin_status();
pin_switch_off(); let voltage = get_current_voltage(&config);
}
else if voltage >= config.voltage_recover && pin_status_cur == 0 { if voltage < config.voltage_off && pin_status_cur > 0 {
println!("Voltage recovered!"); println!("Low voltage!");
pin_switch_on(); pin_switch_off();
} }
else { else if voltage >= config.voltage_recover && pin_status_cur == 0 {
// no change in status needed println!("Voltage recovered!");
let pin_status_text = if pin_status_cur == 0 { "off" } else { "on" }; pin_switch_on();
println!("No change in inverter status. Current status: {}", pin_status_text); }
} 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 //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 { fn pin_status () -> u8 {
let pin_level: u8 = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().read() as u8; let pin_level: u8 = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().read() as u8;
return pin_level; return pin_level;
@@ -89,7 +102,8 @@ fn pin_switch_off () {
println!("Inverter switched 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(); pin_switch_on();
thread::sleep(Duration::from_millis(config.test_duration_seconds * 1000)); thread::sleep(Duration::from_millis(config.test_duration_seconds * 1000));
pin_switch_off(); pin_switch_off();