mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 14:42:18 +03:00
Global change
This commit is contained in:
370
src/Mqtt.cpp
370
src/Mqtt.cpp
@@ -1,6 +1,7 @@
|
||||
#include "Global.h"
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
String stateMQTT();
|
||||
void sendAllData();
|
||||
void sendAllWigets();
|
||||
@@ -9,74 +10,41 @@ void outcoming_date();
|
||||
|
||||
//===============================================ИНИЦИАЛИЗАЦИЯ================================================
|
||||
void MQTT_init() {
|
||||
ts.add(
|
||||
WIFI_MQTT_CONNECTION_CHECK, wifi_mqtt_reconnecting, [&](void*) {
|
||||
up_time();
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("[VV] WiFi-ok");
|
||||
if (client_mqtt.connected()) {
|
||||
Serial.println("[VV] MQTT-ok");
|
||||
led_blink("off");
|
||||
} else {
|
||||
MQTT_Connecting();
|
||||
if (!just_load) mqtt_lost_error++;
|
||||
}
|
||||
} else {
|
||||
Serial.println("[E] Lost WiFi connection");
|
||||
wifi_lost_error++;
|
||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||
StartAPMode();
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
server.on("/mqttSave", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
if (request->hasArg("mqttServer")) {
|
||||
jsonWriteStr(configSetupJson, "mqttServer", request->getParam("mqttServer")->value());
|
||||
}
|
||||
if (request->hasArg("mqttPort")) {
|
||||
int port = (request->getParam("mqttPort")->value()).toInt();
|
||||
jsonWriteInt(configSetupJson, "mqttPort", port);
|
||||
}
|
||||
if (request->hasArg("mqttPrefix")) {
|
||||
jsonWriteStr(configSetupJson, "mqttPrefix", request->getParam("mqttPrefix")->value());
|
||||
}
|
||||
if (request->hasArg("mqttUser")) {
|
||||
jsonWriteStr(configSetupJson, "mqttUser", request->getParam("mqttUser")->value());
|
||||
}
|
||||
if (request->hasArg("mqttPass")) {
|
||||
jsonWriteStr(configSetupJson, "mqttPass", request->getParam("mqttPass")->value());
|
||||
}
|
||||
saveConfig();
|
||||
mqtt_connection = true;
|
||||
|
||||
request->send(200, "text/text", "ok");
|
||||
});
|
||||
|
||||
server.on("/mqttCheck", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||||
String tmp = "{}";
|
||||
jsonWriteStr(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + stateMQTT());
|
||||
jsonWriteStr(tmp, "class", "pop-up");
|
||||
request->send(200, "text/text", tmp);
|
||||
});
|
||||
ts.add(WIFI_MQTT_CONNECTION_CHECK, wifi_mqtt_reconnecting, [&](void*) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("[VV] WiFi-ok");
|
||||
if (client_mqtt.connected()) {
|
||||
Serial.println("[VV] MQTT-ok");
|
||||
led_blink("off");
|
||||
} else {
|
||||
MQTT_Connecting();
|
||||
if (!just_load) mqtt_lost_error++;
|
||||
}
|
||||
} else {
|
||||
Serial.println("[E] Lost WiFi connection");
|
||||
wifi_lost_error++;
|
||||
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
|
||||
StartAPMode();
|
||||
}
|
||||
}, nullptr, true);
|
||||
}
|
||||
|
||||
void do_mqtt_connection() {
|
||||
if (mqtt_connection) {
|
||||
mqtt_connection = false;
|
||||
client_mqtt.disconnect();
|
||||
MQTT_Connecting();
|
||||
}
|
||||
if (mqtt_connection) {
|
||||
mqtt_connection = false;
|
||||
client_mqtt.disconnect();
|
||||
MQTT_Connecting();
|
||||
}
|
||||
}
|
||||
|
||||
void handleMQTT() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (client_mqtt.connected()) {
|
||||
client_mqtt.loop();
|
||||
}
|
||||
//================================================ОБНОВЛЕНИЕ====================================================
|
||||
void handleMQTT() {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
if (client_mqtt.connected()) {
|
||||
client_mqtt.loop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean MQTT_Connecting() {
|
||||
bool res = false;
|
||||
String mqtt_server = jsonReadStr(configSetupJson, "mqttServer");
|
||||
@@ -94,6 +62,9 @@ boolean MQTT_Connecting() {
|
||||
client_mqtt.subscribe(jsonReadStr(configSetupJson, "mqttPrefix").c_str()); // Для приема получения HELLOW и подтверждения связи
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/+/control").c_str()); // Подписываемся на топики control
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/order").c_str()); // Подписываемся на топики order
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/update").c_str());
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/devc").c_str());
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/devs").c_str());
|
||||
Serial.println("[V] Callback set, subscribe done");
|
||||
res = true;
|
||||
} else {
|
||||
@@ -108,145 +79,168 @@ boolean MQTT_Connecting() {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//=====================================================ВХОДЯЩИЕ ДАННЫЕ========================================================
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
Serial.print("[MQTT] ");
|
||||
Serial.print(topic);
|
||||
String topic_str = String(topic);
|
||||
void callback(char* topic, byte * payload, unsigned int length) {
|
||||
Serial.print("[MQTT] ");
|
||||
Serial.print(topic);
|
||||
String topic_str = String(topic);
|
||||
|
||||
String str;
|
||||
for (int i = 0; i < length; i++) {
|
||||
str += (char)payload[i];
|
||||
}
|
||||
Serial.println(" => " + str);
|
||||
String str;
|
||||
for (int i = 0; i < length; i++) {
|
||||
str += (char)payload[i];
|
||||
}
|
||||
Serial.println(" => " + str);
|
||||
|
||||
if (str == "HELLO") outcoming_date();
|
||||
if (str == "HELLO") outcoming_date();
|
||||
|
||||
//превращает название топика в команду, а значение в параметр команды
|
||||
if (topic_str.indexOf("control") > 0) { //IoTmanager/800324-1458415/button-sw2/control 1 //IoTmanager/800324-1458415/button99/control 1
|
||||
String topic = selectFromMarkerToMarker(topic_str, "/", 3); //button1 //button99
|
||||
topic = add_set(topic); //buttonSet1 //buttonSet99
|
||||
String number = selectToMarkerLast(topic, "Set"); //1 //99
|
||||
topic.replace(number, ""); //buttonSet //buttonSet
|
||||
String final_line = topic + " " + number + " " + str; //buttonSet 1 1 //buttonSet 99 1
|
||||
order_loop += final_line + ",";
|
||||
}
|
||||
//превращает название топика в команду, а значение в параметр команды
|
||||
if (topic_str.indexOf("control") > 0) { //IoTmanager/800324-1458415/button-sw2/control 1 //IoTmanager/800324-1458415/button99/control 1
|
||||
String topic = selectFromMarkerToMarker(topic_str, "/", 3); //button1 //button99
|
||||
topic = add_set(topic); //buttonSet1 //buttonSet99
|
||||
String number = selectToMarkerLast(topic, "Set"); //1 //99
|
||||
topic.replace(number, ""); //buttonSet //buttonSet
|
||||
String final_line = topic + " " + number + " " + str; //buttonSet 1 1 //buttonSet 99 1
|
||||
order_loop += final_line + ",";
|
||||
}
|
||||
|
||||
if (topic_str.indexOf("order") > 0) {
|
||||
str.replace("_", " ");
|
||||
//Serial.println(str);
|
||||
order_loop += str + ",";
|
||||
}
|
||||
if (topic_str.indexOf("update") > 0) {
|
||||
if (str == "1") {
|
||||
upgrade = true;
|
||||
}
|
||||
if (topic_str.indexOf("order") > 0) {
|
||||
str.replace("_", " ");
|
||||
//Serial.println(str);
|
||||
order_loop += str + ",";
|
||||
}
|
||||
if (topic_str.indexOf("update") > 0) {
|
||||
if (str == "1") {
|
||||
upgrade = true;
|
||||
}
|
||||
}
|
||||
if (topic_str.indexOf("devc") > 0) {
|
||||
writeFile("firmware.c.txt", str);
|
||||
Device_init();
|
||||
}
|
||||
if (topic_str.indexOf("devs") > 0) {
|
||||
writeFile("firmware.s.txt", str);
|
||||
Scenario_init();
|
||||
}
|
||||
}
|
||||
|
||||
//данные которые отправляем при подключении или отбновлении страницы
|
||||
void outcoming_date() {
|
||||
sendAllWigets();
|
||||
sendAllData();
|
||||
|
||||
sendAllWigets();
|
||||
sendAllData();
|
||||
|
||||
#ifdef logging_enable
|
||||
choose_log_date_and_send();
|
||||
choose_log_date_and_send();
|
||||
#endif
|
||||
|
||||
Serial.println("[V] Sending all date to iot manager completed");
|
||||
Serial.println("[V] Sending all date to iot manager completed");
|
||||
}
|
||||
|
||||
|
||||
//======================================CONFIG==================================================
|
||||
boolean sendMQTT(String end_of_topik, String data) {
|
||||
String topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + end_of_topik;
|
||||
boolean send_status = client_mqtt.beginPublish(topik.c_str(), data.length(), false);
|
||||
client_mqtt.print(data);
|
||||
client_mqtt.endPublish();
|
||||
return send_status;
|
||||
String topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + end_of_topik;
|
||||
boolean send_status = client_mqtt.beginPublish(topik.c_str(), data.length(), false);
|
||||
client_mqtt.print(data);
|
||||
client_mqtt.endPublish();
|
||||
return send_status;
|
||||
}
|
||||
|
||||
boolean sendCHART(String topik, String data) {
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/" + "status";
|
||||
boolean send_status = client_mqtt.beginPublish(topik.c_str(), data.length(), false);
|
||||
client_mqtt.print(data);
|
||||
client_mqtt.endPublish();
|
||||
return send_status;
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/" + "status";
|
||||
boolean send_status = client_mqtt.beginPublish(topik.c_str(), data.length(), false);
|
||||
client_mqtt.print(data);
|
||||
client_mqtt.endPublish();
|
||||
return send_status;
|
||||
}
|
||||
|
||||
boolean sendCHART_test(String topik, String data) {
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/" + "status";
|
||||
boolean send_status = client_mqtt.publish (topik.c_str(), data.c_str(), false);
|
||||
return send_status;
|
||||
}
|
||||
|
||||
//======================================STATUS==================================================
|
||||
void sendSTATUS(String topik, String state) {
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/" + "status";
|
||||
String json_ = "{}";
|
||||
jsonWriteStr(json_, "status", state);
|
||||
client_mqtt.publish(topik.c_str(), json_.c_str(), false);
|
||||
topik = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipID + "/" + topik + "/" + "status";
|
||||
String json_ = "{}";
|
||||
jsonWriteStr(json_, "status", state);
|
||||
int send_status = client_mqtt.publish (topik.c_str(), json_.c_str(), false);
|
||||
}
|
||||
|
||||
//======================================CONTROL==================================================
|
||||
void sendCONTROL(String id, String topik, String state) {
|
||||
String all_line = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + id + "/" + topik + "/control";
|
||||
client_mqtt.publish(all_line.c_str(), state.c_str(), false);
|
||||
String all_line = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + id + "/" + topik + "/control";
|
||||
int send_status = client_mqtt.publish (all_line.c_str(), state.c_str(), false);
|
||||
}
|
||||
|
||||
//=====================================================ОТПРАВЛЯЕМ ВИДЖЕТЫ========================================================
|
||||
|
||||
#ifdef layout_in_ram
|
||||
void sendAllWigets() {
|
||||
if (all_widgets != "") {
|
||||
int counter = 0;
|
||||
String line;
|
||||
int psn_1 = 0;
|
||||
int psn_2;
|
||||
do {
|
||||
psn_2 = all_widgets.indexOf("\r\n", psn_1); //\r\n
|
||||
line = all_widgets.substring(psn_1, psn_2);
|
||||
line.replace("\n", "");
|
||||
line.replace("\r\n", "");
|
||||
//jsonWriteStr(line, "id", String(counter));
|
||||
//jsonWriteStr(line, "pageId", String(counter));
|
||||
counter++;
|
||||
sendMQTT("config", line);
|
||||
Serial.println("[V] " + line);
|
||||
psn_1 = psn_2 + 1;
|
||||
} while (psn_2 + 2 < all_widgets.length());
|
||||
getMemoryLoad("[i] after send all widgets");
|
||||
}
|
||||
if (all_widgets != "") {
|
||||
int counter = 0;
|
||||
String line;
|
||||
int psn_1 = 0;
|
||||
int psn_2;
|
||||
do {
|
||||
psn_2 = all_widgets.indexOf("\r\n", psn_1); //\r\n
|
||||
line = all_widgets.substring(psn_1, psn_2);
|
||||
line.replace("\n", "");
|
||||
line.replace("\r\n", "");
|
||||
//jsonWriteStr(line, "id", String(counter));
|
||||
//jsonWriteStr(line, "pageId", String(counter));
|
||||
counter++;
|
||||
sendMQTT("config", line);
|
||||
Serial.println("[V] " + line);
|
||||
psn_1 = psn_2 + 1;
|
||||
} while (psn_2 + 2 < all_widgets.length());
|
||||
getMemoryLoad("[i] after send all widgets");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef layout_in_ram
|
||||
void sendAllWigets() {
|
||||
File configFile = SPIFFS.open("/layout.txt", "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet); //поставим курсор в начало файла
|
||||
while (configFile.position() != configFile.size()) {
|
||||
String widget_to_send = configFile.readStringUntil('\n');
|
||||
Serial.println("[V] " + widget_to_send);
|
||||
sendMQTT("config", widget_to_send);
|
||||
}
|
||||
File configFile = SPIFFS.open("/layout.txt", "r");
|
||||
if (!configFile) {
|
||||
return;
|
||||
}
|
||||
configFile.seek(0, SeekSet); //поставим курсор в начало файла
|
||||
while (configFile.position() != configFile.size()) {
|
||||
String widget_to_send = configFile.readStringUntil('\n');
|
||||
Serial.println("[V] " + widget_to_send);
|
||||
sendMQTT("config", widget_to_send);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
//=====================================================ОТПРАВЛЯЕМ ДАННЫЕ В ВИДЖЕТЫ ПРИ ОБНОВЛЕНИИ СТРАНИЦЫ========================================================
|
||||
void sendAllData() { //берет строку json и ключи превращает в топики а значения колючей в них посылает
|
||||
void sendAllData() { //берет строку json и ключи превращает в топики а значения колючей в них посылает
|
||||
|
||||
String current_config = configLiveJson; //{"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"}
|
||||
getMemoryLoad("[i] after send all date");
|
||||
current_config.replace("{", "");
|
||||
current_config.replace("}", ""); //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"
|
||||
current_config += ","; //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1",
|
||||
String current_config = configLiveJson; //{"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"}
|
||||
getMemoryLoad("[i] after send all date");
|
||||
current_config.replace("{", "");
|
||||
current_config.replace("}", ""); //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"
|
||||
current_config += ","; //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1",
|
||||
|
||||
while (current_config.length() != 0) {
|
||||
String tmp = selectToMarker(current_config, ",");
|
||||
String topic = selectToMarker(tmp, ":");
|
||||
topic.replace("\"", "");
|
||||
String state = selectToMarkerLast(tmp, ":");
|
||||
state.replace("\"", "");
|
||||
if (topic != "name" && topic != "lang" && topic != "ip" && topic.indexOf("_in") < 0) {
|
||||
sendSTATUS(topic, state);
|
||||
//Serial.println("-->" + topic + " " + state);
|
||||
}
|
||||
current_config = deleteBeforeDelimiter(current_config, ",");
|
||||
while (current_config.length() != 0) {
|
||||
|
||||
String tmp = selectToMarker (current_config, ",");
|
||||
String topic = selectToMarker (tmp, ":");
|
||||
topic.replace("\"", "");
|
||||
String state = selectToMarkerLast (tmp, ":");
|
||||
state.replace("\"", "");
|
||||
if (topic != "name" && topic != "lang" && topic != "ip" && topic.indexOf("_in") < 0) {
|
||||
sendSTATUS(topic, state);
|
||||
//Serial.println("-->" + topic + " " + state);
|
||||
}
|
||||
current_config = deleteBeforeDelimiter(current_config, ",");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
String stateMQTT() {
|
||||
int state = client_mqtt.state();
|
||||
switch (state) {
|
||||
@@ -285,69 +279,3 @@ String stateMQTT() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*void scenario_devices_topiks_subscribe() {
|
||||
|
||||
//SCENARIO ANALOG > 5 800324-1458415 rel1 0
|
||||
if (jsonReadStr(configSetupJson, "scenario") == "1") {
|
||||
//String all_text = readFile("firmware.s.txt", 1024) + "\r\n";
|
||||
String all_text = scenario + "\r\n";
|
||||
all_text.replace("\r\n", "\n");
|
||||
all_text.replace("\r", "\n");
|
||||
while (all_text.length() != 0) {
|
||||
String line_ = selectToMarker (all_text, "\n");
|
||||
String id = selectFromMarkerToMarker(line_, " ", 4);
|
||||
if (id != "not found") {
|
||||
client_mqtt.subscribe((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + id + "/+/status").c_str(), 0);
|
||||
Serial.println("subscribed to device, id: " + id);
|
||||
}
|
||||
all_text = deleteBeforeDelimiter(all_text, "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*void scenario_devices_test_msg_send() {
|
||||
|
||||
if (jsonReadStr(configSetupJson, "scenario") == "1") {
|
||||
|
||||
String all_text = scenario + "\r\n";
|
||||
all_text.replace("\r\n", "\n");
|
||||
all_text.replace("\r", "\n");
|
||||
while (all_text.length() != 0) {
|
||||
String line_ = selectToMarker (all_text, "\n");
|
||||
String id = selectFromMarkerToMarker(line_, " ", 4);
|
||||
if (id != "not found") {
|
||||
//Serial.println();
|
||||
Serial.println(client_mqtt.publish ((jsonReadStr(configSetupJson, "mqttPrefix") + "/" + id).c_str(), "CHECK", true));
|
||||
|
||||
}
|
||||
all_text = deleteBeforeDelimiter(all_text, "\n");
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
/*
|
||||
//-----------------------------------------------------------------------------------------------------------------------------------------------
|
||||
//jsonWriteStr(tmp, "status", "1");
|
||||
|
||||
String current_config = configLiveJson; //{"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"}
|
||||
current_config.replace("{", "");
|
||||
current_config.replace("}", ""); //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1"
|
||||
current_config += ","; //"name":"MODULES","lang":"","ip":"192.168.43.60","DS":"34.00","rel1":"1","rel2":"1",
|
||||
|
||||
while (current_config.length() != 0) {
|
||||
|
||||
String tmp = selectToMarker (current_config, ","); //"rel1":"1"
|
||||
String topic = selectToMarker (tmp, ":"); //"rel1"
|
||||
topic.replace("\"", ""); //rel1
|
||||
Serial.println(topic);
|
||||
String state = selectToMarkerLast (tmp, ":"); //"1"
|
||||
state.replace("\"", ""); //1
|
||||
|
||||
//if (widget.lastIndexOf(topic) > 0) {
|
||||
jsonWriteStr(tmp, "status", state);
|
||||
//}
|
||||
current_config = deleteBeforeDelimiter(current_config, ",");
|
||||
}
|
||||
//-------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user