From d9d0ea3ff78787cc41dfc4103f57c681385533b6 Mon Sep 17 00:00:00 2001 From: Michael Geddes Date: Wed, 23 Nov 2022 05:50:28 +0800 Subject: [PATCH] Config Wizard - Construct the status checklist from a list. --- .../ovms_webserver/src/web_cfg_init.cpp | 133 +++++++++++------- 1 file changed, 83 insertions(+), 50 deletions(-) diff --git a/vehicle/OVMS.V3/components/ovms_webserver/src/web_cfg_init.cpp b/vehicle/OVMS.V3/components/ovms_webserver/src/web_cfg_init.cpp index a45f3e36b..dc0672df3 100644 --- a/vehicle/OVMS.V3/components/ovms_webserver/src/web_cfg_init.cpp +++ b/vehicle/OVMS.V3/components/ovms_webserver/src/web_cfg_init.cpp @@ -57,6 +57,79 @@ static const char *TAG = "webserver"; #ifdef WEBSRV_HAVE_SETUPWIZARD +struct wizard_info_t { + const char *title, *desc; +}; +static const int wizard_count = 5; +static const wizard_info_t wizards[wizard_count] = { + {"Secure Module", "Secure module access" }, + {"Connect to Internet", "Connect module to internet (via Wifi)"}, +#ifdef CONFIG_OVMS_COMP_OTA + {"Update Firmware", "Update to latest firmware version"}, +#else + {NULL,NULL}, +#endif + {"Vehicle & Server", "Configure vehicle type and server"}, +#ifdef CONFIG_OVMS_COMP_CELLULAR + {"Cellular Modem", "Configure cellular modem (if equipped)"} +#else + {NULL,NULL} +#endif +}; + +static int WizardNumber( int wizardNo) +{ + int count = 0; + for (int i = 0; i < wizardNo; ++i) { + if (wizards[i].desc) + ++count; + } + return count; +} +// Returns the active wizard pages (the number of the last page) +static inline int WizardCount() +{ + return WizardNumber(wizard_count); +} + +/** + * Returns the setup wizard header. + * Indexed from 1. + * Has the current one bolded and previous ones checked. + */ +static std::string WizardStatus(int wizardNo) +{ + --wizardNo; + std::ostringstream os; + os << "

Quick setup wizard

" + "
    "; + for (int i = 0; i < wizard_count; ++i) { + if (!wizards[i].desc) + continue; + os << "
  1. "; + bool isStrong = (i == wizardNo); + if (isStrong) + os << ""; + os << wizards[i].desc; + if (i < wizardNo) + os << ""; + else if (isStrong) + os << ""; + os << "
  2. "; + } + os << "
"; + return os.str(); +} + +static std::string WizardFormHeader(int wizardNo) +{ + std::ostringstream os; + const char *title = wizards[wizardNo-1].title; + os << "Step " << WizardNumber(wizardNo) << '/' << WizardCount() << ": " + << (title ? title : "(not included)"); + return os.str(); +} + /** * HandleCfgInit: /cfg/init: setup wizard dispatcher */ @@ -415,15 +488,7 @@ std::string OvmsWebServer::CfgInit1(PageEntry_t& p, PageContext_t& c, std::strin } // Wizard status: - c.alert("info", - "

Quick setup wizard

" - "
    " - "
  1. Secure module access
  2. " - "
  3. Connect module to internet (via Wifi)
  4. " - "
  5. Update to latest firmware version
  6. " - "
  7. Configure vehicle type and server
  8. " - "
  9. Configure cellular modem (if equipped)
  10. " - "
"); + c.alert("info", WizardStatus(1).c_str()); // create some random passwords: std::ostringstream pwsugg; @@ -434,7 +499,7 @@ std::string OvmsWebServer::CfgInit1(PageEntry_t& p, PageContext_t& c, std::strin pwsugg << "

"; // generate form: - c.panel_start("primary", "Step 1/5: Secure Module"); + c.panel_start("primary", WizardFormHeader(1).c_str()); c.print( "

Your module may currently be accessed by anyone in Wifi range using the default" @@ -564,18 +629,10 @@ std::string OvmsWebServer::CfgInit2(PageEntry_t& p, PageContext_t& c, std::strin } // Wizard status: - c.alert("info", - "

Quick setup wizard

" - "
    " - "
  1. Secure module access
  2. " - "
  3. Connect module to internet (via Wifi)
  4. " - "
  5. Update to latest firmware version
  6. " - "
  7. Configure vehicle type and server
  8. " - "
  9. Configure cellular modem (if equipped)
  10. " - "
"); + c.alert("info", WizardStatus(2).c_str()); // generate form: - c.panel_start("primary", "Step 2/5: Connect to Internet"); + c.panel_start("primary", WizardFormHeader(2).c_str()); c.print( "

Your module now needs an internet connection to download the latest firmware" @@ -829,18 +886,10 @@ std::string OvmsWebServer::CfgInit3(PageEntry_t& p, PageContext_t& c, std::strin } // Wizard status: - c.alert("info", - "

Quick setup wizard

" - "
    " - "
  1. Secure module access
  2. " - "
  3. Connect module to internet (via Wifi)
  4. " - "
  5. Update to latest firmware version
  6. " - "
  7. Configure vehicle type and server
  8. " - "
  9. Configure cellular modem (if equipped)
  10. " - "
"); + c.alert("info", WizardStatus(3).c_str()); // form: - c.panel_start("primary", "Step 3/5: Update Firmware"); + c.panel_start("primary", WizardFormHeader(3).c_str()); c.form_start(p.uri); c.input_radio_start("Update server", "server"); c.input_radio_option("server", "Asia-Pacific (openvehicles.com)", @@ -1027,18 +1076,10 @@ std::string OvmsWebServer::CfgInit4(PageEntry_t& p, PageContext_t& c, std::strin } // Wizard status: - c.alert("info", - "

Quick setup wizard

" - "
    " - "
  1. Secure module access
  2. " - "
  3. Connect module to internet (via Wifi)
  4. " - "
  5. Update to latest firmware version
  6. " - "
  7. Configure vehicle type and server
  8. " - "
  9. Configure cellular modem (if equipped)
  10. " - "
"); + c.alert("info", WizardStatus(4).c_str()); // form: - c.panel_start("primary", "Step 4/5: Vehicle & Server"); + c.panel_start("primary", WizardFormHeader(4).c_str()); c.form_start(p.uri); c.input_select_start("Vehicle type", "vehicletype"); @@ -1207,18 +1248,10 @@ std::string OvmsWebServer::CfgInit5(PageEntry_t& p, PageContext_t& c, std::strin } // Wizard status: - c.alert("info", - "

Quick setup wizard

" - "
    " - "
  1. Secure module access
  2. " - "
  3. Connect module to internet (via Wifi)
  4. " - "
  5. Update to latest firmware version
  6. " - "
  7. Configure vehicle type and server
  8. " - "
  9. Configure cellular modem (if equipped)
  10. " - "
"); + c.alert("info", WizardStatus(5).c_str()); // form: - c.panel_start("primary", "Step 5/5: Cellular Modem"); + c.panel_start("primary", WizardFormHeader(5).c_str()); c.form_start(p.uri); std::string iccid = StdMetrics.ms_m_net_mdm_iccid->AsString();