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]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.9" version = "1.0.10"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",

View file

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

View file

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

View file

@ -25,6 +25,11 @@
cursor: progress; cursor: progress;
} }
.disabled,
.disabled * {
cursor: not-allowed;
}
.selector { .selector {
padding: 1em; padding: 1em;
background-color: gray; background-color: gray;
@ -46,17 +51,39 @@
input[type=radio] { input[type=radio] {
display: none; display: none;
} }
input[type=radio]:checked+label { input[type=radio]:checked+label {
background-color: white; background-color: white;
}
input[type=radio]:checked:disabled+label {
background-color: #ddd;
}
input[type=radio]:disabled+label {
color: #666;
} }
</style> </style>
<script type="text/javascript"> <script type="text/javascript">
const api_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port; 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_interval = register();
refresh(); refresh();
@ -116,6 +143,26 @@
} else { } else {
document.getElementById('control-disabled').checked = true; 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() { function refresh_buttons() {
@ -152,7 +199,10 @@
} }
function get_emoji(charge_state) { function get_emoji(charge_state) {
if (charge_state.charge_rate > 0) { if (charge_state == null) {
return "🤨";
}
else if (charge_state.charge_rate > 0) {
return "🔌"; return "🔌";
} else if (charge_state.battery_level < 60) { } else if (charge_state.battery_level < 60) {
return "🪫" return "🪫"
@ -164,10 +214,10 @@
<body> <body>
<div class="container"> <div class="container">
<h3>Automatic control:</h3> <h3>Automatic control:</h3>
<div class="selector" id="control-selector"> <div class="selector disabled" id="control-selector">
<input id="control-enabled" type="radio" name="control" onclick="enable_automatic_control()"> <input id="control-enabled" type="radio" name="control" onclick="enable_automatic_control()" disabled>
<label for="control-enabled">enabled</label> <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> <label for="control-disabled">disabled</label>
</div> </div>
<br> <br>