From ea68905f4e9d3459755c72b6f2bffa5d8f1ede0a Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Sat, 15 Jan 2022 16:30:34 +0100 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D1=81=20=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=D0=BB=D0=BE=D0=B3=D0=BE=D0=B2=D1=8B=D0=BC=20=D1=81=D0=B5?= =?UTF-8?q?=D0=BD=D1=81=D0=BE=D1=80=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_svelte/config.json | 102 +++++++++--------- include/classes/IoTSensor.h | 38 +++---- include/modules/{IoTSensorA.h => AnalogAdc.h} | 8 +- src/Configuration.cpp | 20 +--- src/classes/IoTSensor.cpp | 4 +- src/modules/AnalogAdc.cpp | 31 ++++++ src/modules/IoTSensorA.cpp | 17 --- src/modules/IoTSensorAnalog.cpp | 29 ----- 8 files changed, 114 insertions(+), 135 deletions(-) rename include/modules/{IoTSensorA.h => AnalogAdc.h} (51%) create mode 100644 src/modules/AnalogAdc.cpp delete mode 100644 src/modules/IoTSensorA.cpp delete mode 100644 src/modules/IoTSensorAnalog.cpp diff --git a/data_svelte/config.json b/data_svelte/config.json index 2bc2c26d..61377c72 100644 --- a/data_svelte/config.json +++ b/data_svelte/config.json @@ -1,53 +1,7 @@ [ - { - "type": "Variable", - "subtype": "button-out", - "id": "btn1", - "widget": "toggle", - "page": "Кнопки", - "descr": "Освещение", - "gpio": 13, - "inv": false - }, - { - "type": "Variable", - "subtype": "pwm-out", - "id": "pwm1", - "widget": "range", - "page": "Регуляторы", - "descr": "Освещение", - "gpio": 14, - "inv": false - }, { "type": "Reading", - "subtype": "button-in", - "id": "btn", - "widget": "toggle", - "page": "Кнопки", - "descr": "Освещение", - "gpio": 12, - "inv": false - }, - { - "type": "Reading", - "subtype": "output-value", - "id": "txt", - "widget": "anydata", - "page": "Предупреждения", - "descr": "Статус" - }, - { - "type": "Reading", - "subtype": "input-value", - "id": "txt", - "widget": "input", - "page": "Лимиты", - "descr": "Порог" - }, - { - "type": "Reading", - "subtype": "analog-adc", + "subtype": "AnalogAdc", "id": "t", "widget": "anydata", "page": "Сенсоры", @@ -57,7 +11,7 @@ }, { "type": "Reading", - "subtype": "analog-adc", + "subtype": "AnalogAdc", "id": "t", "widget": "anydata", "page": "Сенсоры", @@ -65,9 +19,55 @@ "pin": 0, "int": 15 }, + { + "type": "Variable", + "subtype": "ButtonOut", + "id": "btn1", + "widget": "toggle", + "page": "Кнопки", + "descr": "Освещение", + "gpio": 13, + "inv": false + }, + { + "type": "Variable", + "subtype": "PwmOut", + "id": "pwm1", + "widget": "range", + "page": "Регуляторы", + "descr": "Освещение", + "gpio": 14, + "inv": false + }, { "type": "Reading", - "subtype": "bme280", + "subtype": "ButtonIn", + "id": "btn", + "widget": "toggle", + "page": "Кнопки", + "descr": "Освещение", + "gpio": 12, + "inv": false + }, + { + "type": "Reading", + "subtype": "OutputValue", + "id": "txt", + "widget": "anydata", + "page": "Предупреждения", + "descr": "Статус" + }, + { + "type": "Reading", + "subtype": "InputValue", + "id": "txt", + "widget": "input", + "page": "Лимиты", + "descr": "Порог" + }, + { + "type": "Reading", + "subtype": "Bme280", "id": "t", "widget": "anydata", "page": "Сенсоры", @@ -75,7 +75,7 @@ }, { "type": "Reading", - "subtype": "bme280", + "subtype": "Bme280", "id": "p", "widget": "anydata", "page": "Сенсоры", @@ -83,7 +83,7 @@ }, { "type": "Reading", - "subtype": "bme280", + "subtype": "Bme280", "id": "h", "widget": "anydata", "page": "Сенсоры", diff --git a/include/classes/IoTSensor.h b/include/classes/IoTSensor.h index f4fb061c..c7c5cf9e 100644 --- a/include/classes/IoTSensor.h +++ b/include/classes/IoTSensor.h @@ -3,24 +3,26 @@ #include class IoTSensor { - public: - IoTSensor(); - ~IoTSensor(); + public: + IoTSensor(); + ~IoTSensor(); - void loop(); - virtual void doByInterval(); - void init(String key, String id, unsigned long interval); - void regEvent(String value, String consoleInfo); + void loop(); + virtual void doByInterval(); + void init(String key, String id, unsigned long interval); + void regEvent(String value, String consoleInfo); - String getKey(); - String getID(); + String getKey(); + String getID(); - unsigned long currentMillis; - unsigned long prevMillis; - unsigned long difference; - - protected: - String _key; - String _id; - unsigned long _interval; -}; \ No newline at end of file + unsigned long currentMillis; + unsigned long prevMillis; + unsigned long difference; + + protected: + String _key; + String _id; + unsigned long _interval; +}; + +extern IoTSensor* myIoTSensor; \ No newline at end of file diff --git a/include/modules/IoTSensorA.h b/include/modules/AnalogAdc.h similarity index 51% rename from include/modules/IoTSensorA.h rename to include/modules/AnalogAdc.h index 2410d698..e71cde7f 100644 --- a/include/modules/IoTSensorA.h +++ b/include/modules/AnalogAdc.h @@ -2,10 +2,10 @@ #include "Global.h" #include "Classes/IoTSensor.h" -class IoTSensorA : public IoTSensor { +class AnalogAdc : public IoTSensor { public: - IoTSensorA(String parameters); - ~IoTSensorA(); + AnalogAdc(String parameters); + ~AnalogAdc(); void doByInterval(); @@ -13,4 +13,4 @@ class IoTSensorA : public IoTSensor { unsigned int _pin; }; -extern IoTSensorA* mySensorAnalog; \ No newline at end of file +extern void* getAPI_AnalogAdc(String params); \ No newline at end of file diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 7f275f98..0c3d4033 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -1,5 +1,5 @@ #include "Configuration.h" -#include "modules/IoTSensorA.h" +#include "modules/AnalogAdc.h" std::vector iotSensors; @@ -11,20 +11,10 @@ void configure(String path) { if (jsonArrayElement.startsWith(",")) { jsonArrayElement = jsonArrayElement.substring(1, jsonArrayElement.length()); //это нужно оптимизировать в последствии } - String subtype; - if (jsonRead(jsonArrayElement, F("subtype"), subtype)) { - if (subtype == F("button-out")) { - //============================= - } else if (subtype == F("pwm-out")) { - //============================= - } else if (subtype == F("analog-adc")) { - mySensorAnalog = new IoTSensorA(jsonArrayElement); - iotSensors.push_back(mySensorAnalog); - } else { - SerialPrint(F("E"), F("Config"), "type not exist " + subtype); - } - } else { - SerialPrint(F("E"), F("Config"), F("json error")); + + myIoTSensor = (IoTSensor*)getAPI_AnalogAdc(jsonArrayElement); + if (myIoTSensor) { + iotSensors.push_back(myIoTSensor); } } file.close(); diff --git a/src/classes/IoTSensor.cpp b/src/classes/IoTSensor.cpp index 07db9b67..a44198da 100644 --- a/src/classes/IoTSensor.cpp +++ b/src/classes/IoTSensor.cpp @@ -36,4 +36,6 @@ void IoTSensor::regEvent(String value, String consoleInfo = "") { SerialPrint("I", "Sensor", "'" + _id + "' data: " + String(value) + "' " + consoleInfo); } -void IoTSensor::doByInterval() {} \ No newline at end of file +void IoTSensor::doByInterval() {} + +IoTSensor* myIoTSensor; \ No newline at end of file diff --git a/src/modules/AnalogAdc.cpp b/src/modules/AnalogAdc.cpp new file mode 100644 index 00000000..baf351b6 --- /dev/null +++ b/src/modules/AnalogAdc.cpp @@ -0,0 +1,31 @@ +#include "modules/AnalogAdc.h" + +// setup() +AnalogAdc::AnalogAdc(String parameters) { + init(jsonReadStr(parameters, "key"), jsonReadStr(parameters, "id"), jsonReadInt(parameters, "int")); + + _pin = jsonReadInt(parameters, "pin"); +} + +AnalogAdc::~AnalogAdc() {} + +// loop() +void AnalogAdc::doByInterval() { + float value = analogRead(_pin); + + regEvent((String)value, "analog"); +} + +void* getAPI_AnalogAdc(String parameters) { + String subtype; + if (!jsonRead(parameters, F("subtype"), subtype)) { //если нет такого ключа в представленном json или он не валидный + SerialPrint(F("E"), F("Config"), F("json error AnalogAdc")); + return nullptr; + } else { + if (subtype == F("AnalogAdc")) { + return new AnalogAdc(parameters); + } else { + return nullptr; + } + } +} diff --git a/src/modules/IoTSensorA.cpp b/src/modules/IoTSensorA.cpp deleted file mode 100644 index a67085b6..00000000 --- a/src/modules/IoTSensorA.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "modules/IoTSensorA.h" - -IoTSensorA::IoTSensorA(String parameters) { - init(jsonReadStr(parameters, "key"), jsonReadStr(parameters, "id"), jsonReadInt(parameters, "int")); - - _pin = jsonReadInt(parameters, "pin"); -} - -IoTSensorA::~IoTSensorA() {} - -void IoTSensorA::doByInterval() { - float value = analogRead(_pin); - - regEvent((String)value, "analog"); //обязательный вызов для отправки результата работы -} - -IoTSensorA* mySensorAnalog; diff --git a/src/modules/IoTSensorAnalog.cpp b/src/modules/IoTSensorAnalog.cpp deleted file mode 100644 index 05067ddd..00000000 --- a/src/modules/IoTSensorAnalog.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "Utils/JsonUtils.h" -#include "Utils/SerialPrint.h" -#include "Utils/StringUtils.h" -#include "Classes/IoTSensor.h" -#include -class IoTSensorAnalog : public IoTSensor { - private: - //описание переменных экземпляра датчика - аналог глобальных переменных - //описание параметров передаваемых из настроек датчика из веба - unsigned int _pin; - - public: - //аналог setup() - IoTSensorAnalog(String parameters) { - //передаем часть базовых параметров в конструктор базового класса для обеспечения работы его методов - init(jsonReadStr(parameters, "key"), jsonReadStr(parameters, "id"), jsonReadInt(parameters, "int")); - - _pin = jsonReadInt(parameters, "pin"); - } - - //аналог loop() - void doByInterval() { - float value = analogRead(_pin); - - regEvent((String)value, "analog"); //обязательный вызов для отправки результата работы - } - - ~IoTSensorAnalog() {} -}; \ No newline at end of file