From 3898234c6d440d78f9467cd80b112107c7413461 Mon Sep 17 00:00:00 2001 From: biver Date: Tue, 1 Feb 2022 23:04:06 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D0=BC=20=D0=B2=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=BF=D0=BE=D0=B4?= =?UTF-8?q?=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D1=83=20=D0=BC=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D0=B8=D1=87=D0=B5=D1=81=D0=BA=D0=B8=D1=85=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D0=BA=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D1=81=D0=B5?= =?UTF-8?q?=D1=85=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81=D1=82=D1=80=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D0=BC=D1=8B=D1=85=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D0=B9=20float=20=D1=82=D0=B8=D0=BF=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/classes/IoTSensor.h | 9 +++++++++ src/classes/IoTSensor.cpp | 39 ++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/classes/IoTSensor.h b/include/classes/IoTSensor.h index 40807f75..f58dd40c 100644 --- a/include/classes/IoTSensor.h +++ b/include/classes/IoTSensor.h @@ -10,6 +10,7 @@ class IoTSensor { void loop(); virtual void doByInterval(); void regEvent(String value, String consoleInfo); + void regEvent(float value, String consoleInfo); String getSubtype(); String getID(); @@ -22,4 +23,12 @@ class IoTSensor { String _subtype; String _id; unsigned long _interval; + + float _multiply; // умножаем на значение + float _plus; // увеличиваем на значение + int _map1; + int _map2; + int _map3; + int _map4; + int _round; // 1, 10, 100, 1000, 10000 }; diff --git a/src/classes/IoTSensor.cpp b/src/classes/IoTSensor.cpp index fa253b5c..3781d4ec 100644 --- a/src/classes/IoTSensor.cpp +++ b/src/classes/IoTSensor.cpp @@ -5,9 +5,22 @@ IoTSensor::IoTSensor(String parameters) { - _interval = jsonReadInt(parameters, "int") * 1000; - _subtype = jsonReadStr(parameters, "subtype"); - _id = jsonReadStr(parameters, "id"); + jsonRead(parameters, "int", _interval); + _interval = _interval * 1000; + jsonRead(parameters, "subtype", _subtype); + jsonRead(parameters, "id", _id); + jsonRead(parameters, "multiply", _multiply); + jsonRead(parameters, "plus", _plus); + jsonRead(parameters, "round", _round); + + String map; + jsonRead(parameters, "map", map); + if (map != "") { + _map1 = selectFromMarkerToMarker(map, ",", 0).toInt(); + _map2 = selectFromMarkerToMarker(map, ",", 1).toInt(); + _map3 = selectFromMarkerToMarker(map, ",", 2).toInt(); + _map4 = selectFromMarkerToMarker(map, ",", 3).toInt(); + } } IoTSensor::~IoTSensor() {} @@ -29,10 +42,22 @@ void IoTSensor::loop() { } void IoTSensor::regEvent(String value, String consoleInfo = "") { - eventGen2(_id, String(value)); - jsonWriteStr(paramsFlashJson, _id, String(value)); - publishStatus(_id, String(value)); - SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + String(value) + "'"); + eventGen2(_id, value); + jsonWriteStr(paramsFlashJson, _id, value); + publishStatus(_id, value); + SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'"); +} + +void IoTSensor::regEvent(float value, String consoleInfo = "") { + if (_multiply) value = value * _multiply; + if (_plus) value = value + _multiply; + if (_round != 0) { + if (value > 0) value = (int)(value * _round + 0.5) / _round; + if (value < 0) value = (int)(value * _round - 0.5) / _round; + } + if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4); + + regEvent((String)value, consoleInfo); } void IoTSensor::doByInterval() {}