Ready to implement update logic

This commit is contained in:
Pierre HUBERT 2024-09-10 19:55:51 +02:00
parent c74ed0cfbb
commit c1c01058d8

View File

@ -21,7 +21,7 @@ impl DeviceState {
} }
#[derive(Default, Clone)] #[derive(Default, Clone)]
struct RelayState { pub struct RelayState {
on: bool, on: bool,
since: usize, since: usize,
} }
@ -34,19 +34,12 @@ pub struct EnergyEngine {
impl EnergyEngine { impl EnergyEngine {
pub fn device_state(&mut self, dev_id: &DeviceId) -> &mut DeviceState { pub fn device_state(&mut self, dev_id: &DeviceId) -> &mut DeviceState {
if !self.devices_state.contains_key(dev_id) { self.devices_state.entry(dev_id.clone()).or_default();
self.devices_state
.insert(dev_id.clone(), Default::default());
}
self.devices_state.get_mut(dev_id).unwrap() self.devices_state.get_mut(dev_id).unwrap()
} }
pub fn relay_state(&mut self, relay_id: DeviceRelayID) -> &mut RelayState { pub fn relay_state(&mut self, relay_id: DeviceRelayID) -> &mut RelayState {
if !self.relays_state.contains_key(&relay_id) { self.relays_state.entry(relay_id).or_default();
self.relays_state.insert(relay_id, Default::default());
}
self.relays_state.get_mut(&relay_id).unwrap() self.relays_state.get_mut(&relay_id).unwrap()
} }
@ -65,19 +58,31 @@ impl EnergyEngine {
} }
pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) { pub fn refresh(&mut self, curr_consumption: EnergyConsumption, devices: &[Device]) {
// TODO : force creation of missing relays state
let new_relays_state = self.relays_state.clone(); let new_relays_state = self.relays_state.clone();
// Forcefully turn off relays that belongs to offline devices // TODO Forcefully turn off relays that belongs to offline devices
// Forcefully turn off relays with dependency conflicts // TODO Forcefully turn off relays with dependency conflicts
// Virtually turn off all relays that can be stopped // TODO Virtually turn off all relays that can be stopped
// Turn on relays based on priority / dependencies // TODO Turn on relays based on priority / dependencies
// Turn on relays with running constraints // TODO Turn on relays with running constraints
// Commit changes // TODO Commit changes
self.print_summary(curr_consumption, devices); // TODO :replace
for (id, new_state) in &new_relays_state {
let curr_state = self.relay_state(*id);
if curr_state.on != new_state.on {
curr_state.on = new_state.on;
curr_state.since = time_secs() as usize;
}
log::info!("Changing state of {id:?}");
}
self.print_summary(curr_consumption, devices);
} }
} }