Skip to content

Commit

Permalink
Send websocket messages only to one client
Browse files Browse the repository at this point in the history
  • Loading branch information
matjack1 committed Jan 22, 2023
1 parent f56d6c9 commit acd5b98
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
34 changes: 20 additions & 14 deletions src/log.esp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ size_t lastPos; // position counter for fast seek
#define FILES_PER_PAGE 10.0
#define MIN_SPIFF_BYTES 4096

void ICACHE_FLASH_ATTR sendLogFile(int page, String fileName, int logFileType)
void ICACHE_FLASH_ATTR sendLogFile(int page, String fileName, int logFileType, AsyncWebSocketClient *client)
{

// if we are reading the first page then we reset
Expand Down Expand Up @@ -110,15 +110,15 @@ void ICACHE_FLASH_ATTR sendLogFile(int page, String fileName, int logFileType)
if (buffer)
{
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
client->text(buffer);
if (logFileType == LOGTYPE_EVENTLOG)
ws.textAll("{\"command\":\"result\",\"resultof\":\"eventlist\",\"result\": true}");
client->text("{\"command\":\"result\",\"resultof\":\"eventlist\",\"result\": true}");
if (logFileType == LOGTYPE_LATESTLOG)
ws.textAll("{\"command\":\"result\",\"resultof\":\"latestlist\",\"result\": true}");
client->text("{\"command\":\"result\",\"resultof\":\"latestlist\",\"result\": true}");
}
}

void ICACHE_FLASH_ATTR logMaintenance(String action, String filename)
void ICACHE_FLASH_ATTR logMaintenance(String action, String filename, AsyncWebSocketClient *client)
{
#ifdef DEBUG
Serial.printf("[DEBUG] Log Maintenance Action: %s on %s\n", action.c_str(), filename.c_str());
Expand Down Expand Up @@ -170,7 +170,10 @@ void ICACHE_FLASH_ATTR logMaintenance(String action, String filename)

if ((fs_info.totalBytes - fs_info.usedBytes) < (logFile.size() + MIN_SPIFF_BYTES))
{
ws.textAll("{\"command\":\"result\",\"resultof\":\"logfileMaintenance\",\"result\": false,\"message\":\"Not enough space on SPIFF Filesystem\"}");
if (client)
{
client->text("{\"command\":\"result\",\"resultof\":\"logfileMaintenance\",\"result\": false,\"message\":\"Not enough space on SPIFF Filesystem\"}");
}
}
else
{
Expand Down Expand Up @@ -207,10 +210,13 @@ void ICACHE_FLASH_ATTR logMaintenance(String action, String filename)
}

ESP.wdtEnable(5000);
ws.textAll("{\"command\":\"result\",\"resultof\":\"logfileMaintenance\",\"result\": true}");
if (client)
{
client->text("{\"command\":\"result\",\"resultof\":\"logfileMaintenance\",\"result\": true}");
}
}

void ICACHE_FLASH_ATTR sendFileList(int page)
void ICACHE_FLASH_ATTR sendFileList(int page, AsyncWebSocketClient *client)
{

DynamicJsonDocument root(512);
Expand Down Expand Up @@ -249,21 +255,21 @@ void ICACHE_FLASH_ATTR sendFileList(int page)
if (buffer)
{
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
ws.textAll("{\"command\":\"result\",\"resultof\":\"listfiles\",\"result\": true}");
client->text(buffer);
client->text("{\"command\":\"result\",\"resultof\":\"listfiles\",\"result\": true}");
}
}

void ICACHE_FLASH_ATTR sendEventLog(int page, String fileName)
void ICACHE_FLASH_ATTR sendEventLog(int page, String fileName, AsyncWebSocketClient *client)
{
if (fileName.length() == 0)
fileName = "/eventlog.json";
sendLogFile(page, fileName, LOGTYPE_EVENTLOG);
sendLogFile(page, fileName, LOGTYPE_EVENTLOG, client);
}

void ICACHE_FLASH_ATTR sendLatestLog(int page, String fileName)
void ICACHE_FLASH_ATTR sendLatestLog(int page, String fileName, AsyncWebSocketClient *client)
{
if (fileName.length() == 0)
fileName = "/latestlog.json";
sendLogFile(page, fileName, LOGTYPE_LATESTLOG);
sendLogFile(page, fileName, LOGTYPE_LATESTLOG, client);
}
6 changes: 3 additions & 3 deletions src/mqtt.esp
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,9 @@ void processMqttMessage(MqttMessage *incomingMessage)
}
else if (strcmp(command, "deletlog") == 0)
{
logMaintenance("delete", "/latestlog.json.1");
logMaintenance("delete", "/eventlog.json");
logMaintenance("rollover", "/latestlog.json");
logMaintenance("delete", "/latestlog.json.1", nullptr);
logMaintenance("delete", "/eventlog.json", nullptr);
logMaintenance("rollover", "/latestlog.json", nullptr);
mqttPublishAck(command);
}
else if (strcmp(command, "getconf") == 0)
Expand Down
26 changes: 13 additions & 13 deletions src/websocket.esp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void ICACHE_FLASH_ATTR processWsMessage(WsMessage *incomingMessage)
}
else if (strcmp(command, "status") == 0)
{
sendStatus();
sendStatus(client);
}
else if (strcmp(command, "restart") == 0)
{
Expand All @@ -101,22 +101,22 @@ void ICACHE_FLASH_ATTR processWsMessage(WsMessage *incomingMessage)
{
int page = root["page"];
const char *xfileName = root["filename"];
sendEventLog(page, xfileName);
sendEventLog(page, xfileName, client);
}
else if (strcmp(command, "getlatestlog") == 0)
{
int page = root["page"];
const char *xfileName = root["filename"];
sendLatestLog(page, xfileName);
sendLatestLog(page, xfileName, client);
}
else if (strcmp(command, "listfiles") == 0)
{
int page = root["page"];
sendFileList(page);
sendFileList(page, client);
}
else if (strcmp(command, "logMaintenance") == 0)
{
logMaintenance(root["action"],root["filename"]);
logMaintenance(root["action"],root["filename"], client);
}
else if (strcmp(command, "clearevent") == 0)
{
Expand Down Expand Up @@ -147,45 +147,45 @@ void ICACHE_FLASH_ATTR processWsMessage(WsMessage *incomingMessage)
#endif
}
f.close();
ws.textAll("{\"command\":\"result\",\"resultof\":\"userfile\",\"result\": true}");
client->text("{\"command\":\"result\",\"resultof\":\"userfile\",\"result\": true}");
}
else if (strcmp(command, "testrelay1") == 0)
{
activateRelay[0] = true;
previousMillis = millis();
ws.textAll("{\"command\":\"giveAccess\"}");
client->text("{\"command\":\"giveAccess\"}");
}
else if (strcmp(command, "testrelay2") == 0)
{
activateRelay[1] = true;
previousMillis = millis();
ws.textAll("{\"command\":\"giveAccess\"}");
client->text("{\"command\":\"giveAccess\"}");
}
else if (strcmp(command, "testrelay3") == 0)
{
activateRelay[2] = true;
previousMillis = millis();
ws.textAll("{\"command\":\"giveAccess\"}");
client->text("{\"command\":\"giveAccess\"}");
}
else if (strcmp(command, "testrelay4") == 0)
{
activateRelay[3] = true;
previousMillis = millis();
ws.textAll("{\"command\":\"giveAccess\"}");
client->text("{\"command\":\"giveAccess\"}");
}
else if (strcmp(command, "scan") == 0)
{
WiFi.scanNetworksAsync(printScanResult, true);
}
else if (strcmp(command, "gettime") == 0)
{
sendTime();
sendTime(client);
}
else if (strcmp(command, "settime") == 0)
{
time_t t = root["epoch"];
setTime(t);
sendTime();
sendTime(client);
}
else if (strcmp(command, "getconf") == 0)
{
Expand All @@ -197,7 +197,7 @@ void ICACHE_FLASH_ATTR processWsMessage(WsMessage *incomingMessage)
if (buffer)
{
configFile.readBytes((char *)buffer->get(), len + 1);
ws.textAll(buffer);
client->text(buffer);
}
configFile.close();
}
Expand Down
26 changes: 20 additions & 6 deletions src/wsResponses.esp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void ICACHE_FLASH_ATTR sendUserList(int page, AsyncWebSocketClient *client)
serializeJson(root, (char *)buffer->get(), len + 1);
if (client)
{
ws.textAll(buffer);
ws.textAll("{\"command\":\"result\",\"resultof\":\"userlist\",\"result\": true}");
client->text(buffer);
client->text("{\"command\":\"result\",\"resultof\":\"userlist\",\"result\": true}");
}
else
{
Expand All @@ -65,7 +65,7 @@ void ICACHE_FLASH_ATTR sendUserList(int page, AsyncWebSocketClient *client)
}
}

void ICACHE_FLASH_ATTR sendStatus()
void ICACHE_FLASH_ATTR sendStatus(AsyncWebSocketClient *client)
{
struct ip_info info;
FSInfo fsinfo;
Expand Down Expand Up @@ -118,7 +118,14 @@ void ICACHE_FLASH_ATTR sendStatus()
if (buffer)
{
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
if (client)
{
client->text(buffer);
}
else
{
ws.textAll(buffer);
}
}
}

Expand Down Expand Up @@ -166,7 +173,7 @@ void ICACHE_FLASH_ATTR printScanResult(int networksFound)
WiFi.scanDelete();
}

void ICACHE_FLASH_ATTR sendTime()
void ICACHE_FLASH_ATTR sendTime(AsyncWebSocketClient *client)
{
DynamicJsonDocument root(512);
root["command"] = "gettime";
Expand All @@ -177,6 +184,13 @@ void ICACHE_FLASH_ATTR sendTime()
if (buffer)
{
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
if (client)
{
client->text(buffer);
}
else
{
ws.textAll(buffer);
}
}
}

0 comments on commit acd5b98

Please sign in to comment.