2020#define ConfigManager_h
2121
2222#include " IotWebConf2.h"
23- #if IOTWEBCONF_DEBUG_DISABLED == 0 && !PLATFORMIO
23+ #if IOTWEBCONF_DEBUG_DISABLED == 0 && !PLATFORMIO
2424#error "Using Arduino IDE is not recommended, please follow this guide https://github.com/G4lile0/tinyGS/wiki/Arduino-IDE or edit /IotWebCong2/src/IotWebConf2Settings.h and add this line at the beggining of the file #define IOTWEBCONF_DEBUG_DISABLED 1"
2525#endif
2626#include " logos.h"
2727#include < Wire.h>
2828#include " html.h"
2929
3030#ifdef ESP8266
31- #include " ESP8266HTTPUpdateServer.h"
31+ #include " ESP8266HTTPUpdateServer.h"
3232#elif defined(ESP32)
33- #include " IotWebConf2ESP32HTTPUpdateServer.h"
33+ #include " IotWebConf2ESP32HTTPUpdateServer.h"
3434#endif
3535
3636constexpr auto STATION_NAME_LENGTH = 21 ;
@@ -46,7 +46,6 @@ constexpr auto MODEM_LEN = 256;
4646constexpr auto ADVANCED_LEN = 256 ;
4747constexpr auto CB_SELECTED_STR = " selected" ;
4848
49-
5049constexpr auto ROOT_URL = " /" ;
5150constexpr auto CONFIG_URL = " /config" ;
5251constexpr auto DASHBOARD_URL = " /dashboard" ;
@@ -56,18 +55,18 @@ constexpr auto REFRESH_CONSOLE_URL = "/cs";
5655
5756const char TITLE_TEXT[] PROGMEM = " TinyGS Configuration" ;
5857
59-
6058constexpr auto thingName = " My TinyGS" ;
6159constexpr auto initialApPassword = " " ;
6260constexpr auto configVersion = " 0.05" ; // max 4 chars
6361
6462#define MQTT_DEFAULT_SERVER " mqtt.tinygs.com"
65- #define MQTT_DEFAULT_PORT " 8883"
63+ #define MQTT_DEFAULT_PORT " 8883"
6664#define MODEM_DEFAULT " {\" mode\" :\" LoRa\" ,\" freq\" :436.703,\" bw\" :250.0,\" sf\" :10,\" cr\" :5,\" sw\" :18,\" pwr\" :5,\" cl\" :120,\" pl\" :8,\" gain\" :0,\" crc\" :true,\" fldro\" :1,\" sat\" :\" Norbi\" ,\" NORAD\" :46494}"
6765
6866constexpr auto AP_TIMEOUT_MS = " 300000" ;
6967
70- enum boardNum {
68+ enum boardNum
69+ {
7170 HELTEC_V1_LF = 0 ,
7271 HELTEC_V1_HF,
7372 HELTEC_V2_LF,
@@ -89,37 +88,38 @@ enum boardNum {
8988 NUM_BOARDS // this line always has to be the last one
9089};
9190
92- typedef struct {
93- uint8_t OLED__address;
94- uint8_t OLED__SDA;
95- uint8_t OLED__SCL;
96- uint8_t OLED__RST;
97- uint8_t PROG__BUTTON;
98- uint8_t BOARD_LED;
99- uint8_t L_SX127X; // 0 SX1262 1 SX1278
100- uint8_t L_NSS; // CS
101- uint8_t L_DI00;
102- uint8_t L_DI01;
103- uint8_t L_BUSSY;
104- uint8_t L_RST;
105- uint8_t L_MISO;
106- uint8_t L_MOSI;
107- uint8_t L_SCK;
108- float L_TCXO_V;
109- String BOARD;
91+ typedef struct
92+ {
93+ uint8_t OLED__address;
94+ uint8_t OLED__SDA;
95+ uint8_t OLED__SCL;
96+ uint8_t OLED__RST;
97+ uint8_t PROG__BUTTON;
98+ uint8_t BOARD_LED;
99+ uint8_t L_SX127X; // 0 SX1262 1 SX1278
100+ uint8_t L_NSS; // CS
101+ uint8_t L_DI00;
102+ uint8_t L_DI01;
103+ uint8_t L_BUSSY;
104+ uint8_t L_RST;
105+ uint8_t L_MISO;
106+ uint8_t L_MOSI;
107+ uint8_t L_SCK;
108+ float L_TCXO_V;
109+ String BOARD;
110110} board_type;
111111
112- typedef struct {
112+ typedef struct
113+ {
113114 bool flipOled = true ;
114115 bool dnOled = true ;
115116 bool lowPower = false ;
116117} AdvancedConfig;
117118
118-
119119class ConfigManager : public IotWebConf2
120120{
121121public:
122- static ConfigManager& getInstance ()
122+ static ConfigManager & getInstance ()
123123 {
124124 static ConfigManager instance;
125125 return instance;
@@ -130,75 +130,126 @@ class ConfigManager : public IotWebConf2
130130 boolean init ();
131131 void printConfig ();
132132
133- uint16_t getMqttPort () { return (uint16_t ) atoi (mqttPort); }
134- const char * getMqttServer () { return mqttServer; }
135- const char * getMqttUser () { return mqttUser; }
136- const char * getMqttPass () { return mqttPass; }
133+ uint16_t getMqttPort () { return (uint16_t )atoi (mqttPort); }
134+ const char * getMqttServer () { return mqttServer; }
135+ const char * getMqttUser () { return mqttUser; }
136+ const char * getMqttPass () { return mqttPass; }
137137 float getLatitude () { return atof (latitude); }
138138 float getLongitude () { return atof (longitude); }
139- const char * getTZ () { return tz + 3 ; } // +3 removes the first 3 digits used for time zone deduplication
139+ const char * getTZ () { return tz + 3 ; } // +3 removes the first 3 digits used for time zone deduplication
140140 uint8_t getBoard () { return atoi (board); }
141141 uint8_t getOledBright () { return atoi (oledBright); }
142142 bool getAllowTx () { return !strcmp (allowTx, CB_SELECTED_STR); }
143143 bool getRemoteTune () { return !strcmp (remoteTune, CB_SELECTED_STR); }
144144 bool getTelemetry3rd () { return !strcmp (telemetry3rd, CB_SELECTED_STR); }
145145 bool getTestMode () { return !strcmp (testMode, CB_SELECTED_STR); }
146146 bool getAutoUpdate () { return !strcmp (autoUpdate, CB_SELECTED_STR); }
147- void setAllowTx (bool status) { if (status) strcpy (allowTx, CB_SELECTED_STR); else allowTx[0 ] = ' \0 ' ; this ->saveConfig (); }
148- void setRemoteTune (bool status) { if (status) strcpy (remoteTune, CB_SELECTED_STR); else remoteTune[0 ] = ' \0 ' ; this ->saveConfig (); }
149- void setTelemetry3rd (bool status) { if (status) strcpy (telemetry3rd, CB_SELECTED_STR); else telemetry3rd[0 ] = ' \0 ' ; this ->saveConfig (); }
150- void setTestMode (bool status) { if (status) strcpy (testMode, CB_SELECTED_STR); else testMode[0 ] = ' \0 ' ; this ->saveConfig (); }
151- void setAutoUpdate (bool status) { if (status) strcpy (autoUpdate, CB_SELECTED_STR); else autoUpdate[0 ] = ' \0 ' ; this ->saveConfig (); }
152- const char * getModemStartup () { return modemStartup; }
153- void setModemStartup (const char * modemStr) { strcpy (modemStartup, modemStr); this ->saveConfig (); }
154- const char * getAvancedConfig () { return advancedConfig; }
155- void setAvancedConfig (const char * adv_prmStr) { strcpy (advancedConfig, adv_prmStr); this ->saveConfig (); }
156- const char * getBoardTemplate () { return boardTemplate; }
157- void setBoardTemplate (const char * boardTemplateStr) { strcpy (boardTemplate, boardTemplateStr); this ->saveConfig (); }
147+ void setAllowTx (bool status)
148+ {
149+ if (status)
150+ strcpy (allowTx, CB_SELECTED_STR);
151+ else
152+ allowTx[0 ] = ' \0 ' ;
153+ this ->saveConfig ();
154+ }
155+ void setRemoteTune (bool status)
156+ {
157+ if (status)
158+ strcpy (remoteTune, CB_SELECTED_STR);
159+ else
160+ remoteTune[0 ] = ' \0 ' ;
161+ this ->saveConfig ();
162+ }
163+ void setTelemetry3rd (bool status)
164+ {
165+ if (status)
166+ strcpy (telemetry3rd, CB_SELECTED_STR);
167+ else
168+ telemetry3rd[0 ] = ' \0 ' ;
169+ this ->saveConfig ();
170+ }
171+ void setTestMode (bool status)
172+ {
173+ if (status)
174+ strcpy (testMode, CB_SELECTED_STR);
175+ else
176+ testMode[0 ] = ' \0 ' ;
177+ this ->saveConfig ();
178+ }
179+ void setAutoUpdate (bool status)
180+ {
181+ if (status)
182+ strcpy (autoUpdate, CB_SELECTED_STR);
183+ else
184+ autoUpdate[0 ] = ' \0 ' ;
185+ this ->saveConfig ();
186+ }
187+ const char *getModemStartup () { return modemStartup; }
188+ void setModemStartup (const char *modemStr)
189+ {
190+ strcpy (modemStartup, modemStr);
191+ this ->saveConfig ();
192+ parseModemStartup ();
193+ }
194+ const char *getAvancedConfig () { return advancedConfig; }
195+ void setAvancedConfig (const char *adv_prmStr)
196+ {
197+ strcpy (advancedConfig, adv_prmStr);
198+ this ->saveConfig ();
199+ }
200+ const char *getBoardTemplate () { return boardTemplate; }
201+ void setBoardTemplate (const char *boardTemplateStr)
202+ {
203+ strcpy (boardTemplate, boardTemplateStr);
204+ this ->saveConfig ();
205+ }
158206
159- const char * getWiFiSSID () { return getWifiSsidParameter ()->valueBuffer ; }
207+ const char * getWiFiSSID () { return getWifiSsidParameter ()->valueBuffer ; }
160208 bool isConnected () { return getState () == IOTWEBCONF_STATE_ONLINE; };
161209 bool isApMode () { return (getState () != IOTWEBCONF_STATE_CONNECTING && getState () != IOTWEBCONF_STATE_ONLINE); }
162- board_type getBoardConfig (){ return boards[getBoard ()]; }
163- bool getFlipOled (){ return advancedConf.flipOled ; }
164- bool getDayNightOled (){ return advancedConf.dnOled ; }
165- bool getLowPower (){ return advancedConf.lowPower ; }
166-
210+ board_type getBoardConfig () { return boards[getBoard ()]; }
211+ bool getFlipOled () { return advancedConf.flipOled ; }
212+ bool getDayNightOled () { return advancedConf.dnOled ; }
213+ bool getLowPower () { return advancedConf.lowPower ; }
214+ void saveConfig ()
215+ {
216+ remoteSave = true ;
217+ IotWebConf2::saveConfig ();
218+ };
167219
168220private:
169-
170221 class GSConfigHtmlFormatProvider : public iotwebconf2 ::HtmlFormatProvider
171222 {
172223 public:
173- GSConfigHtmlFormatProvider (ConfigManager& x) : configManager(x) { }
224+ GSConfigHtmlFormatProvider (ConfigManager &x) : configManager(x) {}
225+
174226 protected:
175227 String getScriptInner () override
176228 {
177- return
178- iotwebconf2::HtmlFormatProvider::getScriptInner ();
179- // String(FPSTR(CUSTOMHTML_SCRIPT_INNER));
229+ return iotwebconf2::HtmlFormatProvider::getScriptInner ();
230+ // String(FPSTR(CUSTOMHTML_SCRIPT_INNER));
180231 }
181232 String getBodyInner () override
182233 {
183- return
184- String (FPSTR (LOGO)) +
185- iotwebconf2::HtmlFormatProvider::getBodyInner ();
234+ return String (FPSTR (LOGO)) +
235+ iotwebconf2::HtmlFormatProvider::getBodyInner ();
186236 }
187237
188- ConfigManager& configManager;
238+ ConfigManager & configManager;
189239 };
190240
191241 ConfigManager ();
192242 void handleRoot ();
193243 void handleDashboard ();
194244 void handleRefreshConsole ();
195245 void handleRestart ();
196- bool formValidator (iotwebconf2::WebRequestWrapper* webRequestWrapper);
246+ bool formValidator (iotwebconf2::WebRequestWrapper * webRequestWrapper);
197247 void boardDetection ();
198248 void configSavedCallback ();
199249 void parseAdvancedConf ();
200-
201- std::function<boolean(iotwebconf2::WebRequestWrapper*)> formValidatorStd;
250+ void parseModemStartup ();
251+
252+ std::function<boolean(iotwebconf2::WebRequestWrapper *)> formValidatorStd;
202253 DNSServer dnsServer;
203254 WebServer server;
204255#ifdef ESP8266
@@ -207,9 +258,10 @@ class ConfigManager : public IotWebConf2
207258 HTTPUpdateServer httpUpdater;
208259#endif
209260 GSConfigHtmlFormatProvider gsConfigHtmlFormatProvider;
210- board_type boards[NUM_BOARDS];
261+ board_type boards[NUM_BOARDS];
211262 AdvancedConfig advancedConf;
212263 char savedThingName[IOTWEBCONF_WORD_LEN] = " " ;
264+ bool remoteSave = false ;
213265
214266 char latitude[COORDINATE_LENGTH] = " " ;
215267 char longitude[COORDINATE_LENGTH] = " " ;
@@ -231,24 +283,24 @@ class ConfigManager : public IotWebConf2
231283
232284 iotwebconf2::NumberParameter latitudeParam = iotwebconf2::NumberParameter(" Latitude (3 decimals, will be public)" , " lat" , latitude, COORDINATE_LENGTH, NULL , " 0.000" , " required min='-180' max='180' step='0.001'" );
233285 iotwebconf2::NumberParameter longitudeParam = iotwebconf2::NumberParameter(" Longitude (3 decimals, will be public)" , " lng" , longitude, COORDINATE_LENGTH, NULL , " -0.000" , " required min='-180' max='180' step='0.001'" );
234- iotwebconf2::SelectParameter tzParam = iotwebconf2::SelectParameter(" Time Zone" , " tz" , tz, TZ_LENGTH, (char *)TZ_VALUES, (char *)TZ_NAMES, sizeof (TZ_VALUES) / TZ_LENGTH, TZ_NAME_LENGTH);
286+ iotwebconf2::SelectParameter tzParam = iotwebconf2::SelectParameter(" Time Zone" , " tz" , tz, TZ_LENGTH, (char *)TZ_VALUES, (char *)TZ_NAMES, sizeof (TZ_VALUES) / TZ_LENGTH, TZ_NAME_LENGTH);
235287
236- iotwebconf2::ParameterGroup groupMqtt = iotwebconf2::ParameterGroup(" MQTT credentials" , " MQTT credentials (get them <a href='https://t.me/joinchat/DmYSElZahiJGwHX6jCzB3Q'>here</a>)" );
288+ iotwebconf2::ParameterGroup groupMqtt = iotwebconf2::ParameterGroup(" MQTT credentials" , " MQTT credentials (get them <a href='https://t.me/joinchat/DmYSElZahiJGwHX6jCzB3Q'>here</a>)" );
237289 iotwebconf2::TextParameter mqttServerParam = iotwebconf2::TextParameter(" Server address" , " mqtt_server" , mqttServer, MQTT_SERVER_LENGTH, MQTT_DEFAULT_SERVER, MQTT_DEFAULT_SERVER, " required type=\" text\" maxlength=30" );
238290 iotwebconf2::NumberParameter mqttPortParam = iotwebconf2::NumberParameter(" Server Port" , " mqtt_port" , mqttPort, MQTT_PORT_LENGTH, MQTT_DEFAULT_PORT, MQTT_DEFAULT_PORT, " required min=\" 0\" max=\" 65536\" step=\" 1\" " );
239291 iotwebconf2::TextParameter mqttUserParam = iotwebconf2::TextParameter(" MQTT Username" , " mqtt_user" , mqttUser, MQTT_USER_LENGTH, NULL , NULL , " required type=\" text\" maxlength=30" );
240292 iotwebconf2::TextParameter mqttPassParam = iotwebconf2::TextParameter(" MQTT Password" , " mqtt_pass" , mqttPass, MQTT_PASS_LENGTH, NULL , NULL , " required type=\" text\" maxlength=30" );
241293
242- iotwebconf2::ParameterGroup groupBoardConfig = iotwebconf2::ParameterGroup(" Board config" , " Board config" );
243- iotwebconf2::SelectParameter boardParam = iotwebconf2::SelectParameter(" Board type" , " board" , board, BOARD_LENGTH, (char *)BOARD_VALUES, (char *)BOARD_NAMES, sizeof (BOARD_VALUES) / BOARD_LENGTH, BOARD_NAME_LENGTH);
294+ iotwebconf2::ParameterGroup groupBoardConfig = iotwebconf2::ParameterGroup(" Board config" , " Board config" );
295+ iotwebconf2::SelectParameter boardParam = iotwebconf2::SelectParameter(" Board type" , " board" , board, BOARD_LENGTH, (char *)BOARD_VALUES, (char *)BOARD_NAMES, sizeof (BOARD_VALUES) / BOARD_LENGTH, BOARD_NAME_LENGTH);
244296 iotwebconf2::NumberParameter oledBrightParam = iotwebconf2::NumberParameter(" OLED Bright" , " oled_bright" , oledBright, NUMBER_LEN, " 100" , " 0..100" , " min='0' max='100' step='1'" );
245- iotwebconf2::CheckboxParameter AllowTxParam = iotwebconf2::CheckboxParameter(" Enable TX (HAM licence/ no preamp)" , " tx" , allowTx, CHECKBOX_LENGTH, true );
246- iotwebconf2::CheckboxParameter remoteTuneParam = iotwebconf2::CheckboxParameter(" Allow Automatic Tuning" ," remote_tune" ,remoteTune, CHECKBOX_LENGTH, true );
247- iotwebconf2::CheckboxParameter telemetry3rdParam = iotwebconf2::CheckboxParameter(" Allow sending telemetry to third party" ," telemetry3rd" ,telemetry3rd, CHECKBOX_LENGTH, true );
248- iotwebconf2::CheckboxParameter testParam = iotwebconf2::CheckboxParameter(" Test mode" ," test" ,testMode, CHECKBOX_LENGTH, false );
249- iotwebconf2::CheckboxParameter autoUpdateParam = iotwebconf2::CheckboxParameter(" Automatic Firmware Update" ," auto_update" ,autoUpdate, CHECKBOX_LENGTH, true );
297+ iotwebconf2::CheckboxParameter AllowTxParam = iotwebconf2::CheckboxParameter(" Enable TX (HAM licence/ no preamp)" , " tx" , allowTx, CHECKBOX_LENGTH, true );
298+ iotwebconf2::CheckboxParameter remoteTuneParam = iotwebconf2::CheckboxParameter(" Allow Automatic Tuning" , " remote_tune" , remoteTune, CHECKBOX_LENGTH, true );
299+ iotwebconf2::CheckboxParameter telemetry3rdParam = iotwebconf2::CheckboxParameter(" Allow sending telemetry to third party" , " telemetry3rd" , telemetry3rd, CHECKBOX_LENGTH, true );
300+ iotwebconf2::CheckboxParameter testParam = iotwebconf2::CheckboxParameter(" Test mode" , " test" , testMode, CHECKBOX_LENGTH, false );
301+ iotwebconf2::CheckboxParameter autoUpdateParam = iotwebconf2::CheckboxParameter(" Automatic Firmware Update" , " auto_update" , autoUpdate, CHECKBOX_LENGTH, true );
250302
251- iotwebconf2::ParameterGroup groupAdvanced = iotwebconf2::ParameterGroup(" Advanced config" , " Advanced Config (do not modify unless you know what you are doing)" );
303+ iotwebconf2::ParameterGroup groupAdvanced = iotwebconf2::ParameterGroup(" Advanced config" , " Advanced Config (do not modify unless you know what you are doing)" );
252304 iotwebconf2::TextParameter boardTemplateParam = iotwebconf2::TextParameter(" Board Template (requires manual restart)" , " board_template" , boardTemplate, TEMPLATE_LEN, NULL , NULL , " type=\" text\" maxlength=255" );
253305 iotwebconf2::TextParameter modemParam = iotwebconf2::TextParameter(" Modem startup" , " modem_startup" , modemStartup, MODEM_LEN, MODEM_DEFAULT, MODEM_DEFAULT, " type=\" text\" maxlength=255" );
254306 iotwebconf2::TextParameter advancedConfigParam = iotwebconf2::TextParameter(" Advanced parameters" , " advanced_config" , advancedConfig, ADVANCED_LEN, NULL , NULL , " type=\" text\" maxlength=255" );
0 commit comments