chart stuff wip

This commit is contained in:
DeltaLima 2025-04-11 02:01:05 +02:00
parent 535a4b2a43
commit c8f9a817d7
5 changed files with 7 additions and 71 deletions

View file

@ -10,7 +10,7 @@
* DEBUG is less noisy messages
* DEBUG2 are noisy messages
* DEBUG3 are super noisy messages */
#define DEBUG
//#define DEBUG
//#define DEBUG2
//#define DEBUG3

View file

@ -549,7 +549,7 @@ char* Char_Timestamp_yyyymmdd(const unsigned long timestamp) {
#if defined(DEBUG)
snprintf(tmp_timestamp, sizeof(tmp_timestamp), "%d%s%s%s", year(), tmp_month, tmp_day, tmp_minute);
#else
snprintf(tmp_timestamp, sizeof(tmp_timestamp), "%d%s%s", year(), tmp_month, tmp_day;
snprintf(tmp_timestamp, sizeof(tmp_timestamp), "%d%s%s", year(), tmp_month, tmp_day);
#endif
return tmp_timestamp;
}

View file

@ -6,73 +6,9 @@
*
*/
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();
void Api_chart_data(AsyncWebServerRequest *request) {
String json = Str_Chart_Json();
AsyncWebServerResponse *response = request->beginResponse(200, F("application/json"), json);
request->send(response);
}

View file

@ -410,7 +410,7 @@ a.disabled {
max-width: 640px;
margin: 0 auto;
.minw {
min-width: 140px;
min-width: 240px;
max-width: 320px;
text-align: center;
}

View file

@ -346,7 +346,7 @@ function DrawChart() {
let strTimestamp = timestamp.toLocaleDateString('de-DE', {day: 'numeric', month: 'numeric', year: 'numeric'}) + ' ' + timestamp.toLocaleTimeString('de-DE');
cell[j].innerHTML = '<span class=\'data minw\'> ' + strTimestamp + ' (' + ChartJson.chart[j].data[i] + ' ' + ChartJson.chart[j].unit + ')</span>';
cell[j].innerHTML = '<span class=\'data minw\'> <b>' + ChartJson.chart[j].data[i] + ' ' + ChartJson.chart[j].unit + '</b> (' + strTimestamp + ')</span>';
/* add chartscss styles for line chart */
/* build the css string in a var first */