tesla-charge-controller/webapp/index.html
2024-01-19 10:28:00 +11:00

184 lines
4.7 KiB
HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Tesla Charge Control</title>
<link id="favicon" rel="icon"
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>⏳</text></svg>">
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
background-color: #666;
}
.container {
max-width: 40em;
margin: auto;
padding: 0.5em 2em;
border-radius: 10px;
background-color: white;
}
.loading,
.loading * {
cursor: progress;
}
.selector {
padding: 1em;
background-color: gray;
width: max-content;
border: 0.2em;
border-radius: 6px;
}
label {
/* display: block; */
padding: 0.5em 1em;
margin: 0.5em;
font-weight: bold;
transition: all .2s 0s ease;
border-radius: 4px;
text-align: center;
}
input[type=radio] {
display: none;
}
input[type=radio]:checked+label {
background-color: white;
}
</style>
<script type="text/javascript">
const api_url = window.location.protocol + "//" + window.location.hostname + ":" + window.location.port;
refresh_interval = register();
refresh();
document.addEventListener("visibilitychange", () => {
if (document.hidden) {
clearInterval(refresh_interval);
} else {
refresh();
refresh_interval = register();
}
});
function register() {
return setInterval(refresh, 5000);
}
function flash() {
fetch(api_url + "/flash", { method: "POST" });
}
var is_automatic_control;
const delay = () => {
return new Promise(resolve => setTimeout(resolve, 1000));
};
function disable_automatic_control() {
if (is_automatic_control) {
document.getElementById('control-disabled').checked = true;
document.body.classList.add("loading");
fetch(api_url + "/disable-control", { method: "POST" })
.then(async (response) => {
let delayres = await delay();
refresh_buttons();
});
}
}
function enable_automatic_control() {
if (!is_automatic_control) {
document.getElementById('control-enabled').checked = true;
document.body.classList.add("loading");
fetch(api_url + "/enable-control", { method: "POST" })
.then(async (response) => {
let delayres = await delay();
refresh_buttons();
});
}
}
function update_control_buttons(data) {
document.body.classList.remove("loading");
is_automatic_control = data.control_enable;
if (data.control_enable) {
document.getElementById('control-enabled').checked = true;
} else {
document.getElementById('control-disabled').checked = true;
}
}
function refresh_buttons() {
fetch(api_url + "/control-state")
.then((response) => response.json())
.then((json) => update_control_buttons(json));
}
function refresh() {
let favicon = document.getElementById("favicon");
favicon.setAttribute("href", "data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>" + "⏳" + "</text></svg>");
refresh_buttons();
fetch(api_url + "/car-state")
.then((response) => response.json())
.then((json) => update_state(json));
}
function update_state(state) {
let favicon = document.getElementById("favicon");
favicon.setAttribute("href", "data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>" + get_emoji(state.charge_state) + "</text></svg>");
var info_div = document.getElementById("info");
while (info_div.childElementCount > 0) { info_div.removeChild(info_div.firstChild) }
el = document.createElement('p');
state_json = document.createElement('pre');
state_json.appendChild(document.createTextNode(JSON.stringify(state, null, '\t')));
el.appendChild(state_json);
info_div.appendChild(el);
}
function get_emoji(charge_state) {
if (charge_state.charge_rate > 0) {
return "🔌";
} else if (charge_state.battery_level < 60) {
return "🪫"
} else return "🔋";
}
</script>
</head>
<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()">
<label for="control-enabled">enabled</label>
<input id="control-disabled" type="radio" name="control" onclick="disable_automatic_control()">
<label for="control-disabled">disabled</label>
</div>
<br>
<button onclick="flash()">flash</button>
<div id="info"></div>
<div>
<h3>
<a href="/grafana">Grafana</a>
</h3>
</div>
</div>
</body>
</html>