mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-30 09:16:11 +02:00
improved webApi, added /api/setup for compared json (only 1 XHR is needed to get all infos)
This commit is contained in:
parent
b871ed06d2
commit
833e6c4904
4 changed files with 93 additions and 106 deletions
tools/esp8266
|
@ -9,7 +9,6 @@
|
|||
#endif
|
||||
|
||||
#include "webApi.h"
|
||||
#include "AsyncJson.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
webApi::webApi(AsyncWebServer *srv, app *app, sysConfig_t *sysCfg, config_t *config, statistics_t *stat, char version[]) {
|
||||
|
@ -24,34 +23,32 @@ webApi::webApi(AsyncWebServer *srv, app *app, sysConfig_t *sysCfg, config_t *con
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::setup(void) {
|
||||
mSrv->on("/api/system", HTTP_GET, std::bind(&webApi::onSystem, this, std::placeholders::_1));
|
||||
mSrv->on("/api/statistics", HTTP_GET, std::bind(&webApi::onStatistics, this, std::placeholders::_1));
|
||||
mSrv->on("/api/inverter/list", HTTP_GET, std::bind(&webApi::onInverterList, this, std::placeholders::_1));
|
||||
mSrv->on("/api/mqtt", HTTP_GET, std::bind(&webApi::onMqtt, this, std::placeholders::_1));
|
||||
mSrv->on("/api/ntp", HTTP_GET, std::bind(&webApi::onNtp, this, std::placeholders::_1));
|
||||
mSrv->on("/api/pinout", HTTP_GET, std::bind(&webApi::onPinout, this, std::placeholders::_1));
|
||||
mSrv->on("/api/radio", HTTP_GET, std::bind(&webApi::onRadio, this, std::placeholders::_1));
|
||||
mSrv->on("/api/serial", HTTP_GET, std::bind(&webApi::onSerial, this, std::placeholders::_1));
|
||||
mSrv->on("/api", HTTP_GET, std::bind(&webApi::onApi, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::loop(void) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onSystem(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
void webApi::onApi(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse(false, 2048);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("ssid")] = mSysCfg->stationSsid;
|
||||
root[F("device_name")] = mSysCfg->deviceName;
|
||||
root[F("version")] = String(mVersion);
|
||||
root[F("build")] = String(AUTO_GIT_HASH);
|
||||
root[F("ts_uptime")] = mApp->getUptime();
|
||||
root[F("ts_now")] = mApp->getTimestamp();
|
||||
String path = request->url().substring(5);
|
||||
if(path == "system") getSystem(root);
|
||||
else if(path == "statistics") getStatistics(root);
|
||||
else if(path == "inverter/list") getInverterList(root);
|
||||
else if(path == "mqtt") getMqtt(root);
|
||||
else if(path == "ntp") getNtp(root);
|
||||
else if(path == "pinout") getPinout(root);
|
||||
else if(path == "radio") getRadio(root);
|
||||
else if(path == "serial") getSerial(root);
|
||||
else if(path == "setup") getSetup(root);
|
||||
else
|
||||
root["info"] = "not found"; //root["url"] = request->url();
|
||||
|
||||
response->setLength();
|
||||
//response->addHeader("Access-Control-Allow-Origin", "*");
|
||||
|
@ -61,25 +58,28 @@ void webApi::onSystem(AsyncWebServerRequest *request) {
|
|||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onStatistics(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("rx_success")] = mStat->rxSuccess;
|
||||
root[F("rx_fail")] = mStat->rxFail;
|
||||
root[F("frame_cnt")] = mStat->frmCnt;
|
||||
root[F("tx_cnt")] = mApp->mSys->Radio.mSendCnt;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getSystem(JsonObject obj) {
|
||||
obj[F("ssid")] = mSysCfg->stationSsid;
|
||||
obj[F("device_name")] = mSysCfg->deviceName;
|
||||
obj[F("version")] = String(mVersion);
|
||||
obj[F("build")] = String(AUTO_GIT_HASH);
|
||||
obj[F("ts_uptime")] = mApp->getUptime();
|
||||
obj[F("ts_now")] = mApp->getTimestamp();
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onInverterList(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
JsonArray invArr = root.createNestedArray("inverter");
|
||||
void webApi::getStatistics(JsonObject obj) {
|
||||
obj[F("rx_success")] = mStat->rxSuccess;
|
||||
obj[F("rx_fail")] = mStat->rxFail;
|
||||
obj[F("frame_cnt")] = mStat->frmCnt;
|
||||
obj[F("tx_cnt")] = mApp->mSys->Radio.mSendCnt;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::getInverterList(JsonObject obj) {
|
||||
JsonArray invArr = obj.createNestedArray("inverter");
|
||||
|
||||
Inverter<> *iv;
|
||||
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
|
||||
|
@ -101,80 +101,58 @@ void webApi::onInverterList(AsyncWebServerRequest *request) {
|
|||
obj[F("power_limit_option")] = iv->powerLimit[1];
|
||||
}
|
||||
}
|
||||
root[F("interval")] = String(mConfig->sendInterval);
|
||||
root[F("retries")] = String(mConfig->maxRetransPerPyld);
|
||||
root[F("max_num_inverters")] = MAX_NUM_INVERTERS;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
obj[F("interval")] = String(mConfig->sendInterval);
|
||||
obj[F("retries")] = String(mConfig->maxRetransPerPyld);
|
||||
obj[F("max_num_inverters")] = MAX_NUM_INVERTERS;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onMqtt(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("broker")] = String(mConfig->mqtt.broker);
|
||||
root[F("port")] = String(mConfig->mqtt.port);
|
||||
root[F("user")] = String(mConfig->mqtt.user);
|
||||
root[F("pwd")] = (strlen(mConfig->mqtt.pwd) > 0) ? F("{PWD}") : String("");
|
||||
root[F("topic")] = String(mConfig->mqtt.topic);
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getMqtt(JsonObject obj) {
|
||||
obj[F("broker")] = String(mConfig->mqtt.broker);
|
||||
obj[F("port")] = String(mConfig->mqtt.port);
|
||||
obj[F("user")] = String(mConfig->mqtt.user);
|
||||
obj[F("pwd")] = (strlen(mConfig->mqtt.pwd) > 0) ? F("{PWD}") : String("");
|
||||
obj[F("topic")] = String(mConfig->mqtt.topic);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onNtp(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("addr")] = String(mConfig->ntpAddr);
|
||||
root[F("port")] = String(mConfig->ntpPort);
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getNtp(JsonObject obj) {
|
||||
obj[F("addr")] = String(mConfig->ntpAddr);
|
||||
obj[F("port")] = String(mConfig->ntpPort);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onPinout(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("cs")] = mConfig->pinCs;
|
||||
root[F("ce")] = mConfig->pinCe;
|
||||
root[F("irq")] = mConfig->pinIrq;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getPinout(JsonObject obj) {
|
||||
obj[F("cs")] = mConfig->pinCs;
|
||||
obj[F("ce")] = mConfig->pinCe;
|
||||
obj[F("irq")] = mConfig->pinIrq;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onRadio(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("power_level")] = mConfig->amplifierPower;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getRadio(JsonObject obj) {
|
||||
obj[F("power_level")] = mConfig->amplifierPower;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::onSerial(AsyncWebServerRequest *request) {
|
||||
AsyncJsonResponse* response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root[F("interval")] = (uint16_t)mConfig->serialInterval;
|
||||
root[F("show_live_data")] = mConfig->serialShowIv;
|
||||
root[F("debug")] = mConfig->serialDebug;
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
void webApi::getSerial(JsonObject obj) {
|
||||
obj[F("interval")] = (uint16_t)mConfig->serialInterval;
|
||||
obj[F("show_live_data")] = mConfig->serialShowIv;
|
||||
obj[F("debug")] = mConfig->serialDebug;
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void webApi::getSetup(JsonObject obj) {
|
||||
getSystem(obj.createNestedObject("system"));
|
||||
getInverterList(obj.createNestedObject("inverter"));
|
||||
getMqtt(obj.createNestedObject("mqtt"));
|
||||
getNtp(obj.createNestedObject("ntp"));
|
||||
getPinout(obj.createNestedObject("pinout"));
|
||||
getRadio(obj.createNestedObject("radio"));
|
||||
getSerial(obj.createNestedObject("serial"));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue