This commit is contained in:
Dmitry Borisenko
2021-12-05 00:50:52 +01:00
parent 2573e73b3a
commit f8307dcb5e
6 changed files with 27 additions and 40 deletions

File diff suppressed because one or more lines are too long

View File

@@ -4,6 +4,7 @@
namespace HttpServer { namespace HttpServer {
void initWS();
void init(); void init();
} // namespace HttpServer } // namespace HttpServer

View File

@@ -2,7 +2,7 @@
#ifdef WS_enable #ifdef WS_enable
AsyncWebSocket ws; AsyncWebSocket ws;
//AsyncEventSource events; AsyncEventSource events;
#endif #endif
TickerScheduler ts(MYTEST + 1); TickerScheduler ts(MYTEST + 1);
@@ -12,8 +12,8 @@ StringCommand sCmd;
AsyncWebServer server(80); AsyncWebServer server(80);
/* /*
* Global vars * Global vars
*/ */
boolean just_load = true; boolean just_load = true;
boolean telegramInitBeen = false; boolean telegramInitBeen = false;
@@ -43,14 +43,14 @@ int mqttConnectAttempts = 0;
bool changeBroker = false; bool changeBroker = false;
int currentBroker = 1; int currentBroker = 1;
//orders and events // orders and events
String orderBuf = ""; String orderBuf = "";
String eventBuf = ""; String eventBuf = "";
String mysensorBuf = ""; String mysensorBuf = "";
String itemsFile = ""; String itemsFile = "";
String itemsLine = ""; String itemsLine = "";
//key lists and numbers // key lists and numbers
//========================================= //=========================================
String impuls_KeyList = ""; String impuls_KeyList = "";
int impuls_EnterCounter = -1; int impuls_EnterCounter = -1;

View File

@@ -1,9 +1,12 @@
#include "Utils\SerialPrint.h" #include "Utils\SerialPrint.h"
#include "Global.h" #include "Global.h"
#include "HttpServer.h"
void SerialPrint(String errorLevel, String module, String msg) { void SerialPrint(String errorLevel, String module, String msg) {
//if (module == "MySensor") { Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
//} String tosend = "[" + errorLevel + "] [" + module + "] " + msg;
ws.textAll(tosend);
} }

View File

@@ -10,7 +10,6 @@ namespace HttpServer {
/* Forward declaration */ /* Forward declaration */
void initOta(); void initOta();
void initMDNS(); void initMDNS();
void initWS();
void init() { void init() {
String login = jsonReadStr(configSetupJson, "weblogin"); String login = jsonReadStr(configSetupJson, "weblogin");
@@ -84,10 +83,11 @@ void init() {
} }
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
#ifdef WS_enable #ifdef WEBSOCKET_ENABLED
if (type == WS_EVT_CONNECT) { if (type == WS_EVT_CONNECT) {
SerialPrint("I", F("WS"), F("CONNECTED"));
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id()); Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf(json.c_str(), client->id()); // client->printf(json.c_str(), client->id());
// client->ping(); // client->ping();
} else if (type == WS_EVT_DISCONNECT) { } else if (type == WS_EVT_DISCONNECT) {
Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id()); Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
@@ -115,6 +115,13 @@ void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventTyp
} }
Serial.printf("%s\n", msg.c_str()); Serial.printf("%s\n", msg.c_str());
if (msg.startsWith("HELLO")) {
SerialPrint("I", F("WS"), F("Full update"));
// publishWidgetsWS();
// publishStateWS();
// choose_log_date_and_send(); // функцию выгрузки архива с графиком я не сделал. Забираю при выгрузке по MQTT
}
if (info->opcode == WS_TEXT) if (info->opcode == WS_TEXT)
client->text("{}"); client->text("{}");
else else
@@ -198,7 +205,7 @@ void initOta() {
} }
void initWS() { void initWS() {
#ifdef WS_enable #ifdef WEBSOCKET_ENABLED
ws.onEvent(onWsEvent); ws.onEvent(onWsEvent);
server.addHandler(&ws); server.addHandler(&ws);
events.onConnect([](AsyncEventSourceClient *client) { events.onConnect([](AsyncEventSourceClient *client) {
@@ -206,7 +213,6 @@ void initWS() {
}); });
server.addHandler(&events); server.addHandler(&events);
#endif #endif
;
} }
} // namespace HttpServer } // namespace HttpServer

View File

@@ -9,6 +9,7 @@
#include "Cmd.h" #include "Cmd.h"
#include "FileSystem.h" #include "FileSystem.h"
#include "Global.h" #include "Global.h"
#include "HttpServer.h"
#include "Init.h" #include "Init.h"
#include "ItemsList.h" #include "ItemsList.h"
#include "MySensorsDataParse.h" #include "MySensorsDataParse.h"
@@ -66,6 +67,7 @@ void setup() {
uptime_init(); uptime_init();
upgradeInit(); upgradeInit();
HttpServer::init(); HttpServer::init();
HttpServer::initWS();
web_init(); web_init();
initSt(); initSt();
busInit(); busInit();
@@ -89,32 +91,7 @@ void setup() {
ts.add( ts.add(
MYTEST, 5000, [&](void*) { MYTEST, 5000, [&](void*) {
Serial.println(configSetupJson.length()); // ws.textAll("test msg from esp");
Serial.println(F("--------------------------------------"));
if (jsonWriteStr_(configSetupJson, "name", "test value")) {
Serial.println("write sucsess");
}
Serial.print("str test: ");
String valueStr;
if (jsonRead(configSetupJson, "name", valueStr)) {
Serial.println(valueStr);
}
Serial.print("int test: ");
int valueInt;
if (jsonRead(configSetupJson, "mqttPort", valueInt)) {
Serial.println(valueInt);
}
Serial.print("bool test: ");
bool valueBool;
if (jsonRead(configSetupJson, "telegonof", valueBool)) {
Serial.println(valueBool);
}
Serial.println(F("--------------------------------------"));
}, },
nullptr, true); nullptr, true);