|
|
@ -45,13 +45,13 @@ char _last_modified[50]; |
|
|
|
|
|
|
|
bool wsSend(const char * payload) { |
|
|
|
if (ws.count() > 0) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] Broadcasting '%s'\n", payload); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Broadcasting '%s'\n"), payload); |
|
|
|
ws.textAll(payload); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool wsSend(uint32_t client_id, const char * payload) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] Sending '%s' to #%ld\n", payload, client_id); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Sending '%s' to #%ld\n"), payload, client_id); |
|
|
|
ws.text(client_id, payload); |
|
|
|
} |
|
|
|
|
|
|
@ -73,7 +73,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { |
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
|
JsonObject& root = jsonBuffer.parseObject((char *) payload); |
|
|
|
if (!root.success()) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] Error parsing data\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Error parsing data\n")); |
|
|
|
ws.text(client_id, "{\"message\": \"Error parsing data!\"}"); |
|
|
|
return; |
|
|
|
} |
|
|
@ -83,7 +83,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { |
|
|
|
|
|
|
|
String action = root["action"]; |
|
|
|
|
|
|
|
DEBUG_MSG("[WEBSOCKET] Requested action: %s\n", action.c_str()); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Requested action: %s\n"), action.c_str()); |
|
|
|
|
|
|
|
if (action.equals("reset")) { |
|
|
|
ESP.restart(); |
|
|
@ -123,7 +123,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { |
|
|
|
if (action.equals("relay") && root.containsKey("data")) { |
|
|
|
|
|
|
|
JsonObject& data = root["data"]; |
|
|
|
|
|
|
|
|
|
|
|
if (data.containsKey("status")) { |
|
|
|
|
|
|
|
bool state = (strcmp(data["status"], "1") == 0); |
|
|
@ -152,7 +152,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { |
|
|
|
if (root.containsKey("config") && root["config"].is<JsonArray&>()) { |
|
|
|
|
|
|
|
JsonArray& config = root["config"]; |
|
|
|
DEBUG_MSG("[WEBSOCKET] Parsing configuration data\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Parsing configuration data\n")); |
|
|
|
|
|
|
|
unsigned char webMode = WEB_MODE_NORMAL; |
|
|
|
|
|
|
@ -274,7 +274,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { |
|
|
|
} |
|
|
|
|
|
|
|
if (value != getSetting(key)) { |
|
|
|
//DEBUG_MSG("[WEBSOCKET] Storing %s = %s\n", key.c_str(), value.c_str());
|
|
|
|
//DEBUG_MSG_P(PSTR("[WEBSOCKET] Storing %s = %s\n", key.c_str(), value.c_str()));
|
|
|
|
setSetting(key, value); |
|
|
|
save = changed = true; |
|
|
|
if (key.startsWith("mqtt")) changedMQTT = true; |
|
|
@ -533,7 +533,7 @@ bool _wsAuth(AsyncWebSocketClient * client) { |
|
|
|
} |
|
|
|
|
|
|
|
if (index == WS_BUFFER_SIZE) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] Validation check failed\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] Validation check failed\n")); |
|
|
|
ws.text(client->id(), "{\"message\": \"Session expired, please reload page...\"}"); |
|
|
|
return false; |
|
|
|
} |
|
|
@ -553,14 +553,14 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy |
|
|
|
|
|
|
|
if (type == WS_EVT_CONNECT) { |
|
|
|
IPAddress ip = client->remoteIP(); |
|
|
|
DEBUG_MSG("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n", client->id(), ip[0], ip[1], ip[2], ip[3], server->url()); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u connected, ip: %d.%d.%d.%d, url: %s\n"), client->id(), ip[0], ip[1], ip[2], ip[3], server->url()); |
|
|
|
_wsStart(client->id()); |
|
|
|
} else if(type == WS_EVT_DISCONNECT) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] #%u disconnected\n", client->id()); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u disconnected\n"), client->id()); |
|
|
|
} else if(type == WS_EVT_ERROR) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] #%u error(%u): %s\n", client->id(), *((uint16_t*)arg), (char*)data); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u error(%u): %s\n"), client->id(), *((uint16_t*)arg), (char*)data); |
|
|
|
} else if(type == WS_EVT_PONG) { |
|
|
|
DEBUG_MSG("[WEBSOCKET] #%u pong(%u): %s\n", client->id(), len, len ? (char*) data : ""); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSOCKET] #%u pong(%u): %s\n"), client->id(), len, len ? (char*) data : ""); |
|
|
|
} else if(type == WS_EVT_DATA) { |
|
|
|
|
|
|
|
AwsFrameInfo * info = (AwsFrameInfo*)arg; |
|
|
@ -588,7 +588,7 @@ void _wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTy |
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void webLogRequest(AsyncWebServerRequest *request) { |
|
|
|
DEBUG_MSG("[WEBSERVER] Request: %s %s\n", request->methodToString(), request->url().c_str()); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSERVER] Request: %s %s\n"), request->methodToString(), request->url().c_str()); |
|
|
|
} |
|
|
|
|
|
|
|
bool _authenticate(AsyncWebServerRequest *request) { |
|
|
@ -601,20 +601,20 @@ bool _authenticate(AsyncWebServerRequest *request) { |
|
|
|
bool _authAPI(AsyncWebServerRequest *request) { |
|
|
|
|
|
|
|
if (getSetting("apiEnabled").toInt() == 0) { |
|
|
|
DEBUG_MSG("[WEBSERVER] HTTP API is not enabled\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSERVER] HTTP API is not enabled\n")); |
|
|
|
request->send(403); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
if (!request->hasParam("apikey", (request->method() == HTTP_PUT))) { |
|
|
|
DEBUG_MSG("[WEBSERVER] Missing apikey parameter\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSERVER] Missing apikey parameter\n")); |
|
|
|
request->send(403); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
AsyncWebParameter* p = request->getParam("apikey", (request->method() == HTTP_PUT)); |
|
|
|
if (!p->value().equals(getSetting("apiKey"))) { |
|
|
|
DEBUG_MSG("[WEBSERVER] Wrong apikey parameter\n"); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSERVER] Wrong apikey parameter\n")); |
|
|
|
request->send(403); |
|
|
|
return false; |
|
|
|
} |
|
|
@ -725,7 +725,7 @@ void _onRPC(AsyncWebServerRequest *request) { |
|
|
|
|
|
|
|
AsyncWebParameter* p = request->getParam("action"); |
|
|
|
String action = p->value(); |
|
|
|
DEBUG_MSG("[RPC] Action: %s\n", action.c_str()); |
|
|
|
DEBUG_MSG_P(PSTR("[RPC] Action: %s\n"), action.c_str()); |
|
|
|
|
|
|
|
if (action.equals("reset")) { |
|
|
|
response = 200; |
|
|
@ -853,6 +853,6 @@ void webSetup() { |
|
|
|
|
|
|
|
// Run server
|
|
|
|
_server->begin(); |
|
|
|
DEBUG_MSG("[WEBSERVER] Webserver running on port %d\n", getSetting("webPort", WEBSERVER_PORT).toInt()); |
|
|
|
DEBUG_MSG_P(PSTR("[WEBSERVER] Webserver running on port %d\n"), getSetting("webPort", WEBSERVER_PORT).toInt()); |
|
|
|
|
|
|
|
} |