From cdc6fef292aefc8b433547380c19df271295bf9b Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 5 Mar 2023 09:00:59 +0100 Subject: [PATCH 1/3] Code cleanup: remove obsolete JSON_EP --- src/config/config_override_example.h | 3 --- src/web/web.h | 33 ---------------------------- 2 files changed, 36 deletions(-) diff --git a/src/config/config_override_example.h b/src/config/config_override_example.h index b56214c8..ed976ceb 100644 --- a/src/config/config_override_example.h +++ b/src/config/config_override_example.h @@ -30,9 +30,6 @@ #undef MIDNIGHTTICKER_OFFSET #define MIDNIGHTTICKER_OFFSET (mCalculatedTimezoneOffset + 1) -// To enable the json endpoint at /json -// #define ENABLE_JSON_EP - // To enable the endpoint for prometheus to scrape data from at /metrics // #define ENABLE_PROMETHEUS_EP diff --git a/src/web/web.h b/src/web/web.h index d79ace82..2c4f1ad8 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -75,9 +75,6 @@ class Web { mWeb.on("/live", HTTP_ANY, std::bind(&Web::onLive, this, std::placeholders::_1)); //mWeb.on("/api1", HTTP_POST, std::bind(&Web::showWebApi, this, std::placeholders::_1)); - #ifdef ENABLE_JSON_EP - mWeb.on("/json", HTTP_ANY, std::bind(&Web::showJson, this, std::placeholders::_1)); - #endif #ifdef ENABLE_PROMETHEUS_EP mWeb.on("/metrics", HTTP_ANY, std::bind(&Web::showMetrics, this, std::placeholders::_1)); #endif @@ -730,36 +727,6 @@ class Web { request->send(response); } -#ifdef ENABLE_JSON_EP - void showJson(AsyncWebServerRequest *request) { - DPRINTLN(DBG_VERBOSE, F("web::showJson")); - String modJson; - Inverter<> *iv; - record_t<> *rec; - char topic[40], val[25]; - - modJson = F("{\n"); - for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { - iv = mSys->getInverterByPos(id); - if(NULL == iv) - continue; - - rec = iv->getRecordStruct(RealTimeRunData_Debug); - snprintf(topic, 30, "\"%s\": {\n", iv->config->name); - modJson += String(topic); - for(uint8_t i = 0; i < rec->length; i++) { - snprintf(topic, 40, "\t\"ch%d/%s\"", rec->assign[i].ch, iv->getFieldName(i, rec)); - snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i, rec), iv->getUnit(i, rec)); - modJson += String(topic) + ": " + String(val) + F(",\n"); - } - modJson += F("\t\"last_msg\": \"") + ah::getDateTimeStr(rec->ts) + F("\"\n\t},\n"); - } - modJson += F("\"json_ts\": \"") + String(ah::getDateTimeStr(mApp->getTimestamp())) + F("\"\n}\n"); - - AsyncWebServerResponse *response = request->beginResponse(200, F("application/json"), modJson); - request->send(response); - } -#endif #ifdef ENABLE_PROMETHEUS_EP enum { From 0334898e3d038c2c14c70bd722e7b831d70f3b8e Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 7 Apr 2023 09:15:55 +0200 Subject: [PATCH 2/3] Do not send prometheus metric if channel is disabled in configuration --- doc/prometheus_ep_description.md | 2 +- src/web/web.h | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/doc/prometheus_ep_description.md b/doc/prometheus_ep_description.md index 8fb9e002..755fd1e4 100644 --- a/doc/prometheus_ep_description.md +++ b/doc/prometheus_ep_description.md @@ -12,7 +12,7 @@ Prometheus metrics provided at `/metrics`. | name | Inverter name from setup | | serial | Serial number of inverter | | inverter | Inverter name from setup | -| channel | Channel name from setup | +| channel | Channel (Module) name from setup. Label only available if max power level of module is set to non-zero. Be sure to have a cannel name set in configuration. | ## Exported Metrics | Metric name | Type | Description | Labels | diff --git a/src/web/web.h b/src/web/web.h index f8abc0f7..4b7cbb39 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -719,15 +719,20 @@ class Web { rec = iv->getRecordStruct(RealTimeRunData_Debug); if (metricsChannelId < rec->length) { uint8_t channel = rec->assign[metricsChannelId].ch; - std::tie(promUnit, promType) = convertToPromUnits(iv->getUnit(metricsChannelId, rec)); - snprintf(type, sizeof(type), "# TYPE ahoy_solar_%s%s %s", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), promType.c_str()); - if (0 == channel) { - snprintf(topic, sizeof(topic), "ahoy_solar_%s%s{inverter=\"%s\"}", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), iv->config->name); + // Skip entry if maxPwr is 0 and it's not the inverter channel (channel 0) + if (0 == channel || 0 != iv->config->chMaxPwr[channel-1]) { + std::tie(promUnit, promType) = convertToPromUnits(iv->getUnit(metricsChannelId, rec)); + snprintf(type, sizeof(type), "# TYPE ahoy_solar_%s%s %s", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), promType.c_str()); + if (0 == channel) { + snprintf(topic, sizeof(topic), "ahoy_solar_%s%s{inverter=\"%s\"}", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), iv->config->name); + } else { + snprintf(topic, sizeof(topic), "ahoy_solar_%s%s{inverter=\"%s\",channel=\"%s\"}", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), iv->config->name,iv->config->chName[channel-1]); + } + snprintf(val, sizeof(val), "%.3f", iv->getValue(metricsChannelId, rec)); + len = snprintf((char*)buffer,maxLen,"%s\n%s %s\n",type,topic,val); } else { - snprintf(topic, sizeof(topic), "ahoy_solar_%s%s{inverter=\"%s\",channel=\"%s\"}", iv->getFieldName(metricsChannelId, rec), promUnit.c_str(), iv->config->name,iv->config->chName[channel-1]); + len = snprintf((char*)buffer,maxLen,"#\n"); // At least one char to send otherwise the transmission ends. } - snprintf(val, sizeof(val), "%.3f", iv->getValue(metricsChannelId, rec)); - len = snprintf((char*)buffer,maxLen,"%s\n%s %s\n",type,topic,val); metricsChannelId++; } else { From e17f6afc8621a33857800c5a82f5024de1ee5447 Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 7 Apr 2023 17:13:44 +0200 Subject: [PATCH 3/3] Removed /json endpoint availablity from documentation --- Getting_Started.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Getting_Started.md b/Getting_Started.md index 3a031af0..4d35903c 100644 --- a/Getting_Started.md +++ b/Getting_Started.md @@ -264,9 +264,8 @@ When everything is wired up and the firmware is flashed, it is time to connect t | /cmdstat | show stat from the home page | | yes | | /visualization | displays the information from your converter | | yes | | /livedata | displays the live data | | yes | -| /json | gets live-data in JSON format | json output from the livedata | no - enable via config_override.h | | /metrics | gets live-data for prometheus | prometheus metrics from the livedata | no - enable via config_override.h | -| /api | | | yes | +| /api | gets configuration and live-data in JSON format | json output from the configuration or livedata | yes | ## MQTT command to set the DTU without webinterface