This commit is contained in:
proddy
2025-11-20 23:03:06 +01:00
parent 23a660aabb
commit eeba7a3a6b

View File

@@ -452,10 +452,10 @@ void System::get_settings() {
locale_ = settings.locale;
developer_mode_ = settings.developer_mode;
});
}
// Starts up the UART Serial bridge
void System::start() {
// Starts up the UART Serial bridge
void System::start() {
#ifndef EMSESP_STANDALONE
// disable bluetooth module
// periph_module_disable(PERIPH_BT_MODULE);
@@ -499,10 +499,10 @@ void System::get_settings() {
network_init(); // network
uart_init(); // start UART
syslog_init(); // start syslog
}
}
// button single click
void System::button_OnClick(PButton & b) {
// button single click
void System::button_OnClick(PButton & b) {
LOG_NOTICE("Button pressed - single click");
#if defined(EMSESP_TEST)
@@ -511,10 +511,10 @@ void System::get_settings() {
Test::listDir(LittleFS, "/", 3);
#endif
#endif
}
}
// button double click
void System::button_OnDblClick(PButton & b) {
// button double click
void System::button_OnDblClick(PButton & b) {
LOG_NOTICE("Button pressed - double click - wifi reconnect to AP");
// set AP mode to always so will join AP if wifi ssid fails to connect
EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) {
@@ -527,24 +527,24 @@ void System::get_settings() {
return StateUpdateResult::CHANGED;
});
EMSESP::esp32React.getNetworkSettingsService()->callUpdateHandlers(); // in case we've changed ssid or password
}
}
// button long press
void System::button_OnLongPress(PButton & b) {
// button long press
void System::button_OnLongPress(PButton & b) {
LOG_NOTICE("Button pressed - long press - perform factory reset");
#ifndef EMSESP_STANDALONE
System::command_format(nullptr, 0);
#endif
}
}
// button indefinite press - do nothing for now
void System::button_OnVLongPress(PButton & b) {
// button indefinite press - do nothing for now
void System::button_OnVLongPress(PButton & b) {
LOG_NOTICE("Button pressed - very long press - restart from factory/boot partition");
EMSESP::system_.system_restart("boot");
}
}
// push button
void System::button_init() {
// push button
void System::button_init() {
#ifndef EMSESP_STANDALONE
if (!myPButton_.init(pbutton_gpio_, HIGH)) {
LOG_WARNING("Multi-functional button not detected");
@@ -557,10 +557,10 @@ void System::get_settings() {
myPButton_.onLongPress(BUTTON_LongPressDelay, button_OnLongPress);
myPButton_.onVLongPress(BUTTON_VLongPressDelay, button_OnVLongPress);
#endif
}
}
// set the LED to on or off when in normal operating mode
void System::led_init() {
// set the LED to on or off when in normal operating mode
void System::led_init() {
// disabled old led port before setting new one
#if ESP_ARDUINO_VERSION_MAJOR < 3
led_type_ ? neopixelWrite(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
@@ -583,19 +583,19 @@ void System::get_settings() {
} else {
LOG_INFO("LED disabled");
}
}
}
void System::uart_init() {
void System::uart_init() {
EMSuart::stop();
// start UART, GPIOs have already been checked
EMSuart::start(tx_mode_, rx_gpio_, tx_gpio_);
EMSESP::txservice_.start(); // reset counters and send devices request
}
}
// checks system health and handles LED flashing wizardry
void System::loop() {
// checks system health and handles LED flashing wizardry
void System::loop() {
// check if we're supposed to do a reset/restart
if (systemStatus() == SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED) {
system_restart();
@@ -613,11 +613,11 @@ void System::get_settings() {
system_check(); // check system health
send_info_mqtt();
#endif
}
}
// send MQTT info topic appended with the version information as JSON, as a retained flag
// this is only done once when the connection is established
void System::send_info_mqtt() {
// send MQTT info topic appended with the version information as JSON, as a retained flag
// this is only done once when the connection is established
void System::send_info_mqtt() {
static uint8_t _connection = 0;
uint8_t connection = (ethernet_connected() ? 1 : 0) + ((WiFi.status() == WL_CONNECTED) ? 2 : 0) + (ntp_connected_ ? 4 : 0) + (has_ipv6_ ? 8 : 0);
// check if connection status has changed
@@ -675,10 +675,10 @@ void System::get_settings() {
}
#endif
Mqtt::queue_publish_retain(F_(info), doc.as<JsonObject>()); // topic called "info" and it's Retained
}
}
// create the json for heartbeat
void System::heartbeat_json(JsonObject output) {
// create the json for heartbeat
void System::heartbeat_json(JsonObject output) {
switch (EMSESP::bus_status()) {
case EMSESP::BUS_STATUS_OFFLINE:
output["bus_status"] = "connecting"; // EMS-ESP is booting...
@@ -732,10 +732,10 @@ void System::get_settings() {
output["wifireconnects"] = EMSESP::esp32React.getWifiReconnects();
}
#endif
}
}
// send periodic MQTT message with system information
void System::send_heartbeat() {
// send periodic MQTT message with system information
void System::send_heartbeat() {
// don't send heartbeat if WiFi or MQTT is not connected
if (!Mqtt::connected()) {
return;
@@ -748,10 +748,10 @@ void System::get_settings() {
heartbeat_json(json);
Mqtt::queue_publish(F_(heartbeat), json); // send to MQTT with retain off. This will add to MQTT queue.
}
}
// initializes network
void System::network_init() {
// initializes network
void System::network_init() {
last_system_check_ = 0; // force the LED to go from fast flash to pulse
#if CONFIG_IDF_TARGET_ESP32
@@ -790,10 +790,10 @@ void System::get_settings() {
eth_present_ = ETH.begin(type, phy_addr, mdc, mdio, power, clock_mode);
#endif
#endif
}
}
// check health of system, done every 5 seconds
void System::system_check() {
// check health of system, done every 5 seconds
void System::system_check() {
if (!last_system_check_ || ((uint32_t)(uuid::get_uptime() - last_system_check_) >= SYSTEM_CHECK_FREQUENCY)) {
last_system_check_ = uuid::get_uptime();
@@ -855,11 +855,11 @@ void System::get_settings() {
}
}
}
}
}
// commands - takes static function pointers
// can be called via Console using 'call system <cmd>'
void System::commands_init() {
// commands - takes static function pointers
// can be called via Console using 'call system <cmd>'
void System::commands_init() {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(read), System::command_read, FL_(read_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, FL_(fetch_cmd), CommandFlag::ADMIN_ONLY);
@@ -876,13 +876,13 @@ void System::get_settings() {
// MQTT subscribe "ems-esp/system/#"
Mqtt::subscribe(EMSdevice::DeviceType::SYSTEM, "system/#", nullptr); // use empty function callback
}
}
// uses LED to show system health
// 1 x flash = the EMS bus is not connected
// 2 x flash = the network (wifi or ethernet) is not connected
// 3 x flash = both EMS bus and network are failing. This is a critical error!
void System::led_monitor() {
// uses LED to show system health
// 1 x flash = the EMS bus is not connected
// 2 x flash = the network (wifi or ethernet) is not connected
// 3 x flash = both EMS bus and network are failing. This is a critical error!
void System::led_monitor() {
// we only need to run the LED healthcheck if there are errors
if (!healthcheck_ || !led_gpio_) {
return; // all good
@@ -982,14 +982,14 @@ void System::get_settings() {
}
}
}
}
}
// Return the quality (Received Signal Strength Indicator) of the WiFi network as a %
// High quality: 90% ~= -55dBm
// Medium quality: 50% ~= -75dBm
// Low quality: 30% ~= -85dBm
// Unusable quality: 8% ~= -96dBm
int8_t System::wifi_quality(int8_t dBm) {
// Return the quality (Received Signal Strength Indicator) of the WiFi network as a %
// High quality: 90% ~= -55dBm
// Medium quality: 50% ~= -75dBm
// Low quality: 30% ~= -85dBm
// Unusable quality: 8% ~= -96dBm
int8_t System::wifi_quality(int8_t dBm) {
if (dBm <= -100) {
return 0;
}
@@ -998,10 +998,10 @@ void System::get_settings() {
return 100;
}
return 2 * (dBm + 100);
}
}
// print users to console
void System::show_users(uuid::console::Shell & shell) {
// print users to console
void System::show_users(uuid::console::Shell & shell) {
shell.printfln("Users:");
#ifndef EMSESP_STANDALONE
@@ -1013,10 +1013,10 @@ void System::get_settings() {
#endif
shell.println();
}
}
// shell command 'show system'
void System::show_system(uuid::console::Shell & shell) {
// shell command 'show system'
void System::show_system(uuid::console::Shell & shell) {
refreshHeapMem(); // refresh free heap and max alloc heap
shell.println();
@@ -1153,11 +1153,11 @@ void System::get_settings() {
shell.println();
#endif
}
}
// see if there is a restore of an older settings file that needs to be applied
// note there can be only one file at a time
bool System::check_restore() {
// see if there is a restore of an older settings file that needs to be applied
// note there can be only one file at a time
bool System::check_restore() {
bool reboot_required = false; // true if we need to reboot
#ifndef EMSESP_STANDALONE
@@ -1209,12 +1209,12 @@ void System::get_settings() {
#endif
return reboot_required;
}
}
// handle upgrades from previous versions
// this function will not be called on a clean install, with no settings files yet created
// returns true if we need a reboot
bool System::check_upgrade(bool factory_settings) {
// handle upgrades from previous versions
// this function will not be called on a clean install, with no settings files yet created
// returns true if we need a reboot
bool System::check_upgrade(bool factory_settings) {
bool missing_version = true;
std::string settingsVersion;
@@ -1334,10 +1334,10 @@ void System::get_settings() {
}
return false;
}
}
// convert settings file into json object
void System::extractSettings(const char * filename, const char * section, JsonObject output) {
// convert settings file into json object
void System::extractSettings(const char * filename, const char * section, JsonObject output) {
#ifndef EMSESP_STANDALONE
File settingsFile = LittleFS.open(filename);
if (settingsFile) {
@@ -1353,10 +1353,10 @@ void System::get_settings() {
}
settingsFile.close();
#endif
}
}
// save settings file using input from a json object
bool System::saveSettings(const char * filename, const char * section, JsonObject input) {
// save settings file using input from a json object
bool System::saveSettings(const char * filename, const char * section, JsonObject input) {
#ifndef EMSESP_STANDALONE
JsonObject section_json = input[section];
if (section_json) {
@@ -1370,10 +1370,10 @@ void System::get_settings() {
}
#endif
return false; // not found
}
}
// set a entity of services 'network', 'settings', 'mqtt', etc.
bool System::command_service(const char * cmd, const char * value) {
// set a entity of services 'network', 'settings', 'mqtt', etc.
bool System::command_service(const char * cmd, const char * value) {
bool ok = false;
bool b;
if (Helpers::value2bool(value, b)) {
@@ -1456,10 +1456,10 @@ void System::get_settings() {
LOG_INFO("System command '%s' with value '%s'", cmd, value);
}
return ok;
}
}
// return back a system value
bool System::get_value_info(JsonObject output, const char * cmd) {
// return back a system value
bool System::get_value_info(JsonObject output, const char * cmd) {
if (cmd == nullptr || strlen(cmd) == 0) {
LOG_ERROR("empty system command");
return false;
@@ -1531,9 +1531,9 @@ void System::get_settings() {
}
}
return false;
}
}
void System::get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val) {
void System::get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val) {
output["name"] = name;
if (circuit.length()) {
output["circuit"] = circuit;
@@ -1551,11 +1551,11 @@ void System::get_settings() {
output["value"] = val.as<std::string>();
output["type"] = "string";
}
}
}
// export status information including the device information
// http://ems-esp/api/system/info
bool System::command_info(const char * value, const int8_t id, JsonObject output) {
// export status information including the device information
// http://ems-esp/api/system/info
bool System::command_info(const char * value, const int8_t id, JsonObject output) {
JsonObject node;
// System
@@ -1889,30 +1889,30 @@ void System::get_settings() {
}
return true; // this function always returns true!
}
}
#if defined(EMSESP_TEST)
// run a test, e.g. http://ems-esp/api?device=system&cmd=test&data=boiler
bool System::command_test(const char * value, const int8_t id) {
// run a test, e.g. http://ems-esp/api?device=system&cmd=test&data=boiler
bool System::command_test(const char * value, const int8_t id) {
if (value) {
return Test::test(value, id);
} else {
return false;
}
}
}
#endif
// takes a board profile and populates a data array with GPIO configurations
// returns false if profile is unknown
//
// data = led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode, led_type
//
// clock modes:
// 0 = RMII clock input to GPIO0
// 1 = RMII clock output from GPIO0
// 2 = RMII clock output from GPIO16
// 3 = RMII clock output from GPIO17, for 50hz inverted clock
bool System::load_board_profile(std::vector<int8_t> & data, const std::string & board_profile) {
// takes a board profile and populates a data array with GPIO configurations
// returns false if profile is unknown
//
// data = led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode, led_type
//
// clock modes:
// 0 = RMII clock input to GPIO0
// 1 = RMII clock output from GPIO0
// 2 = RMII clock output from GPIO16
// 3 = RMII clock output from GPIO17, for 50hz inverted clock
bool System::load_board_profile(std::vector<int8_t> & data, const std::string & board_profile) {
if (board_profile == "S32") {
data = {2, 18, 23, 5, 0, PHY_type::PHY_TYPE_NONE, 0, 0, 0, 0}; // BBQKees Gateway S32
} else if (board_profile == "E32") {
@@ -1962,10 +1962,10 @@ void System::get_settings() {
// LOG_DEBUG("Found data for board profile %s", board_profile.c_str());
return true;
}
}
// format command - factory reset, removing all config files
bool System::command_format(const char * value, const int8_t id) {
// format command - factory reset, removing all config files
bool System::command_format(const char * value, const int8_t id) {
LOG_INFO("Formatting FS, removing all config files");
#ifndef EMSESP_STANDALONE
if (LittleFS.format()) {
@@ -1978,10 +1978,10 @@ void System::get_settings() {
// restart will be handled by the main loop
EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED);
return true;
}
}
// restart command - perform a hard reset (system reboot)
bool System::command_restart(const char * value, const int8_t id) {
// restart command - perform a hard reset (system reboot)
bool System::command_restart(const char * value, const int8_t id) {
if (id == 0) {
// if it has an id then it's a web call and we need to queue the restart
// default id is -1 when calling /api/system/restart directly for example
@@ -1994,12 +1994,12 @@ void System::get_settings() {
// restart will be handled by the main loop
EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED);
return true;
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
std::string System::reset_reason(uint8_t cpu) const {
std::string System::reset_reason(uint8_t cpu) const {
#ifndef EMSESP_STANDALONE
switch (rtc_get_reset_reason(cpu)) {
case 1:
@@ -2038,11 +2038,11 @@ void System::get_settings() {
}
#endif
return "Unknown";
}
}
#pragma GCC diagnostic pop
// set NTP status
void System::ntp_connected(bool b) {
// set NTP status
void System::ntp_connected(bool b) {
if (b != ntp_connected_) {
if (b) {
LOG_INFO("NTP connected");
@@ -2053,20 +2053,20 @@ void System::get_settings() {
ntp_connected_ = b;
ntp_last_check_ = b ? uuid::get_uptime_sec() : 0;
}
}
// get NTP status
bool System::ntp_connected() {
// get NTP status
bool System::ntp_connected() {
// timeout 2 hours, ntp sync is normally every hour.
if ((uuid::get_uptime_sec() - ntp_last_check_ > 7201) && ntp_connected_) {
ntp_connected(false);
}
return ntp_connected_;
}
}
// see if its a BBQKees Gateway by checking the nvs values
String System::getBBQKeesGatewayDetails(uint8_t detail) {
// see if its a BBQKees Gateway by checking the nvs values
String System::getBBQKeesGatewayDetails(uint8_t detail) {
#ifndef EMSESP_STANDALONE
/*
if (!EMSESP::nvs_.isKey("mfg")) {
@@ -2128,13 +2128,13 @@ void System::get_settings() {
#else
return "";
#endif
}
}
// Stream from an URL and send straight to OTA uploader service.
//
// This function needs to be called twice, 1st pass once with a url to persist it, 2nd pass with no arguments to start the upload
// This is to avoid timeouts in callback functions, like calling from a web hook.
bool System::uploadFirmwareURL(const char * url) {
// Stream from an URL and send straight to OTA uploader service.
//
// This function needs to be called twice, 1st pass once with a url to persist it, 2nd pass with no arguments to start the upload
// This is to avoid timeouts in callback functions, like calling from a web hook.
bool System::uploadFirmwareURL(const char * url) {
#ifndef EMSESP_STANDALONE
static String saved_url;
@@ -2219,11 +2219,11 @@ void System::get_settings() {
#endif
return true; // OK
}
}
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
// from Console use quotes so: call system read "<deviceID> <type ID> [offset] [length]"
bool System::readCommand(const char * data) {
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
// from Console use quotes so: call system read "<deviceID> <type ID> [offset] [length]"
bool System::readCommand(const char * data) {
if (!data) {
return false;
}
@@ -2275,25 +2275,25 @@ void System::get_settings() {
EMSESP::set_read_id(type_id);
return true;
}
}
// system read command
bool System::command_read(const char * value, const int8_t id) {
// system read command
bool System::command_read(const char * value, const int8_t id) {
return readCommand(value);
}
}
// set the system status code - SYSTEM_STATUS in system.h
void System::systemStatus(uint8_t status_code) {
// set the system status code - SYSTEM_STATUS in system.h
void System::systemStatus(uint8_t status_code) {
systemStatus_ = status_code;
// LOG_DEBUG("Setting System status code %d", status_code);
}
}
uint8_t System::systemStatus() {
uint8_t System::systemStatus() {
return systemStatus_;
}
}
// takes a string range like "6-11, 1, 23, 24-48" which has optional ranges and single values and converts to a vector of ints
std::vector<uint8_t> System::string_range_to_vector(const std::string & range) {
// takes a string range like "6-11, 1, 23, 24-48" which has optional ranges and single values and converts to a vector of ints
std::vector<uint8_t> System::string_range_to_vector(const std::string & range) {
std::vector<uint8_t> gpios;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
@@ -2326,14 +2326,14 @@ void System::get_settings() {
process_part(range.substr(prev));
return gpios;
}
}
// initialize a list of valid GPIOs based on the ESP32 board
// notes:
// - we allow 0, which is used on some board for the button
// - we also allow input only pins are accepted (34-39) on some boards
// - and allow pins 33-38 for octal SPI for 32M vchip version on some boards
void System::set_valid_system_gpios(bool exclude_used) {
// initialize a list of valid GPIOs based on the ESP32 board
// notes:
// - we allow 0, which is used on some board for the button
// - we also allow input only pins are accepted (34-39) on some boards
// - and allow pins 33-38 for octal SPI for 32M vchip version on some boards
void System::set_valid_system_gpios(bool exclude_used) {
valid_system_gpios_.clear(); // reset system list
used_gpios_.clear(); // reset used list
@@ -2357,10 +2357,10 @@ void System::get_settings() {
valid_system_gpios_.erase(std::remove(valid_system_gpios_.begin(), valid_system_gpios_.end(), 17), valid_system_gpios_.end());
}
#endif
}
}
// check if a pin is valid ESP32 pin and if not already used, add to the used gpio list
bool System::check_valid_gpio(uint8_t pin, const char * source_name) {
// check if a pin is valid ESP32 pin and if not already used, add to the used gpio list
bool System::check_valid_gpio(uint8_t pin, const char * source_name) {
bool ok = false;
// check if we're allowed to use this pin
if (std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin) != valid_system_gpios_.end()) {
@@ -2380,10 +2380,10 @@ void System::get_settings() {
LOG_DEBUG("GPIO %d for %s is not valid", pin, source_name);
}
return ok;
}
}
// return a list of valid and unused GPIOs still available for use
std::vector<uint8_t> System::valid_gpio_list() {
// return a list of valid and unused GPIOs still available for use
std::vector<uint8_t> System::valid_gpio_list() {
std::vector<uint8_t> gpios;
for (const auto & gpio : valid_system_gpios_) {
if (std::find(used_gpios_.begin(), used_gpios_.end(), gpio) == used_gpios_.end()) {
@@ -2391,7 +2391,7 @@ void System::get_settings() {
}
}
return gpios;
}
}
} // namespace emsesp