Adding peaks to data.json and MQTT RAW

This commit is contained in:
Gunnar Skjold 2022-08-23 20:21:19 +02:00
parent a6f3bc3f71
commit a3c7a09211
5 changed files with 45 additions and 2 deletions

View File

@ -260,6 +260,37 @@ float EnergyAccounting::getMonthMax() {
return maxHour > 0 ? maxHour / count / 100.0 : 0.0;
}
float EnergyAccounting::getPeak(uint8_t num) {
if(num < 1 || num > 5) return 0.0;
uint8_t count = 0;
bool included[5] = { false, false, false, false, false };
while(count < config->hours) {
uint8_t maxIdx = 0;
uint16_t maxVal = 0;
for(uint8_t i = 0; i < 5; i++) {
if(included[i]) continue;
if(data.peaks[i].value > maxVal) {
maxVal = data.peaks[i].value;
maxIdx = i;
}
}
included[maxIdx] = true;
count++;
}
uint8_t pos = 0;
for(uint8_t i = 0; i < 5; i++) {
if(!included[i]) continue;
pos++;
if(pos == num) {
return data.peaks[i].value / 100.0;
}
}
return 0.0;
}
bool EnergyAccounting::load() {
if(!LittleFS.begin()) {
if(debugger->isActive(RemoteDebug::ERROR)) {

View File

@ -57,6 +57,7 @@ public:
float getMonthMax();
uint8_t getCurrentThreshold();
float getPeak(uint8_t);
EnergyAccountingData getData();
void setData(EnergyAccountingData&);

View File

@ -74,6 +74,9 @@ bool RawMqttHandler::publish(AmsData* data, AmsData* meterState, EnergyAccountin
}
mqtt->publish(topic + "/realtime/import/hour", String(ea->getUseThisHour(), 3));
mqtt->publish(topic + "/realtime/import/day", String(ea->getUseToday(), 2));
for(uint8_t i = 1; i <= ea->getConfig()->hours; i++) {
mqtt->publish(topic + "/realtime/import/peak/" + String(i, 10), String(ea->getPeak(i), 10), true, 0);
}
mqtt->publish(topic + "/realtime/import/threshold", String(ea->getCurrentThreshold(), 10), true, 0);
mqtt->publish(topic + "/realtime/import/monthmax", String(ea->getMonthMax(), 3), true, 0);
mqtt->publish(topic + "/realtime/export/hour", String(ea->getProducedThisHour(), 3));

View File

@ -591,9 +591,9 @@ void AmsWebServer::configThresholdsHtml() {
String html = String((const __FlashStringHelper*) THRESHOLDS_HTML);
for(int i = 0; i < 9; i++) {
html.replace("{t" + String(i) + "}", String(config->thresholds[i]));
html.replace("{t" + String(i) + "}", String(config->thresholds[i], 10));
}
html.replace("{h}", String(config->hours));
html.replace("{h}", String(config->hours, 10));
server.setContentLength(html.length() + HEAD_HTML_LEN + FOOT_HTML_LEN);
server.send_P(200, MIME_HTML, HEAD_HTML);
@ -708,6 +708,12 @@ void AmsWebServer::dataJson() {
if(eapi != NULL && strlen(eapi->getToken()) > 0)
price = eapi->getValueForHour(0);
String peaks = "";
for(uint8_t i = 1; i <= ea->getConfig()->hours; i++) {
if(!peaks.isEmpty()) peaks += ",";
peaks += String(ea->getPeak(i));
}
snprintf_P(buf, BufferSize, DATA_JSON,
maxPwr == 0 ? meterState->isThreePhase() ? 20000 : 10000 : maxPwr,
meterConfig->productionCapacity,
@ -744,6 +750,7 @@ void AmsWebServer::dataJson() {
meterState->getMeterType(),
meterConfig->distributionSystem,
ea->getMonthMax(),
peaks.c_str(),
ea->getCurrentThreshold(),
ea->getUseThisHour(),
ea->getCostThisHour(),

View File

@ -35,6 +35,7 @@
"ds" : %d,
"ea" : {
"x" : %.1f,
"p" : [ %s ],
"t" : %d,
"h" : {
"u" : %.2f,