From 650411bac4a5dcc3e91096f736b13840e717e904 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Sun, 24 Mar 2019 18:46:34 +0100 Subject: [PATCH] Consistent use of updateControl on websocket event - rename updateControl to updateControlValue - some earlie returns --- README.md | 2 +- src/ESPUI.cpp | 67 +++++++++++++++++++++++++++------------------------ src/ESPUI.h | 8 +++--- 3 files changed, 42 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 782e371..41cd6eb 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ THIS IS THE 2.0.0 DEVELOPMENT BRANCH, NOT GUARANTIED TO WORK - less updateCotrol functions - proper wrappers for all create/update actions - OptionList by @eringerli -- Better returnvalues +- Better return values - Min Max on slider - Accelerometer Widget - Cleanup Example diff --git a/src/ESPUI.cpp b/src/ESPUI.cpp index 7496b44..b9ad932 100644 --- a/src/ESPUI.cpp +++ b/src/ESPUI.cpp @@ -353,23 +353,28 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp } else if (msg.startsWith("pcup:")) { c->callback(c, P_CENTER_UP); } else if (msg.startsWith("sactive:")) { - ESPUI.updateSwitcher(c->id, true); + c->value = "1"; + ESPUI.updateControl(c, client->id()); c->callback(c, S_ACTIVE); } else if (msg.startsWith("sinactive:")) { - ESPUI.updateSwitcher(c->id, false); + c->value = "0"; + ESPUI.updateControl(c, client->id()); c->callback(c, S_INACTIVE); } else if (msg.startsWith("slvalue:")) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); - ESPUI.updateControl(c); + ESPUI.updateControl(c, client->id()); c->callback(c, SL_VALUE); } else if (msg.startsWith("nvalue:")) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); + ESPUI.updateControl(c, client->id()); c->callback(c, N_VALUE); } else if (msg.startsWith("tvalue:")) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); + ESPUI.updateControl(c, client->id()); c->callback(c, T_VALUE); } else if (msg.startsWith("svalue:")) { c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); + ESPUI.updateControl(c, client->id()); c->callback(c, S_VALUE); } else { if (ESPUI.verbosity) { @@ -463,32 +468,32 @@ void ESPUIClass::updateControl(Control *control, int clientId) { root["color"] = (int)control->color; serializeJson(document, json); - if (clientId > 0) { - // This is a hacky workaround because ESPAsyncWebServer does not have a - // function like this and it's clients array is private - int tryId = 0; + if (this->verbosity >= Verbosity::VerboseJSON) { + Serial.println(json); + } - for (int count = 0; count < this->ws->count();) { - if (this->ws->hasClient(tryId)) { - if (clientId != tryId) { - this->ws->client(tryId)->text(json); + if (clientId < 0) { + this->ws->textAll(json); + return; + } + // This is a hacky workaround because ESPAsyncWebServer does not have a + // function like this and it's clients array is private + int tryId = 0; - if (this->verbosity >= Verbosity::VerboseJSON) { - Serial.println(json); - } + for (int count = 0; count < this->ws->count();) { + if (this->ws->hasClient(tryId)) { + if (clientId != tryId) { + this->ws->client(tryId)->text(json); + + if (this->verbosity >= Verbosity::VerboseJSON) { + Serial.println(json); } - - count++; } - tryId++; - } - } else { - if (this->verbosity >= Verbosity::VerboseJSON) { - Serial.println(json); + count++; } - this->ws->textAll(json); + tryId++; } } @@ -504,7 +509,7 @@ void ESPUIClass::updateControl(uint16_t id, int clientId) { } } -void ESPUIClass::updateControl(Control *control, String value, int clientId) { +void ESPUIClass::updateControlValue(Control *control, String value, int clientId) { if (!control) { return; } @@ -513,7 +518,7 @@ void ESPUIClass::updateControl(Control *control, String value, int clientId) { updateControl(control, clientId); } -void ESPUIClass::updateControl(uint16_t id, String value, int clientId) { +void ESPUIClass::updateControlValue(uint16_t id, String value, int clientId) { Control *control = getControl(id); if (control) { @@ -525,19 +530,19 @@ void ESPUIClass::updateControl(uint16_t id, String value, int clientId) { } } -void ESPUIClass::print(uint16_t id, String value) { updateControl(id, value); } +void ESPUIClass::print(uint16_t id, String value) { updateControlValue(id, value); } -void ESPUIClass::updateLabel(uint16_t id, String value) { updateControl(id, value); } +void ESPUIClass::updateLabel(uint16_t id, String value) { updateControlValue(id, value); } -void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) { updateControl(id, String(nValue), clientId); } +void ESPUIClass::updateSlider(uint16_t id, int nValue, int clientId) { updateControlValue(id, String(nValue), clientId); } -void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) { updateControl(id, String(nValue ? "1" : "0"), clientId); } +void ESPUIClass::updateSwitcher(uint16_t id, bool nValue, int clientId) { updateControlValue(id, String(nValue ? "1" : "0"), clientId); } -void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) { updateControl(id, String(number), clientId); } +void ESPUIClass::updateNumber(uint16_t id, int number, int clientId) { updateControlValue(id, String(number), clientId); } -void ESPUIClass::updateText(uint16_t id, String text, int clientId) { updateControl(id, text, clientId); } +void ESPUIClass::updateText(uint16_t id, String text, int clientId) { updateControlValue(id, text, clientId); } -void ESPUIClass::updateSelect(uint16_t id, String text, int clientId) { updateControl(id, text, clientId); } +void ESPUIClass::updateSelect(uint16_t id, String text, int clientId) { updateControlValue(id, text, clientId); } /* Convert & Transfer Arduino elements to JSON elements diff --git a/src/ESPUI.h b/src/ESPUI.h index 1b0b1eb..46dcc03 100644 --- a/src/ESPUI.h +++ b/src/ESPUI.h @@ -163,7 +163,7 @@ enum Verbosity : uint8_t { Quiet = 0, Verbose, VerboseJSON }; class ESPUIClass { public: ESPUIClass() { verbosity = Verbosity::Quiet; } - ยด void setVerbosity(Verbosity verbosity); + void setVerbosity(Verbosity verbosity); void begin(const char *_title, const char *username = nullptr, const char *password = nullptr); // Setup server and page in Memorymode void beginSPIFFS(const char *_title, const char *username = nullptr, const char *password = nullptr); // Setup server and page in SPIFFSmode @@ -173,6 +173,7 @@ public: uint16_t addControl(ControlType type, const char *label, String value = String(""), ControlColor color = ControlColor::Turquoise, uint16_t parentControl = Control::noParent, void (*callback)(Control *, int) = nullptr); + // create Elements int button(const char *label, void (*callback)(Control *, int), ControlColor color, String value = ""); // Create Event Button int switcher(const char *label, bool startState, void (*callback)(Control *, int), ControlColor color); // Create Toggle Button int pad(const char *label, bool centerButton, void (*callback)(Control *, int), ControlColor color); // Create Pad Control @@ -189,8 +190,9 @@ public: Control *getControl(uint16_t id); // Update Elements - void updateControl(uint16_t id, String value, int clientId = -1); - void updateControl(Control *control, String value, int clientId = -1); + void updateControlValue(uint16_t id, String value, int clientId = -1); + void updateControlValue(uint16_t id, String value, int clientId = -1); + void updateControlValue(Control *control, String value, int clientId = -1); void updateControl(uint16_t id, int clientId = -1); void updateControl(Control *control, int clientId = -1);