35 lines
1013 B
C++
35 lines
1013 B
C++
#include "fgc/sensors/McuEnvSensor.h"
|
|
|
|
#include <chrono>
|
|
|
|
namespace fgc {
|
|
|
|
namespace {
|
|
long long epochMs() {
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
.count();
|
|
}
|
|
} // namespace
|
|
|
|
McuEnvSensor::McuEnvSensor(IMotorController& motor, std::function<long long()> now_ms)
|
|
: motor_(motor), now_ms_(now_ms ? std::move(now_ms) : epochMs) {}
|
|
|
|
std::optional<EnvSample> McuEnvSensor::current() const {
|
|
const auto r = motor_.latestEnv();
|
|
if (!r || !r->sensor_ok) return std::nullopt;
|
|
if (now_ms_() - r->epoch_ms > kStaleMs) return std::nullopt;
|
|
EnvSample s;
|
|
s.valid = true;
|
|
s.temp_c = static_cast<float>(r->temp_c);
|
|
s.humidity_pct = static_cast<float>(r->humidity);
|
|
s.timestamp_ms = r->epoch_ms;
|
|
return s;
|
|
}
|
|
|
|
bool McuEnvSensor::connected() const { return current().has_value(); }
|
|
|
|
std::optional<EnvSample> McuEnvSensor::sample() { return current(); }
|
|
|
|
} // namespace fgc
|