mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 11:59:12 +03:00
сокеты
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "сlasses/sendJsonWs.h"
|
|
||||||
|
|
||||||
class SendJson;
|
class SendJson;
|
||||||
|
|
||||||
class SendJson : SendJsonWs {
|
class SendJson {
|
||||||
public:
|
public:
|
||||||
SendJson();
|
SendJson();
|
||||||
~SendJson();
|
~SendJson();
|
||||||
@@ -13,6 +12,10 @@ class SendJson : SendJsonWs {
|
|||||||
|
|
||||||
void loop();
|
void loop();
|
||||||
|
|
||||||
|
void sendWs(String& jsonArrayElement);
|
||||||
|
|
||||||
|
void sendMqtt(String& jsonArrayElement);
|
||||||
|
|
||||||
uint8_t _num;
|
uint8_t _num;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include "Global.h"
|
|
||||||
|
|
||||||
class SendJsonWs;
|
|
||||||
|
|
||||||
class SendJsonWs {
|
|
||||||
public:
|
|
||||||
SendJsonWs();
|
|
||||||
~SendJsonWs();
|
|
||||||
|
|
||||||
void send(String& jsonArrayElement, uint8_t& _num);
|
|
||||||
};
|
|
||||||
@@ -189,6 +189,10 @@ void standWebSocketsInit() {
|
|||||||
|
|
||||||
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
case WStype_ERROR: {
|
||||||
|
Serial.printf("[%u] Error!\n", num);
|
||||||
|
} break;
|
||||||
|
|
||||||
case WStype_DISCONNECTED: {
|
case WStype_DISCONNECTED: {
|
||||||
Serial.printf("[%u] Disconnected!\n", num);
|
Serial.printf("[%u] Disconnected!\n", num);
|
||||||
} break;
|
} break;
|
||||||
@@ -196,13 +200,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
|
|||||||
case WStype_CONNECTED: {
|
case WStype_CONNECTED: {
|
||||||
IPAddress ip = standWebSocket.remoteIP(num);
|
IPAddress ip = standWebSocket.remoteIP(num);
|
||||||
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
Serial.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
|
||||||
|
|
||||||
// send message to client
|
|
||||||
standWebSocket.sendTXT(num, "Connected");
|
standWebSocket.sendTXT(num, "Connected");
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case WStype_TEXT: {
|
case WStype_TEXT: {
|
||||||
Serial.printf("[%u] get Text: %s\n", num, payload);
|
// Serial.printf("[%u] get Text: %s\n", num, payload);
|
||||||
|
|
||||||
String payloadStr;
|
String payloadStr;
|
||||||
payloadStr.reserve(length + 1);
|
payloadStr.reserve(length + 1);
|
||||||
@@ -210,29 +212,49 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
|
|||||||
payloadStr += (char)payload[i];
|
payloadStr += (char)payload[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payloadStr.startsWith("/config")) {
|
if (payloadStr.startsWith("/config")) { //если прилетел url страницы /config то отправим пакеты с меткой /config.json
|
||||||
if (mySendJson) mySendJson->sendFile("/config.json", num);
|
if (mySendJson) mySendJson->sendFile("/config.json", num);
|
||||||
}
|
}
|
||||||
|
if (payloadStr.startsWith("/gifnoc.json")) { //если прилетел измененный пакет с меткой /gifnoc то перепишем файл
|
||||||
if (payloadStr.startsWith("/changed")) {
|
payloadStr.replace("/gifnoc.json", "");
|
||||||
payloadStr.replace("/changed", "");
|
|
||||||
writeFile(F("config.json"), payloadStr);
|
writeFile(F("config.json"), payloadStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send message to client
|
|
||||||
// standWebSocket.sendTXT(num, "message here");
|
|
||||||
|
|
||||||
// send data to all connected clients
|
|
||||||
// standWebSocket.broadcastTXT("message here");
|
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
case WStype_BIN: {
|
case WStype_BIN: {
|
||||||
Serial.printf("[%u] get binary length: %u\n", num, length);
|
Serial.printf("[%u] get binary length: %u\n", num, length);
|
||||||
// hexdump(payload, length);
|
// hexdump(payload, length);
|
||||||
|
|
||||||
// send message to client
|
|
||||||
// standWebSocket.sendBIN(num, payload, length);
|
// standWebSocket.sendBIN(num, payload, length);
|
||||||
} break;
|
} break;
|
||||||
|
|
||||||
|
case WStype_FRAGMENT_TEXT_START: {
|
||||||
|
Serial.printf("[%u] fragment test start: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case WStype_FRAGMENT_BIN_START: {
|
||||||
|
Serial.printf("[%u] fragment bin start: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case WStype_FRAGMENT: {
|
||||||
|
Serial.printf("[%u] fragment: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case WStype_FRAGMENT_FIN: {
|
||||||
|
Serial.printf("[%u] fragment finish: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case WStype_PING: {
|
||||||
|
Serial.printf("[%u] ping: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case WStype_PONG: {
|
||||||
|
Serial.printf("[%u] pong: %u\n", num, length);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
Serial.printf("[%u] not recognized: %u\n", num, length);
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#include "сlasses/sendJson.h"
|
#include "сlasses/sendJson.h"
|
||||||
|
|
||||||
//базовый класс
|
|
||||||
|
|
||||||
SendJson::SendJson() {}
|
SendJson::SendJson() {}
|
||||||
SendJson::~SendJson() {}
|
SendJson::~SendJson() {}
|
||||||
|
|
||||||
@@ -14,8 +12,16 @@ void SendJson::sendFile(String path, uint8_t num) {
|
|||||||
void SendJson::loop() {
|
void SendJson::loop() {
|
||||||
if (file.available()) {
|
if (file.available()) {
|
||||||
String jsonArrayElement = _path + file.readStringUntil('}') + "}";
|
String jsonArrayElement = _path + file.readStringUntil('}') + "}";
|
||||||
send(jsonArrayElement, _num);
|
sendWs(jsonArrayElement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendJson::sendWs(String& jsonArrayElement) {
|
||||||
|
standWebSocket.sendTXT(_num, jsonArrayElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendJson::sendMqtt(String& jsonArrayElement) {
|
||||||
|
// mqtt send to do
|
||||||
|
}
|
||||||
|
|
||||||
SendJson* mySendJson;
|
SendJson* mySendJson;
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
#include "сlasses/sendJsonWs.h"
|
|
||||||
|
|
||||||
SendJsonWs::SendJsonWs() {}
|
|
||||||
SendJsonWs::~SendJsonWs() {}
|
|
||||||
|
|
||||||
void SendJsonWs::send(String& jsonArrayElement, uint8_t& _num) {
|
|
||||||
standWebSocket.sendTXT(_num, jsonArrayElement);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user