From 7f4a88072047f16cc473d8d953d797ea8bb54752 Mon Sep 17 00:00:00 2001
From: Alex Janka
Date: Fri, 26 Jan 2024 09:04:06 +1100
Subject: [PATCH] webui shutoff voltage + better start/stop requests
---
Cargo.lock | 2 +-
Cargo.toml | 2 +-
src/server/mod.rs | 50 ++++++++++++++++++++++++--------
src/tesla_charge_rate.rs | 1 +
webapp/index.html | 1 +
webapp/script.js | 60 +++++++++++++++++++++++++++++++++++++--
webapp/shutoff/index.html | 29 +++++++++++++++++++
7 files changed, 129 insertions(+), 16 deletions(-)
create mode 100644 webapp/shutoff/index.html
diff --git a/Cargo.lock b/Cargo.lock
index c7dbdd4..423c60e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2633,7 +2633,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
-version = "1.0.29"
+version = "1.0.30"
dependencies = [
"chrono",
"clap 4.4.11",
diff --git a/Cargo.toml b/Cargo.toml
index 6645351..21cc1e3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "tesla-charge-controller"
-version = "1.0.29"
+version = "1.0.30"
edition = "2021"
license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data"
diff --git a/src/server/mod.rs b/src/server/mod.rs
index 34b8e94..e8dfaad 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -62,6 +62,9 @@ fn rocket(state: ServerState) -> rocket::Rocket {
flash,
disable_control,
enable_control,
+ set_shutoff,
+ set_shutoff_time,
+ shutoff_status,
set_max,
set_min,
set_proportional_gain,
@@ -125,27 +128,50 @@ async fn flash(state: &State, remote_addr: std::net::IpAddr) {
#[post("/disable-control")]
async fn disable_control(state: &State, remote_addr: std::net::IpAddr) {
log::warn!("disabling control: {remote_addr:?}");
- match state
- .tcrc_requests
- .send(TcrcRequest::DisableAutomaticControl)
- {
- Ok(_) => {}
- Err(e) => log::error!("Error sending stop control request: {e:?}"),
+ match state.tcrc_state.write() {
+ Ok(mut handle) => handle.control_enable = false,
+ Err(e) => log::error!("Error disabling control: {e:?}"),
}
}
#[post("/enable-control")]
async fn enable_control(state: &State, remote_addr: std::net::IpAddr) {
log::warn!("enabling control: {remote_addr:?}");
- match state
- .tcrc_requests
- .send(TcrcRequest::EnableAutomaticControl)
- {
- Ok(_) => {}
- Err(e) => log::error!("Error sending stop control request: {e:?}"),
+ match state.tcrc_state.write() {
+ Ok(mut handle) => handle.control_enable = true,
+ Err(e) => log::error!("Error enabling control: {e:?}"),
}
}
+#[post("/shutoff/voltage/")]
+async fn set_shutoff(voltage: f64, remote_addr: std::net::IpAddr) {
+ log::warn!("setting shutoff voltage: {remote_addr:?}");
+ let voltage = voltage.clamp(40., 60.);
+ write_to_config().shutoff_voltage = voltage;
+}
+
+#[post("/shutoff/time/
+
+
+
Shutoff voltage:
+
+
+
+
+
+
+
+ ←Home
+
+
+
diff --git a/webapp/script.js b/webapp/script.js
index e431f4a..14da470 100644
--- a/webapp/script.js
+++ b/webapp/script.js
@@ -40,6 +40,15 @@ function init_pid() {
});
}
+function init_shutoff() {
+ refresh_shutoff();
+ document.addEventListener("visibilitychange", () => {
+ if (!document.hidden) {
+ refresh_shutoff();
+ }
+ });
+}
+
function init_info() {
refresh_interval = register(refresh_info);
@@ -138,6 +147,34 @@ function set_derivative() {
}
}
+function set_shutoff_voltage() {
+ var set_button = document.getElementById("set-shutoff-voltage");
+ var number_input = document.getElementById("shutoff-voltage");
+ if (!isNaN(number_input.value)) {
+ set_button.disabled = true;
+ number_input.disabled = true;
+ fetch(api_url + "/shutoff/voltage/" + number_input.value, { method: "POST" })
+ .then(async (response) => {
+ let delayres = await delay(100);
+ refresh_shutoff();
+ });
+ }
+}
+
+function set_shutoff_time() {
+ var set_button = document.getElementById("set-shutoff-time");
+ var number_input = document.getElementById("shutoff-time");
+ if (!isNaN(number_input.value)) {
+ set_button.disabled = true;
+ number_input.disabled = true;
+ fetch(api_url + "/shutoff/time/" + number_input.value, { method: "POST" })
+ .then(async (response) => {
+ let delayres = await delay(100);
+ refresh_shutoff();
+ });
+ }
+}
+
function set_load_divisor() {
var set_button = document.getElementById("set-load-divisor");
var number_input = document.getElementById("load-divisor");
@@ -159,7 +196,7 @@ function disable_automatic_control() {
fetch(api_url + "/disable-control", { method: "POST" })
.then(async (response) => {
- let delayres = await delay(1000);
+ let delayres = await delay(100);
refresh_buttons();
});
}
@@ -171,7 +208,7 @@ function enable_automatic_control() {
document.body.classList.add("loading");
fetch(api_url + "/enable-control", { method: "POST" })
.then(async (response) => {
- let delayres = await delay(1000);
+ let delayres = await delay(100);
refresh_buttons();
});
}
@@ -242,6 +279,25 @@ function update_gains(data) {
load_divisor_input.value = data.load_divisor;
}
+function refresh_shutoff() {
+ fetch(api_url + "/shutoff/status")
+ .then((response) => response.json())
+ .then((json) => update_shutoff(json));
+}
+
+function update_shutoff(data) {
+ var voltage_set_button = document.getElementById("set-shutoff-voltage");
+ var voltage_number_input = document.getElementById("shutoff-voltage");
+ voltage_set_button.disabled = false;
+ voltage_number_input.disabled = false;
+ voltage_number_input.value = data.voltage;
+ var time_set_button = document.getElementById("set-shutoff-time");
+ var time_number_input = document.getElementById("shutoff-time");
+ time_set_button.disabled = false;
+ time_number_input.disabled = false;
+ time_number_input.value = data.time;
+}
+
function refresh_buttons() {
fetch(api_url + "/control-state")
.then((response) => response.json())
diff --git a/webapp/shutoff/index.html b/webapp/shutoff/index.html
new file mode 100644
index 0000000..cb17d1c
--- /dev/null
+++ b/webapp/shutoff/index.html
@@ -0,0 +1,29 @@
+
+
+
+