Merge pull request #216 from biveraxe/ver4dev

Включаем сетевые сценарии на базе MQTT
This commit is contained in:
2022-10-22 08:02:31 +03:00
committed by GitHub
11 changed files with 47 additions and 41 deletions

View File

@@ -161,29 +161,33 @@ void mqttCallback(char* topic, uint8_t* payload, size_t length) {
else if (topicStr.indexOf("event") != -1) {
//пока не работает сетевой обмен этот код будет закомментирован
// if (!jsonReadBool(settingsFlashJson, "mqttin")) {
// return;
// }
// if (topicStr.indexOf(chipId) == -1) {
// String devId = selectFromMarkerToMarker(topicStr, "/", 2);
// String id = selectFromMarkerToMarker(topicStr, "/", 3);
// IoTItem* itemExist = findIoTItem(id);
// if (itemExist) {
// String valAsStr;
// if (jsonRead(payloadStr, F("val"), valAsStr, false)) {
// itemExist->setValue(valAsStr);
// unsigned long interval;
// jsonRead(payloadStr, F("int"), interval);
// itemExist->setInterval(interval);
// }
// } else {
// //добавим событие в базу
// // IoTItems.push_back((IoTItem*)new externalVariable(payloadStr));
// }
// //запустим проверку его в сценариях
// generateEvent(id, payloadStr);
// SerialPrint("i", F("=>MQTT"), "Received event from other device: '" + devId + "' " + id + " " + payloadStr);
// }
if (!jsonReadBool(settingsFlashJson, "mqttin")) {
return;
}
if (topicStr.indexOf(chipId) == -1) {
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
String id = selectFromMarkerToMarker(topicStr, "/", 3);
String valAsStr;
jsonRead(payloadStr, F("val"), valAsStr, false);
IoTItem* itemExist = findIoTItem(id);
if (itemExist) {
unsigned long interval;
jsonRead(payloadStr, F("int"), interval);
itemExist->setInterval(interval);
itemExist->setValue(valAsStr, false);
} else {
//добавим событие в базу
itemExist = (IoTItem*)new externalVariable(payloadStr);
IoTItems.push_back(itemExist);
}
//запустим проверку его в сценариях
generateEvent(id, valAsStr);
publishStatusMqtt(id, valAsStr);
publishStatusWs(id, valAsStr);
//itemExist->regEvent(valAsStr, "");
SerialPrint("i", F("=>MQTT"), "Received event from other device: '" + devId + "' " + id + " " + valAsStr);
}
}
//здесь мы получаем прямые команды которые сразу выполнятся на этом устройстве

View File

@@ -64,7 +64,7 @@ void IoTItem::setValue(String valStr, bool generateEvent) {
} else {
value.valS = valStr;
}
if (generateEvent) setValue(value, generateEvent);
setValue(value, generateEvent);
}
//
@@ -96,14 +96,14 @@ void IoTItem::regEvent(String value, String consoleInfo = "") {
// SerialPrint("i", F("=>ALLMQTT"), "Broadcast event: ");
// }
//отправка события другим устройствам в сети==============================
// if (jsonReadBool(settingsFlashJson, "mqttin")) {
// String json = "{}";
// jsonWriteStr_(json, "id", _id);
// jsonWriteStr_(json, "val", value);
// jsonWriteInt_(json, "int", _interval + 5000); // 5 секунд про запас
// publishEvent(_id, json);
// SerialPrint("i", F("<=MQTT"), "Broadcast event: " + json);
//}
if (jsonReadBool(settingsFlashJson, "mqttin")) {
String json = "{}";
jsonWriteStr_(json, "id", _id);
jsonWriteStr_(json, "val", value);
jsonWriteInt_(json, "int", _interval/1000 + 5); // 5 секунд про запас
publishEvent(_id, json);
SerialPrint("i", F("<=MQTT"), "Broadcast event: " + json);
}
//========================================================================
}

View File

@@ -411,7 +411,9 @@ IoTValue sysExecute(SysOp command, std::vector<IoTValue> &param) {
case sysop_mqttPub:
if (param.size() == 2) {
// Serial.printf("Call from sysExecute %s %s\n", param[0].valS.c_str(), param[1].valS.c_str());
value.valD = mqtt.publish(param[0].valS.c_str(), param[1].valS.c_str(), false);
String tmpStr = param[1].valS;
if (param[1].isDecimal) tmpStr = param[1].valD;
value.valD = mqtt.publish(param[0].valS.c_str(), tmpStr.c_str(), false);
}
break;
case sysop_getUptime:

View File

@@ -178,7 +178,7 @@ public:
int b = map(value.valD, 1,1024,1,255);
_strip->setBrightness(b);
_strip->show();
regEvent(value.valD, "Ws2812b");
if (generateEvent) regEvent(value.valD, "Ws2812b");
}
~Ws2812b(){};

View File

@@ -70,7 +70,7 @@ class ButtonIn : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
regEvent((String)(int)value.valD, "ButtonIn");
if (generateEvent) regEvent((String)(int)value.valD, "ButtonIn");
}
String getValue() {

View File

@@ -40,7 +40,7 @@ class ButtonOut : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
IoTgpio.digitalWrite(_pin, _inv?!value.valD:value.valD);
regEvent((String)(int)value.valD, "ButtonOut");
if (generateEvent) regEvent((String)(int)value.valD, "ButtonOut");
}
String getValue() {

View File

@@ -57,7 +57,7 @@ class IoTServo : public IoTItem {
if (value.isDecimal & (_oldValue != value.valD)) {
_oldValue = value.valD;
servObj.write(_oldValue);
regEvent(value.valD, "IoTServo");
if (generateEvent) regEvent(value.valD, "IoTServo");
}
}

View File

@@ -57,7 +57,7 @@ class Pwm32 : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
ledcWrite(_ledChannel, value.valD);
regEvent(value.valD, "Pwm32");
if (generateEvent) regEvent(value.valD, "Pwm32");
}
//=======================================================================================================

View File

@@ -47,7 +47,7 @@ class Pwm8266 : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
IoTgpio.analogWrite(_pin, value.valD);
regEvent(value.valD, "Pwm8266");
if (generateEvent) regEvent(value.valD, "Pwm8266");
}
//=======================================================================================================

View File

@@ -313,7 +313,7 @@ class Date : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
regEvent(value.valS, "");
if (generateEvent) regEvent(value.valS, "");
//отправка данных при изменении даты
for (std::list<IoTItem *>::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) {
if ((*it)->getSubtype() == "Loging") {

View File

@@ -8,7 +8,7 @@ class VButton : public IoTItem {
void setValue(IoTValue Value, bool generateEvent = true) {
value = Value;
regEvent((String)(int)value.valD, "VButton");
if (generateEvent) regEvent((String)(int)value.valD, "VButton");
}
String getValue() {