Skip to content

Commit

Permalink
Add manual trip of the emergency mode from the web interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed Apr 17, 2024
1 parent da5ca97 commit ed55d2d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/core/fs/pkgs/platform_esp32/src/devtools.be
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DevtoolsPlugin : Plugin
end

def make_form(ctx, state)
var group = ctx.create_group('devtools', { 'label': 'Devtools' })
var group = ctx.create_group('statistics', { 'label': 'Statistics' })

var system_load_btn = group.button_field('cpuStatsBtn', {
'label': "Get CPU statistics",
Expand Down Expand Up @@ -99,8 +99,19 @@ class DevtoolsPlugin : Plugin
ctx.refresh_interval = 0
else
ctx.refresh_interval = 2000
end

var testing_group = ctx.create_group('testing', { 'label': 'Testing' })

var trip_emergency_mode_btn = testing_group.button_field('tripEmergencyModeBtn', {
'label': "Trip emergency mode",
'buttonText': "Trip",
})

if trip_emergency_mode_btn.has_been("click")
core.trip_emergency_mode("Tripped from devtools page")
end

end
end

Expand Down
15 changes: 10 additions & 5 deletions src/core/main/app/CoreBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
#include <memory>
#include "BellUtils.h"
#include "CoreEvents.h"
#include "EmergencyMode.h"

#include <filesystem>
#include <fstream>
#include <streambuf>


#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>


#ifdef ESP_PLATFORM
#include "esp_system.h"
#else
Expand Down Expand Up @@ -56,6 +55,8 @@ void CoreBindings::setupBindings() {

ctx->vm->export_this("clear_config", this, &CoreBindings::_clearWifiConfig,
"wifi");
ctx->vm->export_this("trip_emergency_mode", this,
&CoreBindings::_tripEmergencyMode, "core");
}

std::string CoreBindings::_getPlatform() {
Expand Down Expand Up @@ -175,15 +176,15 @@ void CoreBindings::_sleepMS(int ms) {
void CoreBindings::_deleteConfigFiles() {
std::string path = fmt::format("{}/cfg/", ctx->rootPath);
BELL_LOG(debug, TAG, "Deleting config files in: %s", path.c_str());
struct dirent *entry;
DIR *dir = opendir(path.c_str());
struct dirent* entry;
DIR* dir = opendir(path.c_str());
while ((entry = readdir(dir)) != NULL) {
std::string filename = entry->d_name;
if (filename == "." || filename == "..") {
continue;
}
std::string filepath = path + filename;

remove(filepath.c_str());
}
}
Expand All @@ -202,3 +203,7 @@ void CoreBindings::_clearWifiConfig() {
// this->ctx->storage->executeFromTask(
// [this]() { this->ctx->connectivity->clearConfig(); });
}

void CoreBindings::_tripEmergencyMode(std::string message) {
this->ctx->emergencyMode->trip(EmergencyModeReason::MANUAL_TRIP, message);
}
1 change: 1 addition & 0 deletions src/core/main/app/EmergencyMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ std::string EmergencyMode::getReasonString(EmergencyModeReason reason) {
switch (reason) {
CASE_RETURN_STR(NOT_ACTIVE);
CASE_RETURN_STR(BERRY_INIT_ERROR);
CASE_RETURN_STR(MANUAL_TRIP);
}

return "__FIXME_UNKNOWN_ENUM_VALUE__";
Expand Down
1 change: 1 addition & 0 deletions src/core/main/app/include/CoreBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CoreBindings {
void _deleteConfigFiles();
void _restart();
void _clearWifiConfig();
void _tripEmergencyMode(std::string message);

private:
const std::string TAG = "core-bindings";
Expand Down
9 changes: 9 additions & 0 deletions src/core/main/app/include/EmergencyMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ enum class EmergencyModeReason {
*/
NOT_ACTIVE = 0,

/**
* @brief A berry error has occured while initializing scripts.
*
*/
BERRY_INIT_ERROR = 1,

/**
* @brief Emeregency mode tripped manually by the user.
*/
MANUAL_TRIP = 2,
};

/**
Expand Down

0 comments on commit ed55d2d

Please sign in to comment.