1
0
mirror of https://github.com/s00500/ESPUI.git synced 2024-11-22 04:00:55 +00:00

Consistent use of updateControl on websocket event

- rename updateControl to updateControlValue
- some earlie returns
This commit is contained in:
Lukas Bachschwell 2019-03-24 18:46:34 +01:00
parent b66d41a98b
commit 650411bac4
3 changed files with 42 additions and 35 deletions

View File

@ -30,7 +30,7 @@ THIS IS THE 2.0.0 DEVELOPMENT BRANCH, NOT GUARANTIED TO WORK
- less updateCotrol functions - less updateCotrol functions
- proper wrappers for all create/update actions - proper wrappers for all create/update actions
- OptionList by @eringerli - OptionList by @eringerli
- Better returnvalues - Better return values
- Min Max on slider - Min Max on slider
- Accelerometer Widget - Accelerometer Widget
- Cleanup Example - Cleanup Example

View File

@ -353,23 +353,28 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
} else if (msg.startsWith("pcup:")) { } else if (msg.startsWith("pcup:")) {
c->callback(c, P_CENTER_UP); c->callback(c, P_CENTER_UP);
} else if (msg.startsWith("sactive:")) { } else if (msg.startsWith("sactive:")) {
ESPUI.updateSwitcher(c->id, true); c->value = "1";
ESPUI.updateControl(c, client->id());
c->callback(c, S_ACTIVE); c->callback(c, S_ACTIVE);
} else if (msg.startsWith("sinactive:")) { } else if (msg.startsWith("sinactive:")) {
ESPUI.updateSwitcher(c->id, false); c->value = "0";
ESPUI.updateControl(c, client->id());
c->callback(c, S_INACTIVE); c->callback(c, S_INACTIVE);
} else if (msg.startsWith("slvalue:")) { } else if (msg.startsWith("slvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c); ESPUI.updateControl(c, client->id());
c->callback(c, SL_VALUE); c->callback(c, SL_VALUE);
} else if (msg.startsWith("nvalue:")) { } else if (msg.startsWith("nvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id());
c->callback(c, N_VALUE); c->callback(c, N_VALUE);
} else if (msg.startsWith("tvalue:")) { } else if (msg.startsWith("tvalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id());
c->callback(c, T_VALUE); c->callback(c, T_VALUE);
} else if (msg.startsWith("svalue:")) { } else if (msg.startsWith("svalue:")) {
c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':')); c->value = msg.substring(msg.indexOf(':') + 1, msg.lastIndexOf(':'));
ESPUI.updateControl(c, client->id());
c->callback(c, S_VALUE); c->callback(c, S_VALUE);
} else { } else {
if (ESPUI.verbosity) { if (ESPUI.verbosity) {
@ -463,32 +468,32 @@ void ESPUIClass::updateControl(Control *control, int clientId) {
root["color"] = (int)control->color; root["color"] = (int)control->color;
serializeJson(document, json); serializeJson(document, json);
if (clientId > 0) { if (this->verbosity >= Verbosity::VerboseJSON) {
// This is a hacky workaround because ESPAsyncWebServer does not have a Serial.println(json);
// function like this and it's clients array is private }
int tryId = 0;
for (int count = 0; count < this->ws->count();) { if (clientId < 0) {
if (this->ws->hasClient(tryId)) { this->ws->textAll(json);
if (clientId != tryId) { return;
this->ws->client(tryId)->text(json); }
// 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) { for (int count = 0; count < this->ws->count();) {
Serial.println(json); if (this->ws->hasClient(tryId)) {
} if (clientId != tryId) {
this->ws->client(tryId)->text(json);
if (this->verbosity >= Verbosity::VerboseJSON) {
Serial.println(json);
} }
count++;
} }
tryId++; count++;
}
} else {
if (this->verbosity >= Verbosity::VerboseJSON) {
Serial.println(json);
} }
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) { if (!control) {
return; return;
} }
@ -513,7 +518,7 @@ void ESPUIClass::updateControl(Control *control, String value, int clientId) {
updateControl(control, 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); Control *control = getControl(id);
if (control) { 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 Convert & Transfer Arduino elements to JSON elements

View File

@ -163,7 +163,7 @@ enum Verbosity : uint8_t { Quiet = 0, Verbose, VerboseJSON };
class ESPUIClass { class ESPUIClass {
public: public:
ESPUIClass() { verbosity = Verbosity::Quiet; } 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 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 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 addControl(ControlType type, const char *label, String value = String(""), ControlColor color = ControlColor::Turquoise,
uint16_t parentControl = Control::noParent, void (*callback)(Control *, int) = nullptr); 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 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 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 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); Control *getControl(uint16_t id);
// Update Elements // Update Elements
void updateControl(uint16_t id, String value, int clientId = -1); void updateControlValue(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(Control *control, String value, int clientId = -1);
void updateControl(uint16_t id, int clientId = -1); void updateControl(uint16_t id, int clientId = -1);
void updateControl(Control *control, int clientId = -1); void updateControl(Control *control, int clientId = -1);