Fix Exit Air Temp Sensor

Add custom destructor for CFM sensor as it is configuration
driven and can be destroyed unlike exit air. Having this not
have a destructor caused the old sensor loop to keep running
and not be destroyed. Also attempt to pre-populate inlet temp
as it doesn't get updated as frequently as tach readings.

Tested-by: sensor list started to work again and readings
were appropriate.

Change-Id: Id5b42a73a22ff860149ced1ba073d81fe84a9375
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/include/ExitAirTempSensor.hpp b/include/ExitAirTempSensor.hpp
index f18fd9b..bd8b675 100644
--- a/include/ExitAirTempSensor.hpp
+++ b/include/ExitAirTempSensor.hpp
@@ -23,7 +23,7 @@
               sdbusplus::asio::object_server &objectServer,
               std::vector<thresholds::Threshold> &&thresholds,
               std::shared_ptr<ExitAirTempSensor> &parent);
-    ~CFMSensor() = default;
+    ~CFMSensor();
 
     bool calculate(double &);
     void updateReading(void);
@@ -35,6 +35,7 @@
     boost::container::flat_map<std::string, std::pair<double, double>>
         tachRanges;
     std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
+    sdbusplus::asio::object_server &objServer;
     void addTachRanges(const std::string &serviceName, const std::string &path);
 };
 
@@ -74,4 +75,4 @@
     double getTotalCFM(void);
     bool calculate(double &val);
     void setupMatches(void);
-};
\ No newline at end of file
+};
diff --git a/service_files/xyz.openbmc_project.exitairsensor.service b/service_files/xyz.openbmc_project.exitairsensor.service
index 647eb06..e0b2a0d 100644
--- a/service_files/xyz.openbmc_project.exitairsensor.service
+++ b/service_files/xyz.openbmc_project.exitairsensor.service
@@ -7,3 +7,6 @@
 RestartSec=5
 ExecStart=/usr/sbin/exitairtempsensor
 
+[Install]
+WantedBy=multi-user.target
+
diff --git a/src/ExitAirTempSensor.cpp b/src/ExitAirTempSensor.cpp
index 4983f52..a8a5748 100644
--- a/src/ExitAirTempSensor.cpp
+++ b/src/ExitAirTempSensor.cpp
@@ -64,6 +64,11 @@
             }
             double value = sdbusplus::message::variant_ns::visit(
                 VariantToDoubleVisitor(), findValue->second);
+            if (std::isnan(value))
+            {
+                return;
+            }
+
             callback(value, message);
         };
     matches.emplace_back(connection,
@@ -86,7 +91,7 @@
            "" /* todo: remove arg from base*/, std::move(thresholds),
            sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
            cfmMaxReading, cfmMinReading),
-    dbusConnection(conn), parent(parent)
+    dbusConnection(conn), parent(parent), objServer(objectServer)
 {
     sensorInterface =
         objectServer.add_interface("/xyz/openbmc_project/sensors/cfm/" + name,
@@ -122,6 +127,13 @@
             }));
 }
 
+CFMSensor::~CFMSensor()
+{
+    objServer.remove_interface(thresholdInterfaceWarning);
+    objServer.remove_interface(thresholdInterfaceCritical);
+    objServer.remove_interface(sensorInterface);
+}
+
 void CFMSensor::addTachRanges(const std::string& serviceName,
                               const std::string& path)
 {
@@ -171,6 +183,7 @@
     double totalCFM = 0;
     for (const std::string& tachName : tachs)
     {
+
         auto findReading = std::find_if(
             tachReadings.begin(), tachReadings.end(), [&](const auto& item) {
                 return boost::ends_with(item.first, tachName);
@@ -185,7 +198,7 @@
             {
                 std::cerr << "Can't find " << tachName << "in readings\n";
             }
-            return false; // haven't gotten a reading
+            continue; // haven't gotten a reading
         }
 
         if (findRange == tachRanges.end())
@@ -242,6 +255,7 @@
 
     // divide by 100 since rpm is in percent
     value = totalCFM / 100;
+    return true;
 }
 
 static constexpr double exitAirMaxReading = 127;
@@ -305,6 +319,22 @@
                              updateReading();
                          });
     }
+    dbusConnection->async_method_call(
+        [this](boost::system::error_code ec,
+               const std::variant<double>& value) {
+            if (ec)
+            {
+                // sensor not ready yet
+                return;
+            }
+
+            inletTemp = sdbusplus::message::variant_ns::visit(
+                VariantToDoubleVisitor(), value);
+        },
+        "xyz.openbmc_project.HwmonTempSensor",
+        std::string("/xyz/openbmc_project/sensors/") + inletTemperatureSensor,
+        "org.freedesktop.DBus.Properties", "Get",
+        "xyz.openbmc_project.Sensor.Value", "Value");
 }
 
 void ExitAirTempSensor::updateReading(void)