UpdateVirtualSensor uses information from signals

UpdateVirtualSensor uses information obtained from signals to avoid
extensive dbus queries.

Tested:
1:Execute DBus cmd to see if VirtualSensor can correctly obtain the
value of DbusSensor
    busctl tree xyz.openbmc_project.VirtualSensor
    busctl introspect xyz.openbmc_project.VirtualSensor xxx
2:Waiting for the value change of DbusSensor,Check if the value of
the virtual sensor has changed after the dbusSensor changes.

Fixes openbmc/phosphor-virtual-sensor#1

Change-Id: If11f9017b31ce5cf06f910a38c65637c55d74b24
Signed-off-by: Tao Lin <lintao.lc@ieisystem.com>
diff --git a/dbusSensor.hpp b/dbusSensor.hpp
index 5c119a5..41d8c69 100644
--- a/dbusSensor.hpp
+++ b/dbusSensor.hpp
@@ -1,11 +1,12 @@
-#include "dbusUtils.hpp"
+#pragma once
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/bus/match.hpp>
 
-const char* sensorIntf = "xyz.openbmc_project.Sensor.Value";
+namespace phosphor::virtual_sensor
+{
 
-int handleDbusSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err);
+class VirtualSensor;
 
 class DbusSensor
 {
@@ -18,44 +19,48 @@
      * @param[in] bus     - Handle to system dbus
      * @param[in] path    - The Dbus path of sensor
      */
-    DbusSensor(sdbusplus::bus_t& bus, const std::string& path, void* ctx) :
-        bus(bus), path(path),
-        signal(
-            bus,
-            sdbusplus::bus::match::rules::propertiesChanged(path, sensorIntf),
-            handleDbusSignal, ctx)
-    {}
+    DbusSensor(sdbusplus::bus_t& bus, const std::string& path,
+               VirtualSensor& virtualSensor);
 
-    /** @brief Get sensor value property from D-bus interface */
-    double getSensorValue()
-    {
-        if (servName.empty())
-        {
-            servName = getService(bus, path, sensorIntf);
-            if (servName.empty())
-            {
-                return std::numeric_limits<double>::quiet_NaN();
-            }
-        }
-
-        try
-        {
-            return getDbusProperty<double>(bus, servName, path, sensorIntf,
-                                           "Value");
-        }
-        catch (const std::exception& e)
-        {
-            return std::numeric_limits<double>::quiet_NaN();
-        }
-    }
+    /** @brief Get sensor value from local */
+    double getSensorValue();
 
   private:
     /** @brief sdbusplus bus client connection. */
     sdbusplus::bus_t& bus;
+
     /** @brief complete path for sensor */
     std::string path{};
+
     /** @brief service name for the sensor daemon */
     std::string servName{};
+
+    /** @brief point to the VirtualSensor */
+    VirtualSensor& virtualSensor;
+
     /** @brief signal for sensor value change */
-    sdbusplus::bus::match_t signal;
+    sdbusplus::bus::match_t signalPropChange;
+
+    /** @brief signal for sensor interface remove */
+    sdbusplus::bus::match_t signalRemove;
+
+    /** @brief Match for this dbus sensor service destroy  */
+    std::unique_ptr<sdbusplus::bus::match_t> signalNameOwnerChanged;
+
+    /** @brief dbus sensor value */
+    double value = std::numeric_limits<double>::quiet_NaN();
+
+    /** @brief Get sensor value property from D-bus interface */
+    void initSensorValue();
+
+    /** @brief Handle for this dbus sensor NameOwnerChanged */
+    void handleDbusSignalNameOwnerChanged(sdbusplus::message_t& msg);
+
+    /** @brief Handle for this dbus sensor PropertyChanged */
+    void handleDbusSignalPropChange(sdbusplus::message_t& msg);
+
+    /** @brief Handle for this dbus sensor InterfaceRemove */
+    void handleDbusSignalRemove(sdbusplus::message_t& msg);
 };
+
+} // namespace phosphor::virtual_sensor