Remove duplicate functions

There are many duplicate functions in the sensors,
collapse them into base class.

Change-Id: Ic0e4a1c396c221ea637cea5b8f429a751fc31f99
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/ADCSensor.hpp b/include/ADCSensor.hpp
index 59b4450..d7eb933 100644
--- a/include/ADCSensor.hpp
+++ b/include/ADCSensor.hpp
@@ -7,7 +7,6 @@
 class ADCSensor : public Sensor
 {
   public:
-    std::string configuration;
     ADCSensor(const std::string &path,
               sdbusplus::asio::object_server &objectServer,
               std::shared_ptr<sdbusplus::asio::connection> &conn,
@@ -22,14 +21,8 @@
     boost::asio::deadline_timer waitTimer;
     boost::asio::streambuf readBuf;
     int errCount;
-    double maxValue;
-    double minValue;
     double scaleFactor;
     void setupRead(void);
     void handleResponse(const boost::system::error_code &err);
-    void checkThresholds(void);
-    void updateValue(const double &newValue);
-
-    void setInitialProperties(
-        std::shared_ptr<sdbusplus::asio::connection> &conn);
+    void checkThresholds(void) override;
 };
diff --git a/include/CPUSensor.hpp b/include/CPUSensor.hpp
index 41cc02f..4eb2ae0 100644
--- a/include/CPUSensor.hpp
+++ b/include/CPUSensor.hpp
@@ -7,7 +7,6 @@
 class CPUSensor : public Sensor
 {
   public:
-    std::string configuration;
     CPUSensor(const std::string &path, const std::string &objectType,
               sdbusplus::asio::object_server &objectServer,
               std::shared_ptr<sdbusplus::asio::connection> &conn,
@@ -19,20 +18,13 @@
     static constexpr unsigned int sensorPollMs = 1000;
 
   private:
-    std::string objectType;
     sdbusplus::asio::object_server &objServer;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
     boost::asio::posix::stream_descriptor inputDev;
     boost::asio::deadline_timer waitTimer;
     boost::asio::streambuf readBuf;
     int errCount;
-    double maxValue;
-    double minValue;
     void setupRead(void);
     void handleResponse(const boost::system::error_code &err);
-    void checkThresholds(void);
-    void updateValue(const double &newValue);
-
-    void setInitialProperties(
-        std::shared_ptr<sdbusplus::asio::connection> &conn);
+    void checkThresholds(void) override;
 };
diff --git a/include/HwmonTempSensor.hpp b/include/HwmonTempSensor.hpp
index 117f286..3e582ab 100644
--- a/include/HwmonTempSensor.hpp
+++ b/include/HwmonTempSensor.hpp
@@ -7,7 +7,6 @@
 class HwmonTempSensor : public Sensor
 {
   public:
-    std::string configuration;
     HwmonTempSensor(const std::string &path, const std::string &objectType,
                     sdbusplus::asio::object_server &objectServer,
                     std::shared_ptr<sdbusplus::asio::connection> &conn,
@@ -17,19 +16,12 @@
     ~HwmonTempSensor();
 
   private:
-    std::string objectType;
     sdbusplus::asio::object_server &objServer;
     boost::asio::posix::stream_descriptor inputDev;
     boost::asio::deadline_timer waitTimer;
     boost::asio::streambuf readBuf;
     int errCount;
-    double maxValue;
-    double minValue;
     void setupRead(void);
     void handleResponse(const boost::system::error_code &err);
-    void checkThresholds(void);
-    void updateValue(const double &newValue);
-
-    void setInitialProperties(
-        std::shared_ptr<sdbusplus::asio::connection> &conn);
+    void checkThresholds(void) override;
 };
\ No newline at end of file
diff --git a/include/TachSensor.hpp b/include/TachSensor.hpp
index c3d3e58..c9aa80a 100644
--- a/include/TachSensor.hpp
+++ b/include/TachSensor.hpp
@@ -45,8 +45,7 @@
 class TachSensor : public Sensor
 {
   public:
-    std::string configuration;
-    TachSensor(const std::string &path,
+    TachSensor(const std::string &path, const std::string &objectType,
                sdbusplus::asio::object_server &objectServer,
                std::shared_ptr<sdbusplus::asio::connection> &conn,
                std::unique_ptr<PresenceSensor> &&presence,
@@ -65,13 +64,7 @@
     boost::asio::deadline_timer waitTimer;
     boost::asio::streambuf readBuf;
     int errCount;
-    double maxValue;
-    double minValue;
     void setupRead(void);
     void handleResponse(const boost::system::error_code &err);
-    void checkThresholds(void);
-    void updateValue(const double &newValue);
-
-    void setInitialProperties(
-        std::shared_ptr<sdbusplus::asio::connection> &conn);
+    void checkThresholds(void) override;
 };
diff --git a/include/sensor.hpp b/include/sensor.hpp
index 62eec62..f333e6a 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -6,15 +6,23 @@
 constexpr size_t sensorFailedPollTimeMs = 5000;
 struct Sensor
 {
-    Sensor(const std::string& name, const std::string& path,
-           std::vector<thresholds::Threshold>&& thresholdData) :
+    Sensor(const std::string &name, const std::string &path,
+           std::vector<thresholds::Threshold> &&thresholdData,
+           const std::string &configurationPath, const std::string &objectType,
+           const double max, const double min) :
         name(name),
-        path(path), thresholds(std::move(thresholdData))
+        path(path), thresholds(std::move(thresholdData)),
+        configurationPath(configurationPath), maxValue(max), minValue(min)
     {
     }
     virtual ~Sensor() = default;
+    virtual void checkThresholds(void) = 0;
     std::string name;
     std::string path;
+    std::string configurationPath;
+    std::string objectType;
+    double maxValue;
+    double minValue;
     std::vector<thresholds::Threshold> thresholds;
     std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
     std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
@@ -22,7 +30,8 @@
     double value = std::numeric_limits<double>::quiet_NaN();
     double overriddenValue = std::numeric_limits<double>::quiet_NaN();
     bool internalSet = false;
-    int setSensorValue(const double& newValue, double& oldValue)
+
+    int setSensorValue(const double &newValue, double &oldValue)
     {
         if (internalSet)
         {
@@ -33,4 +42,96 @@
         overriddenValue = newValue;
         return 1;
     }
+
+    void
+        setInitialProperties(std::shared_ptr<sdbusplus::asio::connection> &conn)
+    {
+        sensorInterface->register_property("MaxValue", maxValue);
+        sensorInterface->register_property("MinValue", minValue);
+        sensorInterface->register_property(
+            "Value", value, [&](const double &newValue, double &oldValue) {
+                return setSensorValue(newValue, oldValue);
+            });
+
+        for (auto &threshold : thresholds)
+        {
+            std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
+            std::string level;
+            std::string alarm;
+            if (threshold.level == thresholds::Level::CRITICAL)
+            {
+                iface = thresholdInterfaceCritical;
+                if (threshold.direction == thresholds::Direction::HIGH)
+                {
+                    level = "CriticalHigh";
+                    alarm = "CriticalAlarmHigh";
+                }
+                else
+                {
+                    level = "CriticalLow";
+                    alarm = "CriticalAlarmLow";
+                }
+            }
+            else if (threshold.level == thresholds::Level::WARNING)
+            {
+                iface = thresholdInterfaceWarning;
+                if (threshold.direction == thresholds::Direction::HIGH)
+                {
+                    level = "WarningHigh";
+                    alarm = "WarningAlarmHigh";
+                }
+                else
+                {
+                    level = "WarningLow";
+                    alarm = "WarningAlarmLow";
+                }
+            }
+            else
+            {
+                std::cerr << "Unknown threshold level" << threshold.level
+                          << "\n";
+                continue;
+            }
+            if (!iface)
+            {
+                std::cout << "trying to set uninitialized interface\n";
+                continue;
+            }
+            iface->register_property(
+                level, threshold.value,
+                [&](const double &request, double &oldValue) {
+                    oldValue = request; // todo, just let the config do this?
+                    threshold.value = request;
+                    thresholds::persistThreshold(configurationPath, objectType,
+                                                 threshold, conn);
+                    return 1;
+                });
+            iface->register_property(alarm, false);
+        }
+        if (!sensorInterface->initialize())
+        {
+            std::cerr << "error initializing value interface\n";
+        }
+        if (thresholdInterfaceWarning &&
+            !thresholdInterfaceWarning->initialize())
+        {
+            std::cerr << "error initializing warning threshold interface\n";
+        }
+
+        if (thresholdInterfaceCritical &&
+            !thresholdInterfaceCritical->initialize())
+        {
+            std::cerr << "error initializing critical threshold interface\n";
+        }
+    }
+
+    void updateValue(const double &newValue)
+    {
+        // Indicate that it is internal set call
+        internalSet = true;
+        sensorInterface->set_property("Value", newValue);
+        internalSet = false;
+        value = newValue;
+        checkThresholds();
+    }
 };
\ No newline at end of file