v0.12.0-preety_code
This commit is contained in:
+36
-11
@@ -1,4 +1,9 @@
|
||||
fn record_zone_history(state: &AppState, zone: &Zone, outdoor_temperature: Option<f64>, poll_interval_seconds: u64) {
|
||||
fn record_zone_history(
|
||||
state: &AppState,
|
||||
zone: &Zone,
|
||||
outdoor_temperature: Option<f64>,
|
||||
poll_interval_seconds: u64,
|
||||
) {
|
||||
let device = match state.db.get_device(&zone.device_id) {
|
||||
Ok(Some(device)) => device,
|
||||
Ok(None) => return,
|
||||
@@ -14,12 +19,22 @@ fn record_zone_history(state: &AppState, zone: &Zone, outdoor_temperature: Optio
|
||||
timestamp: Utc::now(),
|
||||
gree_temperature: zone.device_temperature.or(device.current_temperature),
|
||||
external_temperature: zone.external_temperature,
|
||||
control_temperature: zone.current_temperature.or(zone.device_temperature).or(device.current_temperature),
|
||||
target_temperature: zone.effective_setpoint.or(zone.manual_setpoint).or(Some(zone.setpoint)),
|
||||
control_temperature: zone
|
||||
.current_temperature
|
||||
.or(zone.device_temperature)
|
||||
.or(device.current_temperature),
|
||||
target_temperature: zone
|
||||
.effective_setpoint
|
||||
.or(zone.manual_setpoint)
|
||||
.or(Some(zone.setpoint)),
|
||||
device_setpoint: zone.device_setpoint.or(Some(device.target_temperature)),
|
||||
outdoor_temperature: outdoor_temperature.or(device.outdoor_temperature),
|
||||
power: device.power,
|
||||
mode: if zone.effective_mode.is_empty() { device.mode.clone() } else { zone.effective_mode.clone() },
|
||||
mode: if zone.effective_mode.is_empty() {
|
||||
device.mode.clone()
|
||||
} else {
|
||||
zone.effective_mode.clone()
|
||||
},
|
||||
fan_speed: device.fan_speed,
|
||||
demand: zone.demand,
|
||||
control_source: zone.control_temperature_source.clone(),
|
||||
@@ -53,7 +68,9 @@ fn record_ha_history(
|
||||
match state.db.add_ha_reading_if_due(&reading, interval) {
|
||||
Ok(true) => queue_influx_ha(state, reading),
|
||||
Ok(false) => {}
|
||||
Err(err) => tracing::warn!(error=?err, entity_id=%entity_id, "cannot save Home Assistant history sample"),
|
||||
Err(err) => {
|
||||
tracing::warn!(error=?err, entity_id=%entity_id, "cannot save Home Assistant history sample")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,7 +78,9 @@ fn queue_influx_device(state: &AppState, reading: Reading) {
|
||||
let state = state.clone();
|
||||
tokio::spawn(async move {
|
||||
let settings = state.settings.read().await.influxdb.clone();
|
||||
if !settings.enabled { return; }
|
||||
if !settings.enabled {
|
||||
return;
|
||||
}
|
||||
if let Err(err) = influxdb::write_device(&state.http, &settings, &reading).await {
|
||||
tracing::warn!(error=?err, device_id=%reading.device_id, "cannot write device metric to InfluxDB");
|
||||
}
|
||||
@@ -72,7 +91,9 @@ fn queue_influx_zone(state: &AppState, reading: ZoneReading) {
|
||||
let state = state.clone();
|
||||
tokio::spawn(async move {
|
||||
let settings = state.settings.read().await.influxdb.clone();
|
||||
if !settings.enabled { return; }
|
||||
if !settings.enabled {
|
||||
return;
|
||||
}
|
||||
if let Err(err) = influxdb::write_zone(&state.http, &settings, &reading).await {
|
||||
tracing::warn!(error=?err, zone_id=%reading.zone_id, "cannot write zone metric to InfluxDB");
|
||||
}
|
||||
@@ -83,7 +104,9 @@ fn queue_influx_ha(state: &AppState, reading: HaReading) {
|
||||
let state = state.clone();
|
||||
tokio::spawn(async move {
|
||||
let settings = state.settings.read().await.influxdb.clone();
|
||||
if !settings.enabled { return; }
|
||||
if !settings.enabled {
|
||||
return;
|
||||
}
|
||||
if let Err(err) = influxdb::write_ha(&state.http, &settings, &reading).await {
|
||||
tracing::warn!(error=?err, entity_id=%reading.entity_id, "cannot write HA metric to InfluxDB");
|
||||
}
|
||||
@@ -91,12 +114,15 @@ fn queue_influx_ha(state: &AppState, reading: HaReading) {
|
||||
}
|
||||
|
||||
fn gree_outdoor_temperature(devices: &[Device]) -> Option<f64> {
|
||||
let mut values: Vec<f64> = devices.iter()
|
||||
let mut values: Vec<f64> = devices
|
||||
.iter()
|
||||
.filter(|device| device.enabled && device.online && device.communication_failures == 0)
|
||||
.filter_map(|device| device.outdoor_temperature)
|
||||
.filter(|value| value.is_finite() && (-60.0..=70.0).contains(value))
|
||||
.collect();
|
||||
if values.is_empty() { return None; }
|
||||
if values.is_empty() {
|
||||
return None;
|
||||
}
|
||||
values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
|
||||
let middle = values.len() / 2;
|
||||
let value = if values.len() % 2 == 0 {
|
||||
@@ -106,4 +132,3 @@ fn gree_outdoor_temperature(devices: &[Device]) -> Option<f64> {
|
||||
};
|
||||
Some((value * 10.0).round() / 10.0)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user