added command line option to customize the configuration file path
This commit is contained in:
+29
-15
@@ -30,25 +30,30 @@ 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<String> = 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 config: Config = read_config(&config_file_path);
|
||||
let pin_status_cur = pin_status();
|
||||
let voltage = get_current_voltage(&config);
|
||||
|
||||
@@ -65,11 +70,19 @@ fn main () {
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user