From 286ba1fe6cd64111ec67150c692d14737122b7c6 Mon Sep 17 00:00:00 2001
From: Marcel Peterkau
Date: Thu, 11 Jan 2024 14:55:33 +0100
Subject: [PATCH] some buttons are now websocket-handled
---
Software/data_src/index.htm | 8 +--
Software/data_src/static/js/websocket.js | 40 +++++++++----
Software/src/webui.cpp | 71 +++++++++++++++---------
3 files changed, 77 insertions(+), 42 deletions(-)
diff --git a/Software/data_src/index.htm b/Software/data_src/index.htm
index 8713756..ea1ccb4 100644
--- a/Software/data_src/index.htm
+++ b/Software/data_src/index.htm
@@ -225,8 +225,8 @@
@@ -235,13 +235,11 @@
Gerät neustarten
-
diff --git a/Software/data_src/static/js/websocket.js b/Software/data_src/static/js/websocket.js
index 9261683..8bf2ec6 100644
--- a/Software/data_src/static/js/websocket.js
+++ b/Software/data_src/static/js/websocket.js
@@ -15,12 +15,31 @@ function initWebSocket() {
}
function initButtons() {
- document
- .getElementById("btn-ws-stop")
- .addEventListener("click", livedebug_stop);
- document
- .getElementById("btn-ws-start")
- .addEventListener("click", livedebug_start);
+
+ var elements = document.getElementsByClassName("btn-wsevent");
+
+ if (elements.length > 0) {
+ for (var i = 0; i < elements.length; i++) {
+ var element = elements[i];
+ element.addEventListener("click", function () {
+ websocket_sendevent("btn-" + element.id, 0);
+ });
+ }
+ }
+}
+
+function initSettingInputs() {
+
+ var elements = document.getElementsByClassName("btn-wssetting");
+
+ if (elements.length > 0) {
+ for (var i = 0; i < elements.length; i++) {
+ var element = elements[i];
+ element.addEventListener("change", function () {
+ websocket_sendevent("set-" + element.id, element.value);
+ });
+ }
+ }
}
function onOpen(event) {
@@ -112,14 +131,11 @@ function processDataString(dataString, mapping) {
function onLoad(event) {
initWebSocket();
initButtons();
+ initSettingInputs();
}
-function livedebug_start() {
- websocket.send("start");
-}
-
-function livedebug_stop() {
- websocket.send("stop");
+function websocket_sendevent(element_id, element_value) {
+ websocket.send(element_id + ":" + element_value);
}
function do_resize(textbox) {
diff --git a/Software/src/webui.cpp b/Software/src/webui.cpp
index 5122fd6..e93182a 100644
--- a/Software/src/webui.cpp
+++ b/Software/src/webui.cpp
@@ -32,6 +32,8 @@ void Websocket_HandleMessage(void *arg, uint8_t *data, size_t len);
void Websocket_RefreshClientData_DTCs(uint32_t client_id);
void Websocket_RefreshClientData_Status(uint32_t client_id, bool send_mapping = false);
void Websocket_RefreshClientData_Static(uint32_t client_id, bool send_mapping = false);
+void Websocket_HandleButtons(uint8_t *data);
+void Websocket_HandleSettings(uint8_t *data);
/**
* @brief Initializes the web-based user interface (WebUI) for the ChainLube application.
@@ -180,8 +182,6 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
globals.requestEEAction = EE_CFG_SAVE;
// end: POST Form Oiltank
// begin: POST Form Maintenance
- if (p->name() == "purgepulse")
- LubeConfig.BleedingPulses = p->value().toInt();
if (p->name() == "maintsave")
globals.requestEEAction = EE_CFG_SAVE;
if (p->name() == "resettank")
@@ -204,15 +204,6 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
globals.requestEEAction = globals.requestEEAction == EE_PDS_FORMAT ? EE_FORMAT_ALL : EE_CFG_FORMAT;
}
}
- if (p->name() == "purgenow")
- {
- globals.systemStatus = sysStat_Purge;
- globals.purgePulses = LubeConfig.BleedingPulses;
- }
- if (p->name() == "reboot")
- {
- globals.systemStatus = sysStat_Shutdown;
- }
// end: POST Form Maintenance
// begin: POST Form LED Settings
if (p->name() == "ledmaxbrightness")
@@ -234,12 +225,6 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
globals.requestEEAction = EE_CFG_SAVE;
}
// end: POST Form LED SEttings
- // begin: POST Form Measure Pulses
- if (p->name() == "measurereset")
- globals.measuredPulses = 0;
- if (p->name() == "measurestartstop")
- globals.measurementActive = !globals.measurementActive;
- // end: POST Form Measure Pulses
}
}
@@ -530,23 +515,59 @@ void Websocket_HandleMessage(void *arg, uint8_t *data, size_t len)
{
data[len] = 0;
- Debug_pushMessage("Got WebSocket Message: %s \n", (char *)data);
-
- if (strcmp((char *)data, "start") == 0)
+ if (strncmp((char *)data, "btn-", strlen("btn-")) == 0)
{
- SetDebugportStatus(dbg_Webui, enabled);
+ Websocket_HandleButtons(data + strlen("btn-"));
}
- else if (strcmp((char *)data, "stop") == 0)
+ if (strncmp((char *)data, "set-", strlen("set-")) == 0)
{
- SetDebugportStatus(dbg_Webui, disabled);
+ Websocket_HandleSettings(data + strlen("set-"));
}
- else if (strcmp((char *)data, "foo") == 0)
+ else
{
- Debug_pushMessage("Got WebSocket Message 'foo' from client\n");
+ Debug_pushMessage("Got unknown Websocket-Message '%s' from client\n", (char *)data);
}
}
}
+void Websocket_HandleButtons(uint8_t *data)
+{
+ if (strcmp((char *)data, "debugstart") == 0)
+ {
+ SetDebugportStatus(dbg_Webui, enabled);
+ }
+ else if (strcmp((char *)data, "debugstop") == 0)
+ {
+ SetDebugportStatus(dbg_Webui, disabled);
+ }
+ else if (strcmp((char *)data, "measurereset") == 0)
+ {
+ globals.measuredPulses = 0;
+ }
+ else if (strcmp((char *)data, "measurestartstop") == 0)
+ {
+ globals.measurementActive = !globals.measurementActive;
+ }
+ else if (strcmp((char *)data, "purgenow") == 0)
+ {
+ globals.systemStatus = sysStat_Purge;
+ globals.purgePulses = LubeConfig.BleedingPulses;
+ }
+ else if (strcmp((char *)data, "reboot") == 0)
+ {
+ globals.systemStatus = sysStat_Shutdown;
+ }
+ else
+ {
+ Debug_pushMessage("Got unknown Button-id '%s' from ws-client\n", (char *)data);
+ }
+}
+
+void Websocket_HandleSettings(uint8_t *data)
+{
+ Debug_pushMessage("Got Settings-id and value '%s' from ws-client\n", (char *)data);
+}
+
/**
* @brief Pushes live debug messages to all WebSocket clients.
*