diff --git a/src/patrix/INDEX_HTML.cpp b/src/patrix/INDEX_HTML.cpp
new file mode 100644
index 0000000..7cba36f
--- /dev/null
+++ b/src/patrix/INDEX_HTML.cpp
@@ -0,0 +1,29 @@
+#include "INDEX_HTML.h"
+
+const char* INDEX_HTML = R"(
+
+
+ TEST
+
+
+
+
+
+
+)";
\ No newline at end of file
diff --git a/src/patrix/INDEX_HTML.h b/src/patrix/INDEX_HTML.h
new file mode 100644
index 0000000..6f974b3
--- /dev/null
+++ b/src/patrix/INDEX_HTML.h
@@ -0,0 +1,6 @@
+#ifndef INDEX_HTML_H
+#define INDEX_HTML_H
+
+extern const char *INDEX_HTML;
+
+#endif
diff --git a/src/patrix/Node.cpp b/src/patrix/Node.cpp
deleted file mode 100644
index 0c2b14d..0000000
--- a/src/patrix/Node.cpp
+++ /dev/null
@@ -1 +0,0 @@
-#include "Node.h"
diff --git a/src/patrix/Node.h b/src/patrix/Node.h
index 079f854..3bf3f9e 100644
--- a/src/patrix/Node.h
+++ b/src/patrix/Node.h
@@ -17,17 +17,17 @@ public:
}
}
- void send() {
+ void send(const bool mqtt, const bool websocket) {
JsonDocument json;
toJson(json.to());
- 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);
}
}
diff --git a/src/patrix/http.cpp b/src/patrix/http.cpp
index 79ab274..9bcf311 100644
--- a/src/patrix/http.cpp
+++ b/src/patrix/http.cpp
@@ -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("");
- while ((value = sensor->getValue(valueIndex++)) != nullptr) {
- response->println("- ");
- response->printf("
%s = %.1f
\n", value->getName(), value->getCurrentValue());
- response->println(" ");
- }
- response->println("
");
-}
-
-void printSensors(AsyncResponseStream *const response) {
- auto sensorIndex = 0;
- Sensor *sensor;
- response->println("");
- while ((sensor = node.getSensor(sensorIndex++)) != nullptr) {
- response->println("- ");
- response->printf("
%s
\n", sensor->getName());
- printValues(response, sensor);
- response->println(" ");
- }
- response->println("
");
-}
+AsyncWebSocket ws("/ws");
void httpIndex(AsyncWebServerRequest *request) {
- const auto response = request->beginResponseStream("text/html");
- response->println("");
- response->println("TEST");
- response->println("");
- response->println("");
- response->println("Neustart");
- printSensors(response);
- response->println("");
- 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);
+}
diff --git a/src/patrix/http.h b/src/patrix/http.h
index abcba1f..e4c0a70 100644
--- a/src/patrix/http.h
+++ b/src/patrix/http.h
@@ -3,4 +3,8 @@
void httpSetup();
+void httpLoop();
+
+void httpPublish(char *payload);
+
#endif
diff --git a/src/patrix/main.cpp b/src/patrix/main.cpp
index cb0729c..b1d4539 100644
--- a/src/patrix/main.cpp
+++ b/src/patrix/main.cpp
@@ -23,12 +23,13 @@ void setup() {
delay(500);
Serial.print("Startup\n");
bootDelay();
- httpSetup();
node.setup();
+ httpSetup();
}
void loop() {
wifiLoop();
mqttLoop();
node.loop();
+ httpLoop();
}
diff --git a/src/patrix/mqtt.cpp b/src/patrix/mqtt.cpp
index ed11b3f..aba8e98 100644
--- a/src/patrix/mqtt.cpp
+++ b/src/patrix/mqtt.cpp
@@ -3,6 +3,7 @@
#include
#include
+#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);
- return mqtt.publish(topic.c_str(), buffer);
+ if (websocketSend) {
+ httpPublish(buffer);
+ }
+ if (mqttSend) {
+ return mqtt.publish(topic.c_str(), buffer);
+ }
+ return true;
}
diff --git a/src/patrix/mqtt.h b/src/patrix/mqtt.h
index 5335539..a7f52af 100644
--- a/src/patrix/mqtt.h
+++ b/src/patrix/mqtt.h
@@ -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
diff --git a/src/patrix/sensor/Sensor.h b/src/patrix/sensor/Sensor.h
index 53b16ad..f18563c 100644
--- a/src/patrix/sensor/Sensor.h
+++ b/src/patrix/sensor/Sensor.h
@@ -34,7 +34,7 @@ public:
void send() {
JsonDocument json;
toJson(json.to());
- mqttPublish(String(name) + "/json", json);
+ mqttPublish(String(name) + "/json", json, true, false);
}
void toJson(const JsonObject& json) {
diff --git a/src/patrix/sensor/Value.h b/src/patrix/sensor/Value.h
index d31cd46..eb9a6d1 100644
--- a/src/patrix/sensor/Value.h
+++ b/src/patrix/sensor/Value.h
@@ -59,7 +59,7 @@ public:
JsonDocument json;
toJson(json.to());
- mqttPublish(String(parent) + "/" + name + "/json", json);
+ mqttPublish(String(parent) + "/" + name + "/json", json, true, false);
markSent();
}