Add base class to simplify threshold logic

Add base class for the default case check_thresholds
calls the thresholds.cpp function. This way for normal
things we can just pass sensors around as the base class.

Make it so that theshold alerts are only sent once.

Change-Id: Ic8e3ae1900aeb12b74c099f637f45bc038bd3c9a
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/sensors/include/ADCSensor.hpp b/sensors/include/ADCSensor.hpp
index 07c7893..d59bd5f 100644
--- a/sensors/include/ADCSensor.hpp
+++ b/sensors/include/ADCSensor.hpp
@@ -2,8 +2,9 @@
 
 #include <Thresholds.hpp>
 #include <sdbusplus/asio/object_server.hpp>
+#include <sensor.hpp>
 
-class ADCSensor
+class ADCSensor : public Sensor
 {
   public:
     std::string name;
@@ -20,16 +21,9 @@
   private:
     std::string path;
     sdbusplus::asio::object_server &objServer;
-    std::vector<thresholds::Threshold> thresholds;
-    std::shared_ptr<sdbusplus::asio::dbus_interface> sensor_interface;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_warning;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_critical;
     boost::asio::posix::stream_descriptor input_dev;
     boost::asio::deadline_timer wait_timer;
     boost::asio::streambuf read_buf;
-    double value;
     int err_count;
     double max_value;
     double min_value;
@@ -38,8 +32,7 @@
     void handle_response(const boost::system::error_code &err);
     void check_thresholds(void);
     void update_value(const double &new_value);
-    void assert_thresholds(thresholds::Level level,
-                           thresholds::Direction direction, bool assert);
+
     void set_initial_properties(
         std::shared_ptr<sdbusplus::asio::connection> &conn);
 };
\ No newline at end of file
diff --git a/sensors/include/CPUSensor.hpp b/sensors/include/CPUSensor.hpp
index 85ae831..8f7d2f0 100644
--- a/sensors/include/CPUSensor.hpp
+++ b/sensors/include/CPUSensor.hpp
@@ -2,8 +2,9 @@
 
 #include <Thresholds.hpp>
 #include <sdbusplus/asio/object_server.hpp>
+#include <sensor.hpp>
 
-class CPUSensor
+class CPUSensor : public Sensor
 {
   private:
     std::string path;
@@ -11,16 +12,9 @@
     sdbusplus::asio::object_server &objServer;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
     std::string name;
-    std::vector<thresholds::Threshold> thresholds;
-    std::shared_ptr<sdbusplus::asio::dbus_interface> sensor_interface;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_warning;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_critical;
     boost::asio::posix::stream_descriptor input_dev;
     boost::asio::deadline_timer wait_timer;
     boost::asio::streambuf read_buf;
-    double value;
     int err_count;
     double max_value;
     double min_value;
@@ -28,8 +22,7 @@
     void handle_response(const boost::system::error_code &err);
     void check_thresholds(void);
     void update_value(const double &new_value);
-    void assert_thresholds(thresholds::Level level,
-                           thresholds::Direction direction, bool assert);
+
     void set_initial_properties(
         std::shared_ptr<sdbusplus::asio::connection> &conn);
 
diff --git a/sensors/include/HwmonTempSensor.hpp b/sensors/include/HwmonTempSensor.hpp
index 58eb534..ebdaea6 100644
--- a/sensors/include/HwmonTempSensor.hpp
+++ b/sensors/include/HwmonTempSensor.hpp
@@ -2,8 +2,9 @@
 
 #include <Thresholds.hpp>
 #include <sdbusplus/asio/object_server.hpp>
+#include <sensor.hpp>
 
-class HwmonTempSensor
+class HwmonTempSensor : public Sensor
 {
   public:
     std::string name;
@@ -20,16 +21,9 @@
     std::string path;
     std::string objectType;
     sdbusplus::asio::object_server &objServer;
-    std::vector<thresholds::Threshold> thresholds;
-    std::shared_ptr<sdbusplus::asio::dbus_interface> sensor_interface;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_warning;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_critical;
     boost::asio::posix::stream_descriptor input_dev;
     boost::asio::deadline_timer wait_timer;
     boost::asio::streambuf read_buf;
-    double value;
     int err_count;
     double max_value;
     double min_value;
@@ -37,8 +31,7 @@
     void handle_response(const boost::system::error_code &err);
     void check_thresholds(void);
     void update_value(const double &new_value);
-    void assert_thresholds(thresholds::Level level,
-                           thresholds::Direction direction, bool assert);
+
     void set_initial_properties(
         std::shared_ptr<sdbusplus::asio::connection> &conn);
 };
\ No newline at end of file
diff --git a/sensors/include/TachSensor.hpp b/sensors/include/TachSensor.hpp
index 2c8b535..e352855 100644
--- a/sensors/include/TachSensor.hpp
+++ b/sensors/include/TachSensor.hpp
@@ -2,8 +2,9 @@
 
 #include <Thresholds.hpp>
 #include <sdbusplus/asio/object_server.hpp>
+#include <sensor.hpp>
 
-class TachSensor
+class TachSensor : public Sensor
 {
   public:
     std::string name;
@@ -20,16 +21,9 @@
     std::string path;
     sdbusplus::asio::object_server &objServer;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
-    std::vector<thresholds::Threshold> thresholds;
-    std::shared_ptr<sdbusplus::asio::dbus_interface> sensor_interface;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_warning;
-    std::shared_ptr<sdbusplus::asio::dbus_interface>
-        threshold_interface_critical;
     boost::asio::posix::stream_descriptor input_dev;
     boost::asio::deadline_timer wait_timer;
     boost::asio::streambuf read_buf;
-    double value;
     int err_count;
     double max_value;
     double min_value;
@@ -37,8 +31,7 @@
     void handle_response(const boost::system::error_code &err);
     void check_thresholds(void);
     void update_value(const double &new_value);
-    void assert_thresholds(thresholds::Level level,
-                           thresholds::Direction direction, bool assert);
+
     void set_initial_properties(
         std::shared_ptr<sdbusplus::asio::connection> &conn);
 };
diff --git a/sensors/include/Thresholds.hpp b/sensors/include/Thresholds.hpp
index 5b194c6..90e148c 100644
--- a/sensors/include/Thresholds.hpp
+++ b/sensors/include/Thresholds.hpp
@@ -2,6 +2,7 @@
 #include <Utils.hpp>
 #include <nlohmann/json.hpp>
 
+struct Sensor;
 namespace thresholds
 {
 enum Level
@@ -26,6 +27,7 @@
     Direction direction;
     double value;
     bool writeable;
+    bool asserted = false;
 };
 
 bool ParseThresholdsFromConfig(
@@ -38,10 +40,15 @@
                              const double scale_factor);
 bool HasCriticalInterface(
     const std::vector<thresholds::Threshold> &threshold_vector);
+
 bool HasWarningInterface(
     const std::vector<thresholds::Threshold> &threshold_vector);
 
 void persistThreshold(const std::string &baseInterface, const std::string &path,
                       const thresholds::Threshold &threshold,
                       std::shared_ptr<sdbusplus::asio::connection> &conn);
+
+void checkThresholds(Sensor *sensor);
+void assertThresholds(Sensor *sensor, thresholds::Level level,
+                      thresholds::Direction direction, bool assert);
 } // namespace thresholds