Files
IoTManager/src/UpgradeFirm.cpp

136 lines
4.2 KiB
C++
Raw Normal View History

2020-10-18 00:07:57 +03:00
#include "Upgrade.h"
2020-09-17 21:33:54 +03:00
2020-12-20 01:24:08 +03:00
#include "FileSystem.h"
2020-10-18 09:52:17 +03:00
#include "Class/NotAsync.h"
2020-10-18 00:07:57 +03:00
#ifdef ESP8266
2020-09-17 21:33:54 +03:00
#include "ESP8266.h"
2020-10-18 00:07:57 +03:00
#else
#include <HTTPUpdate.h>
#endif
2020-09-17 21:33:54 +03:00
#include "Global.h"
void upgradeInit() {
2020-10-18 09:52:17 +03:00
myNotAsyncActions->add(
2020-09-17 21:33:54 +03:00
do_UPGRADE, [&](void*) {
upgrade_firmware(3);
},
nullptr);
2020-10-18 09:52:17 +03:00
myNotAsyncActions->add(
2020-09-17 21:33:54 +03:00
do_GETLASTVERSION, [&](void*) {
getLastVersion();
},
nullptr);
if (isNetworkActive()) {
getLastVersion();
if (lastVersion > 0) {
2020-10-20 22:55:45 +03:00
SerialPrint("I", "Update", "available version: " + String(lastVersion));
2020-11-17 01:01:42 +03:00
if (lastVersion > FIRMWARE_VERSION) {
jsonWriteStr(configSetupJson, "warning2", F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Вышла новая версия прошивки, нажмите <b>обновить прошивку</b></p></font></div>"));
}
2020-09-17 21:33:54 +03:00
}
};
2020-12-10 19:12:15 +03:00
SerialPrint("I", F("Update"), F("Updater Init"));
2020-09-17 21:33:54 +03:00
}
void getLastVersion() {
if ((WiFi.status() == WL_CONNECTED)) {
2020-11-17 01:01:42 +03:00
#ifdef ESP8266
String tmp = getURL(serverIP + F("/projects/iotmanager/esp8266/esp8266ver/esp8266ver.txt"));
#else
String tmp = getURL(serverIP + F("/projects/iotmanager/esp32/esp32ver/esp32ver.txt"));
#endif
2020-09-17 21:33:54 +03:00
if (tmp == "error") {
lastVersion = -1;
2020-12-09 04:08:36 +03:00
}
2020-11-17 01:01:42 +03:00
else {
2020-09-17 21:33:54 +03:00
lastVersion = tmp.toInt();
}
2020-12-09 04:08:36 +03:00
}
2020-11-17 01:01:42 +03:00
else {
2020-09-17 21:33:54 +03:00
lastVersion = -2;
}
jsonWriteInt(configSetupJson, "last_version", lastVersion);
}
void upgrade_firmware(int type) {
2020-10-02 01:37:40 +03:00
String scenario_ForUpdate;
String devconfig_ForUpdate;
String configSetup_ForUpdate;
2020-12-13 00:59:56 +03:00
scenario_ForUpdate = readFile(String(DEVICE_SCENARIO_FILE), 4096);
devconfig_ForUpdate = readFile(String(DEVICE_CONFIG_FILE), 4096);
2020-10-02 01:37:40 +03:00
configSetup_ForUpdate = configSetupJson;
2020-09-17 21:33:54 +03:00
if (type == 1) { //only build
if (upgradeBuild()) restartEsp();
2020-10-02 01:37:40 +03:00
}
else if (type == 2) { //only spiffs
if (upgradeFS()) {
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
writeFile("config.json", configSetup_ForUpdate);
restartEsp();
}
}
else if (type == 3) { //spiffs and build
2020-09-17 21:33:54 +03:00
if (upgradeFS()) {
2020-10-02 01:37:40 +03:00
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
writeFile("config.json", configSetup_ForUpdate);
saveConfig();
2020-09-17 21:33:54 +03:00
if (upgradeBuild()) {
restartEsp();
}
}
}
}
bool upgradeFS() {
WiFiClient wifiClient;
bool ret = false;
2020-12-20 01:24:08 +03:00
Serial.printf("Start upgrade %s, please wait...", FS_NAME);
2020-10-18 00:07:57 +03:00
#ifdef ESP8266
2020-09-17 21:33:54 +03:00
ESPhttpUpdate.rebootOnUpdate(false);
2020-12-20 01:24:08 +03:00
t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp8266/") + FS_NAME + "/"+ FS_NAME+ ".bin");
2020-10-18 00:07:57 +03:00
#else
httpUpdate.rebootOnUpdate(false);
2020-11-17 01:01:42 +03:00
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp32/littlefs/spiffs.bin"));
2020-10-18 00:07:57 +03:00
#endif
2020-09-17 21:33:54 +03:00
if (retFS == HTTP_UPDATE_OK) { //если FS обновилась успешно
2020-12-20 01:24:08 +03:00
SerialPrint("I", "Update", "FS upgrade done!");
2020-09-17 21:33:54 +03:00
ret = true;
2020-12-09 04:08:36 +03:00
}
2020-09-17 21:33:54 +03:00
return ret;
}
bool upgradeBuild() {
WiFiClient wifiClient;
bool ret = false;
Serial.println("Start upgrade BUILD, please wait...");
2020-10-18 00:07:57 +03:00
#ifdef ESP8266
2020-09-17 21:33:54 +03:00
ESPhttpUpdate.rebootOnUpdate(false);
2020-11-17 01:01:42 +03:00
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp8266/firmware/firmware.bin"));
2020-10-18 00:07:57 +03:00
#else
httpUpdate.rebootOnUpdate(false);
2020-11-17 01:01:42 +03:00
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp32/firmware/firmware.bin"));
2020-10-18 00:07:57 +03:00
#endif
2020-09-17 21:33:54 +03:00
if (retBuild == HTTP_UPDATE_OK) { //если BUILD обновился успешно
SerialPrint("I", "Update", "BUILD upgrade done!");
ret = true;
2020-12-09 04:08:36 +03:00
}
2020-09-17 21:33:54 +03:00
return ret;
}
void restartEsp() {
Serial.println("Restart ESP....");
delay(1000);
ESP.restart();
2020-10-01 20:38:46 +03:00
}