mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-12 00:16:42 +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
|
@ -261,13 +261,17 @@
|
|||
document.getElementsByName("serIntvl")[0].value = obj["interval"];
|
||||
}
|
||||
|
||||
getAjax("/api/system", parseSys);
|
||||
getAjax("/api/inverter/list", parseIv);
|
||||
getAjax("/api/mqtt", parseMqtt);
|
||||
getAjax("/api/ntp", parseNtp);
|
||||
getAjax("/api/pinout", parsePinout);
|
||||
getAjax("/api/radio", parseRadio);
|
||||
getAjax("/api/serial", parseSerial);
|
||||
function parse(root) {
|
||||
parseSys(root["system"]);
|
||||
parseIv(root["inverter"]);
|
||||
parseMqtt(root["mqtt"]);
|
||||
parseNtp(root["ntp"]);
|
||||
parsePinout(root["pinout"]);
|
||||
parseRadio(root["radio"]);
|
||||
parseSerial(root["serial"]);
|
||||
}
|
||||
|
||||
getAjax("/api/setup", parse);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -55,7 +55,7 @@ void web::setup(void) {
|
|||
mWeb->on("/visualization", HTTP_ANY, std::bind(&web::showVisualization, this, std::placeholders::_1));
|
||||
mWeb->on("/livedata", HTTP_ANY, std::bind(&web::showLiveData, this, std::placeholders::_1));
|
||||
mWeb->on("/json", HTTP_ANY, std::bind(&web::showJson, this, std::placeholders::_1));
|
||||
mWeb->on("/api", HTTP_POST, std::bind(&web::showWebApi, this, std::placeholders::_1));
|
||||
mWeb->on("/api1", HTTP_POST, std::bind(&web::showWebApi, this, std::placeholders::_1));
|
||||
|
||||
|
||||
mWeb->on("/update", HTTP_GET, std::bind(&web::showUpdateForm, this, std::placeholders::_1));
|
||||
|
@ -288,7 +288,7 @@ void web::showSave(AsyncWebServerRequest *request) {
|
|||
|
||||
// mqtt
|
||||
if(request->arg("mqttAddr") != "") {
|
||||
String addr = mWeb->arg("mqttAddr");
|
||||
String addr = request->arg("mqttAddr");
|
||||
addr.trim();
|
||||
addr.toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN);
|
||||
request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN);
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "dbg.h"
|
||||
#include "ESPAsyncTCP.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include "AsyncJson.h"
|
||||
#include "app.h"
|
||||
|
||||
|
||||
|
@ -17,14 +18,18 @@ class webApi {
|
|||
void loop(void);
|
||||
|
||||
private:
|
||||
void onSystem(AsyncWebServerRequest *request);
|
||||
void onStatistics(AsyncWebServerRequest *request);
|
||||
void onInverterList(AsyncWebServerRequest *request);
|
||||
void onMqtt(AsyncWebServerRequest *request);
|
||||
void onNtp(AsyncWebServerRequest *request);
|
||||
void onPinout(AsyncWebServerRequest *request);
|
||||
void onRadio(AsyncWebServerRequest *request);
|
||||
void onSerial(AsyncWebServerRequest *request);
|
||||
void onApi(AsyncWebServerRequest *request);
|
||||
|
||||
void getSystem(JsonObject obj);
|
||||
void getStatistics(JsonObject obj);
|
||||
void getInverterList(JsonObject obj);
|
||||
void getMqtt(JsonObject obj);
|
||||
void getNtp(JsonObject obj);
|
||||
void getPinout(JsonObject obj);
|
||||
void getRadio(JsonObject obj);
|
||||
void getSerial(JsonObject obj);
|
||||
|
||||
void getSetup(JsonObject obj);
|
||||
|
||||
AsyncWebServer *mSrv;
|
||||
app *mApp;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue