diff --git a/src/core/main/app/Core.cpp b/src/core/main/app/Core.cpp index 22957c89..f137ff6a 100644 --- a/src/core/main/app/Core.cpp +++ b/src/core/main/app/Core.cpp @@ -113,6 +113,7 @@ void Core::initialize() { {"connectivity", this->connectivity->getData().toJson()}, {"version", "0.1.0"}, {"onboarding", false}, + {"emergencyMode", ctx->emergencyMode->getJsonStatus()}, }; return this->http->getServer()->makeJsonResponse(state.dump()); }); @@ -131,7 +132,8 @@ void Core::initialize() { #endif } catch (berry::BerryErrorException& e) { - this->ctx->emergencyMode->trip(EmergencyModeReason::BERRY_INIT_ERROR, e.what()); + this->ctx->emergencyMode->trip(EmergencyModeReason::BERRY_INIT_ERROR, + e.what()); } // Initialize HTTP diff --git a/src/core/main/app/EmergencyMode.cpp b/src/core/main/app/EmergencyMode.cpp index e9f465ad..24bca188 100644 --- a/src/core/main/app/EmergencyMode.cpp +++ b/src/core/main/app/EmergencyMode.cpp @@ -48,6 +48,23 @@ bool EmergencyMode::tryServe(struct mg_connection* conn) { return true; } +nlohmann::json EmergencyMode::getJsonStatus() { + nlohmann::json status; + bool isActive = this->isActive(); + status["active"] = isActive; + + if (isActive) { + status["reason"] = getReasonString(this->reason); + std::shared_lock lock(this->messageMutex); + status["message"] = this->message; + } else { + status["reason"] = nullptr; + status["message"] = nullptr; + } + + return status; +} + std::string EmergencyMode::getReasonString(EmergencyModeReason reason) { #define CASE_RETURN_STR(x) \ diff --git a/src/core/main/app/include/EmergencyMode.h b/src/core/main/app/include/EmergencyMode.h index 9efec198..3d33d6fd 100644 --- a/src/core/main/app/include/EmergencyMode.h +++ b/src/core/main/app/include/EmergencyMode.h @@ -7,6 +7,7 @@ #include #include #include "civetweb.h" +#include namespace euph { @@ -54,6 +55,19 @@ class EmergencyMode { */ bool tryServe(struct mg_connection* conn); + /** + * @brief Get the status of the emergency mode as a JSON object. + * To be used as part of the /system/info endpoint. + * + * The json object contains the following fields: + * - active: true if emergency mode is active, false otherwise + * - reason: the reason why emergency mode is active (null if not active) + * - message: an optional message with additional information (null if not active) + * + * @return nlohmann::json Status information. + */ + nlohmann::json getJsonStatus(); + /** * @brief Returns a human-readable string for the given emergency mode reason. *