2022-01-14 21:48:43 +01:00
|
|
|
#include "Utils/JsonUtils.h"
|
|
|
|
|
#include "Utils/SerialPrint.h"
|
|
|
|
|
#include "Classes/ScenarioClass3.h"
|
2022-02-06 09:11:07 +03:00
|
|
|
#include "Classes/IoTItem.h"
|
2022-02-02 21:40:45 +01:00
|
|
|
#include "WsServer.h"
|
2022-01-14 21:48:43 +01:00
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
IoTItem::IoTItem(String parameters) {
|
2022-02-01 23:04:06 +03:00
|
|
|
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);
|
2022-02-02 21:17:50 +01:00
|
|
|
|
2022-02-01 23:04:06 +03:00
|
|
|
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();
|
2022-02-02 21:17:50 +01:00
|
|
|
}
|
2022-02-01 12:51:34 +03:00
|
|
|
}
|
2022-02-06 09:11:07 +03:00
|
|
|
IoTItem::~IoTItem() {}
|
2022-01-14 21:48:43 +01:00
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
String IoTItem::getSubtype() {
|
2022-02-01 12:30:13 +03:00
|
|
|
return _subtype;
|
2022-01-14 21:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
String IoTItem::getID() {
|
2022-01-14 21:48:43 +01:00
|
|
|
return _id;
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
void IoTItem::loop() {
|
2022-01-14 21:48:43 +01:00
|
|
|
currentMillis = millis();
|
|
|
|
|
difference = currentMillis - prevMillis;
|
|
|
|
|
if (difference >= _interval) {
|
|
|
|
|
prevMillis = millis();
|
|
|
|
|
this->doByInterval();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
void IoTItem::regEvent(String value, String consoleInfo = "") {
|
2022-02-01 23:04:06 +03:00
|
|
|
eventGen2(_id, value);
|
2022-02-08 00:20:49 +01:00
|
|
|
jsonWriteStr(paramsHeapJson, _id, value);
|
2022-02-02 21:17:50 +01:00
|
|
|
publishStatusMqtt(_id, value);
|
2022-02-02 21:40:45 +01:00
|
|
|
publishStatusWs(_id, value);
|
2022-02-01 23:04:06 +03:00
|
|
|
SerialPrint("I", "Sensor " + consoleInfo, "'" + _id + "' data: " + value + "'");
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
void IoTItem::regEvent(float value, String consoleInfo = "") {
|
2022-02-01 23:04:06 +03:00
|
|
|
if (_multiply) value = value * _multiply;
|
|
|
|
|
if (_plus) value = value + _multiply;
|
2022-02-02 21:17:50 +01:00
|
|
|
if (_round != 0) {
|
2022-02-02 11:08:40 +03:00
|
|
|
if (value > 0) {
|
|
|
|
|
value = (int)(value * _round + 0.5F);
|
|
|
|
|
value = value / _round;
|
|
|
|
|
}
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
value = (int)(value * _round - 0.5F);
|
|
|
|
|
value = value / _round;
|
|
|
|
|
}
|
2022-02-01 23:04:06 +03:00
|
|
|
}
|
|
|
|
|
if (_map1 != _map2) value = map(value, _map1, _map2, _map3, _map4);
|
|
|
|
|
|
2022-02-02 11:08:40 +03:00
|
|
|
// убираем лишние нули
|
|
|
|
|
char buf[20];
|
|
|
|
|
sprintf(buf, "%g", value);
|
|
|
|
|
regEvent((String)buf, consoleInfo);
|
2022-01-14 21:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
void IoTItem::doByInterval() {}
|
2022-01-15 16:30:34 +01:00
|
|
|
|
2022-02-06 09:32:28 +03:00
|
|
|
void IoTItem::execute(String command, String param) {}
|
|
|
|
|
|
2022-02-06 09:11:07 +03:00
|
|
|
IoTItem* myIoTItem;
|