2023-11-10 22:18:14 +00:00
|
|
|
#include "ota.hpp"
|
|
|
|
|
|
|
|
TaskHandle_t taskOtaHandle = NULL;
|
2024-06-29 00:19:25 +00:00
|
|
|
bool isOtaUpdating = false;
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void setupOTA() {
|
2024-07-11 12:08:37 +00:00
|
|
|
if (preferences.getBool("otaEnabled", DEFAULT_OTA_ENABLED)) {
|
2023-11-10 22:18:14 +00:00
|
|
|
ArduinoOTA.onStart(onOTAStart);
|
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
ArduinoOTA.onProgress(onOTAProgress);
|
2023-11-13 00:02:01 +00:00
|
|
|
ArduinoOTA.onError(onOTAError);
|
2023-11-12 23:33:48 +00:00
|
|
|
ArduinoOTA.onEnd(onOTAComplete);
|
2023-11-10 22:18:14 +00:00
|
|
|
|
|
|
|
ArduinoOTA.setHostname(getMyHostname().c_str());
|
|
|
|
ArduinoOTA.setMdnsEnabled(false);
|
2023-11-12 23:33:48 +00:00
|
|
|
ArduinoOTA.setRebootOnSuccess(false);
|
2023-11-10 22:18:14 +00:00
|
|
|
ArduinoOTA.begin();
|
2023-11-30 21:38:01 +00:00
|
|
|
// downloadUpdate();
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
xTaskCreate(handleOTATask, "handleOTA", 4096, NULL, tskIDLE_PRIORITY,
|
|
|
|
&taskOtaHandle);
|
2023-11-12 23:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void onOTAProgress(unsigned int progress, unsigned int total) {
|
2023-11-12 23:33:48 +00:00
|
|
|
uint percentage = progress / (total / 100);
|
|
|
|
pixels.fill(pixels.Color(0, 255, 0));
|
2023-11-30 21:38:01 +00:00
|
|
|
if (percentage < 100) {
|
2023-11-12 23:33:48 +00:00
|
|
|
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
|
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
if (percentage < 75) {
|
2023-11-12 23:33:48 +00:00
|
|
|
pixels.setPixelColor(1, pixels.Color(0, 0, 0));
|
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
if (percentage < 50) {
|
2023-11-12 23:33:48 +00:00
|
|
|
pixels.setPixelColor(2, pixels.Color(0, 0, 0));
|
|
|
|
}
|
2023-11-30 21:38:01 +00:00
|
|
|
if (percentage < 25) {
|
2023-11-12 23:33:48 +00:00
|
|
|
pixels.setPixelColor(3, pixels.Color(0, 0, 0));
|
|
|
|
}
|
|
|
|
pixels.show();
|
2023-11-10 22:18:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void onOTAStart() {
|
2023-11-12 23:33:48 +00:00
|
|
|
forceFullRefresh();
|
2023-11-30 21:38:01 +00:00
|
|
|
std::array<String, NUM_SCREENS> epdContent = {"U", "P", "D", "A",
|
|
|
|
"T", "E", "!"};
|
2023-11-12 23:33:48 +00:00
|
|
|
setEpdContent(epdContent);
|
|
|
|
// Stop all timers
|
|
|
|
esp_timer_stop(screenRotateTimer);
|
|
|
|
esp_timer_stop(minuteTimer);
|
2024-06-29 00:19:25 +00:00
|
|
|
isOtaUpdating = true;
|
2023-11-12 23:33:48 +00:00
|
|
|
// Stop or suspend all tasks
|
2023-11-12 11:38:28 +00:00
|
|
|
// vTaskSuspend(priceUpdateTaskHandle);
|
2023-11-12 23:33:48 +00:00
|
|
|
// vTaskSuspend(blockUpdateTaskHandle);
|
|
|
|
vTaskSuspend(workerTaskHandle);
|
|
|
|
vTaskSuspend(taskScreenRotateTaskHandle);
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
vTaskSuspend(ledTaskHandle);
|
|
|
|
vTaskSuspend(buttonTaskHandle);
|
2023-11-10 22:18:14 +00:00
|
|
|
|
2023-11-12 23:33:48 +00:00
|
|
|
stopWebServer();
|
|
|
|
stopBlockNotify();
|
|
|
|
stopPriceNotify();
|
2023-11-10 22:18:14 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void handleOTATask(void *parameter) {
|
|
|
|
for (;;) {
|
2023-11-30 21:56:50 +00:00
|
|
|
ArduinoOTA.handle(); // Allow OTA updates to occur
|
2024-06-29 00:19:25 +00:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
2023-11-10 22:18:14 +00:00
|
|
|
}
|
2023-11-12 11:38:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void downloadUpdate() {
|
2023-11-13 19:02:58 +00:00
|
|
|
WiFiClientSecure client;
|
|
|
|
client.setInsecure();
|
|
|
|
HTTPClient http;
|
|
|
|
http.setUserAgent(USER_AGENT);
|
|
|
|
|
|
|
|
// Send HTTP request to CoinGecko API
|
|
|
|
http.useHTTP10(true);
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
http.begin(client,
|
|
|
|
"https://api.github.com/repos/btclock/btclock_v3/releases/latest");
|
2023-11-13 19:02:58 +00:00
|
|
|
int httpCode = http.GET();
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
if (httpCode == 200) {
|
|
|
|
// WiFiClient * stream = http->getStreamPtr();
|
2023-11-13 19:02:58 +00:00
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument filter;
|
2023-11-13 19:02:58 +00:00
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonObject filter_assets_0 = filter["assets"].add<JsonObject>();
|
2023-11-13 19:02:58 +00:00
|
|
|
filter_assets_0["name"] = true;
|
|
|
|
filter_assets_0["browser_download_url"] = true;
|
|
|
|
|
2024-03-10 19:24:55 +00:00
|
|
|
JsonDocument doc;
|
2023-11-13 19:02:58 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
DeserializationError error = deserializeJson(
|
|
|
|
doc, http.getStream(), DeserializationOption::Filter(filter));
|
2023-11-13 19:02:58 +00:00
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
if (error) {
|
2023-11-13 19:02:58 +00:00
|
|
|
Serial.print("deserializeJson() failed: ");
|
|
|
|
Serial.println(error.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String downloadUrl;
|
2023-11-30 21:38:01 +00:00
|
|
|
for (JsonObject asset : doc["assets"].as<JsonArray>()) {
|
2023-11-13 19:02:58 +00:00
|
|
|
if (asset["name"].as<String>().compareTo("firmware.bin") == 0) {
|
|
|
|
downloadUrl = asset["browser_download_url"].as<String>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.printf("Download update from %s", downloadUrl);
|
|
|
|
|
|
|
|
// esp_http_client_config_t config = {
|
|
|
|
// .url = CONFIG_FIRMWARE_UPGRADE_URL,
|
|
|
|
// };
|
|
|
|
// esp_https_ota_config_t ota_config = {
|
|
|
|
// .http_config = &config,
|
|
|
|
// };
|
|
|
|
// esp_err_t ret = esp_https_ota(&ota_config);
|
|
|
|
// if (ret == ESP_OK)
|
|
|
|
// {
|
|
|
|
// esp_restart();
|
|
|
|
// }
|
|
|
|
}
|
2023-11-12 23:33:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void onOTAError(ota_error_t error) {
|
2024-04-11 22:17:40 +00:00
|
|
|
Serial.println(F("\nOTA update error, restarting"));
|
2023-11-13 00:02:01 +00:00
|
|
|
Wire.end();
|
|
|
|
SPI.end();
|
2024-06-29 00:19:25 +00:00
|
|
|
isOtaUpdating = false;
|
2023-11-13 00:02:01 +00:00
|
|
|
delay(1000);
|
2023-11-13 19:02:58 +00:00
|
|
|
ESP.restart();
|
2023-11-13 00:02:01 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 21:38:01 +00:00
|
|
|
void onOTAComplete() {
|
2024-04-11 22:17:40 +00:00
|
|
|
Serial.println(F("\nOTA update finished"));
|
2023-11-12 23:33:48 +00:00
|
|
|
Wire.end();
|
|
|
|
SPI.end();
|
|
|
|
delay(1000);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
2024-06-29 00:19:25 +00:00
|
|
|
|
|
|
|
bool getIsOTAUpdating() {
|
|
|
|
return isOtaUpdating;
|
|
|
|
}
|