roo_control
API Documentation for roo_control
Loading...
Searching...
No Matches
expiring_thermometer.h
Go to the documentation of this file.
1#pragma once
2
4
5namespace roo_control {
6
7/// Reports readings of another thermometer if fresher than an expiration.
8///
9/// Otherwise, reports Unknown.
11 public:
14
16 roo_time::Duration expiration)
17 : thermometer_(thermometer), expiration_(expiration), cached_() {}
18
19 void setExpiration(roo_time::Duration expiration) {
20 expiration_ = expiration;
21 }
22
23 roo_time::Duration expiration() const { return expiration_; }
24
25 Reading readTemperature() const override {
26 Reading reading = thermometer_->readTemperature();
27 if (reading.time > cached_.time && cached_.value.isUnknown()) {
28 cached_ = reading;
29 }
30 if (reading.value.isUnknown() && !cached_.value.isUnknown()) {
31 reading = cached_;
32 }
33 if (reading.time + expiration_ < roo_time::Uptime::Now()) {
34 reading.value = roo_quantity::UnknownTemperature();
35 }
36 return reading;
37 }
38
39 private:
40 const Thermometer *thermometer_;
41 roo_time::Duration expiration_;
42 mutable Reading cached_;
43};
44
45/// Convenience function for expiring thermometer readings.
46///
47/// Returns the temperature if it is fresher than the expiration threshold, and
48/// Unknown otherwise. Useful when stale thermometer readings (e.g. due to
49/// disconnected devices) should not be used.
51 const Thermometer &t, roo_time::Duration expiration) {
52 return ExpiringThermometer(&t, expiration).readTemperature();
53}
54
55} // namespace roo_control
Reports readings of another thermometer if fresher than an expiration.
roo_time::Duration expiration() const
ExpiringThermometer(const Thermometer *thermometer)
Reading readTemperature() const override
Returns the latest available temperature reading.
ExpiringThermometer(const Thermometer *thermometer, roo_time::Duration expiration)
void setExpiration(roo_time::Duration expiration)
An abstraction of a multi-state settable switch.
Definition switch.h:24
An abstraction of a thermometer, i.e., a device that reports temperature.
Definition thermometer.h:10
Thermometer::Reading ReadExpiringTemperature(const Thermometer &t, roo_time::Duration expiration)
Convenience function for expiring thermometer readings.
Captures a temperature measured at a specific time.
Definition thermometer.h:13
roo_quantity::Temperature value
Measured temperature value.
Definition thermometer.h:15
roo_time::Uptime time
Timestamp of the reading.
Definition thermometer.h:17