Browse Source

Add JSON output; Available via HTTP data point /json

pull/82/head
wtl0 3 years ago
parent
commit
e5adf23ee9
  1. 2
      .gitignore
  2. 42
      tools/esp8266/app.cpp
  3. 2
      tools/esp8266/app.h

2
.gitignore

@ -10,6 +10,8 @@ compile_commands.json
CTestTestfile.cmake
_deps
build
tools/esp8266/tmp
tools/esp8266/binaries
/**/Debug
/**/v16/*
*.db

42
tools/esp8266/app.cpp

@ -52,13 +52,14 @@ void app::setup(uint32_t timeout) {
DPRINTLN(DBG_VERBOSE, F("app::setup"));
Main::setup(timeout);
mWeb->on("/", std::bind(&app::showIndex, this));
mWeb->on("/setup", std::bind(&app::showSetup, this));
mWeb->on("/save", std::bind(&app::showSave, this));
mWeb->on("/erase", std::bind(&app::showErase, this));
mWeb->on("/cmdstat", std::bind(&app::showStatistics, this));
mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this));
mWeb->on("/livedata", std::bind(&app::showLiveData, this));
mWeb->on("/", std::bind(&app::showIndex, this));
mWeb->on("/setup", std::bind(&app::showSetup, this));
mWeb->on("/save", std::bind(&app::showSave, this));
mWeb->on("/erase", std::bind(&app::showErase, this));
mWeb->on("/cmdstat", std::bind(&app::showStatistics, this));
mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this));
mWeb->on("/livedata", std::bind(&app::showLiveData, this));
mWeb->on("/json", std::bind(&app::showJSON, this));
if(mSettingsValid) {
mEep->read(ADDR_INV_INTERVAL, &mSendInterval);
@ -739,9 +740,34 @@ void app::showLiveData(void) {
#endif
}
}
mWeb->send(200, F("text/html"), modHtml);
}
mWeb->send(200, F("text/html"), modHtml);
//-----------------------------------------------------------------------------
void app::showJSON(void) {
DPRINTLN(DBG_VERBOSE, F("app::showJSON"));
String modJson;
modJson = F("{\n");
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if(NULL != iv) {
char topic[40], val[25];
snprintf(topic, 30, "\"%s\": {\n", iv->name);
modJson += String(topic);
for(uint8_t i = 0; i < iv->listLen; i++) {
snprintf(topic, 40, "\t\"ch%d/%s\"", iv->assign[i].ch, iv->getFieldName(i));
snprintf(val, 25, "[%.3f, \"%s\"]", iv->getValue(i), iv->getUnit(i));
modJson += String(topic) + ": " + String(val) + F(",\n");
}
modJson += F("\t\"last_msg\": \"") + getDateTimeStr(iv->ts) + F("\"\n\t},\n");
}
}
modJson += F("\"json_ts\": \"") + String(getDateTimeStr(mTimestamp)) + F("\"\n}\n");
// mWeb->send(200, F("text/json"), modJson);
mWeb->send(200, F("application/json"), modJson); // the preferred content-type (https://stackoverflow.com/questions/22406077/what-is-the-exact-difference-between-content-type-text-json-and-application-jso)
}

2
tools/esp8266/app.h

@ -67,6 +67,8 @@ class app : public Main {
void showStatistics(void);
void showHoymiles(void);
void showLiveData(void);
void showJSON(void);
void saveValues(bool webSend);
void updateCrc(void);

Loading…
Cancel
Save