This commit is contained in:
Mateusz Gruszczyński
2026-09-14 16:32:28 +02:00
parent c3fbc5ccc6
commit 021cbddba5
68 changed files with 7780 additions and 202 deletions
+19 -2
View File
@@ -23,6 +23,18 @@ impl Db {
Ok(serde_json::to_string(value)?)
}
/// Device protocol keys are intentionally omitted by the public `Device` serializer.
/// Persist them only in the private SQLite payload so normal API responses never expose them.
fn device_to_storage_json(device: &Device) -> Result<String> {
let mut value = serde_json::to_value(device)?;
if let Some(object) = value.as_object_mut() {
if let Some(key) = device.key.as_ref().filter(|key| !key.is_empty()) {
object.insert("key".into(), serde_json::Value::String(key.clone()));
}
}
Ok(serde_json::to_string(&value)?)
}
pub fn count_devices(&self) -> Result<u64> {
let conn = self.lock()?;
let count: i64 = conn.query_row(queries::COUNT_DEVICES, [], |row| row.get(0))?;
@@ -30,13 +42,17 @@ impl Db {
}
pub fn save_device(&self, device: &Device) -> Result<()> {
let payload = Self::to_json(device)?;
let payload = Self::device_to_storage_json(device)?;
let index_mac = match device.connection_type {
ConnectionType::Local => device.mac.clone(),
ConnectionType::GreeCloud => format!("cloud:{}", device.cloud_device_id.as_deref().unwrap_or(&device.mac)),
};
let conn = self.lock()?;
conn.execute(
queries::UPSERT_DEVICE,
params![
device.id,
device.mac,
index_mac,
device.name,
device.ip,
device.simulated as i64,
@@ -76,6 +92,7 @@ impl Db {
let mut conn = self.lock()?;
let tx = conn.transaction()?;
tx.execute(queries::DELETE_DEVICE_READINGS, [id])?;
tx.execute(queries::DELETE_DEVICE_ENERGY_READINGS, [id])?;
tx.execute(queries::DELETE_ZONE_READINGS_BY_DEVICE_ID, [id])?;
tx.execute(queries::DELETE_SCHEDULES_BY_DEVICE_ID, [id])?;
tx.execute(queries::DELETE_ZONES_BY_DEVICE_ID, [id])?;