mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 06:32:19 +03:00
great changes
This commit is contained in:
167
src/udp_.cpp
Normal file
167
src/udp_.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
#include "Class/NotAsinc.h"
|
||||
#include "udp.h"
|
||||
#include "udp_.h"
|
||||
#include "Global.h"
|
||||
|
||||
static const char* MODULE = "Udp";
|
||||
|
||||
#ifdef ESP8266
|
||||
IPAddress udp_multicastIP(255, 255, 255, 255);
|
||||
WiFiUDP udp;
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
IPAddress udp_multicastIP(239, 255, 255, 255);
|
||||
AsyncUDP udp;
|
||||
#endif
|
||||
String remote_ip;
|
||||
String received;
|
||||
int udp_period;
|
||||
boolean udp_busy = false;
|
||||
unsigned int udp_port = 4210;
|
||||
|
||||
|
||||
#ifdef UDP_ENABLED
|
||||
void udpInit() {
|
||||
myNotAsincActions->add(
|
||||
do_UDPDATAPARSE, [&](void*) {
|
||||
do_udp_data_parse();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
myNotAsincActions->add(
|
||||
do_MQTTUDP, [&](void*) {
|
||||
send_mqtt_to_udp();
|
||||
},
|
||||
nullptr);
|
||||
|
||||
removeFile("dev.csv");
|
||||
addFileLn("dev.csv", "device id;device name;ip address");
|
||||
|
||||
#ifdef ESP8266
|
||||
udp.begin(udp_port);
|
||||
#endif
|
||||
|
||||
handleUdp_esp32();
|
||||
|
||||
randomSeed(micros());
|
||||
udp_period = random(50000, 60000);
|
||||
|
||||
ts.add(
|
||||
UDP, udp_period, [&](void*) {
|
||||
if (jsonReadBool(configSetupJson, "udponoff") && isNetworkActive() && !udp_busy) {
|
||||
pm.info("send info");
|
||||
String payload = "iotm;";
|
||||
payload += chipId;
|
||||
payload += ";";
|
||||
payload += jsonReadStr(configSetupJson, "name");
|
||||
#ifdef ESP8266
|
||||
udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
udp.write(payload.c_str());
|
||||
udp.endPacket();
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
udp.broadcast(line_to_send.c_str());
|
||||
#endif
|
||||
}
|
||||
},
|
||||
nullptr, false);
|
||||
}
|
||||
|
||||
bool isUdpEnabled() {
|
||||
return jsonReadBool(configSetupJson, "udponoff") && isNetworkActive();
|
||||
}
|
||||
|
||||
void loopUdp() {
|
||||
#ifdef ESP8266
|
||||
if (!isUdpEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int packetSize = udp.parsePacket();
|
||||
if (!packetSize) {
|
||||
return;
|
||||
}
|
||||
|
||||
char udp_packet[255];
|
||||
remote_ip = udp.remoteIP().toString();
|
||||
|
||||
pm.info(prettyBytes(packetSize) + " from " + remote_ip + ":" + udp.remotePort());
|
||||
|
||||
int len = udp.read(udp_packet, 255);
|
||||
if (len) {
|
||||
udp_packet[len] = '\x00';
|
||||
}
|
||||
received = String(udp_packet);
|
||||
if (received.indexOf("iotm;") >= 0 || received.indexOf("mqttServer") >= 0) {
|
||||
myNotAsincActions->make(do_UDPDATAPARSE);
|
||||
}
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
void handleUdp_esp32() {
|
||||
#ifdef ESP32
|
||||
if (udp.listenMulticast(udp_multicastIP, udp_port)) {
|
||||
udp.onPacket([](AsyncUDPPacket packet) {
|
||||
received = (char*)packet.data();
|
||||
remote_ip = packet.remoteIP().toString();
|
||||
if (jsonReadStr(configSetupJson, "udponoff") == "1") {
|
||||
if (received.indexOf("iotm;") >= 0) {
|
||||
myNotAsincActions->make(do_UDPDATAPARSE);
|
||||
}
|
||||
if (received.indexOf("mqttServer") >= 0) {
|
||||
myNotAsincActions->make(do_UDPDATAPARSE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void do_udp_data_parse() {
|
||||
if (received.indexOf("mqttServer") >= 0) {
|
||||
pm.info("received setting");
|
||||
jsonWriteStr(configSetupJson, "mqttServer", jsonReadStr(received, "mqttServer"));
|
||||
jsonWriteInt(configSetupJson, "mqttPort", jsonReadInt(received, "mqttPort"));
|
||||
jsonWriteStr(configSetupJson, "mqttPrefix", jsonReadStr(received, "mqttPrefix"));
|
||||
jsonWriteStr(configSetupJson, "mqttUser", jsonReadStr(received, "mqttUser"));
|
||||
jsonWriteStr(configSetupJson, "mqttPass", jsonReadStr(received, "mqttPass"));
|
||||
saveConfig();
|
||||
myNotAsincActions->make(do_MQTTPARAMSCHANGED);
|
||||
}
|
||||
if (received.indexOf("iotm;") >= 0) {
|
||||
add_dev_in_list("dev.csv", selectFromMarkerToMarker(received, ";", 1), selectFromMarkerToMarker(received, ";", 2), received);
|
||||
}
|
||||
}
|
||||
|
||||
void add_dev_in_list(String filename, String id, String dev_name, String ip) {
|
||||
auto file = seekFile("/" + filename);
|
||||
if (!file.find(id.c_str())) {
|
||||
addFileLn(filename, id + ";" + dev_name + "; <a href=\"http://" + ip + "\" target=\"_blank\"\">" + ip + "</a>");
|
||||
}
|
||||
}
|
||||
|
||||
void send_mqtt_to_udp() {
|
||||
if (!isUdpEnabled()) {
|
||||
return;
|
||||
}
|
||||
udp_busy = true;
|
||||
String data = "{}";
|
||||
jsonWriteStr(data, "mqttServer", jsonReadStr(configSetupJson, "mqttServer"));
|
||||
jsonWriteInt(data, "mqttPort", jsonReadInt(configSetupJson, "mqttPort"));
|
||||
jsonWriteStr(data, "mqttPrefix", jsonReadStr(configSetupJson, "mqttPrefix"));
|
||||
jsonWriteStr(data, "mqttUser", jsonReadStr(configSetupJson, "mqttUser"));
|
||||
jsonWriteStr(data, "mqttPass", jsonReadStr(configSetupJson, "mqttPass"));
|
||||
#ifdef ESP8266
|
||||
udp.beginPacketMulticast(udp_multicastIP, udp_port, WiFi.localIP());
|
||||
udp.write(data.c_str());
|
||||
udp.endPacket();
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
udp.broadcast(mqtt_data.c_str());
|
||||
#endif
|
||||
pm.info("sent info");
|
||||
udp_busy = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user