offer chart data as json via api /api/chart/
This commit is contained in:
parent
1c7aba5e0a
commit
1cc01bba9b
2 changed files with 81 additions and 0 deletions
include
|
@ -29,6 +29,7 @@
|
|||
* include Api header files
|
||||
*/
|
||||
#include "Webserver/Api_sensor.h"
|
||||
#include "Webserver/Api_chartdata.h"
|
||||
|
||||
AsyncWebServer webserver(80);
|
||||
// load requestLogger middleware
|
||||
|
@ -103,6 +104,8 @@ void Webserver_Init() {
|
|||
webserver.on("/api/sensor/raw", HTTP_GET, Api_sensor_data_raw);
|
||||
webserver.on("/api/sensor/driver", HTTP_GET, Api_sensor_driver);
|
||||
|
||||
webserver.on("/api/chart/", HTTP_GET, Api_chart_data);
|
||||
|
||||
|
||||
/* DEBUG only - offer config for direct download */
|
||||
#ifdef DEBUG
|
||||
|
|
78
include/Webserver/Api_chartdata.h
Normal file
78
include/Webserver/Api_chartdata.h
Normal file
|
@ -0,0 +1,78 @@
|
|||
/*
|
||||
*
|
||||
* include/Webserver/Api_chartdata.h - Chart Data API header file
|
||||
*
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
void Api_chart_data(AsyncWebServerRequest* request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot().to<JsonObject>();
|
||||
//root["hello"] = "world";
|
||||
for(byte i = 0 ; i < Max_Dashboard_Chart ; i++) {
|
||||
if(config.grow.dashboard.chartConfigured[i] == true) {
|
||||
|
||||
JsonObject objChart = root["chart"].add<JsonObject>();
|
||||
objChart["id"] = i;
|
||||
//objSensor["unit"] = Sensor_Read_unit[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]];
|
||||
|
||||
objChart["name"] = config.system.sensor.name[config.grow.dashboard.chartSensor[i]];
|
||||
/* when for a RAW reading rawConvert is set, return the converted description and unit */
|
||||
if((SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]] == SENSOR_READ_TYPE_RAW) && (config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]] > 0)) {
|
||||
objChart["descr"] = FPSTR(Sensor_Convert_Raw_descr[config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]]]);
|
||||
objChart["unit"] = FPSTR(Sensor_Convert_Raw_unit[config.system.sensor.rawConvert[config.grow.dashboard.chartSensor[i]][config.grow.dashboard.chartRead[i]]]);
|
||||
} else {
|
||||
objChart["descr"] = FPSTR(Sensor_Read_descr[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]]);
|
||||
objChart["unit"] = FPSTR(Sensor_Read_unit[SensorIndex[config.system.sensor.type[config.grow.dashboard.chartSensor[i]]].read[config.grow.dashboard.chartRead[i]]]);
|
||||
}
|
||||
|
||||
/* build path to chart data dir */
|
||||
char chartDirPath[10];
|
||||
snprintf(chartDirPath, sizeof(chartDirPath), "%s%d", CHART_BASE_PATH, i);
|
||||
/* count number of datasets stored */
|
||||
unsigned short dataIndex = 0;
|
||||
|
||||
//Serial.println(chartDirPath);
|
||||
|
||||
/* https://github.com/cotestatnt/async-esp-fs-webserver/blob/master/src/AsyncFsWebServer.cpp */
|
||||
File rootDir = LittleFS.open(chartDirPath, "r");
|
||||
File file = rootDir.openNextFile();
|
||||
//float previousData = 0;
|
||||
/* iterate through all files in chart directory */
|
||||
while (file) {
|
||||
if (!file.isDirectory()) {
|
||||
|
||||
/* absolute path to the file we want to read */
|
||||
char chartFilePath[32];
|
||||
snprintf(chartFilePath, sizeof(chartFilePath), "%s/%s", chartDirPath, file.name());
|
||||
//Serial.println(chartFilePath);
|
||||
/* the actual file we are reading */
|
||||
File fileRead = LittleFS.open(chartFilePath, "r");
|
||||
|
||||
while(fileRead.available()) {
|
||||
/* convert timestamp to unsigned long */
|
||||
unsigned long timestamp = strtoul(fileRead.readStringUntil(';').c_str(), NULL, 0);
|
||||
/* convert the data point to float*/
|
||||
float dataPoint = fileRead.readStringUntil('\n').toFloat();
|
||||
|
||||
objChart["data"][dataIndex] = dataPoint;
|
||||
objChart["timestamp"][dataIndex] = timestamp;
|
||||
|
||||
/* increment datacount */
|
||||
dataIndex++;
|
||||
}
|
||||
fileRead.close();
|
||||
|
||||
}
|
||||
file = rootDir.openNextFile();
|
||||
}
|
||||
|
||||
objChart["datapoints"] = dataIndex;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue