add isRedundant (#2031)

Co-authored-by: Matthew Kennedy <makenne@microsoft.com>
This commit is contained in:
Matthew Kennedy 2020-12-06 13:55:06 -06:00 committed by GitHub
parent 70467a0117
commit 9ccda6bbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 2 deletions

View File

@ -15,6 +15,11 @@ public:
SensorResult get() const override;
bool isRedundant() const override {
// This sensor is redundant when not ignoring the second channel
return !m_ignoreSecond;
}
void showInfo(Logging* logger, const char* sensorName) const override;
private:

View File

@ -118,14 +118,24 @@ public:
float getRaw() const {
const auto sensor = m_sensor;
if (m_sensor) {
return m_sensor->getRaw();
if (sensor) {
return sensor->getRaw();
}
// We've exhausted all valid ways to return something - sensor not found.
return 0;
}
bool isRedundant() const {
const auto sensor = m_sensor;
if (sensor) {
return sensor->isRedundant();
}
return false;
}
private:
bool m_useMock = false;
float m_mockValue;
@ -181,6 +191,12 @@ bool Sensor::Register() {
return entry ? entry->getRaw() : 0;
}
/*static*/ bool Sensor::isRedundant(SensorType type) {
const auto entry = getEntryForType(type);
return entry ? entry->isRedundant() : false;
}
/*static*/ bool Sensor::hasSensor(SensorType type) {
const auto entry = getEntryForType(type);

View File

@ -92,6 +92,11 @@ public:
*/
static float getRaw(SensorType type);
/*
* Get whether a sensor is redundant (a composite of multiple other sensors that can check consistency between them)
*/
static bool isRedundant(SensorType type);
/*
* Query whether there is a sensor of a particular type currently registered.
*/
@ -137,6 +142,14 @@ public:
return 0;
}
/*
* Get whether this sensor is redundant (backed by multiple other sensors)
*/
virtual bool isRedundant() const {
// By default sensors are not redundant
return false;
}
protected:
// Protected constructor - only subclasses call this
explicit Sensor(SensorType type)

View File

@ -71,6 +71,8 @@ TEST_F(SensorRedundant, SetTwoSensors)
auto result = dut.get();
EXPECT_TRUE(result.Valid);
EXPECT_FLOAT_EQ(result.Value, 25.0f);
EXPECT_TRUE(dut.isRedundant());
}
}
@ -176,6 +178,8 @@ TEST_F(SensorRedundantIgnoreSecond, OnlyFirst)
auto result = dut.get();
EXPECT_TRUE(result.Valid);
EXPECT_FLOAT_EQ(result.Value, 44.0f);
EXPECT_FALSE(dut.isRedundant());
}
}