Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export itho settings as json file #181

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
11 changes: 6 additions & 5 deletions software/NRG_itho_wifi/main/IthoSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,20 @@ void getSetting(const uint8_t index, const bool updateState, const bool updatewe
}
}

void processSettingResult(const uint8_t index, const bool loop)
const char * getIthoDescription(const uint8_t index)
{

const uint8_t version = currentItho_fwversion();

const struct ihtoDeviceType *settingsPtr = ithoDeviceptr;
return settingsPtr->settingsDescriptions[static_cast<int>(*(*(settingsPtr->settingsMapping + version) + index))];
}

void processSettingResult(const uint8_t index, const bool loop)
{
StaticJsonDocument<512> doc;
JsonObject root = doc.to<JsonObject>();

root["Index"] = index;
root["Description"] = settingsPtr->settingsDescriptions[static_cast<int>(*(*(settingsPtr->settingsMapping + version) + index))];

root["Description"] = getIthoDescription(index);
auto timeoutmillis = millis() + 3000; // 1 sec. + 2 sec. for potential i2c queue pause on CVE devices
while (resultPtr2410 == nullptr && millis() < timeoutmillis)
{
Expand Down
1 change: 1 addition & 0 deletions software/NRG_itho_wifi/main/IthoSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ uint16_t currentIthoSettingsLength();
int16_t currentIthoStatusLabelLength();
int getSettingsLength(const uint8_t deviceGroup, const uint8_t deviceID, const uint8_t version);
void getSetting(const uint8_t i, const bool updateState, const bool updateweb, const bool loop = false);
const char * getIthoDescription(const uint8_t index);
void processSettingResult(const uint8_t index, const bool loop);
int getStatusLabelLength(const uint8_t deviceGroup, const uint8_t deviceID, const uint8_t version);
const char *getSatusLabel(const uint8_t i, const struct ihtoDeviceType *statusPtr);
Expand Down
39 changes: 39 additions & 0 deletions software/NRG_itho_wifi/main/generic_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,45 @@ void getIthoSettingsBackupJSON(JsonObject root)
}
}

void getIthoSettingsBackupJSONPlus(JsonArray sumJson) //include description in JSON Array
{
if (ithoSettingsArray != nullptr)
{
for (uint16_t i = 0; i < currentIthoSettingsLength(); i++)
{
StaticJsonDocument<256> root;
root["Index"] = i;
root["Description"] = getIthoDescription(i);

if (ithoSettingsArray[i].type == ithoSettings::is_int && ithoSettingsArray[i].length == 1 && ithoSettingsArray[i].is_signed)
{
int8_t val;
std::memcpy(&val, &ithoSettingsArray[i].value, sizeof(val));
root["Current"] = val;
}
else if (ithoSettingsArray[i].type == ithoSettings::is_int && ithoSettingsArray[i].length == 2 && ithoSettingsArray[i].is_signed)
{
int16_t val;
std::memcpy(&val, &ithoSettingsArray[i].value, sizeof(val));
root["Current"] = val;
}
else if (ithoSettingsArray[i].type == ithoSettings::is_int && ithoSettingsArray[i].length == 4 && ithoSettingsArray[i].is_signed)
{
root["Current"] = ithoSettingsArray[i].value;
}
else
{
uint32_t val;
std::memcpy(&val, &ithoSettingsArray[i].value, sizeof(val));
root["Current"] = val;
}
sumJson.add(root); // append the JsonObject at the end of the JsonArray
root.clear(); // clear the JsonObject at the end of each iteration
}
}
}


bool ithoExecCommand(const char *command, cmdOrigin origin)
{
D_LOG("EXEC COMMAND:%s", command);
Expand Down
1 change: 1 addition & 0 deletions software/NRG_itho_wifi/main/generic_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const char *hostName();
void getIthoStatusJSON(JsonObject root);
void getRemotesInfoJSON(JsonObject root);
void getIthoSettingsBackupJSON(JsonObject root);
void getIthoSettingsBackupJSONPlus(JsonArray sumJson);
bool ithoExecCommand(const char *command, cmdOrigin origin);
bool ithoExecRFCommand(uint8_t remote_index, const char *command, cmdOrigin origin);
bool ithoSetSpeed(const char *speed, cmdOrigin origin);
Expand Down
38 changes: 38 additions & 0 deletions software/NRG_itho_wifi/main/tasks/task_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ void webServerInit()

// server.on("/crash", HTTP_GET, handleCoreCrash);
server.on("/getcoredump", HTTP_GET, handleCoredumpDownload);

server.on("/getithosettings", HTTP_GET, handleIthosettingsDownload); // Download IthoSettings.json

// css_code
server.on("/pure-min.css", HTTP_GET, [](AsyncWebServerRequest *request)
Expand Down Expand Up @@ -866,6 +868,42 @@ void handlePrevLogDownload(AsyncWebServerRequest *request)
request->send(ACTIVE_FS, link, "", true);
}

/*
handleIthosettingsDownload() is called from server.on("/getithosettings",..
The string /getithosettings is generated in html_ithosettings.html when the button 'Download IthoSettings.json' is pressed
The Json array sumJson is filled in getIthoSettingsBackupJSONPlus.cpp (in generic_functions.cpp).
The array sumJson is serialized and sent to file IthoSettings.json, that then is offered for download.
*/
void handleIthosettingsDownload(AsyncWebServerRequest *request)
{
// Define JSON array to accumulate all Itho settings (in function getIthoSettingsBackupJSONPlus)
DynamicJsonDocument content(110*currentIthoSettingsLength()); // e.g. 10000 is sufficient for 100 indexes, 13 kB file
JsonArray sumJson = content.to<JsonArray>(); // Create Json array
File file = ACTIVE_FS.open("/IthoSettings.json", "w+"); // Create empty file
if (!file)
{
E_LOG("Failed to create IthoSettings.json file for writing");
return;
}
getIthoSettingsBackupJSONPlus(sumJson);
size_t len = measureJsonPretty(sumJson);
// D_LOG("sumJson length %d, memory %d", len, sumJson.memoryUsage());
std::unique_ptr<char[]> buffer(new char[len]); // Ensure buffer memory if released after the end of this function
if (buffer)
{
serializeJsonPretty(sumJson, buffer.get(), len);
file.write(reinterpret_cast<const uint8_t*>(buffer.get()), len);
}
file.close(); // Close the file

char link[24]{};
if (ACTIVE_FS.exists("/IthoSettings.json"))
{
strlcpy(link, "/IthoSettings.json", sizeof(link));
request->send(ACTIVE_FS, link, "", true);
}
}

void handleFileCreate(AsyncWebServerRequest *request)
{
if (systemConfig.syssec_edit)
Expand Down
2 changes: 2 additions & 0 deletions software/NRG_itho_wifi/main/tasks/task_web.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void handleAPI(AsyncWebServerRequest *request);
void handleCoredumpDownload(AsyncWebServerRequest *request);
void handleCurLogDownload(AsyncWebServerRequest *request);
void handlePrevLogDownload(AsyncWebServerRequest *request);
void handleIthosettingsDownload(AsyncWebServerRequest *request);
void handleFileCreate(AsyncWebServerRequest *request);
void handleFileDelete(AsyncWebServerRequest *request);
void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
Expand All @@ -70,3 +71,4 @@ bool mg_handleFileRead(struct mg_connection *c, void *ev_data, bool download);
void mg_handleStatus(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
void mg_handleFileDelete(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
void mg_handleFileCreate(struct mg_connection *c, int ev, void *ev_data, void *fn_data);
void getIthoSettingsBackupJSONPlus(JsonArray sumJson);
2 changes: 1 addition & 1 deletion software/NRG_itho_wifi/main/version.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define FWVERSION "2.5.6"
#define FWVERSION "2.5.6-settingsjson"
Loading