268 Single Scenario changed to MQTT source

This commit is contained in:
Dmitry Borisenko
2020-12-02 04:12:10 +03:00
parent 38924e4ed3
commit e88718ada0
8 changed files with 83 additions and 34 deletions

View File

@@ -17,7 +17,8 @@
"telegonof": "0", "telegonof": "0",
"weblogin": "admin", "weblogin": "admin",
"webpass": "admin", "webpass": "admin",
"onescen": "1", "snaUdp": "0",
"snaMqtt": "0",
"blink": "1", "blink": "1",
"oneWirePin": "2", "oneWirePin": "2",
"serverip": "http://206.189.49.244" "serverip": "http://206.189.49.244"

View File

@@ -172,18 +172,18 @@
}, },
{ {
"type": "checkbox", "type": "checkbox",
"name": "onescen", "name": "snaMqtt",
"title": "Включить единые сценарии для всех устройств", "title": "Включить единые сценарии для всех устройств",
"action": "/set?onescen=[[onescen]]", "action": "/set?snaMqtt=[[snaMqtt]]",
"state": "{{onescen}}" "state": "{{snaMqtt}}"
}, },
{ {
"type": "hr" "type": "hr"
}, },
{ {
"type": "button", "type": "button",
"title": "Отправить сценарии всем устройствам в локальной сети", "title": "Синхронизировать сценарии на всех устройствах",
"action": "/set?scenudp", "action": "/set?scenMqtt",
"class": "btn btn-block btn-default" "class": "btn btn-block btn-default"
}, },
{ {

View File

@@ -3,11 +3,11 @@
//===========Firmware============================================================================================================================================= //===========Firmware=============================================================================================================================================
#ifdef ESP8266 #ifdef ESP8266
#define FIRMWARE_NAME "esp8266-iotm" #define FIRMWARE_NAME "esp8266-iotm"
#define FIRMWARE_VERSION 267 #define FIRMWARE_VERSION 268
#endif #endif
#ifdef ESP32 #ifdef ESP32
#define FIRMWARE_NAME "esp32-iotm" #define FIRMWARE_NAME "esp32-iotm"
#define FIRMWARE_VERSION 267 #define FIRMWARE_VERSION 268
#endif #endif
#define FLASH_4MB true #define FLASH_4MB true
@@ -31,7 +31,7 @@
//#define MDNS_ENABLED //#define MDNS_ENABLED
//#define WEBSOCKET_ENABLED //#define WEBSOCKET_ENABLED
//#define LAYOUT_IN_RAM //#define LAYOUT_IN_RAM
#define UDP_ENABLED //#define UDP_ENABLED
//#define SSDP_ENABLED //#define SSDP_ENABLED
//=========Sensors enable/disable================================================================================================================================= //=========Sensors enable/disable=================================================================================================================================
@@ -87,6 +87,7 @@ enum NotAsyncActions {
do_deviceInit, do_deviceInit,
do_delChoosingItems, do_delChoosingItems,
do_sendScenUDP, do_sendScenUDP,
do_sendScenMQTT,
do_LAST, do_LAST,
}; };

View File

@@ -17,6 +17,7 @@ boolean publishChart(const String& topic, const String& data);
boolean publishControl(String id, String topic, String state); boolean publishControl(String id, String topic, String state);
boolean publishChart_test(const String& topic, const String& data); boolean publishChart_test(const String& topic, const String& data);
boolean publishStatus(const String& topic, const String& data); boolean publishStatus(const String& topic, const String& data);
boolean publishInfo(const String& topic, const String& data);
void publishWidgets(); void publishWidgets();
void publishState(); void publishState();

View File

@@ -16,7 +16,7 @@ void eventGen2(String eventName, String eventValue) {
void streamEventUDP(String event) { void streamEventUDP(String event) {
#ifdef UDP_ENABLED #ifdef UDP_ENABLED
if (!jsonReadBool(configSetupJson, "onescen")) { if (!jsonReadBool(configSetupJson, "snaUdp")) {
return; return;
} }

View File

@@ -39,6 +39,13 @@ void mqttInit() {
} }
}, },
nullptr, true); nullptr, true);
myNotAsyncActions->add(
do_sendScenMQTT, [&](void*) {
String scen = readFile(String(DEVICE_SCENARIO_FILE), 2048);
publishInfo("scen", scen);
},
nullptr);
} }
void mqttDisconnect() { void mqttDisconnect() {
@@ -63,8 +70,11 @@ void mqttSubscribe() {
mqtt.subscribe(mqttPrefix.c_str()); mqtt.subscribe(mqttPrefix.c_str());
mqtt.subscribe((mqttRootDevice + "/+/control").c_str()); mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
mqtt.subscribe((mqttRootDevice + "/update").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() { boolean mqttConnect() {
@@ -101,14 +111,14 @@ boolean mqttConnect() {
void mqttCallback(char* topic, uint8_t* payload, size_t length) { void mqttCallback(char* topic, uint8_t* payload, size_t length) {
String topicStr = String(topic); String topicStr = String(topic);
SerialPrint("I", "MQTT", topicStr); //SerialPrint("I", "=>MQTT", topicStr);
String payloadStr; String payloadStr;
payloadStr.reserve(length + 1); payloadStr.reserve(length + 1);
for (size_t i = 0; i < length; i++) { for (size_t i = 0; i < length; i++) {
payloadStr += (char)payload[i]; payloadStr += (char)payload[i];
} }
SerialPrint("I", "MQTT", payloadStr); //SerialPrint("I", "=>MQTT", payloadStr);
if (payloadStr.startsWith("HELLO")) { if (payloadStr.startsWith("HELLO")) {
SerialPrint("I", "MQTT", "Full update"); SerialPrint("I", "MQTT", "Full update");
@@ -119,7 +129,8 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
#endif #endif
} }
else if (topicStr.indexOf("control")) {
else if (topicStr.indexOf("control") != -1) {
String key = selectFromMarkerToMarker(topicStr, "/", 3); String key = selectFromMarkerToMarker(topicStr, "/", 3);
@@ -128,23 +139,37 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
orderBuf += payloadStr; orderBuf += payloadStr;
orderBuf += ","; orderBuf += ",";
} SerialPrint("I", "=>MQTT", "Msg from iotmanager app: " + key + " " + payloadStr);
else if (topicStr.indexOf("order")) {
//payloadStr.replace("_", " ");
//orderBuf += payloadStr;
//orderBuf += ",";
} }
//else if (topicStr.indexOf("event")) { else if (topicStr.indexOf("status") != -1) {
// eventBuf += payloadStr; 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");
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);
// }
//} //}
else if (topicStr.indexOf("update")) {
if (payloadStr == "1") {
myNotAsyncActions->make(do_UPGRADE);
}
}
} }
boolean publish(const String& topic, const String& data) { 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); 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 #ifdef LAYOUT_IN_RAM
void publishWidgets() { void publishWidgets() {
if (all_widgets != "") { if (all_widgets != "") {

View File

@@ -9,7 +9,7 @@ AsyncUDP asyncUdp;
void asyncUdpInit() { void asyncUdpInit() {
if (!jsonReadBool(configSetupJson, "onescen")) { if (!jsonReadBool(configSetupJson, "snaUdp")) {
return; return;
} }

View File

@@ -61,16 +61,32 @@ void web_init() {
request->send(200); request->send(200);
} }
if (request->hasArg("onescen")) { //if (request->hasArg("snaUdp")) {
bool value = request->getParam("onescen")->value().toInt(); // bool value = request->getParam("snaUdp")->value().toInt();
jsonWriteBool(configSetupJson, "onescen", value); // 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(); saveConfig();
asyncUdpInit(); mqtt.subscribe((mqttPrefix + "/+/+/status").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
request->send(200); request->send(200);
} }
if (request->hasArg("scenudp")) { if (request->hasArg("scenMqtt")) {
myNotAsyncActions->make(do_sendScenUDP); myNotAsyncActions->make(do_sendScenMQTT);
request->send(200); request->send(200);
} }