Sensor3/lib/patrix/sensors/DallasSensor.h

58 lines
1021 B
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 time_t timeoutSec;
double lastValue = NAN;
time_t lastTimestamp = 0;
public:
DallasSensor(Dallas &sensors, const uint64_t address, const time_t timeoutSec) :
sensors(sensors),
address(address),
timeoutSec(timeoutSec) {
// -
}
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();
lastValue = value;
lastTimestamp = timestamp;
}
}
[[nodiscard]] double getLastValue() const {
return lastValue;
}
[[nodiscard]] time_t getLastTimestamp() const {
return lastTimestamp;
}
};
#endif