v0.14.2
This commit is contained in:
+51
-7
@@ -121,13 +121,7 @@ fn queue_influx_ha(state: &AppState, reading: HaReading) {
|
||||
});
|
||||
}
|
||||
|
||||
fn gree_outdoor_temperature(devices: &[Device]) -> Option<f64> {
|
||||
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();
|
||||
fn median_outdoor_temperature(mut values: Vec<f64>) -> Option<f64> {
|
||||
if values.is_empty() {
|
||||
return None;
|
||||
}
|
||||
@@ -140,3 +134,53 @@ fn gree_outdoor_temperature(devices: &[Device]) -> Option<f64> {
|
||||
};
|
||||
Some((value * 10.0).round() / 10.0)
|
||||
}
|
||||
|
||||
fn valid_outdoor_temperature(device: &Device) -> Option<f64> {
|
||||
if !device.enabled || !device.online || device.communication_failures != 0 {
|
||||
return None;
|
||||
}
|
||||
device
|
||||
.outdoor_temperature
|
||||
.filter(|value| value.is_finite() && (-60.0..=70.0).contains(value))
|
||||
}
|
||||
|
||||
fn gree_outdoor_temperature(devices: &[Device], device_groups: &[DeviceGroup]) -> Option<f64> {
|
||||
let by_id: HashMap<&str, &Device> = devices
|
||||
.iter()
|
||||
.map(|device| (device.id.as_str(), device))
|
||||
.collect();
|
||||
let mut grouped_ids = HashSet::new();
|
||||
let mut values = Vec::new();
|
||||
|
||||
for group in device_groups {
|
||||
for device_id in &group.device_ids {
|
||||
grouped_ids.insert(device_id.as_str());
|
||||
}
|
||||
let group_value = group
|
||||
.outdoor_temperature_device_id
|
||||
.as_deref()
|
||||
.and_then(|source_id| by_id.get(source_id).copied())
|
||||
.and_then(valid_outdoor_temperature)
|
||||
.or_else(|| {
|
||||
median_outdoor_temperature(
|
||||
group
|
||||
.device_ids
|
||||
.iter()
|
||||
.filter_map(|id| by_id.get(id.as_str()).copied())
|
||||
.filter_map(valid_outdoor_temperature)
|
||||
.collect(),
|
||||
)
|
||||
});
|
||||
if let Some(value) = group_value {
|
||||
values.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
values.extend(
|
||||
devices
|
||||
.iter()
|
||||
.filter(|device| !grouped_ids.contains(device.id.as_str()))
|
||||
.filter_map(valid_outdoor_temperature),
|
||||
);
|
||||
median_outdoor_temperature(values)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user