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>
body {
font-size: 4vw;
font-family: monospace;
}
table {
width: 100%;
}
th {
text-align: left;
font-weight: inherit;
}
td {
text-align: right;
}
@media (min-width: 1200px) {
@ -18,50 +32,58 @@ const char *INDEX_HTML = R"(<!DOCTYPE html>
</style>
</head>
<body>
<pre id="content"></pre>
<table id="content"></table>
<pre id="time"></pre>
<script>
const time = document.getElementById("time");
const content = document.getElementById("content");
const map = new Map();
const time = document.getElementById("time");
const content = document.getElementById("content");
let last = null;
let last = null;
function updateTime() {
if (last === null) {
time.innerText = "Noch keine Daten";
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");
}
function updateTime() {
if (last === null) {
time.innerText = "Noch keine Daten";
return;
}
function connect() {
console.log("connecting websocket...");
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();
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();
});
const ageSeconds = Math.floor((Date.now() - last) / 1000);
if (ageSeconds === 0) {
time.innerText = "Gerade eben";
} else {
time.innerText = "Vor " + ageSeconds + " Sekunde" + (ageSeconds === 1 ? "" : "n");
}
}
updateTime();
connect();
setInterval(updateTime, 1000);
function connect() {
console.log("connecting websocket...");
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>
</body>
</html>

View File

@ -1,9 +1,9 @@
#ifndef DALLAS_H
#define DALLAS_H
#include "../log.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "OneWire.h"
#include "../log.h"
#define DALLAS_INTERVAL_MILLISECONDS 2000
@ -42,7 +42,7 @@ public:
const auto count = sensors.getDeviceCount();
if (count != 0) {
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);
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 changed = dueToNAN || dueToThreshold || dueToTime;
if (changed) {
info("%s = %f", name.c_str(), currentValue);
mqttPublish(name + "/retain", String(currentValue), RETAIN);
websocketSendAll(toJson(false));
websocketSendAll(toJson(true));
sentValue = currentValue;
sentInterval = now / overdueSeconds;
}