Refactor Web commands

Refactor Web commands
This commit is contained in:
Theo Arends 2019-07-28 17:57:09 +02:00
parent 765a625e5a
commit 50e0de0dd5
4 changed files with 106 additions and 79 deletions

View File

@ -715,6 +715,11 @@ int GetCommandCode(char* destination, size_t destination_size, const char* needl
return result; return result;
} }
int GetCommand(const char* haystack)
{
return GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, haystack);
}
int GetStateNumber(char *state_text) int GetStateNumber(char *state_text)
{ {
char command[CMDSZ]; char command[CMDSZ];

View File

@ -152,7 +152,7 @@ void CommandHandler(char* topic, uint8_t* data, uint32_t data_len)
XdrvMailbox.topic = type; XdrvMailbox.topic = type;
XdrvMailbox.data = dataBuf; XdrvMailbox.data = dataBuf;
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, type, kTasmotaCommands); int command_code = GetCommand(kTasmotaCommands);
if (command_code >= 0) { if (command_code >= 0) {
TasmotaCommand[command_code](); TasmotaCommand[command_code]();
} else { } else {

View File

@ -2425,8 +2425,6 @@ int WebSend(char *buffer)
return status; return status;
} }
/*********************************************************************************************/
bool JsonWebColor(const char* dataBuf) bool JsonWebColor(const char* dataBuf)
{ {
// Default (light) // Default (light)
@ -2455,52 +2453,94 @@ bool JsonWebColor(const char* dataBuf)
return true; return true;
} }
enum WebCommands { CMND_WEBSERVER, CMND_WEBPASSWORD, CMND_WEBLOG, CMND_WEBREFRESH, CMND_WEBSEND, CMND_WEBCOLOR, CMND_EMULATION };
const char kWebCommands[] PROGMEM = D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_WEBREFRESH "|" D_CMND_WEBSEND "|" D_CMND_WEBCOLOR "|" D_CMND_EMULATION ;
const char kWebSendStatus[] PROGMEM = D_JSON_DONE "|" D_JSON_WRONG_PARAMETERS "|" D_JSON_CONNECT_FAILED "|" D_JSON_HOST_NOT_FOUND ; const char kWebSendStatus[] PROGMEM = D_JSON_DONE "|" D_JSON_WRONG_PARAMETERS "|" D_JSON_CONNECT_FAILED "|" D_JSON_HOST_NOT_FOUND ;
bool WebCommand(void) const char kWebCommands[] PROGMEM =
{ #ifdef USE_EMULATION
char command[CMDSZ]; D_CMND_EMULATION "|"
bool serviced = true; #endif
D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_WEBREFRESH "|" D_CMND_WEBSEND "|" D_CMND_WEBCOLOR ;
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kWebCommands); void (* const WebCommand[])(void) PROGMEM = {
if (-1 == command_code) { #ifdef USE_EMULATION
serviced = false; // Unknown command &CmndEmulation,
#endif
&CmndWebServer, &CmndWebPassword, &CmndWeblog, &CmndWebRefresh, &CmndWebSend, &CmndWebColor };
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
#ifdef USE_EMULATION
void CmndEmulation(void)
{
#if defined(USE_EMULATION_WEMO) && defined(USE_EMULATION_HUE)
if ((XdrvMailbox.payload >= EMUL_NONE) && (XdrvMailbox.payload < EMUL_MAX)) {
#else
#ifndef USE_EMULATION_WEMO
if ((EMUL_NONE == XdrvMailbox.payload) || (EMUL_HUE == XdrvMailbox.payload)) {
#endif
#ifndef USE_EMULATION_HUE
if ((EMUL_NONE == XdrvMailbox.payload) || (EMUL_WEMO == XdrvMailbox.payload)) {
#endif
#endif
Settings.flag2.emulation = XdrvMailbox.payload;
restart_flag = 2;
}
Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.flag2.emulation);
}
#endif // USE_EMULATION
void CmndWebServer(void)
{
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
Settings.webserver = XdrvMailbox.payload;
} }
if (CMND_WEBSERVER == command_code) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) { Settings.webserver = XdrvMailbox.payload; }
if (Settings.webserver) { if (Settings.webserver) {
Response_P(PSTR("{\"" D_CMND_WEBSERVER "\":\"" D_JSON_ACTIVE_FOR " %s " D_JSON_ON_DEVICE " %s " D_JSON_WITH_IP_ADDRESS " %s\"}"), Response_P(PSTR("{\"" D_CMND_WEBSERVER "\":\"" D_JSON_ACTIVE_FOR " %s " D_JSON_ON_DEVICE " %s " D_JSON_WITH_IP_ADDRESS " %s\"}"),
(2 == Settings.webserver) ? D_ADMIN : D_USER, my_hostname, WiFi.localIP().toString().c_str()); (2 == Settings.webserver) ? D_ADMIN : D_USER, my_hostname, WiFi.localIP().toString().c_str());
} else { } else {
Response_P(S_JSON_COMMAND_SVALUE, command, GetStateText(0)); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, GetStateText(0));
} }
} }
else if (CMND_WEBPASSWORD == command_code) {
void CmndWebPassword(void)
{
if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.data_len < sizeof(Settings.web_password))) { if ((XdrvMailbox.data_len > 0) && (XdrvMailbox.data_len < sizeof(Settings.web_password))) {
strlcpy(Settings.web_password, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? WEB_PASSWORD : XdrvMailbox.data, sizeof(Settings.web_password)); strlcpy(Settings.web_password, (SC_CLEAR == Shortcut()) ? "" : (SC_DEFAULT == Shortcut()) ? WEB_PASSWORD : XdrvMailbox.data, sizeof(Settings.web_password));
Response_P(S_JSON_COMMAND_SVALUE, command, Settings.web_password); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, Settings.web_password);
} else { } else {
Response_P(S_JSON_COMMAND_ASTERISK, command); Response_P(S_JSON_COMMAND_ASTERISK, XdrvMailbox.command);
} }
} }
else if (CMND_WEBLOG == command_code) {
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_ALL)) { Settings.weblog_level = XdrvMailbox.payload; } void CmndWeblog(void)
Response_P(S_JSON_COMMAND_NVALUE, command, Settings.weblog_level); {
if ((XdrvMailbox.payload >= LOG_LEVEL_NONE) && (XdrvMailbox.payload <= LOG_LEVEL_ALL)) {
Settings.weblog_level = XdrvMailbox.payload;
} }
else if (CMND_WEBREFRESH == command_code) { Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.weblog_level);
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload <= 10000)) { Settings.web_refresh = XdrvMailbox.payload; }
Response_P(S_JSON_COMMAND_NVALUE, command, Settings.web_refresh);
} }
else if (CMND_WEBSEND == command_code) {
void CmndWebRefresh(void)
{
if ((XdrvMailbox.payload > 999) && (XdrvMailbox.payload <= 10000)) {
Settings.web_refresh = XdrvMailbox.payload;
}
Response_P(S_JSON_COMMAND_NVALUE, XdrvMailbox.command, Settings.web_refresh);
}
void CmndWebSend(void)
{
if (XdrvMailbox.data_len > 0) { if (XdrvMailbox.data_len > 0) {
uint32_t result = WebSend(XdrvMailbox.data); uint32_t result = WebSend(XdrvMailbox.data);
char stemp1[20]; char stemp1[20];
Response_P(S_JSON_COMMAND_SVALUE, command, GetTextIndexed(stemp1, sizeof(stemp1), result, kWebSendStatus)); Response_P(S_JSON_COMMAND_SVALUE, XdrvMailbox.command, GetTextIndexed(stemp1, sizeof(stemp1), result, kWebSendStatus));
} }
} }
else if (CMND_WEBCOLOR == command_code) {
void CmndWebColor(void)
{
if (XdrvMailbox.data_len > 0) { if (XdrvMailbox.data_len > 0) {
if (strstr(XdrvMailbox.data, "{") == nullptr) { // If no JSON it must be parameter if (strstr(XdrvMailbox.data, "{") == nullptr) { // If no JSON it must be parameter
if ((XdrvMailbox.data_len > 3) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= COL_LAST)) { if ((XdrvMailbox.data_len > 3) && (XdrvMailbox.index > 0) && (XdrvMailbox.index <= COL_LAST)) {
@ -2521,28 +2561,6 @@ bool WebCommand(void)
} }
ResponseAppend_P(PSTR("]}")); ResponseAppend_P(PSTR("]}"));
} }
#ifdef USE_EMULATION
else if (CMND_EMULATION == command_code) {
#if defined(USE_EMULATION_WEMO) && defined(USE_EMULATION_HUE)
if ((XdrvMailbox.payload >= EMUL_NONE) && (XdrvMailbox.payload < EMUL_MAX)) {
#else
#ifndef USE_EMULATION_WEMO
if ((EMUL_NONE == XdrvMailbox.payload) || (EMUL_HUE == XdrvMailbox.payload)) {
#endif
#ifndef USE_EMULATION_HUE
if ((EMUL_NONE == XdrvMailbox.payload) || (EMUL_WEMO == XdrvMailbox.payload)) {
#endif
#endif
Settings.flag2.emulation = XdrvMailbox.payload;
restart_flag = 2;
}
Response_P(S_JSON_COMMAND_NVALUE, command, Settings.flag2.emulation);
}
#endif // USE_EMULATION
else serviced = false; // Unknown command
return serviced;
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
@ -2560,7 +2578,11 @@ bool Xdrv01(uint8_t function)
#endif // USE_EMULATION #endif // USE_EMULATION
break; break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = WebCommand(); int command_code = GetCommand(kWebCommands);
if (command_code >= 0) {
WebCommand[command_code]();
result = true;
}
break; break;
} }
return result; return result;

View File

@ -1058,7 +1058,7 @@ bool Xdrv02(uint8_t function)
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_COMMAND: case FUNC_COMMAND:
int command_code = GetCommandCode(XdrvMailbox.command, CMDSZ, XdrvMailbox.topic, kMqttCommands); int command_code = GetCommand(kMqttCommands);
if (command_code >= 0) { if (command_code >= 0) {
MqttCommand[command_code](); MqttCommand[command_code]();
result = true; result = true;