Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement WebQuery Command #13209

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tasmota/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
#define D_CMND_WEBLOG "WebLog"
#define D_CMND_WEBREFRESH "WebRefresh"
#define D_CMND_WEBSEND "WebSend"
#define D_CMND_WEBQUERY "WebQuery"
#define D_CMND_WEBCOLOR "WebColor"
#define D_CMND_WEBBUTTON "WebButton"
#define D_CMND_WEBSENSOR "WebSensor"
Expand Down
101 changes: 91 additions & 10 deletions tasmota/xdrv_01_webserver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3098,14 +3098,86 @@ int WebSend(char *buffer)
}
url += F("cmnd="); // url = |http://192.168.178.86/cm?cmnd=| or |http://192.168.178.86/cm?user=admin&password=joker&cmnd=|
}
url += command; // url = |http://192.168.178.86/cm?cmnd=POWER1 ON|
url += command;
url += F(" GET"); // url = |http://192.168.178.86/cm?cmnd=POWER1 ON|

DEBUG_CORE_LOG(PSTR("WEB: Uri |%s|"), url.c_str());
status = WebQuery(const_cast<char*>(url.c_str()));
}
return status;
}

int WebQuery(char *buffer)
{
// http://192.168.1.1/path GET -> Sends HTTP GET http://192.168.1.1/path
// http://192.168.1.1/path POST {"some":"message"} -> Sends HTTP POST to http://192.168.1.1/path with body {"some":"message"}
// http://192.168.1.1/path PUT [Autorization: Bearer abcdxyz] potato -> Sends HTTP PUT to http://192.168.1.1/path with authorization header and body "potato"
// http://192.168.1.1/path PATCH patchInfo -> Sends HTTP PATCH to http://192.168.1.1/path with body "potato"

// Valid HTTP Commands: GET, POST, PUT, and PATCH
// An unlimited number of headers can be sent per request, and a body can be sent for all command types
// The body will be ignored if sending a GET command

int status = WEBCMND_WRONG_PARAMETERS;
int http_code;
WiFiClient http_client;
HTTPClient http;
String methodStr;
String bodyStr;
char *url;
char *header;
char *headerBody;
char *body;
char *method;
char *temp;
bool headerFound;

url = strtok_r(buffer, " ", &temp);
method = strtok_r(temp, " ", &temp);

if(url && method) {
if (http.begin(http_client, UrlEncode(url))) {
if(temp) { //There is a body and/or header
if(temp[0] == '[') { //Header information was sent; decode it
temp += 1;
temp = strtok_r(temp, "]", &body);
headerFound = true;
while(headerFound) {
header = strtok_r(temp, ":", &temp);
if(header) {
headerBody = strtok_r(temp, "|", &temp);
if(headerBody) {
http.addHeader(header, headerBody);
}
else headerFound = false;
}
else headerFound = false;
}
}
else { //No header information was sent, but there was a body
body = temp;
}
}

//No body sent; define empty string for the body
else {
body = (char*)malloc(1);
JVital2013 marked this conversation as resolved.
Show resolved Hide resolved
body[0] = 0;
}

//Format Strings
methodStr = String(method);
methodStr.toUpperCase();
bodyStr = String(body);
bodyStr.trim();

//Perform specified web request type
if(methodStr.equals("GET")) http_code = http.GET();
else if(methodStr.equals("POST")) http_code = http.POST(body);
else if(methodStr.equals("PUT")) http_code = http.PUT(body);
else if(methodStr.equals("PATCH")) http_code = http.PATCH(body);
else return status;

WiFiClient http_client;
HTTPClient http;
if (http.begin(http_client, UrlEncode(url))) { // UrlEncode(url) = |http://192.168.178.86/cm?cmnd=POWER1%20ON|
int http_code = http.GET(); // Start connection and send HTTP header
if (http_code > 0) { // http_code will be negative on error
if (http_code == HTTP_CODE_OK || http_code == HTTP_CODE_MOVED_PERMANENTLY) {
#ifdef USE_WEBSEND_RESPONSE
Expand All @@ -3125,7 +3197,7 @@ int WebSend(char *buffer)
// recursive call must be possible in this case
tasm_cmd_activ = 0;
#endif // USE_SCRIPT
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_CMND_WEBSEND));
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_CMND_WEBQUERY));
#endif // USE_WEBSEND_RESPONSE
}
status = WEBCMND_DONE;
Expand Down Expand Up @@ -3240,8 +3312,8 @@ const char kWebCommands[] PROGMEM = "|" // No prefix
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
D_CMND_SENDMAIL "|"
#endif
D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_WEBREFRESH "|" D_CMND_WEBSEND "|" D_CMND_WEBCOLOR "|"
D_CMND_WEBSENSOR "|" D_CMND_WEBBUTTON
D_CMND_WEBSERVER "|" D_CMND_WEBPASSWORD "|" D_CMND_WEBLOG "|" D_CMND_WEBREFRESH "|" D_CMND_WEBSEND "|" D_CMND_WEBQUERY "|"
D_CMND_WEBCOLOR "|" D_CMND_WEBSENSOR "|" D_CMND_WEBBUTTON
#ifdef USE_WEBGETCONFIG
"|" D_CMND_WEBGETCONFIG
#endif
Expand All @@ -3257,8 +3329,8 @@ void (* const WebCommand[])(void) PROGMEM = {
#if defined(USE_SENDMAIL) || defined(USE_ESP32MAIL)
&CmndSendmail,
#endif
&CmndWebServer, &CmndWebPassword, &CmndWeblog, &CmndWebRefresh, &CmndWebSend, &CmndWebColor,
&CmndWebSensor, &CmndWebButton
&CmndWebServer, &CmndWebPassword, &CmndWeblog, &CmndWebRefresh, &CmndWebSend, &CmndWebQuery,
&CmndWebColor, &CmndWebSensor, &CmndWebButton
#ifdef USE_WEBGETCONFIG
, &CmndWebGetConfig
#endif
Expand Down Expand Up @@ -3359,6 +3431,15 @@ void CmndWebSend(void)
}
}

void CmndWebQuery(void)
{
if (XdrvMailbox.data_len > 0) {
uint32_t result = WebQuery(XdrvMailbox.data);
char stemp1[20];
ResponseCmndChar(GetTextIndexed(stemp1, sizeof(stemp1), result, kWebCmndStatus));
}
}

#ifdef USE_WEBGETCONFIG
void CmndWebGetConfig(void)
{
Expand Down