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() {}