NVMeSensorMain: Define extractBusNumber()

Start lifting code from the lambda to simplify its implementation.

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I4663af6f00e9118938cb56dc9cb514118ef62fb3
diff --git a/src/NVMeSensorMain.cpp b/src/NVMeSensorMain.cpp
index b017bca..96f42c9 100644
--- a/src/NVMeSensorMain.cpp
+++ b/src/NVMeSensorMain.cpp
@@ -20,6 +20,7 @@
 #include <NVMeSensor.hpp>
 #include <boost/asio/deadline_timer.hpp>
 
+#include <optional>
 #include <regex>
 
 static constexpr const char* sensorType =
@@ -34,6 +35,20 @@
     return nvmeDeviceMap;
 }
 
+static std::optional<int>
+    extractBusNumber(const std::string& path,
+                     const SensorBaseConfigMap& properties)
+{
+    auto findBus = properties.find("Bus");
+    if (findBus == properties.end())
+    {
+        std::cerr << "could not determine bus number for " << path << "\n";
+        return std::nullopt;
+    }
+
+    return std::visit(VariantToIntVisitor(), findBus->second);
+}
+
 static void handleSensorConfigurations(
     boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
@@ -70,14 +85,14 @@
         {
             continue;
         }
-        auto findBus = baseConfiguration->second.find("Bus");
-        if (findBus == baseConfiguration->second.end())
+
+        std::optional<int> busNumber =
+            extractBusNumber(sensor.first, baseConfiguration->second);
+        if (!busNumber)
         {
             continue;
         }
 
-        int busNumber = std::visit(VariantToIntVisitor(), findBus->second);
-
         auto findSensorName = baseConfiguration->second.find("Name");
         if (findSensorName == baseConfiguration->second.end())
         {
@@ -95,10 +110,10 @@
                       << "\n";
         }
 
-        int rootBus = busNumber;
+        int rootBus = *busNumber;
 
         std::string muxPath = "/sys/bus/i2c/devices/i2c-" +
-                              std::to_string(busNumber) + "/mux_device";
+                              std::to_string(*busNumber) + "/mux_device";
 
         if (std::filesystem::is_symlink(muxPath))
         {
@@ -131,7 +146,7 @@
 
         std::shared_ptr<NVMeSensor> sensorPtr = std::make_shared<NVMeSensor>(
             objectServer, io, dbusConnection, sensorName,
-            std::move(sensorThresholds), interfacePath, busNumber);
+            std::move(sensorThresholds), interfacePath, *busNumber);
 
         context->addSensor(sensorPtr);
     }