mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
refactor: define OTA upgrade flag and MQTT mutex inline
This commit is contained in:
1 parent
aa7385c70f
commit
cc6131eac9
7 files changed
+22
-27
No files matched your search
@@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <atomic>
|
|
||||||
|
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
@@ -401,8 +400,4 @@ struct Variables {
|
|||||||
bool resetFault = false;
|
bool resetFault = false;
|
||||||
bool resetDiagnostic = false;
|
bool resetDiagnostic = false;
|
||||||
} actions;
|
} actions;
|
||||||
|
|
||||||
struct {
|
|
||||||
std::atomic<bool> upgrading{false};
|
|
||||||
} states;
|
|
||||||
};
|
};
|
||||||
+3
-1
@@ -5,6 +5,7 @@
|
|||||||
#include <MycilaHADiscovery.h>
|
#include <MycilaHADiscovery.h>
|
||||||
#include <MycilaTaskManager.h>
|
#include <MycilaTaskManager.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -71,6 +72,7 @@ SettingResult fromJson(const JsonVariantConst src, Variables& dst);
|
|||||||
bool applyAction(std::string_view action, bool value);
|
bool applyAction(std::string_view action, bool value);
|
||||||
|
|
||||||
// Application state and task managers
|
// Application state and task managers
|
||||||
|
inline std::atomic<bool> upgrading{false};
|
||||||
extern Variables vars;
|
extern Variables vars;
|
||||||
extern TaskManager coreTaskManager;
|
extern TaskManager coreTaskManager;
|
||||||
extern TaskManager systemTaskManager;
|
extern TaskManager systemTaskManager;
|
||||||
@@ -107,7 +109,7 @@ extern AsyncWebServer* webServer;
|
|||||||
void initPortal();
|
void initPortal();
|
||||||
extern Task mqttConfigureTask;
|
extern Task mqttConfigureTask;
|
||||||
// Protect MQTT client and task pointers while checking and using them.
|
// Protect MQTT client and task pointers while checking and using them.
|
||||||
extern std::mutex mqttMutex;
|
inline std::mutex mqttMutex;
|
||||||
extern Task* mqttPublishSettingsTask;
|
extern Task* mqttPublishSettingsTask;
|
||||||
extern Task* mqttPublishStateTask;
|
extern Task* mqttPublishStateTask;
|
||||||
extern Task* mqttPublishSensorsTask;
|
extern Task* mqttPublishSensorsTask;
|
||||||
|
|||||||
+2
-2
@@ -3606,7 +3606,7 @@ static SettingResult applyAction(Variables& dst, std::string_view action, bool v
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool applyAction(std::string_view action, bool value) {
|
bool applyAction(std::string_view action, bool value) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3617,7 +3617,7 @@ SettingResult fromJson(const JsonVariantConst src, Variables& dst) {
|
|||||||
SettingResult result;
|
SettingResult result;
|
||||||
|
|
||||||
// Admit the whole request before one of its actions schedules a restart.
|
// Admit the whole request before one of its actions schedules a restart.
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -18,8 +18,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
std::mutex mqttMutex;
|
|
||||||
|
|
||||||
static std::unique_ptr<MQTT> mqttClient;
|
static std::unique_ptr<MQTT> mqttClient;
|
||||||
|
|
||||||
// Keep the applied prefix separate from settings until MQTT is reconfigured.
|
// Keep the applied prefix separate from settings until MQTT is reconfigured.
|
||||||
@@ -55,7 +53,7 @@ static bool isSensorReconfigurationPending(uint8_t sensorId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool isMqttBlocked() {
|
static bool isMqttBlocked() {
|
||||||
return vars.states.upgrading || restartTask.scheduled() || restartTask.active();
|
return upgrading || restartTask.scheduled() || restartTask.active();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isMqttReady() {
|
static bool isMqttReady() {
|
||||||
@@ -1154,7 +1152,7 @@ Task mqttConfigureTask("Configure MQTT", Task::Type::ONCE, [](void*) {
|
|||||||
void initMqtt() {
|
void initMqtt() {
|
||||||
// Keep only the configuration dispatcher when MQTT publications are disabled.
|
// Keep only the configuration dispatcher when MQTT publications are disabled.
|
||||||
mqttConfigureTask.pause().setEnabledWhen([] {
|
mqttConfigureTask.pause().setEnabledWhen([] {
|
||||||
return !vars.states.upgrading && !restartTask.scheduled() && !restartTask.active();
|
return !upgrading && !restartTask.scheduled() && !restartTask.active();
|
||||||
});
|
});
|
||||||
|
|
||||||
coreTaskManager.addTask(mqttConfigureTask);
|
coreTaskManager.addTask(mqttConfigureTask);
|
||||||
|
|||||||
+13
-13
@@ -236,15 +236,15 @@ static void restApi() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// UpgradeHandler invokes this callback for every upload chunk.
|
// UpgradeHandler invokes this callback for every upload chunk.
|
||||||
if (vars.states.upgrading) {
|
if (upgrading) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
vars.states.upgrading = true;
|
upgrading = true;
|
||||||
coreTaskManager.pause();
|
coreTaskManager.pause();
|
||||||
// Core owns MQTT; finish its current callback before stopping the client.
|
// Core owns MQTT; finish its current callback before stopping the client.
|
||||||
if (!coreTaskManager.waitForPaused(5000)) {
|
if (!coreTaskManager.waitForPaused(5000)) {
|
||||||
vars.states.upgrading = false;
|
upgrading = false;
|
||||||
if (!restartTask.scheduled() && !restartTask.active()) {
|
if (!restartTask.scheduled() && !restartTask.active()) {
|
||||||
coreTaskManager.resume();
|
coreTaskManager.resume();
|
||||||
}
|
}
|
||||||
@@ -256,14 +256,14 @@ static void restApi() {
|
|||||||
|
|
||||||
// An MQTT command already in flight may have scheduled a restart before end().
|
// An MQTT command already in flight may have scheduled a restart before end().
|
||||||
if (restartTask.scheduled() || restartTask.active()) {
|
if (restartTask.scheduled() || restartTask.active()) {
|
||||||
vars.states.upgrading = false;
|
upgrading = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
sensorsTaskManager.pause();
|
sensorsTaskManager.pause();
|
||||||
if (!sensorsTaskManager.waitForPaused(5000)) {
|
if (!sensorsTaskManager.waitForPaused(5000)) {
|
||||||
requestReconfigureMqtt();
|
requestReconfigureMqtt();
|
||||||
vars.states.upgrading = false;
|
upgrading = false;
|
||||||
if (!restartTask.scheduled() && !restartTask.active()) {
|
if (!restartTask.scheduled() && !restartTask.active()) {
|
||||||
coreTaskManager.resume();
|
coreTaskManager.resume();
|
||||||
sensorsTaskManager.resume();
|
sensorsTaskManager.resume();
|
||||||
@@ -275,7 +275,7 @@ static void restApi() {
|
|||||||
openthermTaskManager.pause();
|
openthermTaskManager.pause();
|
||||||
if (!openthermTaskManager.waitForPaused(5000)) {
|
if (!openthermTaskManager.waitForPaused(5000)) {
|
||||||
requestReconfigureMqtt();
|
requestReconfigureMqtt();
|
||||||
vars.states.upgrading = false;
|
upgrading = false;
|
||||||
if (!restartTask.scheduled() && !restartTask.active()) {
|
if (!restartTask.scheduled() && !restartTask.active()) {
|
||||||
coreTaskManager.resume();
|
coreTaskManager.resume();
|
||||||
sensorsTaskManager.resume();
|
sensorsTaskManager.resume();
|
||||||
@@ -302,7 +302,7 @@ static void restApi() {
|
|||||||
requestReconfigureMqtt();
|
requestReconfigureMqtt();
|
||||||
}
|
}
|
||||||
|
|
||||||
vars.states.upgrading = false;
|
upgrading = false;
|
||||||
if (status != 202 && !restartTask.scheduled() && !restartTask.active()) {
|
if (status != 202 && !restartTask.scheduled() && !restartTask.active()) {
|
||||||
coreTaskManager.resume();
|
coreTaskManager.resume();
|
||||||
sensorsTaskManager.resume();
|
sensorsTaskManager.resume();
|
||||||
@@ -374,7 +374,7 @@ static void restApi() {
|
|||||||
|
|
||||||
// restart
|
// restart
|
||||||
webServer->on("/api/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
webServer->on("/api/restart", HTTP_POST, [](AsyncWebServerRequest *request) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return request->send(503);
|
return request->send(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,7 +398,7 @@ static void restApi() {
|
|||||||
}).addMiddleware(&authMiddleware);
|
}).addMiddleware(&authMiddleware);
|
||||||
|
|
||||||
auto& nsHandler = webServer->on("/api/network/settings", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
auto& nsHandler = webServer->on("/api/network/settings", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return request->send(503);
|
return request->send(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,7 +532,7 @@ static void restApi() {
|
|||||||
}).addMiddleware(&authMiddleware);
|
}).addMiddleware(&authMiddleware);
|
||||||
|
|
||||||
auto& settingsHandler = webServer->on(AsyncURIMatcher::prefix("/api/settings/"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
auto& settingsHandler = webServer->on(AsyncURIMatcher::prefix("/api/settings/"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
request->send(503);
|
request->send(503);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -712,7 +712,7 @@ static void restApi() {
|
|||||||
}).addMiddleware(&authMiddleware);
|
}).addMiddleware(&authMiddleware);
|
||||||
|
|
||||||
auto& ssHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
auto& ssHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return request->send(503);
|
return request->send(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -818,7 +818,7 @@ static void restApi() {
|
|||||||
}).addMiddleware(&authMiddleware);
|
}).addMiddleware(&authMiddleware);
|
||||||
|
|
||||||
auto& sensorValueHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor/value"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
auto& sensorValueHandler = webServer->on(AsyncURIMatcher::exact("/api/sensor/value"), HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return request->send(503);
|
return request->send(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -881,7 +881,7 @@ static void restApi() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
auto& vHandler = webServer->on("/api/vars", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
auto& vHandler = webServer->on("/api/vars", HTTP_POST, [](AsyncWebServerRequest *request, JsonVariant &requestDoc) {
|
||||||
if (vars.states.upgrading || restartTask.scheduled() || restartTask.active()) {
|
if (upgrading || restartTask.scheduled() || restartTask.active()) {
|
||||||
return request->send(503);
|
return request->send(503);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -770,7 +770,7 @@ static void updateConnectionStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool canRunSensors() {
|
static bool canRunSensors() {
|
||||||
return !vars.states.upgrading && !restartTask.scheduled() && !restartTask.active();
|
return !upgrading && !restartTask.scheduled() && !restartTask.active();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Task readDallasSensorsTask("Read Dallas sensors", [](void*) {
|
static Task readDallasSensorsTask("Read Dallas sensors", [](void*) {
|
||||||
|
|||||||
+1
-1
@@ -94,7 +94,7 @@ static Task heapTask("Heap monitoring", [](void*) {
|
|||||||
unsigned int maxFreeBlockHeap = memory.heap.largest_free_block;
|
unsigned int maxFreeBlockHeap = memory.heap.largest_free_block;
|
||||||
|
|
||||||
/*if (freeHeap < 2048 || maxFreeBlockHeap < 2048) {
|
/*if (freeHeap < 2048 || maxFreeBlockHeap < 2048) {
|
||||||
if (!vars.states.upgrading) {
|
if (!upgrading) {
|
||||||
esp_system_abort("OTGateway: low internal heap");
|
esp_system_abort("OTGateway: low internal heap");
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|||||||
Reference in new issue
Block a user