This commit is contained in:
@@ -9,9 +9,9 @@
|
|||||||
#include <ArduinoLog.h>
|
#include <ArduinoLog.h>
|
||||||
#include <HTTPUpdate.h>
|
#include <HTTPUpdate.h>
|
||||||
|
|
||||||
OTA::OTA(String server_url, String currentVersion, String currentDeviceConfiguration) {
|
OTA::OTA(String server_url, Version currentVersion, String currentDeviceConfiguration) {
|
||||||
_serverUrl = server_url;
|
_serverUrl = server_url;
|
||||||
_currentVersion = parseVersion(currentVersion.c_str());
|
_currentVersion = currentVersion;
|
||||||
_current_device_configuration = currentDeviceConfiguration;
|
_current_device_configuration = currentDeviceConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +59,13 @@ Firmware OTA::getLatestVersionOnServer() {
|
|||||||
return createErrorResponse("No valid configuration found in the JSON response");
|
return createErrorResponse("No valid configuration found in the JSON response");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Configuration latest = getLatestConfiguration(configs.data(), configs.size());
|
Configuration latest = getLatestConfiguration(configs.data(), configs.size());
|
||||||
|
|
||||||
|
if (!isVersionNewer(_currentVersion, latest.version)) {
|
||||||
|
Log.verbose("No newer version found. Server version: %d.%d.%d", latest.version.major, latest.version.minor, latest.version.patch);
|
||||||
|
}
|
||||||
|
|
||||||
return Firmware{
|
return Firmware{
|
||||||
latest.version,
|
latest.version,
|
||||||
latest.url,
|
latest.url,
|
||||||
|
|||||||
@@ -11,22 +11,18 @@
|
|||||||
|
|
||||||
class OTA {
|
class OTA {
|
||||||
public:
|
public:
|
||||||
OTA(String server_url, String currentVersion, String currentDeviceConfiguration);
|
OTA(String server_url, Version currentVersion, String currentDeviceConfiguration);
|
||||||
Firmware getLatestVersionOnServer();
|
Firmware getLatestVersionOnServer();
|
||||||
|
|
||||||
bool checkForUpdate();
|
bool checkForUpdate();
|
||||||
void run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error);
|
void run_ota_update(String url, std::function<void()> callback_started, std::function<void()> callback_finished, std::function<void(int, int)> callback_progress, std::function<void(int)> callback_error);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _isHTTPS = false;
|
bool _isHTTPS = false;
|
||||||
String _serverUrl;
|
String _serverUrl;
|
||||||
Version _currentVersion;
|
Version _currentVersion;
|
||||||
String _current_device_configuration;
|
String _current_device_configuration;
|
||||||
Firmware createErrorResponse(const String& errorMsg);
|
Firmware createErrorResponse(const String& errorMsg);
|
||||||
void update_started();
|
|
||||||
void update_finished();
|
|
||||||
void update_progress(int cur, int total);
|
|
||||||
void update_error(int err);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -30,4 +30,13 @@ Configuration getLatestConfiguration(Configuration *configs, int count) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return latest;
|
return latest;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isVersionNewer(Version oldVersion, Version newVersion) {
|
||||||
|
if (newVersion.major > oldVersion.major ||
|
||||||
|
(newVersion.major == oldVersion.major && newVersion.minor > oldVersion.minor) ||
|
||||||
|
(newVersion.major == oldVersion.major && newVersion.minor == oldVersion.minor && newVersion.patch > oldVersion.patch)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
@@ -28,4 +28,5 @@ struct Configuration {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Version parseVersion(const char *versionStr);
|
Version parseVersion(const char *versionStr);
|
||||||
Configuration getLatestConfiguration(Configuration *configs, int count);
|
Configuration getLatestConfiguration(Configuration *configs, int count);
|
||||||
|
bool isVersionNewer(Version oldVersion, Version newVersion);
|
||||||
@@ -7,6 +7,6 @@
|
|||||||
#define water_level_min_key "water_level_min"
|
#define water_level_min_key "water_level_min"
|
||||||
#define water_level_max_key "water_level_max"
|
#define water_level_max_key "water_level_max"
|
||||||
#define water_volume_key "water_volume"
|
#define water_volume_key "water_volume"
|
||||||
#define current_software_version Version{0, 0, 7}
|
#define current_software_version Version{0, 0, 8}
|
||||||
|
|
||||||
#define RESISTOR_VALUE 4
|
#define RESISTOR_VALUE 4
|
||||||
27
src/main.cpp
27
src/main.cpp
@@ -37,30 +37,16 @@ extern "C" int rom_phy_get_vdd33();
|
|||||||
|
|
||||||
AsyncWebServer server(80);
|
AsyncWebServer server(80);
|
||||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
#define FORMAT_LITTLEFS_IF_FAILED true
|
||||||
void update_started() {
|
|
||||||
Log.verbose("OTA Update started");
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_finished() {
|
|
||||||
Log.verbose("OTA Update finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_progress(int cur, int total) {
|
|
||||||
Log.verbose("OTA Update progress: %d/%d", cur, total);
|
|
||||||
}
|
|
||||||
|
|
||||||
void update_error(int err) {
|
|
||||||
Log.error("OTA Update error: %d", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void run_ota(void* parameter) {
|
void run_ota(void* parameter) {
|
||||||
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233");
|
// OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", "1.1.1", "INA233");
|
||||||
Firmware fw = ota.getLatestVersionOnServer();
|
// Firmware fw = ota.getLatestVersionOnServer();
|
||||||
Log.verbose("we are done");
|
// Log.verbose("we are done");
|
||||||
Serial.printf("Firmware Info: Valid: %d, Version: %d.%d.%d, URL: %s\n", fw.valid, fw.version.major, fw.version.minor, fw.version.patch, fw.url.c_str());
|
// Serial.printf("Firmware Info: Valid: %d, Version: %d.%d.%d, URL: %s\n", fw.valid, fw.version.major, fw.version.minor, fw.version.patch, fw.url.c_str());
|
||||||
Log.verbose("Error message: %s", fw.error.c_str());
|
// Log.verbose("Error message: %s", fw.error.c_str());
|
||||||
ota.run_ota_update(fw.url, update_started, update_finished, update_progress, update_error);
|
// ota.run_ota_update(fw.url, update_started, update_finished, update_progress, update_error);
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,6 +220,7 @@ void setup()
|
|||||||
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
xTaskCreate(display_task, "DisplayTask", 10000, NULL, 1, NULL);
|
||||||
xTaskCreate(read_sensor_task, "ReadSensorTask", 2048, NULL, 1, NULL);
|
xTaskCreate(read_sensor_task, "ReadSensorTask", 2048, NULL, 1, NULL);
|
||||||
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
xTaskCreate(collect_internal_telemetry_task, "InternalTelemetryTask", 2048, NULL, 1, NULL);
|
||||||
|
xTaskCreate(check_update_task, "CheckUpdateTask", 1024 * 8, NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
|
|||||||
@@ -3,8 +3,10 @@
|
|||||||
#include <ArduinoLog.h>
|
#include <ArduinoLog.h>
|
||||||
#include "../global_data/defines.h"
|
#include "../global_data/defines.h"
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
|
#include <fetchOTA.h>
|
||||||
|
|
||||||
extern Preferences prefs;
|
extern Preferences prefs;
|
||||||
|
extern OTAStatus ota_status;
|
||||||
|
|
||||||
void printSuffix(Print* _logOutput, int logLevel)
|
void printSuffix(Print* _logOutput, int logLevel)
|
||||||
{
|
{
|
||||||
@@ -37,4 +39,44 @@ String processor(const String& var)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return String("Test");
|
return String("Test");
|
||||||
|
}
|
||||||
|
|
||||||
|
// OTA Callbacks
|
||||||
|
void update_started() {
|
||||||
|
Log.verbose("OTA Update started");
|
||||||
|
ota_status.update_progress = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_finished() {
|
||||||
|
Log.verbose("OTA Update finished");
|
||||||
|
ota_status.update_progress = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_progress(int cur, int total) {
|
||||||
|
ota_status.update_progress = total/cur;
|
||||||
|
Log.verbose("OTA Update progress: %d/%d", cur, total);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_error(int err) {
|
||||||
|
Log.error("OTA Update error: %d", err);
|
||||||
|
ota_status.update_progress = -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_update_task(void* parameter) {
|
||||||
|
#ifdef USE_INA226
|
||||||
|
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA226");
|
||||||
|
#else
|
||||||
|
OTA ota("https://iot.tobiasmaier.me/firmware/waterlevel", current_software_version, "INA233");
|
||||||
|
#endif
|
||||||
|
while (true) {
|
||||||
|
Firmware fw = ota.getLatestVersionOnServer();
|
||||||
|
if (fw.valid) {
|
||||||
|
Log.verbose("New firmware available: %d.%d.%d, current versio: %d.%d.%d", fw.version.major, fw.version.minor, fw.version.patch, current_software_version.major, current_software_version.minor, current_software_version.patch);
|
||||||
|
ota_status.update_available = true;
|
||||||
|
ota_status.latest_version = fw.version;
|
||||||
|
} else {
|
||||||
|
Log.verbose("No new firmware available");
|
||||||
|
}
|
||||||
|
delay(1000 * 60 * 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -4,4 +4,5 @@
|
|||||||
void printSuffix(Print* _logOutput, int logLevel);
|
void printSuffix(Print* _logOutput, int logLevel);
|
||||||
void print_prefix(Print* _logOutput, int logLevel);
|
void print_prefix(Print* _logOutput, int logLevel);
|
||||||
bool is_error(ActiveErrors active_errors);
|
bool is_error(ActiveErrors active_errors);
|
||||||
String processor(const String& var);
|
String processor(const String& var);
|
||||||
|
void check_update_task(void* parameter);
|
||||||
@@ -84,6 +84,48 @@ void test_parse_version_extra_content() {
|
|||||||
TEST_ASSERT_EQUAL(0, v.patch);
|
TEST_ASSERT_EQUAL(0, v.patch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_is_version_newer_major() {
|
||||||
|
Version oldVersion = {1, 3, 4};
|
||||||
|
Version newVersion = {2, 1, 1};
|
||||||
|
TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_newer_minor() {
|
||||||
|
Version oldVersion = {1, 0, 8};
|
||||||
|
Version newVersion = {1, 1, 0};
|
||||||
|
TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_newer_patch() {
|
||||||
|
Version oldVersion = {1, 1, 1};
|
||||||
|
Version newVersion = {1, 1, 2};
|
||||||
|
TEST_ASSERT_TRUE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_not_newer_same() {
|
||||||
|
Version oldVersion = {1, 1, 1};
|
||||||
|
Version newVersion = {1, 1, 1};
|
||||||
|
TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_not_newer_major() {
|
||||||
|
Version oldVersion = {2, 0, 0};
|
||||||
|
Version newVersion = {1, 99, 99};
|
||||||
|
TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_not_newer_minor() {
|
||||||
|
Version oldVersion = {1, 2, 0};
|
||||||
|
Version newVersion = {1, 1, 99};
|
||||||
|
TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_is_version_not_newer_patch() {
|
||||||
|
Version oldVersion = {1, 1, 2};
|
||||||
|
Version newVersion = {1, 1, 1};
|
||||||
|
TEST_ASSERT_FALSE(isVersionNewer(oldVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
@@ -96,5 +138,12 @@ int main() {
|
|||||||
RUN_TEST(test_parse_version_extra_content);
|
RUN_TEST(test_parse_version_extra_content);
|
||||||
RUN_TEST(test_parse_version_valid_input_2);
|
RUN_TEST(test_parse_version_valid_input_2);
|
||||||
RUN_TEST(test_parse_version_more_missing_parts);
|
RUN_TEST(test_parse_version_more_missing_parts);
|
||||||
|
RUN_TEST(test_is_version_newer_major);
|
||||||
|
RUN_TEST(test_is_version_newer_minor);
|
||||||
|
RUN_TEST(test_is_version_newer_patch);
|
||||||
|
RUN_TEST(test_is_version_not_newer_same);
|
||||||
|
RUN_TEST(test_is_version_not_newer_major);
|
||||||
|
RUN_TEST(test_is_version_not_newer_minor);
|
||||||
|
RUN_TEST(test_is_version_not_newer_patch);
|
||||||
UNITY_END();
|
UNITY_END();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user