diff --git a/central_backend/src/app_config.rs b/central_backend/src/app_config.rs index 9a89241..8f159c8 100644 --- a/central_backend/src/app_config.rs +++ b/central_backend/src/app_config.rs @@ -241,11 +241,15 @@ impl AppConfig { self.storage_path().join("relays_runtime") } - /// Get relay runtime stats path for a given day - pub fn relay_runtime_file_path(&self, relay_id: DeviceRelayID, day: u64) -> PathBuf { + /// Get relay runtime stats path for a given relay + pub fn relay_runtime_stats_dir(&self, relay_id: DeviceRelayID) -> PathBuf { self.relays_runtime_stats_storage_path() .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()) } } diff --git a/central_backend/src/devices/devices_list.rs b/central_backend/src/devices/devices_list.rs index 1c6bbd1..0f5763e 100644 --- a/central_backend/src/devices/devices_list.rs +++ b/central_backend/src/devices/devices_list.rs @@ -315,6 +315,12 @@ impl DevicesList { .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 let device = self .relay_get_device(relay_id) diff --git a/central_backend/src/energy/energy_actor.rs b/central_backend/src/energy/energy_actor.rs index 1371708..9b677cb 100644 --- a/central_backend/src/energy/energy_actor.rs +++ b/central_backend/src/energy/energy_actor.rs @@ -157,8 +157,19 @@ impl Handler for EnergyActor { fn handle(&mut self, msg: DeleteDevice, _ctx: &mut Context) -> Self::Result { 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)?; - // TODO : delete energy related information + Ok(()) } } diff --git a/central_backend/src/energy/relay_state_history.rs b/central_backend/src/energy/relay_state_history.rs index a84e63e..ac22b1a 100644 --- a/central_backend/src/energy/relay_state_history.rs +++ b/central_backend/src/energy/relay_state_history.rs @@ -26,7 +26,7 @@ impl RelayStateHistory { /// Open relay state history file, if it exists, or create an empty one pub fn open(id: DeviceRelayID, time: u64) -> anyhow::Result { 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() { Ok(Self { @@ -86,7 +86,7 @@ impl RelayStateHistory { /// Persist device relay state history 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())?; std::fs::write(path, &self.buff)?; Ok(())