2022-02-18 20:06:25 +01:00
|
|
|
|
#include "UpgradeFirm.h"
|
|
|
|
|
|
|
|
|
|
|
|
updateFirm update;
|
|
|
|
|
|
|
|
|
|
|
|
void upgrade_firmware(int type) {
|
|
|
|
|
|
putUserDataToRam();
|
|
|
|
|
|
|
|
|
|
|
|
// only build
|
|
|
|
|
|
if (type == 1) {
|
|
|
|
|
|
if (upgradeBuild()) {
|
|
|
|
|
|
saveUserDataToFlash();
|
|
|
|
|
|
restartEsp();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// only littlefs
|
|
|
|
|
|
else if (type == 2) {
|
|
|
|
|
|
if (upgradeFS()) {
|
|
|
|
|
|
saveUserDataToFlash();
|
|
|
|
|
|
restartEsp();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// littlefs and build
|
|
|
|
|
|
else if (type == 3) {
|
|
|
|
|
|
if (upgradeFS()) {
|
|
|
|
|
|
saveUserDataToFlash();
|
|
|
|
|
|
if (upgradeBuild()) {
|
|
|
|
|
|
restartEsp();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool upgradeFS() {
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
WiFiClient wifiClient;
|
2022-02-18 22:26:12 +01:00
|
|
|
|
SerialPrint("!!!", F("Update"), F("Start upgrade FS..."));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, UPDATE_FS_IN_PROGRESS);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
if (getBinPath("") == "error") {
|
|
|
|
|
|
SerialPrint("E", F("Update"), F("FS Path error"));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, PATH_ERROR);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
|
|
ESPhttpUpdate.rebootOnUpdate(false);
|
|
|
|
|
|
t_httpUpdate_return retFS = ESPhttpUpdate.updateFS(wifiClient, getBinPath("littlefs.bin"));
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
|
|
httpUpdate.rebootOnUpdate(false);
|
2023-06-19 18:04:00 +02:00
|
|
|
|
// обновляем little fs с помощью метода обновления spiffs
|
2022-09-19 01:41:15 +02:00
|
|
|
|
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, getBinPath("littlefs.bin"));
|
2022-02-18 20:06:25 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2023-06-19 18:04:00 +02:00
|
|
|
|
// если FS обновилась успешно
|
2022-02-18 20:06:25 +01:00
|
|
|
|
if (retFS == HTTP_UPDATE_OK) {
|
2022-02-18 22:26:12 +01:00
|
|
|
|
SerialPrint("!!!", F("Update"), F("FS upgrade done!"));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, UPDATE_FS_COMPLETED);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
ret = true;
|
2023-06-19 18:04:00 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
handleUpdateStatus(true, UPDATE_FS_FAILED);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
}
|
2023-06-19 18:04:00 +02:00
|
|
|
|
|
2022-02-18 20:06:25 +01:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool upgradeBuild() {
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
WiFiClient wifiClient;
|
2022-02-18 22:26:12 +01:00
|
|
|
|
SerialPrint("!!!", F("Update"), F("Start upgrade BUILD..."));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, UPDATE_BUILD_IN_PROGRESS);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
if (getBinPath("") == "error") {
|
|
|
|
|
|
SerialPrint("E", F("Update"), F("Build Path error"));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, PATH_ERROR);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2023-06-19 18:04:00 +02:00
|
|
|
|
#if defined(esp8266_4mb) || defined(esp8266_1mb) || defined(esp8266_1mb_ota) || defined(esp8266_2mb) || defined(esp8266_2mb_ota)
|
2022-02-18 20:06:25 +01:00
|
|
|
|
ESPhttpUpdate.rebootOnUpdate(false);
|
|
|
|
|
|
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, getBinPath("firmware.bin"));
|
|
|
|
|
|
#endif
|
2023-05-27 02:02:33 +03:00
|
|
|
|
#ifdef ESP32
|
2022-02-18 20:06:25 +01:00
|
|
|
|
httpUpdate.rebootOnUpdate(false);
|
|
|
|
|
|
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, getBinPath("firmware.bin"));
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2023-06-19 18:04:00 +02:00
|
|
|
|
// если BUILD обновился успешно
|
2022-02-18 20:06:25 +01:00
|
|
|
|
if (retBuild == HTTP_UPDATE_OK) {
|
2022-02-18 22:26:12 +01:00
|
|
|
|
SerialPrint("!!!", F("Update"), F("BUILD upgrade done!"));
|
2022-02-19 23:42:24 +01:00
|
|
|
|
handleUpdateStatus(true, UPDATE_BUILD_COMPLETED);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
ret = true;
|
2023-06-19 18:04:00 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
handleUpdateStatus(true, UPDATE_BUILD_FAILED);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void restartEsp() {
|
|
|
|
|
|
Serial.println(F("Restart ESP...."));
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
|
ESP.restart();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const String getBinPath(String file) {
|
|
|
|
|
|
String path = "error";
|
2022-02-18 22:16:38 +01:00
|
|
|
|
int targetVersion = 0;
|
|
|
|
|
|
String serverip;
|
|
|
|
|
|
if (jsonRead(errorsHeapJson, F("chver"), targetVersion)) {
|
|
|
|
|
|
if (targetVersion >= 400) {
|
|
|
|
|
|
if (jsonRead(settingsFlashJson, F("serverip"), serverip)) {
|
|
|
|
|
|
if (serverip != "") {
|
|
|
|
|
|
path = jsonReadStr(settingsFlashJson, F("serverip")) + "/iotm/" + String(FIRMWARE_NAME) + "/" + String(targetVersion) + "/" + file;
|
2022-02-18 20:06:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-02-18 22:16:38 +01:00
|
|
|
|
SerialPrint("i", F("Update"), "path: " + path);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void putUserDataToRam() {
|
|
|
|
|
|
update.configJson = readFile("config.json", 4096);
|
|
|
|
|
|
update.settingsFlashJson = readFile("settings.json", 4096);
|
2022-02-18 23:04:27 +01:00
|
|
|
|
update.layoutJson = readFile("layout.json", 4096);
|
2022-09-29 22:48:33 +02:00
|
|
|
|
update.scenarioTxt = readFile("scenario.txt", 4096);
|
2022-10-02 01:06:25 +02:00
|
|
|
|
update.chartsData = createDataBaseSting();
|
2022-02-18 20:06:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void saveUserDataToFlash() {
|
|
|
|
|
|
writeFile("/config.json", update.configJson);
|
|
|
|
|
|
writeFile("/settings.json", update.settingsFlashJson);
|
2022-02-18 23:04:27 +01:00
|
|
|
|
writeFile("/layout.json", update.layoutJson);
|
2022-09-29 22:48:33 +02:00
|
|
|
|
writeFile("/scenario.txt", update.scenarioTxt);
|
2022-10-02 01:06:25 +02:00
|
|
|
|
writeDataBaseSting(update.chartsData);
|
2022-02-19 23:42:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleUpdateStatus(bool send, int state) {
|
|
|
|
|
|
jsonWriteInt_(errorsHeapJson, F("upd"), state);
|
2023-06-19 18:04:00 +02:00
|
|
|
|
if (send) sendStringToWs("errors", errorsHeapJson, -1);
|
2022-02-18 20:06:25 +01:00
|
|
|
|
}
|