2021-12-14 23:05:42 +01:00
|
|
|
#include "Consts.h"
|
|
|
|
|
#ifdef EnableSensorAny
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
|
|
#include "BufferExecute.h"
|
|
|
|
|
#include "Class/LineParsing.h"
|
|
|
|
|
#include "Global.h"
|
2021-12-14 23:20:14 +01:00
|
|
|
#include "YourSensor.h"
|
2021-12-14 23:05:42 +01:00
|
|
|
#include "items/vSensorAny.h"
|
|
|
|
|
|
|
|
|
|
SensorAny::SensorAny(const String& paramsAny) {
|
|
|
|
|
_paramsAny = paramsAny;
|
|
|
|
|
|
|
|
|
|
_interval = jsonReadInt(_paramsAny, "int");
|
|
|
|
|
_c = jsonReadFloat(_paramsAny, "c");
|
2021-12-19 22:19:35 +01:00
|
|
|
_k = jsonReadFloat(_paramsAny, "k");
|
2021-12-14 23:05:42 +01:00
|
|
|
_key = jsonReadStr(_paramsAny, "key");
|
|
|
|
|
_addr = jsonReadStr(_paramsAny, "addr");
|
|
|
|
|
_type = jsonReadStr(_paramsAny, "type");
|
2021-12-19 22:19:35 +01:00
|
|
|
_val = jsonReadStr(_paramsAny, "val");
|
2021-12-14 23:05:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SensorAny::~SensorAny() {}
|
|
|
|
|
|
|
|
|
|
void SensorAny::loop() {
|
|
|
|
|
difference = millis() - prevMillis;
|
2021-12-14 23:55:17 +01:00
|
|
|
if (difference >= _interval * 1000) {
|
2021-12-14 23:05:42 +01:00
|
|
|
prevMillis = millis();
|
|
|
|
|
read();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SensorAny::read() {
|
2021-12-19 22:19:35 +01:00
|
|
|
float value = yourSensorReading(_type, _paramsAny);
|
2021-12-14 23:05:42 +01:00
|
|
|
|
|
|
|
|
value = value * _c;
|
|
|
|
|
|
|
|
|
|
eventGen2(_key, String(value));
|
|
|
|
|
jsonWriteStr(configLiveJson, _key, String(value));
|
|
|
|
|
publishStatus(_key, String(value));
|
|
|
|
|
String path = mqttRootDevice + "/" + _key + "/status";
|
|
|
|
|
String json = "{}";
|
|
|
|
|
jsonWriteStr(json, "status", String(value));
|
|
|
|
|
String MyJson = json;
|
|
|
|
|
jsonWriteStr(MyJson, "topic", path);
|
|
|
|
|
ws.textAll(MyJson);
|
|
|
|
|
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MySensorAnyVector* mySensorAny = nullptr;
|
|
|
|
|
|
|
|
|
|
void AnySensor() {
|
|
|
|
|
String params = "{}";
|
|
|
|
|
myLineParsing.update();
|
|
|
|
|
jsonWriteStr(params, "key", myLineParsing.gkey());
|
|
|
|
|
jsonWriteStr(params, "addr", myLineParsing.gaddr());
|
|
|
|
|
jsonWriteStr(params, "int", myLineParsing.gint());
|
|
|
|
|
jsonWriteStr(params, "c", myLineParsing.gc());
|
2021-12-19 22:19:35 +01:00
|
|
|
jsonWriteStr(params, "k", myLineParsing.gk());
|
|
|
|
|
jsonWriteStr(params, "val", myLineParsing.gval());
|
2021-12-14 23:55:17 +01:00
|
|
|
jsonWriteStr(params, "type", myLineParsing.gtype());
|
2021-12-14 23:05:42 +01:00
|
|
|
myLineParsing.clear();
|
|
|
|
|
|
|
|
|
|
static bool firstTime = true;
|
|
|
|
|
if (firstTime) mySensorAny = new MySensorAnyVector();
|
|
|
|
|
firstTime = false;
|
|
|
|
|
mySensorAny->push_back(SensorAny(params));
|
|
|
|
|
}
|
|
|
|
|
#endif
|