cleanup
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"database": "./VEDirect.db",
|
"database": "./VEDirect.db",
|
||||||
"device_name_voltage": "0xA10C-HQ2535PVZGE",
|
"device_name_voltage": "0xA10C-HQ2535PVZGE",
|
||||||
|
"test_duration_seconds": 1,
|
||||||
"voltage_off": 23.0,
|
"voltage_off": 23.0,
|
||||||
"voltage_recover": 24.0
|
"voltage_recover": 24.0
|
||||||
}
|
}
|
||||||
+17
-14
@@ -2,7 +2,6 @@ use rppal::gpio::{Gpio, OutputPin};
|
|||||||
use serde::{Deserialize};
|
use serde::{Deserialize};
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use sqlite;
|
use sqlite;
|
||||||
use std::error::Error;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
@@ -13,18 +12,18 @@ use std::time::Duration;
|
|||||||
const GPIO_SWITCH: u8 = 24;
|
const GPIO_SWITCH: u8 = 24;
|
||||||
|
|
||||||
// set duration of pin switching test
|
// set duration of pin switching test
|
||||||
const TEST_DURATION: u64 = 1000;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[derive(Debug)]
|
|
||||||
struct Config{
|
struct Config{
|
||||||
database: String,
|
database: String,
|
||||||
device_name_voltage: String,
|
device_name_voltage: String,
|
||||||
|
test_duration_seconds: u64,
|
||||||
voltage_off: f32,
|
voltage_off: f32,
|
||||||
voltage_recover: f32,
|
voltage_recover: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
#[allow(non_snake_case)]
|
||||||
struct DbData{
|
struct DbData{
|
||||||
V: String,
|
V: String,
|
||||||
}
|
}
|
||||||
@@ -44,24 +43,26 @@ fn main () {
|
|||||||
match action {
|
match action {
|
||||||
"on" => pin_switch_on(),
|
"on" => pin_switch_on(),
|
||||||
"off" => pin_switch_off(),
|
"off" => pin_switch_off(),
|
||||||
"test" => pin_test(),
|
"test" => pin_test(&config),
|
||||||
&_ => todo!("So far only the parameters 'on', 'off' and 'test' are supported.")
|
&_ => 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
|
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);
|
let voltage = get_current_voltage(&config);
|
||||||
|
|
||||||
if voltage < config.voltage_off && pin_status() > 0 {
|
if voltage < config.voltage_off && pin_status_cur > 0 {
|
||||||
println!("Siwtching off inverter due to low voltage!");
|
println!("Low voltage!");
|
||||||
pin_switch_off();
|
pin_switch_off();
|
||||||
}
|
}
|
||||||
else if voltage >= config.voltage_recover && pin_status() == 0 {
|
else if voltage >= config.voltage_recover && pin_status_cur == 0 {
|
||||||
println!("Voltage recovered! Switching inverter back on.");
|
println!("Voltage recovered!");
|
||||||
pin_switch_on();
|
pin_switch_on();
|
||||||
}
|
}
|
||||||
else {
|
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 {
|
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;
|
return pin_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,17 +78,19 @@ fn pin_switch_on () {
|
|||||||
let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output();
|
let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output();
|
||||||
pin.set_reset_on_drop(false);
|
pin.set_reset_on_drop(false);
|
||||||
pin.set_high();
|
pin.set_high();
|
||||||
|
println!("Inverter switched on.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pin_switch_off () {
|
fn pin_switch_off () {
|
||||||
let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output();
|
let mut pin: OutputPin = Gpio::new().unwrap().get(GPIO_SWITCH).unwrap().into_output();
|
||||||
pin.set_reset_on_drop(false);
|
pin.set_reset_on_drop(false);
|
||||||
pin.set_low();
|
pin.set_low();
|
||||||
|
println!("Inverter switched off.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pin_test () {
|
fn pin_test (config: &Config) {
|
||||||
pin_switch_on();
|
pin_switch_on();
|
||||||
thread::sleep(Duration::from_millis(TEST_DURATION));
|
thread::sleep(Duration::from_millis(config.test_duration_seconds * 1000));
|
||||||
pin_switch_off();
|
pin_switch_off();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user