implement getJsonConfig command

This commit is contained in:
lorow 2022-11-11 00:43:55 +01:00
parent 326ec8e4cd
commit a223039a5a
3 changed files with 47 additions and 9 deletions

View File

@ -297,16 +297,27 @@ void ProjectConfig::setAPWifiConfig(const std::string &ssid, const std::string &
std::string ProjectConfig::DeviceConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"device_config: {\"OTAPassword\": \"%s\", \"OTAPort\": %u}",
"\"device_config\": {\"OTAPassword\": \"%s\", \"OTAPort\": %u}",
this->OTAPassword.c_str(),
this->OTAPort);
this->OTAPort
);
return json;
}
std::string ProjectConfig::MDNSConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}",
this->hostname.c_str(),
this->service.c_str()
);
return json;
}
std::string ProjectConfig::CameraConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"camera_config: {\"vflip\": %d,\"framesize\": %d,\"href\": %d,\"quality\": %d,\"brightness\": %d}",
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": %d,\"quality\": %d,\"brightness\": %d}",
this->vflip,
this->framesize,
this->href,
@ -318,7 +329,7 @@ std::string ProjectConfig::CameraConfig_t::toRepresentation()
std::string ProjectConfig::WiFiConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"wifi_config: {\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"adhoc\": %s}",
"{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"adhoc\": %s}",
this->name.c_str(),
this->ssid.c_str(),
this->password.c_str(),
@ -330,7 +341,7 @@ std::string ProjectConfig::WiFiConfig_t::toRepresentation()
std::string ProjectConfig::AP_WiFiConfig_t::toRepresentation()
{
std::string json = Helpers::format_string(
"ap_wifi_config: {\"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"adhoc\": %s}",
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", \"channel\": %u, \"adhoc\": %s}",
this->ssid.c_str(),
this->password.c_str(),
this->channel,

View File

@ -125,9 +125,36 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
void BaseAPI::getJsonConfig(AsyncWebServerRequest *request)
{
// go through the config and build the response uisng toRepresentation methods
// consider moving handling the data to fromRepresentation
// returns the current stored config in case it get's deleted on the PC.
switch (_networkMethodsMap_enum[request->method()])
{
case GET:
{
std::string wifiConfigSerialized ="\"wifi_config\": [";
auto networksConfigs = projectConfig->getWifiConfigs();
for(auto networkIterator = networksConfigs->begin(); networkIterator != networksConfigs->end(); networkIterator++)
{
wifiConfigSerialized += networkIterator->toRepresentation() + (std::next(networkIterator) != networksConfigs->end() ? "," : "");
}
wifiConfigSerialized += "]";
std::string json = Helpers::format_string(
"{%s, %s, %s, %s, %s}",
projectConfig->getDeviceConfig()->toRepresentation().c_str(),
projectConfig->getCameraConfig()->toRepresentation().c_str(),
wifiConfigSerialized.c_str(),
projectConfig->getMDNSConfig()->toRepresentation().c_str(),
projectConfig->getAPWifiConfig()->toRepresentation().c_str()
);
request->send(200, MIMETYPE_JSON, json.c_str());
break;
}
default:
{
request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Request\"}");
break;
}
}
}
void BaseAPI::setDeviceConfig(AsyncWebServerRequest *request)

View File

@ -1 +1 @@
300
406