control: Restore `Current` property for each zone

Each JSON based zone object restores the persisted state of their
`Current` property when constructed. If no `Current` property value is
found for the zone, the default of the Control.ThermalMode interface is
used.

After the restore is done on each zone object, the `emit_object_added` for
that zone object can be done.

Change-Id: Id59eb511d76fb45a318660ffd9c0b6a776ba7770
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/control/json/zone.cpp b/control/json/zone.cpp
index be827cf..90aeb66 100644
--- a/control/json/zone.cpp
+++ b/control/json/zone.cpp
@@ -74,6 +74,12 @@
         setInterfaces(jsonObj);
     }
 
+    // Restore thermal control current mode state
+    restoreCurrentMode();
+
+    // Emit object added for this zone dbus object
+    this->emit_object_added();
+
     // Start timer for fan target decreases
     _decTimer.restart(_decInterval);
 }
@@ -373,6 +379,33 @@
     oArch(ThermalObject::current());
 }
 
+void Zone::restoreCurrentMode()
+{
+    auto current = ThermalObject::current();
+    fs::path path{CONTROL_PERSIST_ROOT_PATH};
+    path /= getName();
+    path /= "CurrentMode";
+    fs::create_directories(path.parent_path());
+
+    try
+    {
+        if (fs::exists(path))
+        {
+            std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary);
+            cereal::JSONInputArchive iArch(ifs);
+            iArch(current);
+        }
+    }
+    catch (std::exception& e)
+    {
+        log<level::ERR>(e.what());
+        fs::remove(path);
+        current = ThermalObject::current();
+    }
+
+    this->current(current);
+}
+
 /**
  * Properties of interfaces supported by the zone configuration that return
  * a handler function that sets the zone's property value(s) and persist state.
diff --git a/control/json/zone.hpp b/control/json/zone.hpp
index e39a735..e46522a 100644
--- a/control/json/zone.hpp
+++ b/control/json/zone.hpp
@@ -465,6 +465,12 @@
     void saveCurrentMode();
 
     /**
+     * @brief Restore persisted thermal control current mode property
+     * value, setting the mode to "Default" otherwise
+     */
+    void restoreCurrentMode();
+
+    /**
      * @brief Get the request target base if defined, otherwise the the current
      * target is returned
      *