From fa52f47d9d4647d2a5dc0b0f4fb67418f040547a Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 22 Sep 2023 07:43:56 +0200 Subject: [PATCH] Make router and raspis always on --- devices.conf | 8 ++++---- midea.py | 3 --- resources/js/main.js | 25 +++++++++++++++---------- resources/static/index.html | 3 ++- src/data.rs | 2 +- src/db.rs | 30 +++++++++++++++++++++++------- src/devices.rs | 6 +++--- src/main.rs | 2 +- 8 files changed, 49 insertions(+), 30 deletions(-) delete mode 100644 midea.py diff --git a/devices.conf b/devices.conf index 9ed1d37..8be4159 100644 --- a/devices.conf +++ b/devices.conf @@ -1,8 +1,8 @@ { "plugs": [ - "Tasmota-Plug-1", - "Tasmota-Plug-2", - "Tasmota-Plug-3", - "Tasmota-Plug-4" + ["Tasmota-Plug-1", false], + ["Tasmota-Plug-2", false], + ["Tasmota-Plug-3", true], + ["Tasmota-Plug-4", true] ] } \ No newline at end of file diff --git a/midea.py b/midea.py deleted file mode 100644 index decb0ab..0000000 --- a/midea.py +++ /dev/null @@ -1,3 +0,0 @@ -import midea_ac_lan.midea.core as midea_core - -midea_core \ No newline at end of file diff --git a/resources/js/main.js b/resources/js/main.js index 2748ffe..6430c3a 100644 --- a/resources/js/main.js +++ b/resources/js/main.js @@ -67,7 +67,7 @@ async function startup() { } } let button_icon = document.createElement('i'); - button_icon.className = "fa fa-edit"; + button_icon.className = "fa fa-pencil-square-o"; device_name_entry.appendChild(device_name); device_name_edit.appendChild(button_icon); @@ -106,16 +106,21 @@ async function startup() { let device_power_state = document.createElement('label'); device_power_state.innerText = device_state["power"]; device_power_state.id = "power_" + device_id; - let device_power_on = document.createElement('button'); - device_power_on.innerText = "On" - device_power_on.onclick = async () => { await power_on(device_id) }; - let device_power_off = document.createElement('button'); - device_power_off.innerText = "Off" - device_power_off.onclick = async () => { await power_off(device_id) }; device_power_state_entry.appendChild(device_power_state); - device_power_state_entry.appendChild(device_power_on); - device_power_state_entry.appendChild(device_power_off); + + if (devices[i][2] == true) { + let device_power_on = document.createElement('button'); + device_power_on.innerText = "On" + device_power_on.onclick = async () => { await power_on(device_id) }; + let device_power_off = document.createElement('button'); + device_power_off.innerText = "Off" + device_power_off.onclick = async () => { await power_off(device_id) }; + + device_power_state_entry.appendChild(device_power_on); + device_power_state_entry.appendChild(device_power_off); + } + row_device.appendChild(device_power_state_entry); // create device power draw column @@ -125,7 +130,7 @@ async function startup() { let device_power_draw_graph_button = document.createElement('button'); device_power_draw_graph_button.onclick = async () => { await render_graph(device_id, device_descriptor) }; let device_power_draw_graph_button_icon = document.createElement('i'); - device_power_draw_graph_button_icon.className = "fa fa-chart-bar"; + device_power_draw_graph_button_icon.className = "fa fa-line-chart"; device_power_draw_graph_button.appendChild(device_power_draw_graph_button_icon); device_power_draw_entry.appendChild(device_power_draw); diff --git a/resources/static/index.html b/resources/static/index.html index 26023d9..fa9bd51 100644 --- a/resources/static/index.html +++ b/resources/static/index.html @@ -4,7 +4,8 @@ Smart Homeserver - + diff --git a/src/data.rs b/src/data.rs index 0887dab..95a8921 100644 --- a/src/data.rs +++ b/src/data.rs @@ -192,7 +192,7 @@ mod test { let db = DataBase::new("home_server.db").await?; let devices = Devices::read("devices.conf").await?; - for plug in devices.plugs { + for (plug, _) in devices.plugs { println!("===== data for plug {plug} ====="); let days: HashMap>> = split_into_days( diff --git a/src/db.rs b/src/db.rs index baeaba2..60b0e59 100644 --- a/src/db.rs +++ b/src/db.rs @@ -37,6 +37,7 @@ impl DataBase { id INTEGER PRIMARY KEY, device VARCHAR(60) NOT NULL, type VARCHAR(30) NOT NULL, + control INTEGER NOT NULL, name VARCHAR(80) )", [], @@ -75,7 +76,7 @@ impl DataBase { } pub fn register_devices(&self, devices: &Devices) -> Result<()> { - for device in devices.plugs.iter() { + for (device, control) in devices.plugs.iter() { self.sql.execute( &format!( "INSERT INTO devices (device, type) @@ -90,6 +91,19 @@ impl DataBase { ), [], )?; + + let ctl = if *control { 1 } else { 0 }; + + self.sql.execute( + &format!( + " + UPDATE devices + SET control=\"{ctl}\" + WHERE device=\"{device}\" + " + ), + [], + )?; } Ok(()) @@ -116,16 +130,18 @@ impl DataBase { .sql .prepare(&format!( " - SELECT device, type, name + SELECT device, type, name, control FROM devices " ))? - .query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))? + .query_map([], |row| { + Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?)) + })? { - let (device, dev_type, name): (String, String, Option) = row?; + let (device, dev_type, name, control): (String, String, Option, i32) = row?; match dev_type.as_str() { - "plug" => devices.plugs.push((device, name)), + "plug" => devices.plugs.push((device, name, control != 0)), _ => panic!(), } @@ -195,7 +211,7 @@ mod test { let db = DataBase::new("startup_test.db").await?; db.register_devices(&Devices { - plugs: vec!["test".to_string()], + plugs: vec![("test".to_string(), true)], })?; fs::remove_file("startup_test.db")?; @@ -210,7 +226,7 @@ mod test { let device_name = "test"; db.register_devices(&Devices { - plugs: vec![device_name.to_string()], + plugs: vec![(device_name.to_string(), true)], })?; db.write(device_name, 0, 5.5)?; diff --git a/src/devices.rs b/src/devices.rs index fe5c8c0..596ec71 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -6,7 +6,7 @@ use serde_json::{from_str, to_string, to_string_pretty}; #[derive(Clone, PartialEq, Eq, Deserialize, Serialize, Debug)] pub struct Devices { - pub plugs: Vec, + pub plugs: Vec<(String, bool)>, } impl Devices { @@ -24,7 +24,7 @@ impl Devices { #[derive(Debug, Clone, Deserialize, Serialize, Default)] pub struct DevicesWithName { - pub plugs: Vec<(String, Option)>, + pub plugs: Vec<(String, Option, bool)>, } impl DevicesWithName { @@ -41,7 +41,7 @@ mod test { #[test] fn create_conf() -> Result<()> { let devices = Devices { - plugs: vec!["Dev1".to_string(), "Dev2".to_string()], + plugs: vec![("Dev1".to_string(), true), ("Dev2".to_string(), false)], }; devices.save("test_devices.conf") diff --git a/src/main.rs b/src/main.rs index b187072..254df92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,7 +89,7 @@ async fn main() -> Result<()> { let tasmota_plugs: Vec = devices .plugs .iter() - .map(|plug| Tasmota::new(plug)) + .map(|(plug, _)| Tasmota::new(plug)) .collect(); try_join!(