From 0afe8d273b56cf760dc12c1870f3d01cd5cc0786 Mon Sep 17 00:00:00 2001 From: Aaska Black Wolf Date: Tue, 30 Jun 2026 14:13:19 +0200 Subject: [PATCH] cleanup --- config.json | 1 + src/main.rs | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/config.json b/config.json index 8b4ee42..7a36e61 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,7 @@ { "database": "./VEDirect.db", "device_name_voltage": "0xA10C-HQ2535PVZGE", + "test_duration_seconds": 1, "voltage_off": 23.0, "voltage_recover": 24.0 } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 91b54b8..a51b940 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ use rppal::gpio::{Gpio, OutputPin}; use serde::{Deserialize}; use serde_json; use sqlite; -use std::error::Error; use std::env; use std::fs::File; use std::path::Path; @@ -13,18 +12,18 @@ use std::time::Duration; const GPIO_SWITCH: u8 = 24; // set duration of pin switching test -const TEST_DURATION: u64 = 1000; #[derive(Deserialize)] -#[derive(Debug)] struct Config{ database: String, device_name_voltage: String, + test_duration_seconds: u64, voltage_off: f32, voltage_recover: f32, } #[derive(Deserialize)] +#[allow(non_snake_case)] struct DbData{ V: String, } @@ -44,24 +43,26 @@ fn main () { match action { "on" => pin_switch_on(), "off" => pin_switch_off(), - "test" => pin_test(), - &_ => todo!("So far only the parameters 'on', 'off' and 'test' are supported.") + "test" => pin_test(&config), + &_ => println!("So far only the parameters 'on', 'off' and 'test' are supported.") } } else { // no command line parameter set; check for voltages and switch the inverter accordingly - pin_status(); + let pin_status_cur = pin_status(); let voltage = get_current_voltage(&config); - if voltage < config.voltage_off && pin_status() > 0 { - println!("Siwtching off inverter due to low voltage!"); + if voltage < config.voltage_off && pin_status_cur > 0 { + println!("Low voltage!"); pin_switch_off(); } - else if voltage >= config.voltage_recover && pin_status() == 0 { - println!("Voltage recovered! Switching inverter back on."); + 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); } } @@ -69,7 +70,7 @@ fn main () { } fn pin_status () -> u8 { - let mut 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; } @@ -77,17 +78,19 @@ fn pin_switch_on () { let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output(); pin.set_reset_on_drop(false); pin.set_high(); + println!("Inverter switched on."); } fn pin_switch_off () { let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output(); pin.set_reset_on_drop(false); pin.set_low(); + println!("Inverter switched off."); } -fn pin_test () { +fn pin_test (config: &Config) { pin_switch_on(); - thread::sleep(Duration::from_millis(TEST_DURATION)); + thread::sleep(Duration::from_millis(config.test_duration_seconds * 1000)); pin_switch_off(); }