adapted index.html to reflect websocket changes

This commit is contained in:
Patrick Haßel 2025-01-16 16:29:19 +01:00
parent fc45bf95af
commit 0d63c7cc4b
3 changed files with 64 additions and 41 deletions

View File

@ -8,6 +8,20 @@ const char *INDEX_HTML = R"(<!DOCTYPE html>
<style> <style>
body { body {
font-size: 4vw; font-size: 4vw;
font-family: monospace;
}
table {
width: 100%;
}
th {
text-align: left;
font-weight: inherit;
}
td {
text-align: right;
} }
@media (min-width: 1200px) { @media (min-width: 1200px) {
@ -18,50 +32,58 @@ const char *INDEX_HTML = R"(<!DOCTYPE html>
</style> </style>
</head> </head>
<body> <body>
<pre id="content"></pre> <table id="content"></table>
<pre id="time"></pre> <pre id="time"></pre>
<script> <script>
const time = document.getElementById("time"); const map = new Map();
const content = document.getElementById("content"); const time = document.getElementById("time");
const content = document.getElementById("content");
let last = null; let last = null;
function updateTime() { function updateTime() {
if (last === null) { if (last === null) {
time.innerText = "Noch keine Daten"; time.innerText = "Noch keine Daten";
return; return;
}
const ageSeconds = Math.floor((Date.now() - last) / 1000);
if (ageSeconds === 0) {
time.innerText = "Gerade eben";
} else {
time.innerText = "Vor " + ageSeconds + " Sekunde" + (ageSeconds === 1 ? "" : "n");
}
} }
const ageSeconds = Math.floor((Date.now() - last) / 1000);
function connect() { if (ageSeconds === 0) {
console.log("connecting websocket..."); time.innerText = "Gerade eben";
const host = location.host || "10.0.0.119"; } else {
const port = location.port || "80"; time.innerText = "Vor " + ageSeconds + " Sekunde" + (ageSeconds === 1 ? "" : "n");
const socket = new WebSocket(`ws://${host}:${port}/ws`);
socket.timeout = 1;
socket.addEventListener('open', _ => console.log('websocket connected'));
socket.addEventListener('message', event => {
last = Date.now();
updateTime();
const parsed = JSON.parse(event.data);
console.log("websocket received", parsed);
content.innerText = JSON.stringify(parsed, null, 2);
});
socket.addEventListener('close', _ => {
console.log('websocket disconnected');
connect();
});
} }
}
updateTime(); function connect() {
connect(); console.log("connecting websocket...");
setInterval(updateTime, 1000); const host = location.host || "10.0.0.119";
const port = location.port || "80";
const socket = new WebSocket(`ws://${host}:${port}/ws`);
socket.timeout = 1;
socket.addEventListener('open', _ => console.log('websocket connected'));
socket.addEventListener('message', event => {
last = Date.now();
const parsed = JSON.parse(event.data);
map.set(parsed.name, parsed);
const innerHTML = `<th>${parsed.name}</th><td>${parsed.value?.toFixed(1) || '-'}</td>`;
let tr = document.getElementById(parsed.name);
if (!tr) {
tr = document.createElement("tr");
tr.setAttribute("id", parsed.name);
content.appendChild(tr);
}
tr.innerHTML = innerHTML;
updateTime();
});
socket.addEventListener('close', _ => {
console.log('websocket disconnected');
connect();
});
}
updateTime();
connect();
setInterval(updateTime, 1000);
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,9 +1,9 @@
#ifndef DALLAS_H #ifndef DALLAS_H
#define DALLAS_H #define DALLAS_H
#include "../log.h"
#include "OneWire.h"
#include "DallasTemperature.h" #include "DallasTemperature.h"
#include "OneWire.h"
#include "../log.h"
#define DALLAS_INTERVAL_MILLISECONDS 2000 #define DALLAS_INTERVAL_MILLISECONDS 2000
@ -42,7 +42,7 @@ public:
const auto count = sensors.getDeviceCount(); const auto count = sensors.getDeviceCount();
if (count != 0) { if (count != 0) {
uint64_t address; uint64_t address;
for (int index = 0; index < count; ++index) { for (auto index = 0; index < count; ++index) {
sensors.getAddress(reinterpret_cast<uint8_t *>(&address), index); sensors.getAddress(reinterpret_cast<uint8_t *>(&address), index);
info("Dallas %d/%d 0x%016llX = %5.1f^C", index + 1, count, address, sensors.getTempC(reinterpret_cast<uint8_t *>(&address))); info("Dallas %d/%d 0x%016llX = %5.1f^C", index + 1, count, address, sensors.getTempC(reinterpret_cast<uint8_t *>(&address)));
} }

View File

@ -58,8 +58,9 @@ public:
const auto dueToTime = sentInterval != 0 && sentInterval != now / overdueSeconds; const auto dueToTime = sentInterval != 0 && sentInterval != now / overdueSeconds;
const auto changed = dueToNAN || dueToThreshold || dueToTime; const auto changed = dueToNAN || dueToThreshold || dueToTime;
if (changed) { if (changed) {
info("%s = %f", name.c_str(), currentValue);
mqttPublish(name + "/retain", String(currentValue), RETAIN); mqttPublish(name + "/retain", String(currentValue), RETAIN);
websocketSendAll(toJson(false)); websocketSendAll(toJson(true));
sentValue = currentValue; sentValue = currentValue;
sentInterval = now / overdueSeconds; sentInterval = now / overdueSeconds;
} }