Add safe clearBuffer websocket method

This commit is contained in:
GHOSCHT 2022-04-13 19:09:03 +02:00
parent d285387f38
commit 740abd951d
No known key found for this signature in database
GPG key ID: A35BD466B8871994
3 changed files with 23 additions and 6 deletions

View file

@ -22,7 +22,7 @@ WebsocketCommunicator::WebsocketCommunicator(AsyncWebSocket &socket, AsyncWebSer
break; break;
} }
}; };
msgRead = false;
socket.onEvent(onEvent); socket.onEvent(onEvent);
server.addHandler(&socket); server.addHandler(&socket);
} }
@ -41,11 +41,21 @@ void WebsocketCommunicator::sendMessage(const char message[])
char *WebsocketCommunicator::receiveMessage() char *WebsocketCommunicator::receiveMessage()
{ {
msgRead = true;
return getBuffer(); return getBuffer();
} }
void WebsocketCommunicator::clearBufferSafely()
{
if (msgRead)
{
clearBuffer();
}
}
void WebsocketCommunicator::handleMessage(void *arg, uint8_t *data, size_t len) void WebsocketCommunicator::handleMessage(void *arg, uint8_t *data, size_t len)
{ {
msgRead = false;
int effectiveLen = len < bufferSize ? len : bufferSize; int effectiveLen = len < bufferSize ? len : bufferSize;
strncpy(messageBuffer, (char *)data, effectiveLen); strncpy(messageBuffer, (char *)data, effectiveLen);
} }

View file

@ -5,6 +5,7 @@ class WebsocketCommunicator : public Communicator
{ {
private: private:
void handleMessage(void *arg, uint8_t *data, size_t len); void handleMessage(void *arg, uint8_t *data, size_t len);
bool msgRead;
protected: protected:
AsyncWebSocket &socket; AsyncWebSocket &socket;
@ -15,4 +16,5 @@ public:
void sendMessage(int *values, __SIZE_TYPE__ numberOfValues) override; void sendMessage(int *values, __SIZE_TYPE__ numberOfValues) override;
void sendMessage(const char message[]) override; void sendMessage(const char message[]) override;
char *receiveMessage() override; char *receiveMessage() override;
void clearBufferSafely();
}; };

View file

@ -20,7 +20,7 @@ AsyncWebSocket ws("/ws");
Communicator *computer; Communicator *computer;
Communicator *phone; Communicator *phone;
Communicator *websocket; WebsocketCommunicator *websocket;
LightController *light; LightController *light;
void websocketTask(void *parameter) void websocketTask(void *parameter)
@ -28,15 +28,13 @@ void websocketTask(void *parameter)
while (true) while (true)
{ {
websocket->sendMessage(light->getBjtState(), light->getBjtCount()); websocket->sendMessage(light->getBjtState(), light->getBjtCount());
light->updateState(websocket->receiveMessage(), STEPS);
websocket->clearBuffer();
vTaskDelay(100 / portTICK_PERIOD_MS); vTaskDelay(100 / portTICK_PERIOD_MS);
} }
vTaskDelete(NULL); vTaskDelete(NULL);
} }
void registerTasks() void registerWebSocketTask()
{ {
xTaskCreate(websocketTask, "websocketTask", 10000, NULL, 1, NULL); xTaskCreate(websocketTask, "websocketTask", 10000, NULL, 1, NULL);
} }
@ -79,7 +77,7 @@ void setup()
connectWifi(WIFI_TIMEOUT); connectWifi(WIFI_TIMEOUT);
server.begin(); server.begin();
registerTasks(); registerWebSocketTask();
} }
void computerCycle() void computerCycle()
@ -96,9 +94,16 @@ void phoneCycle()
phone->clearBuffer(); phone->clearBuffer();
} }
void websocketCycle()
{
light->updateState(websocket->receiveMessage(), STEPS);
websocket->clearBufferSafely();
}
void loop() void loop()
{ {
computerCycle(); computerCycle();
phoneCycle(); phoneCycle();
websocketCycle();
ws.cleanupClients(); ws.cleanupClients();
} }