Delete relay energy information

This commit is contained in:
Pierre HUBERT 2024-09-17 22:42:24 +02:00
parent 565db05fb0
commit 92878e6548
4 changed files with 27 additions and 6 deletions

View File

@ -241,11 +241,15 @@ impl AppConfig {
self.storage_path().join("relays_runtime") self.storage_path().join("relays_runtime")
} }
/// Get relay runtime stats path for a given day /// Get relay runtime stats path for a given relay
pub fn relay_runtime_file_path(&self, relay_id: DeviceRelayID, day: u64) -> PathBuf { pub fn relay_runtime_stats_dir(&self, relay_id: DeviceRelayID) -> PathBuf {
self.relays_runtime_stats_storage_path() self.relays_runtime_stats_storage_path()
.join(relay_id.0.to_string()) .join(relay_id.0.to_string())
.join(day.to_string()) }
/// Get relay runtime stats path for a given relay for a given day
pub fn relay_runtime_day_file_path(&self, relay_id: DeviceRelayID, day: u64) -> PathBuf {
self.relay_runtime_stats_dir(relay_id).join(day.to_string())
} }
} }

View File

@ -315,6 +315,12 @@ impl DevicesList {
.into()); .into());
} }
// Delete relay energy information
let stats_dir = AppConfig::get().relay_runtime_stats_dir(relay_id);
if stats_dir.is_dir() {
std::fs::remove_dir_all(stats_dir)?;
}
// Delete the relay // Delete the relay
let device = self let device = self
.relay_get_device(relay_id) .relay_get_device(relay_id)

View File

@ -157,8 +157,19 @@ impl Handler<DeleteDevice> for EnergyActor {
fn handle(&mut self, msg: DeleteDevice, _ctx: &mut Context<Self>) -> Self::Result { fn handle(&mut self, msg: DeleteDevice, _ctx: &mut Context<Self>) -> Self::Result {
log::info!("Requested to delete device {:?}...", &msg.0); log::info!("Requested to delete device {:?}...", &msg.0);
let Some(device) = self.devices.get_single(&msg.0) else {
log::warn!("Requested to delete non-existent device!");
return Ok(());
};
// Delete device relays
for relay in device.relays {
self.devices.relay_delete(relay.id)?;
}
self.devices.delete(&msg.0)?; self.devices.delete(&msg.0)?;
// TODO : delete energy related information
Ok(()) Ok(())
} }
} }

View File

@ -26,7 +26,7 @@ impl RelayStateHistory {
/// Open relay state history file, if it exists, or create an empty one /// Open relay state history file, if it exists, or create an empty one
pub fn open(id: DeviceRelayID, time: u64) -> anyhow::Result<Self> { pub fn open(id: DeviceRelayID, time: u64) -> anyhow::Result<Self> {
let day = day_number(time); let day = day_number(time);
let path = AppConfig::get().relay_runtime_file_path(id, day); let path = AppConfig::get().relay_runtime_day_file_path(id, day);
if path.exists() { if path.exists() {
Ok(Self { Ok(Self {
@ -86,7 +86,7 @@ impl RelayStateHistory {
/// Persist device relay state history /// Persist device relay state history
pub fn save(&self) -> anyhow::Result<()> { pub fn save(&self) -> anyhow::Result<()> {
let path = AppConfig::get().relay_runtime_file_path(self.id, self.day); let path = AppConfig::get().relay_runtime_day_file_path(self.id, self.day);
files_utils::create_directory_if_missing(path.parent().unwrap())?; files_utils::create_directory_if_missing(path.parent().unwrap())?;
std::fs::write(path, &self.buff)?; std::fs::write(path, &self.buff)?;
Ok(()) Ok(())