mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
changes
This commit is contained in:
@@ -23,12 +23,6 @@
|
||||
"action": "/?set.device",
|
||||
"class": "btn btn-block btn-default"
|
||||
},
|
||||
{
|
||||
"type": "link",
|
||||
"title": "Список других устройств в сети",
|
||||
"action": "/?set.udp",
|
||||
"class": "btn btn-block btn-default"
|
||||
},
|
||||
{
|
||||
"type": "link",
|
||||
"title": "Конфигурация WIFI",
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
extern void udpInit();
|
||||
extern bool isUdpEnabled();
|
||||
extern void loopUdp();
|
||||
extern void handleUdp_esp32();
|
||||
extern void do_udp_data_parse();
|
||||
extern void add_dev_in_list(String filename, String id, String dev_name, String ip);
|
||||
extern void send_mqtt_to_udp();
|
||||
@@ -45,7 +45,7 @@ void spaceCmdExecute(String &cmdStr) {
|
||||
void loopCmdExecute() {
|
||||
if (orderBuf.length()) {
|
||||
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
|
||||
SerialPrint("I","module","do: " + tmp);
|
||||
SerialPrint("I","CMD","do: " + tmp);
|
||||
sCmd.readStr(tmp); //выполняем
|
||||
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ const String filepath(const String& filename) {
|
||||
|
||||
bool fileSystemInit() {
|
||||
if (!LittleFS.begin()) {
|
||||
SerialPrint("[E]","module","init");
|
||||
SerialPrint("[E]","Files","init");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -19,10 +19,10 @@ void removeFile(const String& filename) {
|
||||
String path = filepath(filename);
|
||||
if (LittleFS.exists(path)) {
|
||||
if (!LittleFS.remove(path)) {
|
||||
SerialPrint("[E]","module","remove " + path);
|
||||
SerialPrint("[E]","Files","remove " + path);
|
||||
}
|
||||
} else {
|
||||
SerialPrint("I","module","not exist" + path);
|
||||
SerialPrint("I","Files","not exist" + path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ File seekFile(const String& filename, size_t position) {
|
||||
String path = filepath(filename);
|
||||
auto file = LittleFS.open(path, "r");
|
||||
if (!file) {
|
||||
SerialPrint("[E]","module","open " + path);
|
||||
SerialPrint("[E]","Files","open " + path);
|
||||
}
|
||||
// поставим курсор в начало файла
|
||||
file.seek(position, SeekSet);
|
||||
@@ -76,14 +76,14 @@ const String addFile(const String& filename, const String& str) {
|
||||
bool copyFile(const String& src, const String& dst, bool overwrite) {
|
||||
String srcPath = filepath(src);
|
||||
String dstPath = filepath(dst);
|
||||
SerialPrint("I","module","copy " + srcPath + " to " + dstPath);
|
||||
SerialPrint("I","Files","copy " + srcPath + " to " + dstPath);
|
||||
if (!LittleFS.exists(srcPath)) {
|
||||
SerialPrint("[E]","module","not exist: " + srcPath);
|
||||
SerialPrint("[E]","Files","not exist: " + srcPath);
|
||||
return false;
|
||||
}
|
||||
if (LittleFS.exists(dstPath)) {
|
||||
if (!overwrite) {
|
||||
SerialPrint("[E]","module","already exist: " + dstPath);
|
||||
SerialPrint("[E]","Files","already exist: " + dstPath);
|
||||
return false;
|
||||
}
|
||||
LittleFS.remove(dstPath);
|
||||
|
||||
@@ -24,7 +24,7 @@ const String getChipId() {
|
||||
|
||||
void setChipId() {
|
||||
chipId = getChipId();
|
||||
SerialPrint("I","module","id: " + chipId);
|
||||
SerialPrint("I","System","id: " + chipId);
|
||||
}
|
||||
|
||||
#ifdef ESP8266
|
||||
|
||||
@@ -272,6 +272,7 @@ void web_init() {
|
||||
} else if (lastVersion == -2) {
|
||||
msg = F("Устройство не подключено к роутеру!");
|
||||
}
|
||||
|
||||
// else if (lastVersion == "") {
|
||||
//msg = F("Нажмите на кнопку \"обновить прошивку\" повторно...");
|
||||
//} else if (lastVersion == "less") {
|
||||
|
||||
@@ -29,17 +29,17 @@ void init() {
|
||||
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str());
|
||||
|
||||
server.onNotFound([](AsyncWebServerRequest *request) {
|
||||
SerialPrint("[E]","module","not found:\n" + getRequestInfo(request));
|
||||
SerialPrint("[E]","WebServer","not found:\n" + getRequestInfo(request));
|
||||
request->send(404);
|
||||
});
|
||||
|
||||
server.onFileUpload([](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||
// TODO
|
||||
if (!index) {
|
||||
SerialPrint("I","module","start upload " + filename);
|
||||
SerialPrint("I","WebServer","start upload " + filename);
|
||||
}
|
||||
if (final) {
|
||||
SerialPrint("I","module","finish upload: " + prettyBytes(index + len));
|
||||
SerialPrint("I","WebServer","finish upload: " + prettyBytes(index + len));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -60,7 +60,7 @@ void init() {
|
||||
|
||||
server.on("/cmd", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
String cmdStr = request->getParam("command")->value();
|
||||
SerialPrint("I","module","do: " + cmdStr);
|
||||
SerialPrint("I","WebServer","do: " + cmdStr);
|
||||
loopCmdAdd(cmdStr);
|
||||
request->send(200, "text/html", "OK");
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ bool loadWidget(const String& filename, String& buf) {
|
||||
buf = readFile(getWidgetFile(filename), 2048);
|
||||
bool res = !(buf == "Failed" || buf == "Large");
|
||||
if (!res) {
|
||||
SerialPrint("[E]","module","on load" + filename);
|
||||
SerialPrint("[E]","Widgets","on load" + filename);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
32
src/main.cpp
32
src/main.cpp
@@ -35,49 +35,45 @@ void setup() {
|
||||
myNotAsincActions = new NotAsinc(do_LAST);
|
||||
myScenario = new Scenario();
|
||||
|
||||
SerialPrint("I","FS","FS");
|
||||
SerialPrint("I","FS","FS Init");
|
||||
fileSystemInit();
|
||||
|
||||
SerialPrint("I","Conf","Config");
|
||||
SerialPrint("I","Conf","Config Init");
|
||||
loadConfig();
|
||||
|
||||
SerialPrint("I","Time","Clock");
|
||||
SerialPrint("I","Time","Clock Init");
|
||||
clock_init();
|
||||
|
||||
SerialPrint("I","CMD","Commands");
|
||||
SerialPrint("I","CMD","Commands Init");
|
||||
cmd_init();
|
||||
|
||||
SerialPrint("I","Sensors","Sensors");
|
||||
SerialPrint("I","Sensors","Sensors Init");
|
||||
sensorsInit();
|
||||
|
||||
SerialPrint("I","Init","Init");
|
||||
SerialPrint("I","Init","Init Init");
|
||||
all_init();
|
||||
|
||||
SerialPrint("I","WIFI","Network");
|
||||
SerialPrint("I","WIFI","Network Init");
|
||||
startSTAMode();
|
||||
|
||||
SerialPrint("I","Uptime","Uptime");
|
||||
SerialPrint("I","Uptime","Uptime Init");
|
||||
uptime_init();
|
||||
|
||||
SerialPrint("I","Update","Updater");
|
||||
SerialPrint("I","Update","Updater Init");
|
||||
upgradeInit();
|
||||
|
||||
SerialPrint("I","HTTP","HttpServer");
|
||||
SerialPrint("I","HTTP","HttpServer Init");
|
||||
HttpServer::init();
|
||||
|
||||
SerialPrint("I","Web","WebAdmin");
|
||||
SerialPrint("I","Web","WebAdmin Init");
|
||||
web_init();
|
||||
|
||||
SerialPrint("I","Stat","InitSt");
|
||||
SerialPrint("I","Stat","Stat Init");
|
||||
initSt();
|
||||
|
||||
SerialPrint("I","UDP","asyncUdpInit");
|
||||
SerialPrint("I","UDP","Udp Init");
|
||||
asyncUdpInit();
|
||||
|
||||
#ifdef UDP_ENABLED
|
||||
SerialPrint("I","module","Broadcast UDP");
|
||||
udpInit();
|
||||
#endif
|
||||
|
||||
#ifdef SSDP_EN
|
||||
SerialPrint("I","SSDP","Ssdp Init");
|
||||
SsdpInit();
|
||||
|
||||
166
src/udp_.cpp
166
src/udp_.cpp
@@ -1,166 +0,0 @@
|
||||
#include "Class/NotAsinc.h"
|
||||
#include "udp.h"
|
||||
#include "udp_.h"
|
||||
#include "Global.h"
|
||||
|
||||
|
||||
#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) {
|
||||
SerialPrint("I","module","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();
|
||||
|
||||
SerialPrint("I","module",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) {
|
||||
SerialPrint("I","module","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
|
||||
SerialPrint("I","module","sent info");
|
||||
udp_busy = false;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user