Compare commits
2 Commits
5949397e6e
...
3a1003b0af
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a1003b0af | |||
| 33f7a3cfda |
68
src/patrix/INDEX_HTML.cpp
Normal file
68
src/patrix/INDEX_HTML.cpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include "INDEX_HTML.h"
|
||||||
|
|
||||||
|
const char *INDEX_HTML = R"(<!DOCTYPE html>
|
||||||
|
<html lang="de">
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, user-scalable=no">
|
||||||
|
<title>TEST</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-size: 4vw;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (min-width: 1200px) {
|
||||||
|
body {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<pre id="content"></pre>
|
||||||
|
<pre id="time"></pre>
|
||||||
|
<script>
|
||||||
|
const time = document.getElementById("time");
|
||||||
|
const content = document.getElementById("content");
|
||||||
|
|
||||||
|
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 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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
updateTime();
|
||||||
|
connect();
|
||||||
|
setInterval(updateTime, 1000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)";
|
||||||
6
src/patrix/INDEX_HTML.h
Normal file
6
src/patrix/INDEX_HTML.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#ifndef INDEX_HTML_H
|
||||||
|
#define INDEX_HTML_H
|
||||||
|
|
||||||
|
extern const char *INDEX_HTML;
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1 +0,0 @@
|
|||||||
#include "Node.h"
|
|
||||||
@ -17,17 +17,17 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void send() {
|
void send(const bool mqtt, const bool websocket) {
|
||||||
JsonDocument json;
|
JsonDocument json;
|
||||||
toJson(json.to<JsonObject>());
|
toJson(json.to<JsonObject>());
|
||||||
mqttPublish(String(WiFi.getHostname()) + "/json", json);
|
mqttPublish(String(WiFiClass::getHostname()) + "/json", json, mqtt, websocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
loopBeforeSensors();
|
loopBeforeSensors();
|
||||||
const auto changed = loopSensors();
|
const auto changed = loopSensors();
|
||||||
if (changed) {
|
if (changed) {
|
||||||
send();
|
send(true, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,44 +5,14 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
#include "INDEX_HTML.h"
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
void printValues(AsyncResponseStream *const response, Sensor *sensor) {
|
AsyncWebSocket ws("/ws");
|
||||||
auto valueIndex = 0;
|
|
||||||
Value *value;
|
|
||||||
response->println("<ul>");
|
|
||||||
while ((value = sensor->getValue(valueIndex++)) != nullptr) {
|
|
||||||
response->println("<li>");
|
|
||||||
response->printf("<h2>%s = %.1f</h2>\n", value->getName(), value->getCurrentValue());
|
|
||||||
response->println("</li>");
|
|
||||||
}
|
|
||||||
response->println("</ul>");
|
|
||||||
}
|
|
||||||
|
|
||||||
void printSensors(AsyncResponseStream *const response) {
|
|
||||||
auto sensorIndex = 0;
|
|
||||||
Sensor *sensor;
|
|
||||||
response->println("<ul>");
|
|
||||||
while ((sensor = node.getSensor(sensorIndex++)) != nullptr) {
|
|
||||||
response->println("<li>");
|
|
||||||
response->printf("<h3>%s</h3>\n", sensor->getName());
|
|
||||||
printValues(response, sensor);
|
|
||||||
response->println("</li>");
|
|
||||||
}
|
|
||||||
response->println("</ul>");
|
|
||||||
}
|
|
||||||
|
|
||||||
void httpIndex(AsyncWebServerRequest *request) {
|
void httpIndex(AsyncWebServerRequest *request) {
|
||||||
const auto response = request->beginResponseStream("text/html");
|
request->send(200, "text/html", INDEX_HTML);
|
||||||
response->println("<!DOCTYPE html><html><head>");
|
|
||||||
response->println("<title>TEST</title>");
|
|
||||||
response->println("</head>");
|
|
||||||
response->println("<body>");
|
|
||||||
response->println("<a href='reboot'>Neustart</a>");
|
|
||||||
printSensors(response);
|
|
||||||
response->println("</body></html>");
|
|
||||||
request->send(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void httpReboot(AsyncWebServerRequest *request) {
|
void httpReboot(AsyncWebServerRequest *request) {
|
||||||
@ -56,7 +26,42 @@ void httpReboot(AsyncWebServerRequest *request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void httpSetup() {
|
void httpSetup() {
|
||||||
|
ws.onEvent([](AsyncWebSocket *socket, AsyncWebSocketClient *client, AwsEventType type, void *arg, unsigned char *message, unsigned length) {
|
||||||
|
const char *t;
|
||||||
|
switch (type) {
|
||||||
|
case WS_EVT_CONNECT:
|
||||||
|
t = "CONNECT";
|
||||||
|
node.send(false, true); // TODO this currently sends to ALL
|
||||||
|
break;
|
||||||
|
case WS_EVT_DISCONNECT:
|
||||||
|
t = "DISCONNECT";
|
||||||
|
break;
|
||||||
|
case WS_EVT_PONG:
|
||||||
|
t = "PONG";
|
||||||
|
break;
|
||||||
|
case WS_EVT_ERROR:
|
||||||
|
t = "ERROR";
|
||||||
|
break;
|
||||||
|
case WS_EVT_DATA:
|
||||||
|
t = "DATA";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
t = "[???]";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
debug("%s: %s (%d bytes)", client->remoteIP().toString().c_str(), t, length);
|
||||||
|
});
|
||||||
|
server.addHandler(&ws);
|
||||||
|
|
||||||
server.on("/", HTTP_GET, httpIndex);
|
server.on("/", HTTP_GET, httpIndex);
|
||||||
server.on("/reboot", HTTP_GET, httpReboot);
|
server.on("/reboot", HTTP_GET, httpReboot);
|
||||||
server.begin();
|
server.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void httpLoop() {
|
||||||
|
ws.cleanupClients();
|
||||||
|
}
|
||||||
|
|
||||||
|
void httpPublish(char *payload) {
|
||||||
|
ws.textAll(payload);
|
||||||
|
}
|
||||||
|
|||||||
@ -3,4 +3,8 @@
|
|||||||
|
|
||||||
void httpSetup();
|
void httpSetup();
|
||||||
|
|
||||||
|
void httpLoop();
|
||||||
|
|
||||||
|
void httpPublish(char *payload);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -23,12 +23,13 @@ void setup() {
|
|||||||
delay(500);
|
delay(500);
|
||||||
Serial.print("Startup\n");
|
Serial.print("Startup\n");
|
||||||
bootDelay();
|
bootDelay();
|
||||||
httpSetup();
|
|
||||||
node.setup();
|
node.setup();
|
||||||
|
httpSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
wifiLoop();
|
wifiLoop();
|
||||||
mqttLoop();
|
mqttLoop();
|
||||||
node.loop();
|
node.loop();
|
||||||
|
httpLoop();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,7 @@
|
|||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
#include "http.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
|
|
||||||
@ -51,8 +52,14 @@ bool mqttPublish(const String& topic, const double value, const bool retained) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool mqttPublish(const String& topic, const JsonDocument& json) {
|
bool mqttPublish(const String& topic, const JsonDocument& json, const bool mqttSend, const bool websocketSend) {
|
||||||
char buffer[512];
|
char buffer[512];
|
||||||
serializeJson(json, buffer);
|
serializeJson(json, buffer);
|
||||||
return mqtt.publish(topic.c_str(), buffer);
|
if (websocketSend) {
|
||||||
|
httpPublish(buffer);
|
||||||
|
}
|
||||||
|
if (mqttSend) {
|
||||||
|
return mqtt.publish(topic.c_str(), buffer);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,6 @@ void mqttLoop();
|
|||||||
|
|
||||||
bool mqttPublish(const String& topic, const double value, bool retained);
|
bool mqttPublish(const String& topic, const double value, bool retained);
|
||||||
|
|
||||||
bool mqttPublish(const String& topic, const JsonDocument& json);
|
bool mqttPublish(const String& topic, const JsonDocument& json, bool mqtt, bool websocketSend);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public:
|
|||||||
void send() {
|
void send() {
|
||||||
JsonDocument json;
|
JsonDocument json;
|
||||||
toJson(json.to<JsonObject>());
|
toJson(json.to<JsonObject>());
|
||||||
mqttPublish(String(name) + "/json", json);
|
mqttPublish(String(name) + "/json", json, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void toJson(const JsonObject& json) {
|
void toJson(const JsonObject& json) {
|
||||||
|
|||||||
@ -59,7 +59,7 @@ public:
|
|||||||
|
|
||||||
JsonDocument json;
|
JsonDocument json;
|
||||||
toJson(json.to<JsonObject>());
|
toJson(json.to<JsonObject>());
|
||||||
mqttPublish(String(parent) + "/" + name + "/json", json);
|
mqttPublish(String(parent) + "/" + name + "/json", json, true, false);
|
||||||
|
|
||||||
markSent();
|
markSent();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user