|  | @ -9,7 +9,6 @@ | 
			
		
	
		
		
			
				
					|  |  | #endif |  |  | #endif | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | #include "webApi.h" |  |  | #include "webApi.h" | 
			
		
	
		
		
			
				
					|  |  | #include "AsyncJson.h" |  |  |  | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					|  |  | webApi::webApi(AsyncWebServer *srv, app *app, sysConfig_t *sysCfg, config_t *config, statistics_t *stat, char version[]) { |  |  | 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) { |  |  | void webApi::setup(void) { | 
			
		
	
		
		
			
				
					
					|  |  |     mSrv->on("/api/system",        HTTP_GET, std::bind(&webApi::onSystem,       this, std::placeholders::_1)); |  |  |     mSrv->on("/api", HTTP_GET, std::bind(&webApi::onApi, 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)); |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					|  |  | void webApi::loop(void) { |  |  | void webApi::loop(void) { | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onSystem(AsyncWebServerRequest *request) { |  |  | void webApi::onApi(AsyncWebServerRequest *request) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(false, 2048); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  |     JsonObject root = response->getRoot(); |  |  |     JsonObject root = response->getRoot(); | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |     root[F("ssid")]        = mSysCfg->stationSsid; |  |  |     String path = request->url().substring(5); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("device_name")] = mSysCfg->deviceName; |  |  |     if(path == "system")             getSystem(root); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("version")]     = String(mVersion); |  |  |     else if(path == "statistics")    getStatistics(root); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("build")]       = String(AUTO_GIT_HASH); |  |  |     else if(path == "inverter/list") getInverterList(root); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("ts_uptime")]   = mApp->getUptime(); |  |  |     else if(path == "mqtt")          getMqtt(root); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("ts_now")]      = mApp->getTimestamp(); |  |  |     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->setLength(); | 
			
		
	
		
		
			
				
					|  |  |     //response->addHeader("Access-Control-Allow-Origin", "*");
 |  |  |     //response->addHeader("Access-Control-Allow-Origin", "*");
 | 
			
		
	
	
		
		
			
				
					|  | @ -61,25 +58,28 @@ void webApi::onSystem(AsyncWebServerRequest *request) { | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onStatistics(AsyncWebServerRequest *request) { |  |  | void webApi::getSystem(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("ssid")]        = mSysCfg->stationSsid; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     JsonObject root = response->getRoot(); |  |  |     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(); | 
			
		
	
		
		
			
				
					|  |  |  |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     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::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::onInverterList(AsyncWebServerRequest *request) { |  |  | void webApi::getInverterList(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     JsonArray invArr = obj.createNestedArray("inverter"); | 
			
				
				
			
		
	
		
		
			
				
					|  |  |     JsonObject root = response->getRoot(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     JsonArray invArr = root.createNestedArray("inverter"); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     Inverter<> *iv; |  |  |     Inverter<> *iv; | 
			
		
	
		
		
			
				
					|  |  |     for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { |  |  |     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]; |  |  |             obj[F("power_limit_option")] = iv->powerLimit[1]; | 
			
		
	
		
		
			
				
					|  |  |         } |  |  |         } | 
			
		
	
		
		
			
				
					|  |  |     } |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |     root[F("interval")]          = String(mConfig->sendInterval); |  |  |     obj[F("interval")]          = String(mConfig->sendInterval); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("retries")]           = String(mConfig->maxRetransPerPyld); |  |  |     obj[F("retries")]           = String(mConfig->maxRetransPerPyld); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("max_num_inverters")] = MAX_NUM_INVERTERS; |  |  |     obj[F("max_num_inverters")] = MAX_NUM_INVERTERS; | 
			
				
				
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     response->setLength(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     request->send(response); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onMqtt(AsyncWebServerRequest *request) { |  |  | void webApi::getMqtt(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("broker")] = String(mConfig->mqtt.broker); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     JsonObject root = response->getRoot(); |  |  |     obj[F("port")]   = String(mConfig->mqtt.port); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  | 
 |  |  |     obj[F("user")]   = String(mConfig->mqtt.user); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("broker")] = String(mConfig->mqtt.broker); |  |  |     obj[F("pwd")]    = (strlen(mConfig->mqtt.pwd) > 0) ? F("{PWD}") : String(""); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     root[F("port")]   = String(mConfig->mqtt.port); |  |  |     obj[F("topic")]  = String(mConfig->mqtt.topic); | 
			
				
				
			
		
	
		
		
			
				
					|  |  |     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::onNtp(AsyncWebServerRequest *request) { |  |  | void webApi::getNtp(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("addr")] = String(mConfig->ntpAddr); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     JsonObject root = response->getRoot(); |  |  |     obj[F("port")] = String(mConfig->ntpPort); | 
			
				
				
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     root[F("addr")] = String(mConfig->ntpAddr); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     root[F("port")] = String(mConfig->ntpPort); |  |  |  | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     response->setLength(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     request->send(response); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onPinout(AsyncWebServerRequest *request) { |  |  | void webApi::getPinout(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("cs")]  = mConfig->pinCs; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     JsonObject root = response->getRoot(); |  |  |     obj[F("ce")]  = mConfig->pinCe; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  | 
 |  |  |     obj[F("irq")] = mConfig->pinIrq; | 
			
				
				
			
		
	
		
		
			
				
					|  |  |     root[F("cs")]  = mConfig->pinCs; |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     root[F("ce")]  = mConfig->pinCe; |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     root[F("irq")] = mConfig->pinIrq; |  |  |  | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     response->setLength(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     request->send(response); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onRadio(AsyncWebServerRequest *request) { |  |  | void webApi::getRadio(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("power_level")] = mConfig->amplifierPower; | 
			
				
				
			
		
	
		
		
			
				
					|  |  |     JsonObject root = response->getRoot(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     root[F("power_level")] = mConfig->amplifierPower; |  |  |  | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     response->setLength(); |  |  |  | 
			
		
	
		
		
			
				
					|  |  |     request->send(response); |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  |  | 
			
		
	
		
		
			
				
					|  |  | //-----------------------------------------------------------------------------
 |  |  | //-----------------------------------------------------------------------------
 | 
			
		
	
		
		
			
				
					
					|  |  | void webApi::onSerial(AsyncWebServerRequest *request) { |  |  | void webApi::getSerial(JsonObject obj) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     AsyncJsonResponse* response = new AsyncJsonResponse(); |  |  |     obj[F("interval")]       = (uint16_t)mConfig->serialInterval; | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |     JsonObject root = response->getRoot(); |  |  |     obj[F("show_live_data")] = mConfig->serialShowIv; | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					|  |  |  |  |  |     obj[F("debug")]          = mConfig->serialDebug; | 
			
		
	
		
		
			
				
					|  |  |  |  |  | } | 
			
		
	
		
		
			
				
					|  |  | 
 |  |  | 
 | 
			
		
	
		
		
			
				
					|  |  |     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::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")); | 
			
		
	
		
		
			
				
					|  |  | } |  |  | } | 
			
		
	
	
		
		
			
				
					|  | 
 |