mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-06 03:50:13 +02:00
Make new settings editable in UI
This commit is contained in:
parent
00fb213cc1
commit
693689e5af
6 changed files with 52 additions and 18 deletions
|
@ -1,5 +1,7 @@
|
|||
#include "comm_nvm.h"
|
||||
#include "../../communication/can/comm_can.h"
|
||||
#include "../../devboard/mqtt/mqtt.h"
|
||||
#include "../../devboard/wifi/wifi.h"
|
||||
#include "../../include.h"
|
||||
#include "../contactorcontrol/comm_contactorcontrol.h"
|
||||
|
||||
|
@ -108,6 +110,11 @@ void init_stored_settings() {
|
|||
periodic_bms_reset = settings.getBool("PERBMSRESET", false);
|
||||
remote_bms_reset = settings.getBool("REMBMSRESET", false);
|
||||
use_canfd_as_can = settings.getBool("CANFDASCAN", false);
|
||||
|
||||
wifiap_enabled = settings.getBool("WIFIAPENABLED", false);
|
||||
mqtt_enabled = settings.getBool("MQTTENABLED", false);
|
||||
ha_autodiscovery_enabled = settings.getBool("HADISC", false);
|
||||
|
||||
#endif
|
||||
|
||||
settings.end();
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "../../include.h"
|
||||
|
||||
#include <limits>
|
||||
#include "../../datalayer/datalayer.h"
|
||||
#include "../../devboard/utils/events.h"
|
||||
#include "../../devboard/wifi/wifi.h"
|
||||
|
@ -48,14 +49,27 @@ class BatteryEmulatorSettingsStore {
|
|||
|
||||
uint32_t getUInt(const char* name, uint32_t defaultValue) { return settings.getUInt(name, defaultValue); }
|
||||
|
||||
void saveUInt(const char* name, uint32_t value) { settings.putUInt(name, value); }
|
||||
void saveUInt(const char* name, uint32_t value) {
|
||||
auto oldValue = settings.getUInt(name, std::numeric_limits<uint32_t>::max());
|
||||
settings.putUInt(name, value);
|
||||
settingsUpdated = settingsUpdated || value != oldValue;
|
||||
}
|
||||
|
||||
bool getBool(const char* name) { return settings.getBool(name, false); }
|
||||
|
||||
void saveBool(const char* name, bool value) { settings.putBool(name, value); }
|
||||
void saveBool(const char* name, bool value) {
|
||||
auto oldValue = settings.getBool(name, false);
|
||||
settings.putBool(name, value);
|
||||
settingsUpdated = settingsUpdated || value != oldValue;
|
||||
}
|
||||
|
||||
bool were_settings_updated() const { return settingsUpdated; }
|
||||
|
||||
private:
|
||||
Preferences settings;
|
||||
|
||||
// To track if settings were updated
|
||||
bool settingsUpdated = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -4,7 +4,23 @@
|
|||
R"rawliteral(<!doctype html><html><head><title>Battery Emulator</title><meta content="width=device-width"name=viewport><style>html{font-family:Arial;display:inline-block;text-align:center}h2{font-size:3rem}body{max-width:800px;margin:0 auto}</style><body>)rawliteral"
|
||||
#define INDEX_HTML_FOOTER R"rawliteral(</body></html>)rawliteral";
|
||||
|
||||
const char index_html[] = INDEX_HTML_HEADER "%X%" INDEX_HTML_FOOTER;
|
||||
#define COMMON_JAVASCRIPT \
|
||||
R"rawliteral(
|
||||
<script>
|
||||
function askReboot() {
|
||||
if (window.confirm('Are you sure you want to reboot the emulator? NOTE: If emulator is handling contactors, they will open during reboot!')) {
|
||||
reboot();
|
||||
}
|
||||
}
|
||||
function reboot() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', '/reboot', true);
|
||||
xhr.send();
|
||||
}
|
||||
</script>
|
||||
)rawliteral"
|
||||
|
||||
const char index_html[] = INDEX_HTML_HEADER COMMON_JAVASCRIPT "%X%" INDEX_HTML_FOOTER;
|
||||
const char index_html_header[] = INDEX_HTML_HEADER;
|
||||
const char index_html_footer[] = INDEX_HTML_FOOTER;
|
||||
|
||||
|
|
|
@ -192,12 +192,17 @@ String settings_processor(const String& var) {
|
|||
render_checkbox(content, "Remote BMS reset", settings.getBool("REMBMSRESET"), "REMBMSRESET");
|
||||
render_checkbox(content, "Use CanFD as classic CAN", settings.getBool("CANFDASCAN"), "CANFDASCAN");
|
||||
|
||||
render_checkbox(content, "Enable WiFi AP", settings.getBool("WIFIAPENABLED"), "WIFIAPENABLED");
|
||||
render_checkbox(content, "Enable MQTT", settings.getBool("MQTTENABLED"), "MQTTENABLED");
|
||||
render_checkbox(content, "Enable Home Assistant auto discovery", settings.getBool("HADISC"), "HADISC");
|
||||
|
||||
content +=
|
||||
"<div style='grid-column: span 2; text-align: center; padding-top: 10px;'><button "
|
||||
"type='submit'>Save</button></div>";
|
||||
|
||||
if (settingsUpdated) {
|
||||
content += "<p>Settings saved. Reboot to take the settings into use.</p>";
|
||||
content += "<p>Settings saved. Reboot to take the settings into use.";
|
||||
content += "<button onclick='askReboot()'>Reboot</button></p>";
|
||||
}
|
||||
|
||||
content += "</form></div></div>";
|
||||
|
|
|
@ -396,17 +396,17 @@ void init_webserver() {
|
|||
request->send(200, "text/html", response);
|
||||
});
|
||||
|
||||
#ifdef COMMON_IMAGE
|
||||
struct BoolSetting {
|
||||
const char* name;
|
||||
bool existingValue;
|
||||
bool newValue;
|
||||
};
|
||||
|
||||
const char* boolSettingNames[] = {"DBLBTR", "CNTCTRL", "CNTCTRLDBL", "PWMCNTCTRL",
|
||||
"PERBMSRESET", "REMBMSRESET", "CANFDASCAN"};
|
||||
const char* boolSettingNames[] = {"DBLBTR", "CNTCTRL", "CNTCTRLDBL", "PWMCNTCTRL", "PERBMSRESET",
|
||||
"REMBMSRESET", "CANFDASCAN", "WIFIAPENABLED", "MQTTENABLED", "HADISC"};
|
||||
|
||||
#ifdef COMMON_IMAGE
|
||||
// Handles the form POST from UI to save certain settings: battery/inverter type and double battery on/off
|
||||
// Handles the form POST from UI to save settings of the common image
|
||||
server.on("/saveSettings", HTTP_POST, [boolSettingNames](AsyncWebServerRequest* request) {
|
||||
BatteryEmulatorSettingsStore settings;
|
||||
|
||||
|
@ -464,7 +464,7 @@ void init_webserver() {
|
|||
}
|
||||
}
|
||||
|
||||
settingsUpdated = true;
|
||||
settingsUpdated = settings.were_settings_updated();
|
||||
request->redirect("/settings");
|
||||
});
|
||||
#endif
|
||||
|
@ -1552,15 +1552,6 @@ String processor(const String& var) {
|
|||
content += "function CANreplay() { window.location.href = '/canreplay'; }";
|
||||
content += "function Log() { window.location.href = '/log'; }";
|
||||
content += "function Events() { window.location.href = '/events'; }";
|
||||
content +=
|
||||
"function askReboot() { if (window.confirm('Are you sure you want to reboot the emulator? NOTE: If "
|
||||
"emulator is handling contactors, they will open during reboot!')) { "
|
||||
"reboot(); } }";
|
||||
content += "function reboot() {";
|
||||
content += " var xhr = new XMLHttpRequest();";
|
||||
content += " xhr.open('GET', '/reboot', true);";
|
||||
content += " xhr.send();";
|
||||
content += "}";
|
||||
if (WEBSERVER_AUTH_REQUIRED) {
|
||||
content += "function logout() {";
|
||||
content += " var xhr = new XMLHttpRequest();";
|
||||
|
|
|
@ -25,6 +25,7 @@ void init_WiFi_AP();
|
|||
void init_mDNS();
|
||||
|
||||
extern bool wifi_enabled;
|
||||
extern bool wifiap_enabled;
|
||||
extern bool mdns_enabled;
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue