Refactor GUI save settings (prt2)

This commit is contained in:
Theo Arends 2021-04-08 17:57:37 +02:00
parent bacc07b2de
commit 1a2addfc16
3 changed files with 95 additions and 93 deletions

View File

@ -306,6 +306,7 @@
#define D_WCFG_7_WIFIMANAGER_RESET_ONLY "ManagerRst"
#define D_CMND_DEVICENAME "DeviceName"
#define D_CMND_FRIENDLYNAME "FriendlyName"
#define D_CMND_FN "FN"
#define D_CMND_SWITCHMODE "SwitchMode"
#define D_CMND_INTERLOCK "Interlock"
#define D_CMND_TELEPERIOD "TelePeriod"

View File

@ -26,7 +26,7 @@ const char kTasmotaCommands[] PROGMEM = "|" // No prefix
D_CMND_BUTTONDEBOUNCE "|" D_CMND_SWITCHDEBOUNCE "|" D_CMND_SYSLOG "|" D_CMND_LOGHOST "|" D_CMND_LOGPORT "|"
D_CMND_SERIALBUFFER "|" D_CMND_SERIALSEND "|" D_CMND_BAUDRATE "|" D_CMND_SERIALCONFIG "|" D_CMND_SERIALDELIMITER "|"
D_CMND_IPADDRESS "|" D_CMND_NTPSERVER "|" D_CMND_AP "|" D_CMND_SSID "|" D_CMND_PASSWORD "|" D_CMND_HOSTNAME "|" D_CMND_WIFICONFIG "|"
D_CMND_DEVICENAME "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_INTERLOCK "|" D_CMND_TELEPERIOD "|" D_CMND_RESET "|" D_CMND_TIME "|" D_CMND_TIMEZONE "|" D_CMND_TIMESTD "|"
D_CMND_DEVICENAME "|" D_CMND_FN "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_INTERLOCK "|" D_CMND_TELEPERIOD "|" D_CMND_RESET "|" D_CMND_TIME "|" D_CMND_TIMEZONE "|" D_CMND_TIMESTD "|"
D_CMND_TIMEDST "|" D_CMND_ALTITUDE "|" D_CMND_LEDPOWER "|" D_CMND_LEDSTATE "|" D_CMND_LEDMASK "|" D_CMND_LEDPWM_ON "|" D_CMND_LEDPWM_OFF "|" D_CMND_LEDPWM_MODE "|"
D_CMND_WIFIPOWER "|" D_CMND_TEMPOFFSET "|" D_CMND_HUMOFFSET "|" D_CMND_SPEEDUNIT "|" D_CMND_GLOBAL_TEMP "|" D_CMND_GLOBAL_HUM"|" D_CMND_SWITCHTEXT "|"
#ifdef USE_I2C
@ -54,7 +54,7 @@ void (* const TasmotaCommand[])(void) PROGMEM = {
&CmndButtonDebounce, &CmndSwitchDebounce, &CmndSyslog, &CmndLoghost, &CmndLogport,
&CmndSerialBuffer, &CmndSerialSend, &CmndBaudrate, &CmndSerialConfig, &CmndSerialDelimiter,
&CmndIpAddress, &CmndNtpServer, &CmndAp, &CmndSsid, &CmndPassword, &CmndHostname, &CmndWifiConfig,
&CmndDevicename, &CmndFriendlyname, &CmndSwitchMode, &CmndInterlock, &CmndTeleperiod, &CmndReset, &CmndTime, &CmndTimezone, &CmndTimeStd,
&CmndDevicename, &CmndFriendlyname, &CmndFriendlyname, &CmndSwitchMode, &CmndInterlock, &CmndTeleperiod, &CmndReset, &CmndTime, &CmndTimezone, &CmndTimeStd,
&CmndTimeDst, &CmndAltitude, &CmndLedPower, &CmndLedState, &CmndLedMask, &CmndLedPwmOn, &CmndLedPwmOff, &CmndLedPwmMode,
&CmndWifiPower, &CmndTempOffset, &CmndHumOffset, &CmndSpeedUnit, &CmndGlobalTemp, &CmndGlobalHum, &CmndSwitchText,
#ifdef USE_I2C
@ -237,7 +237,7 @@ void CommandHandler(char* topicBuf, char* dataBuf, uint32_t data_len)
type[i] = '\0';
}
AddLog(LOG_LEVEL_DEBUG, PSTR("CMD: " D_GROUP " %d, " D_INDEX " %d, " D_COMMAND " \"%s\", " D_DATA " \"%s\""), grpflg, index, type, dataBuf);
AddLog_P(LOG_LEVEL_DEBUG, PSTR("CMD: " D_GROUP " %d, " D_INDEX " %d, " D_COMMAND " \"%s\", " D_DATA " \"%s\""), grpflg, index, type, dataBuf);
if (type != nullptr) {
Response_P(PSTR("{\"" D_JSON_COMMAND "\":\"" D_JSON_ERROR "\"}"));
@ -1657,14 +1657,23 @@ void CmndSsid(void)
void CmndPassword(void)
{
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 2)) {
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= 4)) {
bool show_asterisk = (XdrvMailbox.index > 2);
if (show_asterisk) {
XdrvMailbox.index -= 2;
}
if ((XdrvMailbox.data_len > 4) || (SC_CLEAR == Shortcut()) || (SC_DEFAULT == Shortcut())) {
SettingsUpdateText(SET_STAPWD1 + XdrvMailbox.index -1,
(SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? (1 == XdrvMailbox.index) ? STA_PASS1 : STA_PASS2 : XdrvMailbox.data);
Settings.sta_active = XdrvMailbox.index -1;
TasmotaGlobal.restart_flag = 2;
ResponseCmndIdxChar(SettingsText(SET_STAPWD1 + XdrvMailbox.index -1));
if (!show_asterisk) {
ResponseCmndIdxChar(SettingsText(SET_STAPWD1 + XdrvMailbox.index -1));
}
} else {
show_asterisk = true;
}
if (show_asterisk) {
Response_P(S_JSON_COMMAND_INDEX_ASTERISK, XdrvMailbox.command, XdrvMailbox.index);
}
}
@ -1708,6 +1717,7 @@ void CmndDevicename(void)
void CmndFriendlyname(void)
{
snprintf_P(XdrvMailbox.command, CMDSZ, PSTR(D_CMND_FRIENDLYNAME)); // Rename result shortcut command FN to FriendlyName
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_FRIENDLYNAMES)) {
if (!XdrvMailbox.usridx && !XdrvMailbox.data_len) {
ResponseCmndAll(SET_FRIENDLYNAME1, MAX_FRIENDLYNAMES);

View File

@ -1403,8 +1403,7 @@ void WSContentSendAdcNiceList(uint32_t option) {
/*-------------------------------------------------------------------------------------------*/
void HandleTemplateConfiguration(void)
{
void HandleTemplateConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
if (Webserver->hasArg(F("save"))) {
@ -1504,12 +1503,11 @@ uint16_t WebGetGpioArg(uint32_t i) {
return gpio;
}
void TemplateSaveSettings(void)
{
char tmp[TOPSZ]; // WebGetArg NAME and GPIO/BASE/FLAG byte value
char svalue[300]; // Template command string
void TemplateSaveSettings(void) {
char tmp[TOPSZ]; // WebGetArg NAME and GPIO/BASE/FLAG byte value
char svalue[300]; // Template command string
WebGetArg(PSTR("s1"), tmp, sizeof(tmp)); // NAME
WebGetArg(PSTR("s1"), tmp, sizeof(tmp)); // NAME
snprintf_P(svalue, sizeof(svalue), PSTR(D_CMND_TEMPLATE " {\"" D_JSON_NAME "\":\"%s\",\"" D_JSON_GPIO "\":["), tmp);
uint32_t j = 0;
@ -1521,13 +1519,13 @@ void TemplateSaveSettings(void)
}
uint32_t flag = 0;
char webindex[5]; // WebGetArg name
char webindex[5]; // WebGetArg name
for (uint32_t i = 0; i < GPIO_FLAG_USED; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("c%d"), i);
uint32_t state = Webserver->hasArg(webindex) << i; // FLAG
uint32_t state = Webserver->hasArg(webindex) << i; // FLAG
flag += state;
}
WebGetArg(PSTR("g99"), tmp, sizeof(tmp)); // BASE
WebGetArg(PSTR("g99"), tmp, sizeof(tmp)); // BASE
uint32_t base = atoi(tmp) +1;
snprintf_P(svalue, sizeof(svalue), PSTR("%s],\"" D_JSON_FLAG "\":%d,\"" D_JSON_BASE "\":%d}"), svalue, flag, base);
@ -1536,8 +1534,7 @@ void TemplateSaveSettings(void)
/*-------------------------------------------------------------------------------------------*/
void HandleModuleConfiguration(void)
{
void HandleModuleConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
if (Webserver->hasArg(F("save"))) {
@ -1603,30 +1600,27 @@ void HandleModuleConfiguration(void)
WSContentStop();
}
void ModuleSaveSettings(void)
{
void ModuleSaveSettings(void) {
char tmp[8]; // WebGetArg numbers only
WebGetArg(PSTR("g99"), tmp, sizeof(tmp));
WebGetArg(PSTR("g99"), tmp, sizeof(tmp)); // Module
uint32_t new_module = (!strlen(tmp)) ? MODULE : atoi(tmp);
Settings.last_module = Settings.module;
Settings.module = new_module;
SetModuleType();
myio template_gp;
TemplateGpios(&template_gp);
String gpios = "";
for (uint32_t i = 0; i < nitems(template_gp.io); i++) {
if (Settings.last_module != new_module) {
Settings.my_gp.io[i] = GPIO_NONE;
} else {
if (ValidGPIO(i, template_gp.io[i])) {
Settings.my_gp.io[i] = WebGetGpioArg(i);
gpios += F(", IO"); gpios += String(i); gpios += F(" "); gpios += String(Settings.my_gp.io[i]);
Settings.my_gp.io[i] = WebGetGpioArg(i); // Gpio
}
}
}
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MODULE "%s " D_CMND_MODULE "%s"), ModuleName().c_str(), gpios.c_str());
char command[32];
snprintf_P(command, sizeof(command), PSTR(D_CMND_BACKLOG "0 " D_CMND_MODULE ";" D_CMND_GPIO));
ExecuteWebCommand(command);
}
/*-------------------------------------------------------------------------------------------*/
@ -1653,8 +1647,7 @@ String HtmlEscape(const String unescaped) {
// Indexed by enum wl_enc_type in file wl_definitions.h starting from -1
const char kEncryptionType[] PROGMEM = "|||" D_WPA_PSK "||" D_WPA2_PSK "|" D_WEP "||" D_NONE "|" D_AUTO;
void HandleWifiConfiguration(void)
{
void HandleWifiConfiguration(void) {
if (!HttpCheckPriviledgedAccess(!WifiIsInManagerMode())) { return; }
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_WIFI));
@ -1806,33 +1799,33 @@ void HandleWifiConfiguration(void)
WSContentStop();
}
void WifiSaveSettings(void)
{
char tmp[TOPSZ]; // Max length is currently 150
WebGetArg(PSTR("h"), tmp, sizeof(tmp));
SettingsUpdateText(SET_HOSTNAME, (!strlen(tmp)) ? WIFI_HOSTNAME : tmp);
if (strchr(SettingsText(SET_HOSTNAME), '%') != nullptr) {
SettingsUpdateText(SET_HOSTNAME, WIFI_HOSTNAME);
}
WebGetArg(PSTR("c"), tmp, sizeof(tmp));
SettingsUpdateText(SET_CORS, (!strlen(tmp)) ? CORS_DOMAIN : tmp);
WebGetArg(PSTR("s1"), tmp, sizeof(tmp));
SettingsUpdateText(SET_STASSID1, (!strlen(tmp)) ? STA_SSID1 : tmp);
WebGetArg(PSTR("s2"), tmp, sizeof(tmp));
SettingsUpdateText(SET_STASSID2, (!strlen(tmp)) ? STA_SSID2 : tmp);
WebGetArg(PSTR("p1"), tmp, sizeof(tmp));
SettingsUpdateText(SET_STAPWD1, (!strlen(tmp)) ? "" : (strlen(tmp) < 5) ? SettingsText(SET_STAPWD1) : tmp);
WebGetArg(PSTR("p2"), tmp, sizeof(tmp));
SettingsUpdateText(SET_STAPWD2, (!strlen(tmp)) ? "" : (strlen(tmp) < 5) ? SettingsText(SET_STAPWD2) : tmp);
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_CMND_HOSTNAME " %s, " D_CMND_SSID "1 %s, " D_CMND_SSID "2 %s, " D_CMND_CORS " %s"),
SettingsText(SET_HOSTNAME), SettingsText(SET_STASSID1), SettingsText(SET_STASSID2), SettingsText(SET_CORS));
void WifiSaveSettings(void) {
char tmp1[CMDSZ];
WebGetArg(PSTR("h"), tmp1, sizeof(tmp1)); // Host name
char tmp2[CMDSZ];
WebGetArg(PSTR("c"), tmp2, sizeof(tmp2)); // Cors domain
char tmp3[CMDSZ];
WebGetArg(PSTR("s1"), tmp3, sizeof(tmp3)); // Ssid1
char tmp4[CMDSZ];
WebGetArg(PSTR("s2"), tmp4, sizeof(tmp4)); // Ssid2
char tmp5[CMDSZ];
WebGetArg(PSTR("p1"), tmp5, sizeof(tmp5)); // Password1
char tmp6[CMDSZ];
WebGetArg(PSTR("p2"), tmp6, sizeof(tmp6)); // Password2
char command[300];
snprintf_P(command, sizeof(command), PSTR(D_CMND_BACKLOG "0 " D_CMND_HOSTNAME " %s;" D_CMND_CORS " %s;" D_CMND_SSID "1 %s;" D_CMND_SSID "2 %s;" D_CMND_PASSWORD "3 %s;" D_CMND_PASSWORD "4 %s"),
(!strlen(tmp1)) ? "1" : tmp1,
(!strlen(tmp2)) ? "1" : tmp2,
(!strlen(tmp3)) ? "1" : tmp3,
(!strlen(tmp4)) ? "1" : tmp4,
(!strlen(tmp5)) ? "\"" : (strlen(tmp5) < 5) ? "" : tmp5,
(!strlen(tmp6)) ? "\"" : (strlen(tmp6) < 5) ? "" : tmp6);
ExecuteWebCommand(command);
}
/*-------------------------------------------------------------------------------------------*/
void HandleLoggingConfiguration(void)
{
void HandleLoggingConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_LOGGING));
@ -1869,22 +1862,21 @@ void HandleLoggingConfiguration(void)
WSContentStop();
}
void LoggingSaveSettings(void)
{
void LoggingSaveSettings(void) {
char tmp1[CMDSZ];
WebGetArg(PSTR("l0"), tmp1, sizeof(tmp1));
WebGetArg(PSTR("l0"), tmp1, sizeof(tmp1)); // Serial log level
char tmp2[CMDSZ];
WebGetArg(PSTR("l1"), tmp2, sizeof(tmp2));
WebGetArg(PSTR("l1"), tmp2, sizeof(tmp2)); // Web log level
char tmp3[CMDSZ];
WebGetArg(PSTR("l2"), tmp3, sizeof(tmp3));
WebGetArg(PSTR("l2"), tmp3, sizeof(tmp3)); // Mqtt log level
char tmp4[CMDSZ];
WebGetArg(PSTR("l3"), tmp4, sizeof(tmp4));
WebGetArg(PSTR("l3"), tmp4, sizeof(tmp4)); // Syslog level
char tmp5[CMDSZ];
WebGetArg(PSTR("lh"), tmp5, sizeof(tmp5));
WebGetArg(PSTR("lh"), tmp5, sizeof(tmp5)); // Syslog host name
char tmp6[CMDSZ];
WebGetArg(PSTR("lp"), tmp6, sizeof(tmp6));
WebGetArg(PSTR("lp"), tmp6, sizeof(tmp6)); // Syslog port number
char tmp7[CMDSZ];
WebGetArg(PSTR("lt"), tmp7, sizeof(tmp7));
WebGetArg(PSTR("lt"), tmp7, sizeof(tmp7)); // Teleperiod
char command[200];
snprintf_P(command, sizeof(command),PSTR(D_CMND_BACKLOG "0 " D_CMND_SERIALLOG " %s;" D_CMND_WEBLOG " %s;" D_CMND_MQTTLOG " %s;" D_CMND_SYSLOG " %s;" D_CMND_LOGHOST " %s;" D_CMND_LOGPORT " %s;" D_CMND_TELEPERIOD " %s"),
(!strlen(tmp1)) ? STR(SERIAL_LOG_LEVEL) : tmp1,
@ -1899,8 +1891,7 @@ void LoggingSaveSettings(void)
/*-------------------------------------------------------------------------------------------*/
void HandleOtherConfiguration(void)
{
void HandleOtherConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_CONFIGURE_OTHER));
@ -1962,42 +1953,36 @@ void HandleOtherConfiguration(void)
WSContentStop();
}
void OtherSaveSettings(void)
{
char tmp[300]; // Needs to hold complete ESP32 template of minimal 230 chars
char webindex[5];
char friendlyname[TOPSZ];
char message[MAX_LOGSZ];
void OtherSaveSettings(void) {
char tmp1[300]; // Needs to hold complete ESP32 template of minimal 230 chars
WebGetArg(PSTR("dn"), tmp1, sizeof(tmp1)); // Device name
char tmp2[100];
WebGetArg(PSTR("wp"), tmp2, sizeof(tmp2)); // Web password
char command[600];
snprintf_P(command, sizeof(command),PSTR(D_CMND_BACKLOG "0 " D_CMND_WEBPASSWORD "2 %s;" D_CMND_SO "3 %d;" D_CMND_DEVICENAME " %s"),
(!strlen(tmp2)) ? "\"" : (strlen(tmp2) < 5) ? "" : tmp2,
Webserver->hasArg(F("b1")), // SetOption3 - Enable MQTT
(!strlen(tmp1)) ? "\"" : tmp1);
char webindex[5];
for (uint32_t i = 0; i < MAX_FRIENDLYNAMES; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("a%d"), i);
WebGetArg(webindex, tmp1, sizeof(tmp1)); // Friendly name 1 to 8
snprintf_P(command, sizeof(command), PSTR("%s;" D_CMND_FN"%d %s"), command, i +1, (!strlen(tmp1)) ? "\"" : tmp1);
}
WebGetArg(PSTR("dn"), tmp, sizeof(tmp));
SettingsUpdateText(SET_DEVICENAME, (!strlen(tmp)) ? "" : (!strcmp(tmp,"1")) ? SettingsText(SET_FRIENDLYNAME1) : tmp);
WebGetArg(PSTR("wp"), tmp, sizeof(tmp));
SettingsUpdateText(SET_WEBPWD, (!strlen(tmp)) ? "" : (strchr(tmp,'*')) ? SettingsText(SET_WEBPWD) : tmp);
Settings.flag.mqtt_enabled = Webserver->hasArg(F("b1")); // SetOption3 - Enable MQTT
#ifdef USE_EMULATION
UdpDisconnect();
#if defined(USE_EMULATION_WEMO) || defined(USE_EMULATION_HUE)
WebGetArg(PSTR("b2"), tmp, sizeof(tmp));
Settings.flag2.emulation = (!strlen(tmp)) ? 0 : atoi(tmp);
WebGetArg(PSTR("b2"), tmp1, sizeof(tmp1)); // Emulation
snprintf_P(command, sizeof(command), PSTR("%s;" D_CMND_EMULATION " %s"), command, (!strlen(tmp1)) ? "0" : tmp1);
#endif // USE_EMULATION_WEMO || USE_EMULATION_HUE
#endif // USE_EMULATION
snprintf_P(message, sizeof(message), PSTR(D_LOG_OTHER D_MQTT_ENABLE " %s, " D_CMND_EMULATION " %d, " D_CMND_DEVICENAME " %s, " D_CMND_FRIENDLYNAME),
GetStateText(Settings.flag.mqtt_enabled), Settings.flag2.emulation, SettingsText(SET_DEVICENAME));
for (uint32_t i = 0; i < MAX_FRIENDLYNAMES; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("a%d"), i);
WebGetArg(webindex, tmp, sizeof(tmp));
snprintf_P(friendlyname, sizeof(friendlyname), PSTR(FRIENDLY_NAME"%d"), i +1);
SettingsUpdateText(SET_FRIENDLYNAME1 +i, (!strlen(tmp)) ? (i) ? friendlyname : PSTR(FRIENDLY_NAME) : tmp);
snprintf_P(message, sizeof(message), PSTR("%s%s %s"), message, (i) ? "," : "", SettingsText(SET_FRIENDLYNAME1 +i));
}
AddLogData(LOG_LEVEL_INFO, message);
WebGetArg(PSTR("t1"), tmp, sizeof(tmp));
if (strlen(tmp)) { // {"NAME":"12345678901234","GPIO":[255,255,255,255,255,255,255,255,255,255,255,255,255],"FLAG":255,"BASE":255}
snprintf_P(message, sizeof(message), PSTR(D_CMND_BACKLOG " " D_CMND_TEMPLATE " %s%s"), tmp, (Webserver->hasArg(F("t2"))) ? PSTR("; " D_CMND_MODULE " 0") : "");
ExecuteWebCommand(message);
WebGetArg(PSTR("t1"), tmp1, sizeof(tmp1)); // Template
if (strlen(tmp1)) { // {"NAME":"12345678901234","GPIO":[255,255,255,255,255,255,255,255,255,255,255,255,255],"FLAG":255,"BASE":255}
snprintf_P(command, sizeof(command), PSTR("%s;" D_CMND_TEMPLATE " %s%s"), command, tmp1, (Webserver->hasArg(F("t2"))) ? PSTR("; " D_CMND_MODULE " 0") : "");
}
ExecuteWebCommand(command);
}
/*-------------------------------------------------------------------------------------------*/
@ -3018,10 +3003,16 @@ void CmndWebServer(void)
void CmndWebPassword(void)
{
bool show_asterisk = (2 == XdrvMailbox.index);
if (XdrvMailbox.data_len > 0) {
SettingsUpdateText(SET_WEBPWD, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? WEB_PASSWORD : XdrvMailbox.data);
ResponseCmndChar(SettingsText(SET_WEBPWD));
if (!show_asterisk) {
ResponseCmndChar(SettingsText(SET_WEBPWD));
}
} else {
show_asterisk = true;
}
if (show_asterisk) {
Response_P(S_JSON_COMMAND_ASTERISK, XdrvMailbox.command);
}
}
@ -3105,7 +3096,7 @@ void CmndWebButton(void)
void CmndCors(void)
{
if (XdrvMailbox.data_len > 0) {
SettingsUpdateText(SET_CORS, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? WEB_PASSWORD : XdrvMailbox.data);
SettingsUpdateText(SET_CORS, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? CORS_DOMAIN : XdrvMailbox.data);
}
ResponseCmndChar(SettingsText(SET_CORS));
}