disable automatic control toggle when car isn't charging
All checks were successful
Build .deb on release / Build-Deb (push) Successful in 1m52s

This commit is contained in:
Alex Janka 2024-01-21 08:36:33 +11:00
parent d30201e5c7
commit 8cb97059d7
4 changed files with 72 additions and 9 deletions

2
Cargo.lock generated
View file

@ -2567,7 +2567,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
version = "1.0.9"
version = "1.0.10"
dependencies = [
"async-channel",
"chrono",

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-charge-controller"
version = "1.0.9"
version = "1.0.10"
edition = "2021"
license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data"

View file

@ -7,6 +7,7 @@ use rocket::{
serde::json::Json,
Request, Response, State,
};
use serde::{Deserialize, Serialize};
use crate::{
api_interface::InterfaceRequest,
@ -82,9 +83,21 @@ async fn car_state(state: &State<ServerState>) -> Result<Json<CarState>, ServerE
Ok(Json(*state.car_state.read()?))
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
struct ControlState {
control_enable: bool,
is_charging_at_home: bool,
}
#[get("/control-state")]
async fn control_state(state: &State<ServerState>) -> Result<Json<TcrcState>, ServerError> {
Ok(Json(*state.tcrc_state.read()?))
async fn control_state(state: &State<ServerState>) -> Result<Json<ControlState>, ServerError> {
let control_enable = state.tcrc_state.read()?.control_enable;
let is_charging_at_home = state.car_state.read()?.is_charging_at_home();
Ok(Json(ControlState {
control_enable,
is_charging_at_home,
}))
}
#[post("/flash")]

View file

@ -25,6 +25,11 @@
cursor: progress;
}
.disabled,
.disabled * {
cursor: not-allowed;
}
.selector {
padding: 1em;
background-color: gray;
@ -46,17 +51,39 @@
input[type=radio] {
display: none;
}
input[type=radio]:checked+label {
background-color: white;
}
input[type=radio]:checked:disabled+label {
background-color: #ddd;
}
input[type=radio]:disabled+label {
color: #666;
}
</style>
<script type="text/javascript">
const api_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port;
Object.prototype.disable = function () {
var that = this;
for (var i = 0, len = that.length; i < len; i++) {
that[i].disabled = true;
}
return that;
};
Object.prototype.enable = function () {
var that = this;
for (var i = 0, len = that.length; i < len; i++) {
that[i].disabled = false;
}
return that;
};
refresh_interval = register();
refresh();
@ -116,6 +143,26 @@
} else {
document.getElementById('control-disabled').checked = true;
}
var control_selector = document.getElementById("control-selector");
if (data.is_charging_at_home) {
if (control_selector.classList.contains('disabled')) {
control_selector.classList.remove('disabled');
}
document.getElementsByName('control').enable();
}
else {
if (!control_selector.classList.contains('disabled')) {
control_selector.classList.add('disabled');
}
document.getElementsByName('control').disable();
}
// for (let i = 0; i < elements.length; i++) {
// console.log(elements[i]);
// elements[i].disabled = data.is_charging_at_home;
// }
}
function refresh_buttons() {
@ -152,7 +199,10 @@
}
function get_emoji(charge_state) {
if (charge_state.charge_rate > 0) {
if (charge_state == null) {
return "🤨";
}
else if (charge_state.charge_rate > 0) {
return "🔌";
} else if (charge_state.battery_level < 60) {
return "🪫"
@ -164,10 +214,10 @@
<body>
<div class="container">
<h3>Automatic control:</h3>
<div class="selector" id="control-selector">
<input id="control-enabled" type="radio" name="control" onclick="enable_automatic_control()">
<div class="selector disabled" id="control-selector">
<input id="control-enabled" type="radio" name="control" onclick="enable_automatic_control()" disabled>
<label for="control-enabled">enabled</label>
<input id="control-disabled" type="radio" name="control" onclick="disable_automatic_control()">
<input id="control-disabled" type="radio" name="control" onclick="disable_automatic_control()" disabled>
<label for="control-disabled">disabled</label>
</div>
<br>