From 7e58db489dea27a064f617603935af4d5e51154f Mon Sep 17 00:00:00 2001
From: Marcel Peterkau <marcel@peterkau.de>
Date: Mon, 17 Apr 2023 22:55:16 +0200
Subject: [PATCH] made active faction recovery configurable

---
 Software/data_src/index.htm | 18 +++++++++++++++++-
 Software/include/config.h   |  1 +
 Software/src/main.cpp       | 10 ++++++++--
 Software/src/webui.cpp      | 15 +++++++++++++++
 4 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/Software/data_src/index.htm b/Software/data_src/index.htm
index fb168f8..0ee7acd 100644
--- a/Software/data_src/index.htm
+++ b/Software/data_src/index.htm
@@ -204,8 +204,8 @@
         <!-- Div Group Battery Type -->
         <hr />
         <p>
-        <h4>Akku-Variante</h4>
         <form action="post.htm" method="POST" class="form-horizontal">
+          <h4>Akku-Variante</h4>
           <div class="form-group row">
             <label for="battery_select" class="control-label col-4">Akku</label>
             <div class="col-8">
@@ -214,6 +214,18 @@
               </select>
             </div>
           </div>
+          <h4>Timer-Einstellungen</h4>
+          <div class="form-group row">
+            <label for="factionreboot_cont" class="control-label col-4">active Faction Recovery</label>
+            <div class="col-8">
+              <div class="form-check">
+                <input class="form-check-input" type="checkbox" name="factionreboot_cont" id="factionreboot_cont" %FACTIONREBOOT_CHECKED%>
+                <label class="form-check-label" for="factionreboot_cont">
+                  aktive Faktion beim booten wiederherstellen ?
+                </label>
+              </div>
+            </div>
+          </div>
           <div class="form-group row">
             <div class="col text-center">
               <button name="settingssave" type="submit" class="btn btn-outline-primary">&Uuml;bernehmen</button>
@@ -267,6 +279,10 @@
               <td>Battery_type</td>
               <td>%BATTERY_TYPE%</td>
             </tr>
+            <tr>
+              <td>Faction_recovery</td>
+              <td>%FACTION_RECOVERY%</td>
+            </tr>
             <tr>
               <td>EEPROM Version</td>
               <td>%EEPROM_VERSION%</td>
diff --git a/Software/include/config.h b/Software/include/config.h
index 8de318d..eeb282a 100644
--- a/Software/include/config.h
+++ b/Software/include/config.h
@@ -49,6 +49,7 @@ typedef struct
 {
   uint8_t EEPROM_Version = 1;
   batteryType_t batteryType = BATTERY_UNDEFINED;
+  bool active_faction_on_reboot = false;
   uint32_t checksum = 0;
 } configData_t;
 
diff --git a/Software/src/main.cpp b/Software/src/main.cpp
index e0c4d0c..6e2c045 100644
--- a/Software/src/main.cpp
+++ b/Software/src/main.cpp
@@ -207,6 +207,9 @@ void setup()
 	tmrFactionTicker.start();
 	tmrInputGetter.start();
 
+	if (ConfigData.active_faction_on_reboot == false)
+		PersistenceData.activeFaction = NONE;
+
 	Serial.print("\nSetup Done\n");
 }
 
@@ -246,7 +249,7 @@ void loop()
 		/* code */
 		break;
 	case sysStat_Shutdown:
-		OverrideDisplay(SHUTDOWN_DELAY_MS , "  re", "boot", "    ");
+		OverrideDisplay(SHUTDOWN_DELAY_MS, "  re", "boot", "    ");
 		SystemShutdown();
 		break;
 	case sysStat_Startup:
@@ -371,6 +374,7 @@ void tmrCallback_InputGetter()
 		if (PersistenceData.activeFaction != FACTION_1)
 		{
 			Serial.println("Faction 1 captured !");
+			globals.requestEEAction = EE_PDS_SAVE;
 		}
 		PersistenceData.activeFaction = FACTION_1;
 	}
@@ -380,6 +384,7 @@ void tmrCallback_InputGetter()
 		if (PersistenceData.activeFaction != FACTION_2)
 		{
 			Serial.println("Faction 2 captured !");
+			globals.requestEEAction = EE_PDS_SAVE;
 		}
 		PersistenceData.activeFaction = FACTION_2;
 	}
@@ -389,6 +394,7 @@ void tmrCallback_InputGetter()
 		if (PersistenceData.activeFaction != FACTION_3)
 		{
 			Serial.println("Faction 3 captured !");
+			globals.requestEEAction = EE_PDS_SAVE;
 		}
 		PersistenceData.activeFaction = FACTION_3;
 	}
@@ -583,7 +589,7 @@ void ProcessKeyCombos(bool *btnState)
 		if (keyCount_Fac2 == 2 && keyCount_Fac3 == 0)
 		{
 			Serial.println("KeyCombo: WiFi AP ON");
-			OverrideDisplay(5000, "NET ", "    ","    ");
+			OverrideDisplay(5000, "NET ", "    ", "    ");
 			toggleWiFiAP(false);
 		}
 		else if (keyCount_Fac2 == 4 && keyCount_Fac3 == 0)
diff --git a/Software/src/webui.cpp b/Software/src/webui.cpp
index 7c55524..df7cce2 100644
--- a/Software/src/webui.cpp
+++ b/Software/src/webui.cpp
@@ -200,6 +200,12 @@ String processor(const String &var)
     return temp;
   }
 
+  if (var == "FACTIONREBOOT_CHECKED")
+    return String(ConfigData.active_faction_on_reboot == true ? "checked" : "");
+
+  if (var == "FACTION_RECOVERY")
+    return String(ConfigData.active_faction_on_reboot);
+
   return String();
 }
 
@@ -256,6 +262,15 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
       ConfigData.batteryType = temp;
     }
 
+    if (request->hasParam("factionreboot_cont", true))
+    {
+      AsyncWebParameter *param = request->getParam("factionreboot_cont", true);
+      if (param->value() == "on")
+        ConfigData.active_faction_on_reboot = true;
+      else
+        ConfigData.active_faction_on_reboot = false;
+    }
+
     if (p->name() == "settingssave")
       globals.requestEEAction = EE_CFG_SAVE;
     // end: POST Form Settings