websocket + INDEX_HTML
This commit is contained in:
parent
5949397e6e
commit
33f7a3cfda
29
src/patrix/INDEX_HTML.cpp
Normal file
29
src/patrix/INDEX_HTML.cpp
Normal file
@ -0,0 +1,29 @@
|
||||
#include "INDEX_HTML.h"
|
||||
|
||||
const char* INDEX_HTML = R"(<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<title>TEST</title>
|
||||
<script>
|
||||
function connect() {
|
||||
console.log("connecting");
|
||||
const socket = new WebSocket('ws://10.0.0.119/ws');
|
||||
socket.timeout = 1;
|
||||
socket.addEventListener('open', _ => console.log('connected'));
|
||||
socket.addEventListener('message', event => {
|
||||
document.getElementById("content").innerText = JSON.stringify(JSON.parse(event.data), null, 2);
|
||||
});
|
||||
socket.addEventListener('close', _ => {
|
||||
console.log('disconnected');
|
||||
connect();
|
||||
});
|
||||
}
|
||||
|
||||
connect();
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<pre id="content"></pre>
|
||||
</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;
|
||||
toJson(json.to<JsonObject>());
|
||||
mqttPublish(String(WiFi.getHostname()) + "/json", json);
|
||||
mqttPublish(String(WiFiClass::getHostname()) + "/json", json, mqtt, websocket);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
loopBeforeSensors();
|
||||
const auto changed = loopSensors();
|
||||
if (changed) {
|
||||
send();
|
||||
send(true, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,44 +5,14 @@
|
||||
#include "log.h"
|
||||
#include "main.h"
|
||||
#include "system.h"
|
||||
#include "INDEX_HTML.h"
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
void printValues(AsyncResponseStream *const response, Sensor *sensor) {
|
||||
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>");
|
||||
}
|
||||
AsyncWebSocket ws("/ws");
|
||||
|
||||
void httpIndex(AsyncWebServerRequest *request) {
|
||||
const auto response = request->beginResponseStream("text/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);
|
||||
request->send(200, "text/html", INDEX_HTML);
|
||||
}
|
||||
|
||||
void httpReboot(AsyncWebServerRequest *request) {
|
||||
@ -56,7 +26,42 @@ void httpReboot(AsyncWebServerRequest *request) {
|
||||
}
|
||||
|
||||
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("/reboot", HTTP_GET, httpReboot);
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void httpLoop() {
|
||||
ws.cleanupClients();
|
||||
}
|
||||
|
||||
void httpPublish(char *payload) {
|
||||
ws.textAll(payload);
|
||||
}
|
||||
|
||||
@ -3,4 +3,8 @@
|
||||
|
||||
void httpSetup();
|
||||
|
||||
void httpLoop();
|
||||
|
||||
void httpPublish(char *payload);
|
||||
|
||||
#endif
|
||||
|
||||
@ -23,12 +23,13 @@ void setup() {
|
||||
delay(500);
|
||||
Serial.print("Startup\n");
|
||||
bootDelay();
|
||||
httpSetup();
|
||||
node.setup();
|
||||
httpSetup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
wifiLoop();
|
||||
mqttLoop();
|
||||
node.loop();
|
||||
httpLoop();
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#include "http.h"
|
||||
#include "log.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];
|
||||
serializeJson(json, 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 JsonDocument& json);
|
||||
bool mqttPublish(const String& topic, const JsonDocument& json, bool mqtt, bool websocketSend);
|
||||
|
||||
#endif
|
||||
|
||||
@ -34,7 +34,7 @@ public:
|
||||
void send() {
|
||||
JsonDocument json;
|
||||
toJson(json.to<JsonObject>());
|
||||
mqttPublish(String(name) + "/json", json);
|
||||
mqttPublish(String(name) + "/json", json, true, false);
|
||||
}
|
||||
|
||||
void toJson(const JsonObject& json) {
|
||||
|
||||
@ -59,7 +59,7 @@ public:
|
||||
|
||||
JsonDocument json;
|
||||
toJson(json.to<JsonObject>());
|
||||
mqttPublish(String(parent) + "/" + name + "/json", json);
|
||||
mqttPublish(String(parent) + "/" + name + "/json", json, true, false);
|
||||
|
||||
markSent();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user