Refactor WebSocket handling: add NFC data sending to clients and improve connection logging; adjust task priority for better scheduling
This commit is contained in:
1 parent
13a0acf2ac
commit
ad145c7d7a
3 files changed
+25
-6
No files matched your search
+3
-2
@@ -216,8 +216,9 @@ void sendLocationAsync(int spoolId, String spoolTagUuid, int locationId, String
|
||||
bool initFilaman() {
|
||||
loadFilamanConfig();
|
||||
queueMutex = xSemaphoreCreateMutex();
|
||||
// Reduce stack size to 6k and set priority to 0 (lowest) to prevent starving WiFi stack
|
||||
xTaskCreatePinnedToCore(filamanApiTask, "FilaManApi", 6144, NULL, 0, NULL, 0);
|
||||
// Move to Core 1 (Hardware Core) to free up Core 0 for WiFi/Webserver
|
||||
// Set priority to 1 (same as Scale/NFC) to ensure fair scheduling
|
||||
xTaskCreatePinnedToCore(filamanApiTask, "FilaManApi", 6144, NULL, 1, NULL, 1);
|
||||
if (checkFilamanRegistration()) sendHeartbeatAsync();
|
||||
return true;
|
||||
}
|
||||
@@ -119,6 +119,8 @@ void loop() {
|
||||
if (intervalElapsed(currentMillis, lastTopRowUpdateTime, DISPLAY_UPDATE_INTERVAL))
|
||||
{
|
||||
oledShowTopRow();
|
||||
// Clean up dead websocket clients periodically instead of on connect
|
||||
if(currentMillis % 10000 < 50) ws.cleanupClients();
|
||||
}
|
||||
|
||||
// Periodic FilaMan heartbeat
|
||||
|
||||
+20
-4
@@ -24,12 +24,28 @@ AsyncWebSocket ws("/ws");
|
||||
uint8_t lastSuccess = 0;
|
||||
nfcReaderStateType lastnfcReaderState = NFC_IDLE;
|
||||
|
||||
void sendNfcDataToClient(AsyncWebSocketClient *client) {
|
||||
if(!client) return;
|
||||
switch(nfcReaderState){
|
||||
case NFC_IDLE: client->text("{\"type\":\"nfcData\", \"payload\":{}}"); break;
|
||||
case NFC_READ_SUCCESS: client->text("{\"type\":\"nfcData\", \"payload\":" + nfcJsonData + "}"); break;
|
||||
case NFC_READ_ERROR: client->text("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Read Error\"}}"); break;
|
||||
case NFC_WRITING: client->text("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Writing...\"}}"); break;
|
||||
case NFC_WRITE_SUCCESS: client->text("{\"type\":\"nfcData\", \"payload\":{\"info\":\"Success\"}}"); break;
|
||||
case NFC_WRITE_ERROR: client->text("{\"type\":\"nfcData\", \"payload\":{\"error\":\"Write Error\"}}"); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
|
||||
if (type == WS_EVT_CONNECT) {
|
||||
Serial.printf("WS Client #%u connected\n", client->id());
|
||||
sendNfcData();
|
||||
ws.text(client->id(), "{\"type\":\"nfcTag\", \"payload\":{\"found\": " + String(lastSuccess) + "}}");
|
||||
server->cleanupClients();
|
||||
Serial.printf("WS Client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
|
||||
sendNfcDataToClient(client);
|
||||
client->text("{\"type\":\"nfcTag\", \"payload\":{\"found\": " + String(lastSuccess) + "}}");
|
||||
} else if (type == WS_EVT_DISCONNECT) {
|
||||
Serial.printf("WS Client #%u disconnected\n", client->id());
|
||||
} else if (type == WS_EVT_ERROR) {
|
||||
Serial.printf("WS Client #%u error: %u\n", client->id(), *((uint16_t*)arg));
|
||||
} else if (type == WS_EVT_DATA) {
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, (char*)data, len);
|
||||
|
||||
Reference in new issue
Block a user