Skip to content

Commit

Permalink
Expose emergency mode status in /system/info
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Apr 13, 2024
1 parent 64f4d5a commit 3f1ead4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/main/app/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/core/main/app/EmergencyMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::shared_mutex> 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) \
Expand Down
14 changes: 14 additions & 0 deletions src/core/main/app/include/EmergencyMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <atomic>
#include <shared_mutex>
#include "civetweb.h"
#include <nlohmann/json.hpp>

namespace euph {

Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 3f1ead4

Please sign in to comment.