Add redundancy sensor and cleanup

Add fan redundancy sensor and move some class members
into the base class. Asio now protects against setting
the same number twice, allow it now to simplify code.

Change-Id: Idb6b5ff4746da92be62c4756fe442d5a5ed23f4f
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/sensors/include/ADCSensor.hpp b/sensors/include/ADCSensor.hpp
index e15ba0a..59b4450 100644
--- a/sensors/include/ADCSensor.hpp
+++ b/sensors/include/ADCSensor.hpp
@@ -7,7 +7,6 @@
 class ADCSensor : public Sensor
 {
   public:
-    std::string name;
     std::string configuration;
     ADCSensor(const std::string &path,
               sdbusplus::asio::object_server &objectServer,
@@ -18,7 +17,6 @@
     ~ADCSensor();
 
   private:
-    std::string path;
     sdbusplus::asio::object_server &objServer;
     boost::asio::posix::stream_descriptor inputDev;
     boost::asio::deadline_timer waitTimer;
diff --git a/sensors/include/CPUSensor.hpp b/sensors/include/CPUSensor.hpp
index c4fca5d..41cc02f 100644
--- a/sensors/include/CPUSensor.hpp
+++ b/sensors/include/CPUSensor.hpp
@@ -7,7 +7,6 @@
 class CPUSensor : public Sensor
 {
   public:
-    std::string name;
     std::string configuration;
     CPUSensor(const std::string &path, const std::string &objectType,
               sdbusplus::asio::object_server &objectServer,
@@ -20,7 +19,6 @@
     static constexpr unsigned int sensorPollMs = 1000;
 
   private:
-    std::string path;
     std::string objectType;
     sdbusplus::asio::object_server &objServer;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
diff --git a/sensors/include/HwmonTempSensor.hpp b/sensors/include/HwmonTempSensor.hpp
index 14cb6c7..117f286 100644
--- a/sensors/include/HwmonTempSensor.hpp
+++ b/sensors/include/HwmonTempSensor.hpp
@@ -7,7 +7,6 @@
 class HwmonTempSensor : public Sensor
 {
   public:
-    std::string name;
     std::string configuration;
     HwmonTempSensor(const std::string &path, const std::string &objectType,
                     sdbusplus::asio::object_server &objectServer,
@@ -18,7 +17,6 @@
     ~HwmonTempSensor();
 
   private:
-    std::string path;
     std::string objectType;
     sdbusplus::asio::object_server &objServer;
     boost::asio::posix::stream_descriptor inputDev;
diff --git a/sensors/include/TachSensor.hpp b/sensors/include/TachSensor.hpp
index 2dc6216..36e3eaa 100644
--- a/sensors/include/TachSensor.hpp
+++ b/sensors/include/TachSensor.hpp
@@ -1,6 +1,8 @@
 #pragma once
 
 #include <Thresholds.hpp>
+#include <boost/container/flat_map.hpp>
+#include <boost/container/flat_set.hpp>
 #include <sdbusplus/asio/object_server.hpp>
 #include <sensor.hpp>
 
@@ -23,23 +25,40 @@
     boost::asio::ip::tcp::socket inputDev;
     int fd;
 };
+
+class RedundancySensor
+{
+  public:
+    RedundancySensor(size_t count, const std::vector<std::string> &children,
+                     sdbusplus::asio::object_server &objectServer);
+    ~RedundancySensor();
+
+    void update(const std::string &name, bool failed);
+
+  private:
+    size_t count;
+    std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
+    sdbusplus::asio::object_server &objectServer;
+    boost::container::flat_map<std::string, bool> statuses;
+};
+
 class TachSensor : public Sensor
 {
   public:
-    std::string name;
     std::string configuration;
     TachSensor(const std::string &path,
                sdbusplus::asio::object_server &objectServer,
                std::shared_ptr<sdbusplus::asio::connection> &conn,
                std::unique_ptr<PresenceSensor> &&presence,
+               std::unique_ptr<RedundancySensor> &redundancy,
                boost::asio::io_service &io, const std::string &fanName,
                std::vector<thresholds::Threshold> &&thresholds,
                const std::string &sensorConfiguration);
     ~TachSensor();
 
   private:
-    std::string path;
     sdbusplus::asio::object_server &objServer;
+    std::unique_ptr<RedundancySensor> &redundancy;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
     std::unique_ptr<PresenceSensor> presence;
     boost::asio::posix::stream_descriptor inputDev;
diff --git a/sensors/include/Thresholds.hpp b/sensors/include/Thresholds.hpp
index 4a07ca2..3f9aa79 100644
--- a/sensors/include/Thresholds.hpp
+++ b/sensors/include/Thresholds.hpp
@@ -48,7 +48,8 @@
                       const thresholds::Threshold &threshold,
                       std::shared_ptr<sdbusplus::asio::connection> &conn);
 
-void checkThresholds(Sensor *sensor);
+// returns false if a critical threshold has been crossed, true otherwise
+bool checkThresholds(Sensor *sensor);
 void assertThresholds(Sensor *sensor, thresholds::Level level,
                       thresholds::Direction direction, bool assert);
 } // namespace thresholds
diff --git a/sensors/include/sensor.hpp b/sensors/include/sensor.hpp
index a19b969..dc9cdbe 100644
--- a/sensors/include/sensor.hpp
+++ b/sensors/include/sensor.hpp
@@ -6,7 +6,15 @@
 constexpr size_t sensorFailedPollTimeMs = 5000;
 struct Sensor
 {
+    Sensor(const std::string& name, const std::string& path,
+           std::vector<thresholds::Threshold>&& thresholdData) :
+        name(name),
+        path(path), thresholds(std::move(thresholdData))
+    {
+    }
     virtual ~Sensor() = default;
+    std::string name;
+    std::string path;
     std::vector<thresholds::Threshold> thresholds;
     std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;