2022-02-07 16:36:29 +01:00
|
|
|
#include "DeviceList.h"
|
|
|
|
|
|
2022-02-14 00:12:58 +01:00
|
|
|
const String getThisDevice() {
|
2022-02-15 00:03:28 +01:00
|
|
|
String thisDevice = "{}";
|
2022-02-14 00:12:58 +01:00
|
|
|
jsonWriteStr_(thisDevice, F("devicelist"), ""); //метка для парсинга
|
|
|
|
|
jsonWriteStr_(thisDevice, F("ip"), jsonReadStr(settingsFlashJson, F("ip")));
|
|
|
|
|
jsonWriteStr_(thisDevice, F("id"), jsonReadStr(settingsFlashJson, F("id")));
|
|
|
|
|
jsonWriteStr_(thisDevice, F("name"), jsonReadStr(settingsFlashJson, F("name")));
|
|
|
|
|
thisDevice = "[" + thisDevice + "]";
|
|
|
|
|
return thisDevice;
|
|
|
|
|
}
|
2022-02-07 23:49:29 +01:00
|
|
|
|
2022-02-14 00:12:58 +01:00
|
|
|
void addThisDeviceToList() {
|
|
|
|
|
devListHeapJson = getThisDevice();
|
2022-02-16 00:53:52 +01:00
|
|
|
SerialPrint("i", "List", "Add this dev to list");
|
2022-02-13 16:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef UDP_ENABLED
|
|
|
|
|
AsyncUDP asyncUdp;
|
|
|
|
|
|
|
|
|
|
void asyncUdpInit() {
|
|
|
|
|
if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210)) {
|
|
|
|
|
asyncUdp.onPacket([](AsyncUDPPacket packet) {
|
|
|
|
|
// Serial.print("UDP Packet Type: ");
|
|
|
|
|
// Serial.println(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
|
|
|
|
|
// Serial.print("From: ");
|
|
|
|
|
// Serial.print(packet.remoteIP());
|
|
|
|
|
// Serial.print(":");
|
|
|
|
|
// Serial.println(packet.remotePort());
|
|
|
|
|
// Serial.print("To: ");
|
|
|
|
|
// Serial.print(packet.localIP());
|
|
|
|
|
// Serial.print(":");
|
|
|
|
|
// Serial.println(packet.localPort());
|
|
|
|
|
// Serial.print(", Length: ");
|
|
|
|
|
// Serial.print(packet.length());
|
|
|
|
|
// Serial.print(", Data: ");
|
|
|
|
|
// Serial.write(packet.data(), packet.length());
|
|
|
|
|
String data = uint8tToString(packet.data(), packet.length());
|
2022-02-14 00:12:58 +01:00
|
|
|
// Serial.println(data);
|
2022-02-13 16:13:51 +01:00
|
|
|
if (udpPacketValidation(data)) {
|
2022-02-22 20:16:33 +01:00
|
|
|
SerialPrint("i", F("UDP"), "IP: " + packet.remoteIP().toString() + ":" + String(packet.remotePort()));
|
2022-02-14 00:12:58 +01:00
|
|
|
// Serial.println(data);
|
2022-02-15 00:03:28 +01:00
|
|
|
jsonMergeArrays(devListHeapJson, data);
|
2022-02-14 00:12:58 +01:00
|
|
|
// Serial.println(devListHeapJson);
|
2022-02-13 16:13:51 +01:00
|
|
|
} else {
|
|
|
|
|
SerialPrint("E", F("UDP"), F("Udp packet invalid"));
|
|
|
|
|
}
|
|
|
|
|
// reply to the client
|
|
|
|
|
// String ip = WiFi.localIP().toString();
|
|
|
|
|
// asyncUdp.broadcastTo(ip.c_str(), packet.remotePort());
|
|
|
|
|
// packet.printf(ip.c_str(), packet.length());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 16:40:46 +01:00
|
|
|
//будем отправлять каждые 30 секунд презентацию данного устройства
|
2022-02-13 16:13:51 +01:00
|
|
|
ts.add(
|
2022-04-24 11:57:38 +03:00
|
|
|
UDP, 60000, [&](void*) { //UDPP
|
2022-02-22 16:40:46 +01:00
|
|
|
if (isNetworkActive()) {
|
|
|
|
|
SerialPrint("i", F("UDP"), F("Broadcast device presentation"));
|
|
|
|
|
asyncUdp.broadcastTo(getThisDevice().c_str(), 4210);
|
|
|
|
|
// asyncUdp.broadcast("test");
|
|
|
|
|
// asyncUdp.print("Hello Server!");
|
|
|
|
|
}
|
2022-02-13 16:13:51 +01:00
|
|
|
},
|
|
|
|
|
nullptr, true);
|
|
|
|
|
|
2022-02-16 00:53:52 +01:00
|
|
|
SerialPrint("i", F("UDP"), F("Udp Init"));
|
2022-02-13 16:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool udpPacketValidation(String& data) {
|
|
|
|
|
if (data.indexOf("devicelist") != -1) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 00:03:28 +01:00
|
|
|
void jsonMergeArrays(String& existJson, String& incJson) {
|
|
|
|
|
DynamicJsonDocument incJsonDoc(1024);
|
|
|
|
|
DeserializationError incJsonError = deserializeJson(incJsonDoc, incJson);
|
|
|
|
|
if (incJsonError) {
|
|
|
|
|
SerialPrint("E", F("UDP"), "Invailed json in incomming udp packet " + String(incJsonError.f_str()));
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
return;
|
2022-02-13 16:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-15 00:03:28 +01:00
|
|
|
DynamicJsonDocument existJsonDoc(1024);
|
|
|
|
|
DeserializationError existJsonError = deserializeJson(existJsonDoc, existJson);
|
|
|
|
|
if (existJsonError) {
|
|
|
|
|
SerialPrint("E", F("UDP"), "Invailed json in existing udp dev list " + String(incJsonError.f_str()));
|
|
|
|
|
jsonErrorDetected();
|
|
|
|
|
return;
|
2022-02-13 16:13:51 +01:00
|
|
|
}
|
2022-02-15 00:03:28 +01:00
|
|
|
JsonArray existJsonArr = existJsonDoc.as<JsonArray>();
|
2022-02-13 16:13:51 +01:00
|
|
|
|
2022-02-15 00:03:28 +01:00
|
|
|
incJson.replace("[", "");
|
|
|
|
|
incJson.replace("]", "");
|
|
|
|
|
String incIP = jsonReadStr(incJson, "ip");
|
2022-02-14 00:12:58 +01:00
|
|
|
String outArr = "[";
|
|
|
|
|
bool ipExistInList = false;
|
2022-02-15 00:03:28 +01:00
|
|
|
int i = 0;
|
|
|
|
|
for (JsonVariant value : existJsonArr) {
|
2022-02-14 00:12:58 +01:00
|
|
|
String locIP = value["ip"].as<String>();
|
|
|
|
|
if (locIP == incIP) {
|
2022-02-15 00:03:28 +01:00
|
|
|
if (i == 0) {
|
|
|
|
|
outArr += incJson;
|
|
|
|
|
} else {
|
|
|
|
|
outArr += ("," + incJson);
|
|
|
|
|
}
|
2022-02-14 00:12:58 +01:00
|
|
|
ipExistInList = true;
|
|
|
|
|
} else {
|
2022-02-15 00:03:28 +01:00
|
|
|
if (i == 0) {
|
|
|
|
|
outArr += value.as<String>();
|
|
|
|
|
} else {
|
|
|
|
|
outArr += ("," + value.as<String>());
|
|
|
|
|
}
|
2022-02-14 00:12:58 +01:00
|
|
|
}
|
2022-02-15 00:03:28 +01:00
|
|
|
i++;
|
2022-02-14 00:12:58 +01:00
|
|
|
}
|
|
|
|
|
if (!ipExistInList) {
|
2022-02-15 00:03:28 +01:00
|
|
|
outArr += "," + incJson;
|
2022-02-14 00:12:58 +01:00
|
|
|
}
|
|
|
|
|
outArr = outArr + "]";
|
2022-02-15 00:03:28 +01:00
|
|
|
existJson = outArr;
|
2022-02-14 00:12:58 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-13 16:13:51 +01:00
|
|
|
String uint8tToString(uint8_t* data, size_t len) {
|
|
|
|
|
String ret;
|
|
|
|
|
while (len--) {
|
|
|
|
|
ret += (char)*data++;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|