Implement main functionality

This commit is contained in:
hodasemi 2023-09-19 11:18:22 +02:00
parent 09ab124103
commit c726ddefc1
4 changed files with 75 additions and 18 deletions

View file

@ -1,7 +1,7 @@
use std::path::Path; use std::path::Path;
use anyhow::Result; use anyhow::Result;
use rusqlite::Connection; use rusqlite::{Connection, ToSql};
pub struct DataBase { pub struct DataBase {
sql: Connection, sql: Connection,
@ -19,10 +19,57 @@ impl DataBase {
} }
fn generate_tables(&self) -> Result<()> { fn generate_tables(&self) -> Result<()> {
todo!() self.sql.execute(
"CREATE TABLE IF NOT EXISTS data (
id INTEGER PRIMARY KEY,
device TEXT NOT NULL,
time BIGINT NOT NULL,
watts REAL NOT NULL
)",
[],
)?;
Ok(())
} }
pub async fn write(&self, device_name: &str, time: u64, watts: f32) -> Result<()> { pub fn write(&self, device_name: &str, time: u64, watts: f32) -> Result<()> {
todo!() let params: &[&dyn ToSql] = &[&device_name, &time, &watts];
self.sql.execute(
"INSERT INTO data (device, time, watts)
VALUES (?1, ?2, ?3)",
params,
)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use std::fs;
use anyhow::Result;
use super::DataBase;
#[tokio::test]
async fn test_connection() -> Result<()> {
DataBase::new("connection_test.db").await?;
fs::remove_file("connection_test.db")?;
Ok(())
}
#[tokio::test]
async fn test_write() -> Result<()> {
let db = DataBase::new("write_test.db").await?;
db.write("test", 0, 5.5)?;
fs::remove_file("write_test.db")?;
Ok(())
} }
} }

View file

@ -14,8 +14,9 @@ impl Devices {
Ok(from_str(&fs::read_to_string(file)?)?) Ok(from_str(&fs::read_to_string(file)?)?)
} }
#[allow(unused)]
pub fn save(&self, file: &str) -> Result<()> { pub fn save(&self, file: &str) -> Result<()> {
fs::write("devices.conf", to_string_pretty(self)?)?; fs::write(file, to_string_pretty(self)?)?;
Ok(()) Ok(())
} }
@ -32,6 +33,6 @@ mod test {
plugs: vec!["Dev1".to_string(), "Dev2".to_string()], plugs: vec!["Dev1".to_string(), "Dev2".to_string()],
}; };
devices.save("devices.conf") devices.save("test_devices.conf")
} }
} }

View file

@ -35,13 +35,12 @@ async fn main() -> Result<()> {
loop { loop {
try_join_all(tasmota_plugs.iter().map(|plug| async { try_join_all(tasmota_plugs.iter().map(|plug| async {
let usage = plug.read_power_usage().await?; if let Ok(usage) = plug.read_power_usage().await {
shared_db shared_db
.lock() .lock()
.unwrap() .unwrap()
.write(plug.name(), since_epoch()?, usage) .write(plug.name(), since_epoch()?, usage)?;
.await?; }
Ok::<(), anyhow::Error>(()) Ok::<(), anyhow::Error>(())
})) }))

View file

@ -38,22 +38,32 @@ impl Tasmota {
} }
pub async fn turn_on_led(&self) -> Result<()> { pub async fn turn_on_led(&self) -> Result<()> {
todo!("LedPower=1") self.post("LedPower=1").await?;
Ok(())
} }
pub async fn turn_off_led(&self) -> Result<()> { pub async fn turn_off_led(&self) -> Result<()> {
todo!("LedPower=2") self.post("LedPower=2").await?;
Ok(())
} }
pub async fn switch_on(&self) -> Result<()> { pub async fn switch_on(&self) -> Result<()> {
todo!("Power0=1") self.post("Power0=1").await?;
Ok(())
} }
pub async fn switch_off(&self) -> Result<()> { pub async fn switch_off(&self) -> Result<()> {
todo!("Power0=0") self.post("Power0=0").await?;
Ok(())
} }
pub async fn read_power_usage(&self) -> Result<f32> { pub async fn read_power_usage(&self) -> Result<f32> {
todo!("Status=8") let res = self.get("Status=8").await?;
Ok(res.parse()?)
} }
} }