RFID message ins backend geht, websocket fürs frontend

This commit is contained in:
Carsten Graf
2025-06-08 00:38:00 +02:00
parent e5c4094cfa
commit c35e857904
11 changed files with 311 additions and 98 deletions

View File

@@ -11,6 +11,8 @@
#include "helper.h"
#include <debug.h>
#include <map>
#include <databasebackend.h>
#include <webserverrouter.h>
struct TimestampData {
uint64_t lastMessageTimestamp; // Timestamp from the device
@@ -35,52 +37,6 @@ typedef struct {
PicoMQTT::Server mqtt;
void processHeartbeat(const char* topic, const char* payload) {
String macAddress = String(topic).substring(15);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.printf("JSON parsing failed for MAC %s: %s\n", macAddress.c_str(), error.c_str());
return;
}
uint64_t messageTimestamp = doc["timestamp"] | 0;
uint64_t currentLocalTime = getCurrentTimestampMs();
// Update timestamps for current device
if (deviceTimestamps.count(macAddress) > 0) {
TimestampData& data = deviceTimestamps[macAddress];
uint64_t messageDiff = messageTimestamp - data.lastMessageTimestamp;
uint64_t localDiff = currentLocalTime - data.lastLocalTimestamp;
data.drift = localDiff - messageDiff;
}
// Calculate drift relative to all other devices
Serial.printf("\nDrift analysis for device %s:\n", macAddress.c_str());
Serial.println("----------------------------------------");
for (const auto& device : deviceTimestamps) {
if (device.first != macAddress) { // Skip comparing to self
int64_t timeDiff = messageTimestamp - device.second.lastMessageTimestamp;
int64_t relativeDrift = timeDiff - (currentLocalTime - device.second.lastLocalTimestamp);
Serial.printf("Relative to %s:\n", device.first.c_str());
Serial.printf(" Time difference: %lld ms\n", timeDiff);
Serial.printf(" Relative drift: %lld ms\n", relativeDrift);
Serial.println("----------------------------------------");
}
}
// Update stored timestamps for current device
deviceTimestamps[macAddress] = {
messageTimestamp,
currentLocalTime,
deviceTimestamps[macAddress].drift
};
}
void readButtonJSON(const char * topic, const char * payload) {
if(strcmp(topic, "aquacross/button/press") == 0){
@@ -133,22 +89,99 @@ void readButtonJSON(const char * topic, const char * payload) {
}
}
void readRFIDfromButton(const char * topic, const char * payload) {
// Create a JSON document to hold the button press data
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
const char* mac = doc["buttonmac"] | "unknown";
const char* uid = doc["uid"] | "unknown";
Serial.printf("RFID Read from Button:\n");
Serial.printf(" Button MAC: %s\n", mac);
Serial.printf(" UID: %s\n", uid);
// Convert buttonmac to byte array for comparison
auto macBytes = macStringToBytes(mac);
// Check if the buttonmac matches buttonConfigs.start1.mac
if (memcmp(macBytes.data(), buttonConfigs.start1.mac, 6) == 0) {
// Fetch user data
UserData userData = checkUser(uid);
if (userData.exists) {
// Log user data
Serial.printf("User found for start1: %s %s, Alter: %d\n",
userData.firstname.c_str(),
userData.lastname.c_str(),
userData.alter);
// Create JSON message to send to the frontend
StaticJsonDocument<128> messageDoc;
messageDoc["firstname"] = userData.firstname;
messageDoc["lastname"] = userData.lastname;
messageDoc["lane"] = "start1"; // Add lane information
String message;
serializeJson(messageDoc, message);
// Push the message to the frontend
pushUpdateToFrontend(message);
Serial.printf("Pushed user data for start1 to frontend: %s\n", message.c_str());
} else {
Serial.println("User not found for UID: " + String(uid));
}
}
// Check if the buttonmac matches buttonConfigs.start2.mac
else if (memcmp(macBytes.data(), buttonConfigs.start2.mac, 6) == 0) {
// Fetch user data
UserData userData = checkUser(uid);
if (userData.exists) {
// Log user data
Serial.printf("User found for start2: %s %s, Alter: %d\n",
userData.firstname.c_str(),
userData.lastname.c_str(),
userData.alter);
// Create JSON message to send to the frontend
StaticJsonDocument<128> messageDoc;
messageDoc["firstname"] = userData.firstname;
messageDoc["lastname"] = userData.lastname;
messageDoc["lane"] = "start2"; // Add lane information
String message;
serializeJson(messageDoc, message);
// Push the message to the frontend
pushUpdateToFrontend(message);
Serial.printf("Pushed user data for start2 to frontend: %s\n", message.c_str());
} else {
Serial.println("User not found for UID: " + String(uid));
}
} else {
Serial.println("Button MAC does not match start1.mac or start2.mac");
}
} else {
Serial.println("Failed to parse RFID JSON");
}
}
void setupMqttServer() {
// Set up the MQTT server with the desired port
// Subscribe to a topic pattern and attach a callback
mqtt.subscribe("#", [](const char * topic, const char * payload) {
if (strncmp(topic, "heartbeat/alive/", 15) == 0) {
processHeartbeat(topic, payload);
} else if (strcmp(topic, "aquacross/button/press") == 0) {
//Message received callback
//Serial.printf("Received message on topic '%s': %s\n", topic, payload);
if (strcmp(topic, "aquacross/button/press") == 0) {
readButtonJSON(topic, payload);
} else if (strncmp(topic, "aquacross/button/rfid/", 22) == 0) {
readRFIDfromButton(topic, payload);
// Handle RFID read messages
}
updateStatusLED(3);
});
// Add the button subscription
// Start the MQTT server
mqtt.begin();
@@ -168,6 +201,7 @@ void loopMqttServer() {
mqtt.publish("sync/time", timeStr);
lastPublish = millis();
}
}
void sendMQTTMessage(const char * topic, const char * message) {
@@ -187,3 +221,5 @@ void sendMQTTJSONMessage(const char * topic, const JsonDocument & doc) {
Serial.printf("Published JSON message to topic '%s': %s\n", topic, jsonString.c_str());
}