Improve host state monitor

This changes to arg0 in the match to limit to just the State.Host
interface and looks just for the CurrentHostState property to
prevent errors printing for properties we don't care about.

It also moves the message.read into a try-catch since it can throw
and changes to get_if instead of get to reduce the number of
possible exceptions.

Tested:
Ran ipmitool power on/off and confirmed that the "Host system DC
power is on/off" message prints correctly without any spurious
error messages.

Change-Id: I7bbe04675db6c8f2a0212529b8ebac9a9879a6b3
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
diff --git a/power-control-x86/src/power_control.cpp b/power-control-x86/src/power_control.cpp
index 84d1500..b8bb313 100644
--- a/power-control-x86/src/power_control.cpp
+++ b/power-control-x86/src/power_control.cpp
@@ -1307,27 +1307,40 @@
         *conn,
         "type='signal',member='PropertiesChanged', "
         "interface='org.freedesktop.DBus.Properties', "
-        "arg0namespace='xyz.openbmc_project.State.Host'",
+        "arg0='xyz.openbmc_project.State.Host'",
         [](sdbusplus::message::message& message) {
             std::string intfName;
             std::map<std::string, std::variant<std::string>> properties;
 
-            message.read(intfName, properties);
-
-            std::variant<std::string> currentHostState;
-
             try
             {
-                currentHostState = properties.at("CurrentHostState");
+                message.read(intfName, properties);
             }
-            catch (const std::out_of_range& e)
+            catch (std::exception& e)
             {
-                std::cerr << "Error in finding CurrentHostState property\n";
-
+                std::cerr << "Unable to read host state\n";
+                return;
+            }
+            if (properties.empty())
+            {
+                std::cerr << "ERROR: Empty PropertiesChanged signal received\n";
                 return;
             }
 
-            if (std::get<std::string>(currentHostState) ==
+            // We only want to check for CurrentHostState
+            if (properties.begin()->first != "CurrentHostState")
+            {
+                return;
+            }
+            std::string* currentHostState =
+                std::get_if<std::string>(&(properties.begin()->second));
+            if (currentHostState == nullptr)
+            {
+                std::cerr << properties.begin()->first << " property invalid\n";
+                return;
+            }
+
+            if (*currentHostState ==
                 "xyz.openbmc_project.State.Host.HostState.Running")
             {
                 pohCounterTimerStart();