Check threshold values

Added check threshold functionality and logging required messages.

Signed-off-by: Vijay Khemka <vijaykhemka@fb.com>
Change-Id: I1621ab1e1e938a9906ea81c3611e9d25d51d207e
diff --git a/virtualSensor.cpp b/virtualSensor.cpp
index fb6d456..63baea5 100644
--- a/virtualSensor.cpp
+++ b/virtualSensor.cpp
@@ -176,6 +176,94 @@
     WarningInterface::warningLow(sensorThreshold.warningLow);
 }
 
+void VirtualSensor::checkSensorThreshold(const double value)
+{
+    auto criticalHigh = CriticalInterface::criticalHigh();
+    auto criticalLow = CriticalInterface::criticalLow();
+    auto warningHigh = WarningInterface::warningHigh();
+    auto warningLow = WarningInterface::warningLow();
+
+    if (value > criticalHigh)
+    {
+        if (!CriticalInterface::criticalAlarmHigh())
+        {
+            CriticalInterface::criticalAlarmHigh(true);
+            log<level::ERR>("ASSERT: Virtual Sensor has exceeded "
+                            "critical high threshold",
+                            entry("NAME = %s", name.c_str()));
+        }
+        return;
+    }
+
+    if (CriticalInterface::criticalAlarmHigh())
+    {
+        CriticalInterface::criticalAlarmHigh(false);
+        log<level::INFO>("DEASSERT: Virtual Sensor is under "
+                         "critical high threshold",
+                         entry("NAME = %s", name.c_str()));
+    }
+
+    if (value > warningHigh)
+    {
+        if (!WarningInterface::warningAlarmHigh())
+        {
+            WarningInterface::warningAlarmHigh(true);
+            log<level::ERR>("ASSERT: Virtual Sensor has exceeded "
+                            "warning high threshold",
+                            entry("NAME = %s", name.c_str()));
+        }
+        return;
+    }
+
+    if (WarningInterface::warningAlarmHigh())
+    {
+        WarningInterface::warningAlarmHigh(false);
+        log<level::INFO>("DEASSERT: Virtual Sensor is under "
+                         "warning high threshold",
+                         entry("NAME = %s", name.c_str()));
+    }
+
+    if (value < criticalLow)
+    {
+        if (!CriticalInterface::criticalAlarmLow())
+        {
+            CriticalInterface::criticalAlarmLow(true);
+            log<level::ERR>("ASSERT: Virtual Sensor is under "
+                            "critical low threshold",
+                            entry("NAME = %s", name.c_str()));
+        }
+        return;
+    }
+
+    if (CriticalInterface::criticalAlarmLow())
+    {
+        CriticalInterface::criticalAlarmLow(false);
+        log<level::ERR>("DEASSERT: Virtual Sensor is above "
+                        "critical low threshold",
+                        entry("NAME = %s", name.c_str()));
+    }
+
+    if (value < warningLow)
+    {
+        if (!WarningInterface::warningAlarmLow())
+        {
+            WarningInterface::warningAlarmLow(true);
+            log<level::ERR>("ASSERT: Virtual Sensor is under "
+                            "warning low threshold",
+                            entry("NAME = %s", name.c_str()));
+        }
+        return;
+    }
+
+    if (WarningInterface::warningAlarmLow())
+    {
+        WarningInterface::warningAlarmLow(false);
+        log<level::ERR>("DEASSERT: Virtual Sensor is above "
+                        "warning low threshold",
+                        entry("NAME = %s", name.c_str()));
+    }
+}
+
 void VirtualSensor::updateVirtualSensor()
 {
     for (auto& param : paramMap)
@@ -193,9 +281,15 @@
         }
     }
     double val = expression.value();
+
+    /* Set sensor value to dbus interface */
     setSensorValue(val);
+
     if (DEBUG)
         std::cout << "Sensor value is " << val << "\n";
+
+    /* Check sensor threshold and log required message */
+    checkSensorThreshold(val);
 }
 
 /** @brief Parsing Virtual Sensor config JSON file  */
@@ -243,8 +337,8 @@
                 std::string objPath(sensorDbusPath);
                 objPath += sensorType + "/" + name;
 
-                auto virtualSensorPtr =
-                    std::make_unique<VirtualSensor>(bus, objPath.c_str(), j);
+                auto virtualSensorPtr = std::make_unique<VirtualSensor>(
+                    bus, objPath.c_str(), j, name);
 
                 log<level::INFO>("Added a new virtual sensor",
                                  entry("NAME = %s", name.c_str()));
diff --git a/virtualSensor.hpp b/virtualSensor.hpp
index 4f9de19..e39f8d9 100644
--- a/virtualSensor.hpp
+++ b/virtualSensor.hpp
@@ -80,9 +80,9 @@
      * @param[in] sensorConfig - Json object for sensor config
      */
     VirtualSensor(sdbusplus::bus::bus& bus, const char* objPath,
-                  const Json& sensorConfig) :
+                  const Json& sensorConfig, const std::string& name) :
         sensorIfaces(bus, objPath),
-        bus(bus)
+        bus(bus), name(name)
     {
         initVirtualSensor(sensorConfig);
     }
@@ -108,6 +108,8 @@
   private:
     /** @brief sdbusplus bus client connection. */
     sdbusplus::bus::bus& bus;
+    /** @brief name of sensor */
+    std::string name;
     /** @brief Expression string for virtual sensor value calculations */
     std::string exprStr;
     /** @brief symbol table from exprtk */
@@ -121,6 +123,8 @@
     void initVirtualSensor(const Json& sensorConfig);
     /** @brief Set Sensor Threshold to D-bus at beginning */
     void setSensorThreshold(Threshold& sensorThreshold);
+    /** @brief Check Sensor threshold and update alarm and log */
+    void checkSensorThreshold(const double value);
 };
 
 class VirtualSensors