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

Added possibility to update without ID

This commit is contained in:
Lukas Bachschwell 2017-11-13 16:10:56 +01:00
parent 404bf89f60
commit 0dc231bc9c
2 changed files with 185 additions and 107 deletions

View File

@ -3,141 +3,213 @@
#include <functional> #include <functional>
// Handle Websockets Communication // Handle Websockets Communication
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
switch(type) { AwsEventType type, void *arg, uint8_t *data, size_t len) {
case WS_EVT_DISCONNECT: switch (type) {
Serial.printf("Disconnected!\n"); case WS_EVT_DISCONNECT:
break; if(debug) Serial.printf("Disconnected!\n");
case WS_EVT_CONNECT: break;
{ case WS_EVT_CONNECT: {
Serial.println("Connected"); if(debug) Serial.println("Connected");
ESPUI.jsonDom(client); ESPUI.jsonDom(client);
Serial.println("JSON Data Sent to Client!"); if(debug) Serial.println("JSON Data Sent to Client!");
} } break;
break; case WS_EVT_DATA:
case WS_EVT_DATA: String msg = "";
String msg = ""; for (size_t i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) { msg += (char)data[i];
msg += (char) data[i];
}
if(msg.startsWith("bdown:")){
ESPUI.controls[msg.substring(6).toInt()]->callback(msg.substring(6).toInt(), B_DOWN);
}else if(msg.startsWith("bup:")){
ESPUI.controls[msg.substring(4).toInt()]->callback(msg.substring(4).toInt(), B_UP);
}else if(msg.startsWith("pfdown:")){
ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_FOR_DOWN);
}else if(msg.startsWith("pfup:")){
ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_FOR_UP);
}else if(msg.startsWith("pldown:")){
ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_LEFT_DOWN);
}else if(msg.startsWith("plup:")){
ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_LEFT_UP);
}else if(msg.startsWith("prdown:")){
ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_RIGHT_DOWN);
}else if(msg.startsWith("prup:")){
ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_RIGHT_UP);
}else if(msg.startsWith("pbdown:")){
ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_BACK_DOWN);
}else if(msg.startsWith("pbup:")){
ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_BACK_UP);
}else if(msg.startsWith("pcdown:")){
ESPUI.controls[msg.substring(7).toInt()]->callback(msg.substring(7).toInt(), P_CENTER_DOWN);
}else if(msg.startsWith("pcup:")){
ESPUI.controls[msg.substring(5).toInt()]->callback(msg.substring(5).toInt(), P_CENTER_UP);
}else if(msg.startsWith("sactive:")){
ESPUI.updateSwitcher(msg.substring(8).toInt(), true);
ESPUI.controls[msg.substring(8).toInt()]->callback(msg.substring(8).toInt(), S_ACTIVE);
}else if(msg.startsWith("sinactive:")){
ESPUI.updateSwitcher(msg.substring(10).toInt(), false);
ESPUI.controls[msg.substring(10).toInt()]->callback(msg.substring(10).toInt(), S_INACTIVE);
}
break;
} }
if (msg.startsWith("bdown:")) {
Control* c = ESPUI.controls[msg.substring(6).toInt()];
c->callback(c, B_DOWN);
} else if (msg.startsWith("bup:")) {
Control* c = ESPUI.controls[msg.substring(4).toInt()];
c->callback(c, B_UP);
} else if (msg.startsWith("pfdown:")) {
Control* c = ESPUI.controls[msg.substring(7).toInt()];
c->callback(c, P_FOR_DOWN);
} else if (msg.startsWith("pfup:")) {
Control* c = ESPUI.controls[msg.substring(5).toInt()];
c->callback(c, P_FOR_UP);
} else if (msg.startsWith("pldown:")) {
Control* c = ESPUI.controls[msg.substring(7).toInt()];
c->callback(c, P_LEFT_DOWN);
} else if (msg.startsWith("plup:")) {
Control* c = ESPUI.controls[msg.substring(5).toInt()];
c->callback(c, P_LEFT_UP);
} else if (msg.startsWith("prdown:")) {
Control* c = ESPUI.controls[msg.substring(7).toInt()];
c->callback(c, P_RIGHT_DOWN);
} else if (msg.startsWith("prup:")) {
Control* c = ESPUI.controls[msg.substring(5).toInt()];
c->callback(c, P_RIGHT_UP);
} else if (msg.startsWith("pbdown:")) {
Control* c = ESPUI.controls[msg.substring(7).toInt()];
c->callback(c, P_BACK_DOWN);
} else if (msg.startsWith("pbup:")) {
Control* c = ESPUI.controls[msg.substring(5).toInt()];
c->callback(c, P_BACK_UP);
} else if (msg.startsWith("pcdown:")) {
Control* c = ESPUI.controls[msg.substring(7).toInt()];
c->callback(c, P_CENTER_DOWN);
} else if (msg.startsWith("pcup:")) {
Control* c = ESPUI.controls[msg.substring(5).toInt()];
c->callback(c, P_CENTER_UP);
} else if (msg.startsWith("sactive:")) {
Control* c = ESPUI.controls[msg.substring(8).toInt()];
ESPUI.updateSwitcher(c->id, true);
c->callback(c, S_ACTIVE);
} else if (msg.startsWith("sinactive:")) {
Control* c = ESPUI.controls[msg.substring(10).toInt()];
ESPUI.updateSwitcher(c->id, false);
c->callback(c, S_INACTIVE);
}
break;
}
} }
void ESPUIClass::label(const char* label, String value){ void ESPUIClass::label(const char *label, String value) {
Control* newL = new Control(); if (labelExists(label)) {
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
return;
}
Control *newL = new Control();
newL->type = UI_LABEL; newL->type = UI_LABEL;
newL->label = label; newL->label = label;
if(value != "") newL->value = value; // Init with labeltext if (value != "")
else newL->value = String(label); newL->value = value; // Init with labeltext
else
newL->value = String(label);
newL->callback = NULL; newL->callback = NULL;
newL->id = cIndex;
controls[cIndex] = newL; controls[cIndex] = newL;
cIndex++; cIndex++;
} }
void ESPUIClass::button(const char* label, void(* callBack)(int, int)){ void ESPUIClass::button(const char *label, void (*callBack)(Control*, int)) {
Control* newB = new Control(); if (labelExists(label)) {
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
return;
}
Control *newB = new Control();
newB->type = UI_BUTTON; newB->type = UI_BUTTON;
newB->label = label; newB->label = label;
newB->callback = callBack; newB->callback = callBack;
newB->id = cIndex;
controls[cIndex] = newB; controls[cIndex] = newB;
cIndex++; cIndex++;
} }
void ESPUIClass::switcher(const char* label, bool startState, void(* callBack)(int, int)){ void ESPUIClass::switcher(const char *label, bool startState, void (*callBack)(Control*, int)) {
Control* newS = new Control(); if (labelExists(label)) {
if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
return;
}
Control *newS = new Control();
newS->type = UI_SWITCHER; newS->type = UI_SWITCHER;
newS->label = label; newS->label = label;
newS->value = String(startState); newS->value = String(startState);
newS->callback = callBack; newS->callback = callBack;
newS->id = cIndex;
controls[cIndex] = newS; controls[cIndex] = newS;
cIndex++; cIndex++;
} }
void ESPUIClass::pad(const char* label, bool center, void(* callBack)(int, int)){ void ESPUIClass::pad(const char *label, bool center, void (*callBack)(Control*, int)) {
Control* newP = new Control(); if (labelExists(label)) {
if(center)newP->type = UI_CPAD; if (debug) Serial.println("UI ERROR: Element " + String(label) + " exists, skipping creating element!");
else newP->type = UI_PAD; return;
}
Control *newP = new Control();
if (center)
newP->type = UI_CPAD;
else
newP->type = UI_PAD;
newP->label = label; newP->label = label;
newP->callback = callBack; newP->callback = callBack;
newP->id = cIndex;
controls[cIndex] = newP; controls[cIndex] = newP;
cIndex++; cIndex++;
} }
void ESPUIClass::print(int id, String value){ void ESPUIClass::print(int id, String value) {
if(id<cIndex && controls[id]->type == UI_LABEL){ if (id < cIndex && controls[id]->type == UI_LABEL) {
controls[id]->value = value; controls[id]->value = value;
String json; String json;
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject &root = jsonBuffer.createObject();
root["type"] = UPDATE_LABEL; root["type"] = UPDATE_LABEL;
root["value"] = value; root["value"] = value;
root["id"] = String(id); root["id"] = String(id);
root.printTo(json); root.printTo(json);
this->ws->textAll(json); this->ws->textAll(json);
}else{ } else {
Serial.println(String("Error: ")+ String(id) +String(" is no label")); if(debug) Serial.println(String("Error: ") + String(id) + String(" is no label"));
} }
} }
void ESPUIClass::updateSwitcher(int id, bool nValue){ void ESPUIClass::print(String label, String value) {
if(id<cIndex && controls[id]->type == UI_SWITCHER){ if (!labelExists(label)) {
controls[id]->value = nValue?1:0; if (debug) Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
return;
}
print(getIdByLabel(label), value);
}
void ESPUIClass::updateSwitcher(int id, bool nValue) {
if (id < cIndex && controls[id]->type == UI_SWITCHER) {
controls[id]->value = nValue ? 1 : 0;
String json; String json;
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject &root = jsonBuffer.createObject();
root["type"] = UPDATE_SWITCH; root["type"] = UPDATE_SWITCH;
root["value"] = nValue?1:0; root["value"] = nValue ? 1 : 0;
root["id"] = String(id); root["id"] = String(id);
root.printTo(json); root.printTo(json);
this->ws->textAll(json); this->ws->textAll(json);
}else{ } else {
Serial.println(String("Error: ")+ String(id) +String(" is no switcher")); if(debug) Serial.println(String("Error: ") + String(id) + String(" is no switcher"));
} }
} }
void ESPUIClass::updateSwitcher(String label, bool nValue) {
if (!labelExists(label)) {
if (debug)
Serial.println("UI ERROR: Element does not " + String(label) + " exist, cannot update!");
return;
}
updateSwitcher(getIdByLabel(label), nValue);
}
int ESPUIClass::getIdByLabel(String label) {
for (int i = 0; i < cIndex; i++) {
if (String(controls[i]->label) == label)
return i;
}
return -1; // failed, nonexistant
}
bool ESPUIClass::labelExists(String label) {
for (int i = 0; i < cIndex; i++) {
if (String(controls[i]->label) == label) return true;
}
return false;
}
// Convert & Transfer Arduino elements to JSON elements // Convert & Transfer Arduino elements to JSON elements
void ESPUIClass::jsonDom(AsyncWebSocketClient * client){ void ESPUIClass::jsonDom(AsyncWebSocketClient *client) {
for(int i=-1; i<cIndex; i++){ for (int i = -1; i < cIndex; i++) {
String json; String json;
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject &root = jsonBuffer.createObject();
if(i == -1){ if (i == -1) {
root["type"] = UI_TITEL; root["type"] = UI_TITEL;
root["label"] = String(ui_title); root["label"] = String(ui_title);
}else{ } else {
root["type"] = controls[i]->type; root["type"] = controls[i]->type;
root["label"] = String(controls[i]->label); root["label"] = String(controls[i]->label);
root["value"] = String(controls[i]->value); root["value"] = String(controls[i]->value);
@ -148,8 +220,7 @@ void ESPUIClass::jsonDom(AsyncWebSocketClient * client){
} }
} }
void ESPUIClass::begin(const char *_title) {
void ESPUIClass::begin(const char * _title){
ui_title = _title; ui_title = _title;
server = new AsyncWebServer(80); server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/ws"); ws = new AsyncWebSocket("/ws");
@ -158,17 +229,16 @@ void ESPUIClass::begin(const char * _title){
server->addHandler(ws); server->addHandler(ws);
server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); server->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
//Heap for general Servertest // Heap for general Servertest
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", String(ESP.getFreeHeap())); request->send(200, "text/plain", String(ESP.getFreeHeap()));
}); });
server->onNotFound([](AsyncWebServerRequest *request){ server->onNotFound(
request->send(404); [](AsyncWebServerRequest *request) { request->send(404); });
});
server->begin(); server->begin();
Serial.println("UI Initialized"); if(debug) Serial.println("UI Initialized");
} }
ESPUIClass ESPUI; ESPUIClass ESPUI;

View File

@ -3,6 +3,8 @@
#define HARDWARE "esp32" #define HARDWARE "esp32"
#define debug true
//ifdef 8266 //ifdef 8266
//#include "Hash.h" //#include "Hash.h"
@ -18,10 +20,11 @@
typedef struct Control typedef struct Control
{ {
unsigned int type; unsigned int type;
const char *label; unsigned int id; // just mirroring the id here for practical reasons
void (*callback)(int, int); const char *label;
String value; void (*callback)(Control*, int);
String value;
} Control; } Control;
// Types // Types
@ -53,30 +56,35 @@ typedef struct Control
class ESPUIClass{ class ESPUIClass {
public: public:
void begin(const char* _title); // Setup servers and page void begin(const char* _title); // Setup servers and page
// Creating Elements // Creating Elements
void label(const char* label, String value = ""); // Create Label void label(const char* label, String value = ""); // Create Label
void button(const char* label, void(* callBack)(int, int)); // Create Event Button void button(const char* label, void (* callBack)(Control*, int)); // Create Event Button
void switcher(const char* label, bool startState, void(* callBack)(int, int)); // Create Toggle Button void switcher(const char* label, bool startState, void (* callBack)(Control*, int)); // Create Toggle Button
void pad(const char* label, bool centerButton, void(* callBack)(int, int)); // Create Pad Control void pad(const char* label, bool centerButton, void (* callBack)(Control*, int)); // Create Pad Control
// Update Elements // Update Elements
void print(int id, String value); void print(int id, String value);
void updateSwitcher(int id, bool nValue); void print(String label, String value);
// Variables --- void updateSwitcher(int id, bool nValue);
const char* ui_title = "ESPUI"; // Store UI Title and Header Name void updateSwitcher(String label, bool nValue);
int cIndex; // Control index
Control* controls[25]; // Variables ---
void jsonDom(AsyncWebSocketClient * client); const char* ui_title = "ESPUI"; // Store UI Title and Header Name
int cIndex = 0; // Control index
Control* controls[25];
void jsonDom(AsyncWebSocketClient * client);
int getIdByLabel(String label);
bool labelExists(String label);
private: private:
AsyncWebServer* server; AsyncWebServer* server;
AsyncWebSocket* ws; AsyncWebSocket* ws;
}; };
extern ESPUIClass ESPUI; extern ESPUIClass ESPUI;