This commit is contained in:
+123
-36
@@ -21,23 +21,24 @@
|
||||
#include "json_builder.h"
|
||||
|
||||
extern Preferences prefs;
|
||||
extern DeviceTelemetry telemetry;
|
||||
extern NetworkData wifi_data;
|
||||
extern NetworkData ethernet_data;
|
||||
extern SensorData shunt_data;
|
||||
extern OTAStatus ota_status;
|
||||
|
||||
AsyncWebSocket webSocket("/webSocket");
|
||||
AsyncWebServer server(80);
|
||||
|
||||
// Local water data cache
|
||||
|
||||
WaterData local_water_data;
|
||||
SensorData local_sensor_data;
|
||||
NetworkData local_wifi_data;
|
||||
NetworkData local_ethernet_data;
|
||||
DeviceTelemetry local_telemetry;
|
||||
OTAStatus local_ota_status;
|
||||
|
||||
|
||||
// Readers-writer lock for local_water_data
|
||||
ReadersWriterLock waterDataLock;
|
||||
|
||||
// Queue to receive water data from the sensor task
|
||||
QueueHandle_t webserverWaterDataQueue;
|
||||
ReadersWriterLock sensorDataLock;
|
||||
ReadersWriterLock networkDataLock;
|
||||
ReadersWriterLock telemetryDataLock;
|
||||
ReadersWriterLock otaStatusLock;
|
||||
|
||||
|
||||
// ======================
|
||||
@@ -91,14 +92,16 @@ void setup_routes() {
|
||||
*/
|
||||
void setup_api_endpoints(){
|
||||
server.on("/sensor_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
rwLockAcquireRead(&sensorDataLock);
|
||||
String output;
|
||||
serializeJson(build_shunt_data_json(shunt_data), output);
|
||||
build_shunt_data_json(local_sensor_data, output);
|
||||
rwLockReleaseRead(&sensorDataLock);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/water_data", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
rwLockAcquireRead(&waterDataLock);
|
||||
String output;
|
||||
serializeJson(build_water_data_json(local_water_data), output);
|
||||
build_water_data_json(local_water_data, output);
|
||||
rwLockReleaseRead(&waterDataLock);
|
||||
request->send(200, "application/json", output);
|
||||
});
|
||||
@@ -118,33 +121,38 @@ void setup_api_endpoints(){
|
||||
});
|
||||
|
||||
server.on("/telemetry", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
rwLockAcquireRead(&telemetryDataLock);
|
||||
String output;
|
||||
serializeJson(build_telemetry_json(telemetry), output);
|
||||
build_telemetry_json(local_telemetry, output);
|
||||
rwLockReleaseRead(&telemetryDataLock);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/network_info", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
rwLockAcquireRead(&networkDataLock);
|
||||
String output;
|
||||
serializeJson(build_network_json(ethernet_data, wifi_data), output);
|
||||
build_network_json(local_ethernet_data, local_wifi_data, output);
|
||||
rwLockReleaseRead(&networkDataLock);
|
||||
request->send(200, "application/json", output);
|
||||
});
|
||||
|
||||
server.on("/ota_update_status", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
rwLockAcquireRead(&otaStatusLock);
|
||||
String output;
|
||||
serializeJson(build_ota_json(ota_status), output);
|
||||
request->send(200, "application/json", output);
|
||||
});
|
||||
build_ota_json(local_ota_status, output);
|
||||
rwLockReleaseRead(&otaStatusLock);
|
||||
request->send(200, "application/json", output); });
|
||||
|
||||
server.on("/run_ota_update", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
if (ota_status.update_progress > -1) {
|
||||
if (local_ota_status.update_progress > -1) {
|
||||
request->send(200, "text/plain", "OTA Update already in progress");
|
||||
return;
|
||||
} else if (!ota_status.update_available) {
|
||||
} else if (!local_ota_status.update_available) {
|
||||
request->send(200, "text/plain", "No update available");
|
||||
return;
|
||||
}
|
||||
|
||||
static TaskArgs_t args = {
|
||||
.ota_status = ota_status
|
||||
.ota_status = local_ota_status
|
||||
};
|
||||
|
||||
xTaskCreate(run_ota_update_task, "RunOTAUpdate", 1024 * 8, (void *)&args, 1, NULL);
|
||||
@@ -235,16 +243,16 @@ void handle_update_sensor_settings(AsyncWebServerRequest* request) {
|
||||
* @brief Main task for the webserver.
|
||||
*
|
||||
* Initializes all routes, starts the webserver, and sets up local water data.
|
||||
* Receives water data from the sensor task via a queue and updates local_water_data.
|
||||
* Receives the current state from the main task via a queue and updates local_water_data.
|
||||
* Runs indefinitely to keep the server active.
|
||||
*
|
||||
* @param pvParameters Task parameters (expected to be a QueueHandle_t for the water data queue).
|
||||
* @param pvParameters Task parameters (expected to be a QueueHandle_t for the state queue).
|
||||
*/
|
||||
void webserver_task(void *pvParameters) {
|
||||
// Extract the queue handle from the task parameters
|
||||
webserverWaterDataQueue = (QueueHandle_t)pvParameters;
|
||||
if (webserverWaterDataQueue == NULL) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Webserver water data queue is NULL");
|
||||
QueueHandle_t stateQueue = (QueueHandle_t)pvParameters;
|
||||
if (stateQueue == NULL) {
|
||||
LOG(ELOG_LEVEL_ERROR, "State queue is NULL");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
@@ -252,33 +260,112 @@ void webserver_task(void *pvParameters) {
|
||||
LOG(ELOG_LEVEL_DEBUG, "Setting up routes");
|
||||
setup_routes();
|
||||
|
||||
// Initialize the readers-writer lock
|
||||
// Initialize the readers-writer locks
|
||||
if (!rwLockInit(&waterDataLock)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize readers-writer lock");
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize water data readers-writer lock");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rwLockInit(&sensorDataLock)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize sensor data readers-writer lock");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rwLockInit(&networkDataLock)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize network data readers-writer lock");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rwLockInit(&telemetryDataLock)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize telemetry data readers-writer lock");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!rwLockInit(&otaStatusLock)) {
|
||||
LOG(ELOG_LEVEL_ERROR, "Failed to initialize OTA status readers-writer lock");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
// Initialize local water data with static values
|
||||
rwLockAcquireWrite(&waterDataLock);
|
||||
local_water_data.level = 50.0f;
|
||||
local_water_data.liters = 100.0f;
|
||||
local_water_data.percentage = 50.0f;
|
||||
local_water_data.level = -1.0;
|
||||
local_water_data.liters = -1.0;
|
||||
local_water_data.percentage = -1.0;
|
||||
rwLockReleaseWrite(&waterDataLock);
|
||||
|
||||
// Initialize local sensor data with static values
|
||||
rwLockAcquireWrite(&sensorDataLock);
|
||||
local_sensor_data.bus_voltage = -1.0;
|
||||
local_sensor_data.shunt_voltage = -1.0;
|
||||
local_sensor_data.shunt_current = -1.0;
|
||||
rwLockReleaseWrite(&sensorDataLock);
|
||||
|
||||
// Initialize local network data with static values
|
||||
rwLockAcquireWrite(&networkDataLock);
|
||||
strcpy(local_wifi_data.ip_address, "0.0.0.0");
|
||||
local_wifi_data.link = false;
|
||||
local_wifi_data.rssi = -1.0;
|
||||
strcpy(local_wifi_data.network_name, "UNKNOWN");
|
||||
|
||||
strcpy(local_ethernet_data.ip_address, "0.0.0.0");
|
||||
local_ethernet_data.link = false;
|
||||
local_ethernet_data.rssi = -1.0;
|
||||
strcpy(local_ethernet_data.network_name, "UNKNOWN");
|
||||
rwLockReleaseWrite(&networkDataLock);
|
||||
|
||||
// Initialize local telemetry data with static values
|
||||
rwLockAcquireWrite(&telemetryDataLock);
|
||||
local_telemetry.heap_used_percent = -1.0;
|
||||
local_telemetry.uptime_seconds = -1;
|
||||
local_telemetry.temperature = -1.0;
|
||||
rwLockReleaseWrite(&telemetryDataLock);
|
||||
|
||||
// Initialize local OTA status with static values
|
||||
rwLockAcquireWrite(&otaStatusLock);
|
||||
local_ota_status.update_available = false;
|
||||
local_ota_status.current_version = {0, 0, 0};
|
||||
local_ota_status.latest_version = {0, 0, 0};
|
||||
local_ota_status.update_progress = -1;
|
||||
local_ota_status.update_url = "UNKNOWN";
|
||||
rwLockReleaseWrite(&otaStatusLock);
|
||||
|
||||
LOG(ELOG_LEVEL_DEBUG, "Starting webserver");
|
||||
server.begin();
|
||||
|
||||
while (1) {
|
||||
// Check if there is new water data in the queue
|
||||
WaterData newWaterData;
|
||||
if (xQueueReceive(webserverWaterDataQueue, &newWaterData, 0) == pdTRUE) {
|
||||
// Check if there is new state data in the queue
|
||||
CurrentState currentState;
|
||||
if (xQueueReceive(stateQueue, ¤tState, portMAX_DELAY) == pdTRUE) {
|
||||
// Update local_water_data with the new data from the queue
|
||||
rwLockAcquireWrite(&waterDataLock);
|
||||
local_water_data = newWaterData;
|
||||
local_water_data = currentState.waterData;
|
||||
rwLockReleaseWrite(&waterDataLock);
|
||||
|
||||
// Update local_sensor_data with the new data from the queue
|
||||
rwLockAcquireWrite(&sensorDataLock);
|
||||
local_sensor_data = currentState.sensorData;
|
||||
rwLockReleaseWrite(&sensorDataLock);
|
||||
|
||||
// Update local network data with the new data from the queue
|
||||
rwLockAcquireWrite(&networkDataLock);
|
||||
local_wifi_data = currentState.wifiData;
|
||||
local_ethernet_data = currentState.ethernetData;
|
||||
rwLockReleaseWrite(&networkDataLock);
|
||||
|
||||
// Update local telemetry data with the new data from the queue
|
||||
rwLockAcquireWrite(&telemetryDataLock);
|
||||
local_telemetry = currentState.telemetryData;
|
||||
rwLockReleaseWrite(&telemetryDataLock);
|
||||
|
||||
// Update local OTA status with the new data from the queue
|
||||
rwLockAcquireWrite(&otaStatusLock);
|
||||
local_ota_status = currentState.otaStatus;
|
||||
rwLockReleaseWrite(&otaStatusLock);
|
||||
}
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS); // Small delay to reduce CPU usage
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user