fix HomeD

This commit is contained in:
Mit4el
2024-11-26 21:04:22 +03:00
parent 13d0b514a3
commit 3aaeb760da
4 changed files with 14 additions and 11 deletions

View File

@@ -124,6 +124,7 @@ extern int mqttPort;
extern String mqttPrefix; extern String mqttPrefix;
extern String mqttUser; extern String mqttUser;
extern String mqttPass; extern String mqttPass;
extern String nameId;
extern unsigned long mqttUptime; extern unsigned long mqttUptime;
extern unsigned long flashWriteNumber; extern unsigned long flashWriteNumber;

View File

@@ -62,6 +62,7 @@ int mqttPort = 0;
String mqttPrefix = ""; String mqttPrefix = "";
String mqttUser = ""; String mqttUser = "";
String mqttPass = ""; String mqttPass = "";
String nameId = "";
unsigned long mqttUptime = 0; unsigned long mqttUptime = 0;
unsigned long flashWriteNumber = 0; unsigned long flashWriteNumber = 0;

View File

@@ -62,7 +62,7 @@ boolean mqttConnect() {
if (mqttUser != "" && mqttPass != "") { if (mqttUser != "" && mqttPass != "") {
if (HOMEdDiscovery) if (HOMEdDiscovery)
{ {
connected = mqtt.connect(chipId.c_str(), mqttUser.c_str(), mqttPass.c_str(), (HOMEdDiscovery->HOMEdTopic + "/device/custom/" + chipId).c_str(), 1, true, "{\"status\":\"offline\"}"); connected = mqtt.connect(chipId.c_str(), mqttUser.c_str(), mqttPass.c_str(), (HOMEdDiscovery->HOMEdTopic + "/device/custom/" + nameId).c_str(), 1, true, "{\"status\":\"offline\"}");
} }
else else
{ {
@@ -72,7 +72,7 @@ boolean mqttConnect() {
} else if (mqttUser == "" && mqttPass == "") { } else if (mqttUser == "" && mqttPass == "") {
if (HOMEdDiscovery) if (HOMEdDiscovery)
{ {
connected = mqtt.connect(chipId.c_str(), (HOMEdDiscovery->HOMEdTopic + "/device/custom/" + chipId).c_str(), 1, true, "{\"status\":\"offline\"}"); connected = mqtt.connect(chipId.c_str(), (HOMEdDiscovery->HOMEdTopic + "/device/custom/" + nameId).c_str(), 1, true, "{\"status\":\"offline\"}");
} }
else else
{ {
@@ -120,6 +120,7 @@ void getMqttData() {
mqttUser = jsonReadStr(settingsFlashJson, F("mqttUser")); mqttUser = jsonReadStr(settingsFlashJson, F("mqttUser"));
mqttPass = jsonReadStr(settingsFlashJson, F("mqttPass")); mqttPass = jsonReadStr(settingsFlashJson, F("mqttPass"));
mqttPrefix = jsonReadStr(settingsFlashJson, F("mqttPrefix")); mqttPrefix = jsonReadStr(settingsFlashJson, F("mqttPrefix"));
nameId = jsonReadStr(settingsFlashJson, F("name"));
mqttRootDevice = mqttPrefix + "/" + chipId; mqttRootDevice = mqttPrefix + "/" + chipId;
} }

View File

@@ -90,8 +90,8 @@ public:
if (mqttIsConnect() && !sendOk && topicOk) if (mqttIsConnect() && !sendOk && topicOk)
{ {
sendOk = true; sendOk = true;
publishRetain(_topic + "/device/custom/" + chipId, "{\"status\":\"online\"}"); publishRetain(_topic + "/device/custom/" + nameId, "{\"status\":\"online\"}");
String HOMEdsubscribeTopic = _topic + "/td/custom/" + chipId; String HOMEdsubscribeTopic = _topic + "/td/custom/" + nameId;
// mqtt.subscribe(HOMEdsubscribeTopic.c_str()); // mqtt.subscribe(HOMEdsubscribeTopic.c_str());
mqttSubscribeExternal(HOMEdsubscribeTopic); mqttSubscribeExternal(HOMEdsubscribeTopic);
} }
@@ -103,7 +103,7 @@ public:
void publishStatusHOMEd(const String &topic, const String &data) void publishStatusHOMEd(const String &topic, const String &data)
{ {
String path_h = HOMEdTopic + "/fd/custom/" + chipId; String path_h = HOMEdTopic + "/fd/custom/" + nameId;
String json_h = "{}"; String json_h = "{}";
if (topic != "onStart") if (topic != "onStart")
{ {
@@ -133,8 +133,8 @@ public:
{ {
deleteFromHOMEd(); deleteFromHOMEd();
getlayoutHOMEd(); getlayoutHOMEd();
publishRetain(HOMEdTopic + "/device/custom/" + chipId, "{\"status\":\"online\"}"); publishRetain(HOMEdTopic + "/device/custom/" + nameId, "{\"status\":\"online\"}");
String HOMEdsubscribeTopic = HOMEdTopic + "/td/custom/" + chipId; String HOMEdsubscribeTopic = HOMEdTopic + "/td/custom/" + nameId;
mqtt.subscribe(HOMEdsubscribeTopic.c_str()); mqtt.subscribe(HOMEdsubscribeTopic.c_str());
} }
} }
@@ -164,7 +164,7 @@ public:
JsonArray arr = doc.as<JsonArray>(); JsonArray arr = doc.as<JsonArray>();
String HOMEdJSON = ""; String HOMEdJSON = "";
HOMEdJSON = "{\"action\":\"updateDevice\","; HOMEdJSON = "{\"action\":\"updateDevice\",";
HOMEdJSON = HOMEdJSON + "\"device\":\"" + chipId + "\","; HOMEdJSON = HOMEdJSON + "\"device\":\"" + nameId + "\",";
HOMEdJSON = HOMEdJSON + "\"data\":{"; HOMEdJSON = HOMEdJSON + "\"data\":{";
HOMEdJSON = HOMEdJSON + "\"active\": true,"; HOMEdJSON = HOMEdJSON + "\"active\": true,";
HOMEdJSON = HOMEdJSON + "\"cloud\": false,"; HOMEdJSON = HOMEdJSON + "\"cloud\": false,";
@@ -178,7 +178,7 @@ public:
{ {
String name = value["descr"]; String name = value["descr"];
String device = selectToMarkerLast(value["topic"].as<String>(), "/"); String device = selectToMarkerLast(value["topic"].as<String>(), "/");
String id = chipId + "-" + device; //String id = chipId + "-" + device;
String expose = value["name"]; String expose = value["name"];
if (value["name"].as<String>() == "toggle") if (value["name"].as<String>() == "toggle")
{ {
@@ -219,7 +219,7 @@ public:
file.close(); file.close();
publishRetain(HOMEdTopic + "/device/custom/" + chipId, "{\"status\":\"online\"}"); publishRetain(HOMEdTopic + "/device/custom/" + nameId, "{\"status\":\"online\"}");
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it)
{ {
@@ -243,7 +243,7 @@ public:
String HOMEdjson = ""; String HOMEdjson = "";
HOMEdjson = "{\"action\":\"removeDevice\","; HOMEdjson = "{\"action\":\"removeDevice\",";
HOMEdjson = HOMEdjson + "\"device\":\""; HOMEdjson = HOMEdjson + "\"device\":\"";
HOMEdjson = HOMEdjson + chipId; HOMEdjson = HOMEdjson + nameId;
HOMEdjson = HOMEdjson + "\"}"; HOMEdjson = HOMEdjson + "\"}";
String topic = (HOMEdTopic + "/command/custom").c_str(); String topic = (HOMEdTopic + "/command/custom").c_str();
if (!publish(topic, HOMEdjson)) if (!publish(topic, HOMEdjson))