Add enhanced fan logic

This commit is contained in:
KenwoodFox
2026-06-18 14:09:14 -04:00
parent 49618f0c0a
commit bbe5fd5452
7 changed files with 66 additions and 67 deletions

View File

@@ -8,12 +8,16 @@ use tracing::{debug, info, warn};
use crate::alarm::SharedAlarms;
use crate::config::{InfluxConfig, RenogyConfig};
use crate::gpio::{Fan, SharedFan};
use crate::renogy::{self, Client as RenogyClient, ControllerData, ControllerInfo};
const RECONNECT_INTERVAL_S: f64 = 15.0;
pub async fn run(
config: RenogyConfig,
fan_on_temp_c: f64,
fan_off_temp_c: f64,
fan: SharedFan,
influx: Option<InfluxConfig>,
alarms: SharedAlarms,
) -> anyhow::Result<()> {
@@ -43,7 +47,16 @@ pub async fn run(
continue;
};
match connect_and_poll(&port, &config, influx.as_ref(), &alarms).await {
match connect_and_poll(
&port,
&config,
fan_on_temp_c,
fan_off_temp_c,
&fan,
influx.as_ref(),
&alarms,
)
.await {
Ok(()) => return Ok(()),
Err(e) => {
alarms.set_fault("renogy", true).await;
@@ -57,6 +70,9 @@ pub async fn run(
async fn connect_and_poll(
port: &str,
config: &RenogyConfig,
fan_on_temp_c: f64,
fan_off_temp_c: f64,
fan: &SharedFan,
influx: Option<&InfluxConfig>,
alarms: &SharedAlarms,
) -> anyhow::Result<()> {
@@ -104,13 +120,23 @@ async fn connect_and_poll(
// Clear fault
alarms.set_fault("renogy", false).await;
// Debug log
debug!(
battery_v = data.battery_voltage,
battery_soc = data.battery_soc,
solar_w = data.solar_watts,
"Renogy poll ok"
);
{
let mut fan = fan.lock().await;
update_fan(
&mut fan,
fan_on_temp_c,
fan_off_temp_c,
data.controller_temperature_c,
);
debug!(
battery_v = data.battery_voltage,
battery_soc = data.battery_soc,
controller_temp_c = data.controller_temperature_c,
solar_w = data.solar_watts,
fan = if fan.on() { "on" } else { "off" },
"Renogy poll ok"
);
}
// Publish data
if let (Some(client), Some(influx)) = (&influx_client, influx) {
@@ -124,6 +150,25 @@ async fn connect_and_poll(
}
}
fn update_fan(fan: &mut Fan, on_temp_c: f64, off_temp_c: f64, controller_temp_c: u8) {
let temp_c = f64::from(controller_temp_c);
if !fan.on() && temp_c > on_temp_c {
fan.set_on(true);
info!(
controller_temp_c = temp_c,
threshold = on_temp_c,
"Fan on"
);
} else if fan.on() && temp_c <= off_temp_c {
fan.set_on(false);
info!(
controller_temp_c = temp_c,
threshold = off_temp_c,
"Fan off"
);
}
}
// Publish data to InfluxDB
async fn publish(
client: &InfluxClient,