diff --git a/include/ESPConfiguration.h b/include/ESPConfiguration.h index d8eab7f5..1e185219 100644 --- a/include/ESPConfiguration.h +++ b/include/ESPConfiguration.h @@ -2,7 +2,7 @@ #include "Global.h" #include "classes/IoTItem.h" -extern std::vector IoTItems; // вектор ссылок базового класса IoTItem - список всех запущенных сенсоров +extern std::list IoTItems; // вектор ссылок базового класса IoTItem - список всех запущенных сенсоров extern void configure(String path); void clearConfigure(); diff --git a/include/Global.h b/include/Global.h index b919cdec..c1bf7a9d 100644 --- a/include/Global.h +++ b/include/Global.h @@ -8,6 +8,7 @@ #include #include #include +#include #ifdef ESP32 #include "WiFi.h" diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index e15c48e5..05a23ffc 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -10,8 +10,7 @@ struct IoTValue { class IoTItem { public: IoTItem(String parameters); - ~IoTItem(); - + virtual ~IoTItem() {} void loop(); virtual void doByInterval(); virtual IoTValue execute(String command, std::vector ¶m); @@ -27,6 +26,8 @@ class IoTItem { unsigned long difference; IoTValue value; // хранение основного значения, котрое обновляется из сценария, execute(), loop() или doByInterval() + + bool iAmDead = false; // признак необходимости удалить объект из базы virtual IoTGpio* getGpioDriver(); virtual void setValue(IoTValue Value); @@ -46,3 +47,13 @@ class IoTItem { }; IoTItem* findIoTItem(String name); // поиск экземпляра элемента модуля по имени + + +class externalVariable: IoTItem { // объект, создаваемый при получении информации о событии на другом контроллере для хранения информации о событии указанное время + + public: + externalVariable(String parameters); + ~externalVariable(); + void doByInterval(); // для данного класса doByInterval+int выполняет роль счетчика обратного отсчета до уничтожения + +}; \ No newline at end of file diff --git a/src/ESPConfiguration.cpp b/src/ESPConfiguration.cpp index 16279a8b..de80d564 100644 --- a/src/ESPConfiguration.cpp +++ b/src/ESPConfiguration.cpp @@ -3,7 +3,7 @@ extern IoTGpio IoTgpio; -std::vector IoTItems; +std::list IoTItems; void* getAPI(String subtype, String params); void configure(String path) { @@ -38,8 +38,8 @@ void configure(String path) { void clearConfigure() { Serial.printf("Start clearing config\n"); - for (unsigned int i = 0; i < IoTItems.size(); i++) { - if (IoTItems[i]) delete IoTItems[i]; + for (std::list::iterator it=IoTItems.begin(); it != IoTItems.end(); ++it) { + if (*it) delete *it; } IoTItems.clear(); } \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 2292bd61..f1ed3ea1 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -56,12 +56,17 @@ void setup() { //загрузка сценария iotScen.loadScenario("/scenario.txt"); - iotScen.ExecScenario(""); + //iotScen.ExecScenario(""); // test Serial.println("-------test start--------"); Serial.println("--------test end---------"); + // симуляция добавления внешних событий + IoTItems.push_back((IoTItem*) new externalVariable("{\"id\":\"rel1\",\"val\":10,\"int\":20}")); + IoTItems.push_back((IoTItem*) new externalVariable("{\"id\":\"rel4\",\"val\":34,\"int\":30}")); + + //тест перебора пинов из расширения // for (int i = 109; i < 112; i++) { // IoTgpio.pinMode(i, OUTPUT); @@ -94,8 +99,14 @@ void loop() { //обновление mqtt mqttLoop(); - for (unsigned int i = 0; i < IoTItems.size(); i++) { - IoTItems[i]->loop(); + // передаем управление каждому элементу конфигурации для выполнения своих функций + for (std::list::iterator it=IoTItems.begin(); it != IoTItems.end(); ++it) { + (*it)->loop(); + if ((*it)->iAmDead) { + delete *it; + IoTItems.erase(it); + break; + } } handleOrder(); diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index f3d9ac12..0dbedc9b 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -15,6 +15,13 @@ IoTItem::IoTItem(String parameters) { jsonRead(parameters, F("plus"), _plus, false); jsonRead(parameters, F("round"), _round, false); + String valAsStr; + if (jsonRead(parameters, F("val"), valAsStr, false)) // значение переменной или датчика при инициализации если есть в конфигурации + if (value.isDecimal = isDigitDotCommaStr(valAsStr)) + value.valD = valAsStr.toFloat(); + else + value.valS = valAsStr; + String map; jsonRead(parameters, F("map"), map, false); if (map != "") { @@ -24,7 +31,8 @@ IoTItem::IoTItem(String parameters) { _map4 = selectFromMarkerToMarker(map, ",", 3).toInt(); } } -IoTItem::~IoTItem() {} + +// IoTItem::~IoTItem() {} String IoTItem::getSubtype() { return _subtype; @@ -79,8 +87,8 @@ void IoTItem::doByInterval() {} IoTValue IoTItem::execute(String command, std::vector& param) { return {}; } IoTItem* findIoTItem(String name) { // поиск элемента модуля в существующей конфигурации - for (unsigned int i = 0; i < IoTItems.size(); i++) { - if (IoTItems[i]->getID() == name) return IoTItems[i]; + for (std::list::iterator it=IoTItems.begin(); it != IoTItems.end(); ++it) { + if ((*it)->getID() == name) return *it; } return nullptr; @@ -96,4 +104,18 @@ void IoTItem::setValue(IoTValue Value) { value = Value; if (value.isDecimal) regEvent(value.valD, ""); else regEvent(value.valS, ""); +} + + +externalVariable::externalVariable(String parameters) : IoTItem(parameters) { + prevMillis = millis(); // запоминаем текущее значение таймера для выполения doByInterval после int сек + Serial.printf("Call from externalVariable: parameters %s %d\n", parameters.c_str(), _interval); +} + +externalVariable::~externalVariable() { + Serial.printf("Call from ~externalVariable: Im dead\n"); +} + +void externalVariable::doByInterval() { // для данного класса doByInterval+int выполняет роль счетчика обратного отсчета до уничтожения + iAmDead = true; } \ No newline at end of file diff --git a/src/modules/Ads1115.cpp b/src/modules/Ads1115.cpp index a49a08c8..9faa1398 100644 --- a/src/modules/Ads1115.cpp +++ b/src/modules/Ads1115.cpp @@ -58,7 +58,7 @@ class Ads1115 : public IoTItem { } } - ~Ads1115(); + ~Ads1115() {}; }; diff --git a/src/modules/Aht20.cpp b/src/modules/Aht20.cpp index 80675077..63dba42e 100644 --- a/src/modules/Aht20.cpp +++ b/src/modules/Aht20.cpp @@ -26,7 +26,7 @@ class Aht20t : public IoTItem { else SerialPrint("E", "Sensor AHTt", "Error"); } - ~Aht20t(); + ~Aht20t() {}; }; @@ -40,7 +40,7 @@ class Aht20h : public IoTItem { else SerialPrint("E", "Sensor AHTt", "Error"); } - ~Aht20h(); + ~Aht20h() {}; }; diff --git a/src/modules/AnalogAdc.cpp b/src/modules/AnalogAdc.cpp index 5d4f1bfb..75e3aa6d 100644 --- a/src/modules/AnalogAdc.cpp +++ b/src/modules/AnalogAdc.cpp @@ -40,7 +40,7 @@ class AnalogAdc : public IoTItem { } //======================================================================================================= - ~AnalogAdc(); + ~AnalogAdc() {}; }; //после замены названия сенсора, на функцию можно не обращать внимания diff --git a/src/modules/Bme280.cpp b/src/modules/Bme280.cpp index ca3589a0..c9e16d33 100644 --- a/src/modules/Bme280.cpp +++ b/src/modules/Bme280.cpp @@ -29,7 +29,7 @@ class Bme280t : public IoTItem { SerialPrint("E", "Sensor Bme280t", "Error"); } - ~Bme280t(); + ~Bme280t() {}; }; class Bme280h : public IoTItem { @@ -49,7 +49,7 @@ class Bme280h : public IoTItem { SerialPrint("E", "Sensor Bme280h", "Error"); } - ~Bme280h(); + ~Bme280h() {}; }; class Bme280p : public IoTItem { @@ -70,7 +70,7 @@ class Bme280p : public IoTItem { SerialPrint("E", "Sensor Bme280p", "Error"); } - ~Bme280p(); + ~Bme280p() {}; }; diff --git a/src/modules/Bmp280.cpp b/src/modules/Bmp280.cpp index 368c9bf7..07e72b2b 100644 --- a/src/modules/Bmp280.cpp +++ b/src/modules/Bmp280.cpp @@ -29,7 +29,7 @@ class Bmp280t : public IoTItem { else SerialPrint("E", "Sensor DHTt", "Error"); } - ~Bmp280t(); + ~Bmp280t() {}; }; @@ -50,7 +50,7 @@ class Bmp280p : public IoTItem { } else SerialPrint("E", "Sensor DHTh", "Error"); } - ~Bmp280p(); + ~Bmp280p() {}; }; diff --git a/src/modules/ButtonOut.cpp b/src/modules/ButtonOut.cpp index 00efb707..46481454 100644 --- a/src/modules/ButtonOut.cpp +++ b/src/modules/ButtonOut.cpp @@ -47,7 +47,7 @@ class ButtonOut : public IoTItem { } //======================================================================================================= - ~ButtonOut(); + ~ButtonOut() {}; }; void* getAPI_ButtonOut(String subtype, String param) { diff --git a/src/modules/Dht1122.cpp b/src/modules/Dht1122.cpp index 98ada9be..9c96f77f 100644 --- a/src/modules/Dht1122.cpp +++ b/src/modules/Dht1122.cpp @@ -29,7 +29,7 @@ class Dht1122t : public IoTItem { else SerialPrint("E", "Sensor DHTt", "Error"); } - ~Dht1122t(); + ~Dht1122t() {}; }; @@ -48,7 +48,7 @@ class Dht1122h : public IoTItem { else SerialPrint("E", "Sensor DHTh", "Error"); } - ~Dht1122h(); + ~Dht1122h() {}; }; diff --git a/src/modules/Ds18b20.cpp b/src/modules/Ds18b20.cpp index 3ead2b7a..1845a16e 100644 --- a/src/modules/Ds18b20.cpp +++ b/src/modules/Ds18b20.cpp @@ -81,7 +81,7 @@ class Ds18b20 : public IoTItem { } //======================================================================================================= - ~Ds18b20(){}; + ~Ds18b20() {}; }; //после замены названия сенсора, на функцию можно не обращать внимания diff --git a/src/modules/GY-21.cpp b/src/modules/GY-21.cpp index 9cb81f3d..16a363e9 100644 --- a/src/modules/GY-21.cpp +++ b/src/modules/GY-21.cpp @@ -24,7 +24,7 @@ class GY21t : public IoTItem { else SerialPrint("E", "Sensor GY21t", "Error"); } - ~GY21t(); + ~GY21t() {}; }; class GY21h : public IoTItem { @@ -38,7 +38,7 @@ class GY21h : public IoTItem { else SerialPrint("E", "Sensor GY21h", "Error"); } - ~GY21h(); + ~GY21h() {}; }; void* getAPI_GY21(String subtype, String param) { diff --git a/src/modules/Hdc1080.cpp b/src/modules/Hdc1080.cpp index 1e891a69..fac2315f 100644 --- a/src/modules/Hdc1080.cpp +++ b/src/modules/Hdc1080.cpp @@ -25,7 +25,7 @@ class Hdc1080t : public IoTItem { else SerialPrint("E", "Sensor Hdc1080t", "Error"); } - ~Hdc1080t(); + ~Hdc1080t() {}; }; class Hdc1080h : public IoTItem { @@ -38,7 +38,7 @@ class Hdc1080h : public IoTItem { else SerialPrint("E", "Sensor Hdc1080h", "Error"); } - ~Hdc1080h(); + ~Hdc1080h() {}; }; void* getAPI_Hdc1080(String subtype, String param) { diff --git a/src/modules/Lcd2004.cpp b/src/modules/Lcd2004.cpp index c3261726..d683258d 100644 --- a/src/modules/Lcd2004.cpp +++ b/src/modules/Lcd2004.cpp @@ -85,7 +85,7 @@ class Lcd2004 : public IoTItem { LCDI2C->print(tmpStr); } - ~Lcd2004(){}; + ~Lcd2004() {}; }; void* getAPI_Lcd2004(String subtype, String param) { diff --git a/src/modules/Sht20.cpp b/src/modules/Sht20.cpp index af677033..bcba7c57 100644 --- a/src/modules/Sht20.cpp +++ b/src/modules/Sht20.cpp @@ -18,7 +18,7 @@ class Sht20t : public IoTItem { else SerialPrint("E", "Sensor Sht20t", "Error"); } - ~Sht20t(); + ~Sht20t() {}; }; class Sht20h : public IoTItem { @@ -32,7 +32,7 @@ class Sht20h : public IoTItem { else SerialPrint("E", "Sensor Sht20h", "Error"); } - ~Sht20h(); + ~Sht20h() {}; }; diff --git a/src/modules/SysExt.cpp b/src/modules/SysExt.cpp index 4c04cc2d..e8272f04 100644 --- a/src/modules/SysExt.cpp +++ b/src/modules/SysExt.cpp @@ -36,7 +36,7 @@ class SysExt : public IoTItem { return {}; // команда поддерживает возвращаемое значения. Т.е. по итогу выполнения команды или общения с внешней системой, можно вернуть значение в сценарий для дальнейшей обработки } - ~SysExt(){}; + ~SysExt() {}; }; void* getAPI_SysExt(String subtype, String param) {