mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 06:32:19 +03:00
268 Single Scenario changed to MQTT source
This commit is contained in:
@@ -16,7 +16,7 @@ void eventGen2(String eventName, String eventValue) {
|
||||
void streamEventUDP(String event) {
|
||||
#ifdef UDP_ENABLED
|
||||
|
||||
if (!jsonReadBool(configSetupJson, "onescen")) {
|
||||
if (!jsonReadBool(configSetupJson, "snaUdp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,13 @@ void mqttInit() {
|
||||
}
|
||||
},
|
||||
nullptr, true);
|
||||
|
||||
myNotAsyncActions->add(
|
||||
do_sendScenMQTT, [&](void*) {
|
||||
String scen = readFile(String(DEVICE_SCENARIO_FILE), 2048);
|
||||
publishInfo("scen", scen);
|
||||
},
|
||||
nullptr);
|
||||
}
|
||||
|
||||
void mqttDisconnect() {
|
||||
@@ -63,8 +70,11 @@ void mqttSubscribe() {
|
||||
mqtt.subscribe(mqttPrefix.c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
|
||||
mqtt.subscribe((mqttRootDevice + "/update").c_str());
|
||||
//mqtt.subscribe((mqttRootDevice + "/order").c_str());
|
||||
//mqtt.subscribe((mqttPrefix + "/event").c_str());
|
||||
|
||||
if (jsonReadBool(configSetupJson, "snaMqtt")) {
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/status").c_str());
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
|
||||
}
|
||||
}
|
||||
|
||||
boolean mqttConnect() {
|
||||
@@ -101,14 +111,14 @@ boolean mqttConnect() {
|
||||
|
||||
void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
String topicStr = String(topic);
|
||||
SerialPrint("I", "MQTT", topicStr);
|
||||
//SerialPrint("I", "=>MQTT", topicStr);
|
||||
String payloadStr;
|
||||
payloadStr.reserve(length + 1);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
payloadStr += (char)payload[i];
|
||||
}
|
||||
|
||||
SerialPrint("I", "MQTT", payloadStr);
|
||||
//SerialPrint("I", "=>MQTT", payloadStr);
|
||||
|
||||
if (payloadStr.startsWith("HELLO")) {
|
||||
SerialPrint("I", "MQTT", "Full update");
|
||||
@@ -119,7 +129,8 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
#endif
|
||||
|
||||
}
|
||||
else if (topicStr.indexOf("control")) {
|
||||
|
||||
else if (topicStr.indexOf("control") != -1) {
|
||||
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
|
||||
@@ -128,23 +139,37 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
|
||||
orderBuf += payloadStr;
|
||||
orderBuf += ",";
|
||||
|
||||
}
|
||||
else if (topicStr.indexOf("order")) {
|
||||
//payloadStr.replace("_", " ");
|
||||
//orderBuf += payloadStr;
|
||||
//orderBuf += ",";
|
||||
|
||||
SerialPrint("I", "=>MQTT", "Msg from iotmanager app: " + key + " " + payloadStr);
|
||||
}
|
||||
|
||||
//else if (topicStr.indexOf("event")) {
|
||||
// eventBuf += payloadStr;
|
||||
//}
|
||||
else if (topicStr.indexOf("status") != -1) {
|
||||
if (!jsonReadBool(configSetupJson, "snaMqtt")) {
|
||||
return;
|
||||
}
|
||||
if (topicStr.indexOf(chipId) == -1) {
|
||||
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
|
||||
String key = selectFromMarkerToMarker(topicStr, "/", 3);
|
||||
String value = jsonReadStr(payloadStr, "status");
|
||||
|
||||
else if (topicStr.indexOf("update")) {
|
||||
if (payloadStr == "1") {
|
||||
myNotAsyncActions->make(do_UPGRADE);
|
||||
SerialPrint("I", "=>MQTT", "Msg from other device: '" + devId + "' " + key + " " + value);
|
||||
|
||||
eventGen2(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
else if (topicStr.indexOf("info") != -1) {
|
||||
if (topicStr.indexOf("scen") != -1) {
|
||||
writeFile(String(DEVICE_SCENARIO_FILE), payloadStr);
|
||||
loadScenario();
|
||||
SerialPrint("I", "=>MQTT", "Scenario received");
|
||||
}
|
||||
}
|
||||
|
||||
//else if (topicStr.indexOf("update")) {
|
||||
// if (payloadStr == "1") {
|
||||
// myNotAsyncActions->make(do_UPGRADE);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
boolean publish(const String& topic, const String& data) {
|
||||
@@ -190,6 +215,11 @@ boolean publishStatus(const String& topic, const String& data) {
|
||||
return mqtt.publish(path.c_str(), json.c_str(), false);
|
||||
}
|
||||
|
||||
boolean publishInfo(const String& topic, const String& data) {
|
||||
String path = mqttRootDevice + "/" + topic + "/info";
|
||||
return mqtt.publish(path.c_str(), data.c_str(), false);
|
||||
}
|
||||
|
||||
#ifdef LAYOUT_IN_RAM
|
||||
void publishWidgets() {
|
||||
if (all_widgets != "") {
|
||||
|
||||
@@ -9,7 +9,7 @@ AsyncUDP asyncUdp;
|
||||
|
||||
void asyncUdpInit() {
|
||||
|
||||
if (!jsonReadBool(configSetupJson, "onescen")) {
|
||||
if (!jsonReadBool(configSetupJson, "snaUdp")) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
28
src/Web.cpp
28
src/Web.cpp
@@ -61,16 +61,32 @@ void web_init() {
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg("onescen")) {
|
||||
bool value = request->getParam("onescen")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "onescen", value);
|
||||
//if (request->hasArg("snaUdp")) {
|
||||
// bool value = request->getParam("snaUdp")->value().toInt();
|
||||
// jsonWriteBool(configSetupJson, "snaUdp", value);
|
||||
// saveConfig();
|
||||
// #ifdef UDP_ENABLED
|
||||
// asyncUdpInit();
|
||||
// #endif
|
||||
// request->send(200);
|
||||
//}
|
||||
|
||||
//if (request->hasArg("scenUdp")) {
|
||||
// myNotAsyncActions->make(do_sendScenUDP);
|
||||
// request->send(200);
|
||||
//}
|
||||
|
||||
if (request->hasArg("snaMqtt")) {
|
||||
bool value = request->getParam("snaMqtt")->value().toInt();
|
||||
jsonWriteBool(configSetupJson, "snaMqtt", value);
|
||||
saveConfig();
|
||||
asyncUdpInit();
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/status").c_str());
|
||||
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
if (request->hasArg("scenudp")) {
|
||||
myNotAsyncActions->make(do_sendScenUDP);
|
||||
if (request->hasArg("scenMqtt")) {
|
||||
myNotAsyncActions->make(do_sendScenMQTT);
|
||||
request->send(200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user