98 lines
2.0 KiB
C++
98 lines
2.0 KiB
C++
#ifndef SENSOR2_DALLAS_SENSOR_H
|
|
#define SENSOR2_DALLAS_SENSOR_H
|
|
|
|
#include "base.h"
|
|
#include "Dallas.h"
|
|
#include "wifi.h"
|
|
#include "mqtt.h"
|
|
#include "data.h"
|
|
|
|
class DallasSensor {
|
|
|
|
private:
|
|
|
|
Dallas &sensors;
|
|
|
|
uint64_t address;
|
|
|
|
const char *name;
|
|
|
|
const double valueThreshold;
|
|
|
|
const time_t timeoutSec;
|
|
|
|
const time_t minIntervalMillis;
|
|
|
|
double lastSentValue = NAN;
|
|
|
|
unsigned long lastSentMillis = 0;
|
|
|
|
double lastValue = NAN;
|
|
|
|
time_t lastTimestamp = 0;
|
|
|
|
double lastValidValue = NAN;
|
|
|
|
time_t lastValidTimestamp = 0;
|
|
|
|
public:
|
|
|
|
DallasSensor(Dallas &sensors, const uint64_t address, const char *name, const double valueThreshold, const time_t timeoutSec, const time_t minIntervalMillis) :
|
|
sensors(sensors),
|
|
address(address),
|
|
name(name),
|
|
valueThreshold(valueThreshold),
|
|
timeoutSec(timeoutSec),
|
|
minIntervalMillis(minIntervalMillis) {
|
|
// -
|
|
}
|
|
|
|
void loop() {
|
|
const time_t now = getTime();
|
|
if (now - lastTimestamp > timeoutSec) {
|
|
lastTimestamp = 0;
|
|
lastValue = NAN;
|
|
}
|
|
if (sensors.isConverted()) {
|
|
const double value = sensors.read(address);
|
|
const time_t timestamp = sensors.getTimestamp();
|
|
const unsigned long millisNow=millis();
|
|
const bool doPublish = !isnan(value) && (abs(lastSentValue - value) >= valueThreshold || millisNow - lastSentMillis >= minIntervalMillis);
|
|
if (doPublish) {
|
|
lastSentValue = value;
|
|
lastSentMillis = millisNow;
|
|
dataAdd(name, timestamp, value);
|
|
}
|
|
lastValue = value;
|
|
lastTimestamp = timestamp;
|
|
if (!isnan(value)) {
|
|
lastValidValue = value;
|
|
lastValidTimestamp = timestamp;
|
|
}
|
|
}
|
|
}
|
|
|
|
const char *getName() const {
|
|
return name;
|
|
}
|
|
|
|
double getLastValue() const {
|
|
return lastValue;
|
|
}
|
|
|
|
time_t getLastTimestamp() const {
|
|
return lastTimestamp;
|
|
}
|
|
|
|
double getLastValidValue() const {
|
|
return lastValidValue;
|
|
}
|
|
|
|
time_t getLastValidTimestamp() const {
|
|
return lastValidTimestamp;
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|