Compare commits

..

No commits in common. "3a1003b0af1e8e6fd1415a79bc49b7b129d928e8" and "5949397e6e2b80e0d6ff878879069d3f60c16ee1" have entirely different histories.

11 changed files with 43 additions and 133 deletions

View File

@ -1,68 +0,0 @@
#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>
)";

View File

@ -1,6 +0,0 @@
#ifndef INDEX_HTML_H
#define INDEX_HTML_H
extern const char *INDEX_HTML;
#endif

1
src/patrix/Node.cpp Normal file
View File

@ -0,0 +1 @@
#include "Node.h"

View File

@ -17,17 +17,17 @@ public:
}
}
void send(const bool mqtt, const bool websocket) {
void send() {
JsonDocument json;
toJson(json.to<JsonObject>());
mqttPublish(String(WiFiClass::getHostname()) + "/json", json, mqtt, websocket);
mqttPublish(String(WiFi.getHostname()) + "/json", json);
}
void loop() {
loopBeforeSensors();
const auto changed = loopSensors();
if (changed) {
send(true, true);
send();
}
}

View File

@ -5,14 +5,44 @@
#include "log.h"
#include "main.h"
#include "system.h"
#include "INDEX_HTML.h"
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
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>");
}
void httpIndex(AsyncWebServerRequest *request) {
request->send(200, "text/html", INDEX_HTML);
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);
}
void httpReboot(AsyncWebServerRequest *request) {
@ -26,42 +56,7 @@ 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);
}

View File

@ -3,8 +3,4 @@
void httpSetup();
void httpLoop();
void httpPublish(char *payload);
#endif

View File

@ -23,13 +23,12 @@ void setup() {
delay(500);
Serial.print("Startup\n");
bootDelay();
node.setup();
httpSetup();
node.setup();
}
void loop() {
wifiLoop();
mqttLoop();
node.loop();
httpLoop();
}

View File

@ -3,7 +3,6 @@
#include <PubSubClient.h>
#include <WiFi.h>
#include "http.h"
#include "log.h"
#include "wifi.h"
@ -52,14 +51,8 @@ bool mqttPublish(const String& topic, const double value, const bool retained) {
}
bool mqttPublish(const String& topic, const JsonDocument& json, const bool mqttSend, const bool websocketSend) {
bool mqttPublish(const String& topic, const JsonDocument& json) {
char buffer[512];
serializeJson(json, buffer);
if (websocketSend) {
httpPublish(buffer);
}
if (mqttSend) {
return mqtt.publish(topic.c_str(), buffer);
}
return true;
return mqtt.publish(topic.c_str(), buffer);
}

View File

@ -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 mqtt, bool websocketSend);
bool mqttPublish(const String& topic, const JsonDocument& json);
#endif

View File

@ -34,7 +34,7 @@ public:
void send() {
JsonDocument json;
toJson(json.to<JsonObject>());
mqttPublish(String(name) + "/json", json, true, false);
mqttPublish(String(name) + "/json", json);
}
void toJson(const JsonObject& json) {

View File

@ -59,7 +59,7 @@ public:
JsonDocument json;
toJson(json.to<JsonObject>());
mqttPublish(String(parent) + "/" + name + "/json", json, true, false);
mqttPublish(String(parent) + "/" + name + "/json", json);
markSent();
}