mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 07:49:52 +03:00
scheduler http tests
This commit is contained in:
27
lib_standalone/HTTPClient.h
Normal file
27
lib_standalone/HTTPClient.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef HTTPClient_H_
|
||||
#define HTTPClient_H_
|
||||
|
||||
#include "WString.h"
|
||||
|
||||
class HTTPClient {
|
||||
public:
|
||||
// HTTPClient();
|
||||
// ~HTTPClient();
|
||||
|
||||
bool begin(String url) {
|
||||
return true;
|
||||
};
|
||||
void end(void) {};
|
||||
int GET() {
|
||||
return 200;
|
||||
};
|
||||
int POST(String payload) {
|
||||
return 200;
|
||||
};
|
||||
void addHeader(const String & name, const String & value, bool first = false, bool replace = true) {};
|
||||
String getString(void) {
|
||||
return "Hello, World!";
|
||||
};
|
||||
};
|
||||
|
||||
#endif /* HTTPClient_H_ */
|
||||
@@ -53,6 +53,14 @@ class String {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int len() const {
|
||||
return _str.size();
|
||||
}
|
||||
|
||||
bool startsWith(const char * prefix) const {
|
||||
return _str.find(prefix) == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _str;
|
||||
};
|
||||
|
||||
@@ -342,22 +342,44 @@ bool WebSchedulerService::command(const char * name, const char * cmd, const cha
|
||||
HTTPClient http;
|
||||
String url = doc["url"];
|
||||
if (http.begin(url)) {
|
||||
// It's an HTTP call
|
||||
|
||||
// add any given headers
|
||||
for (JsonPair p : doc["header"].as<JsonObject>()) {
|
||||
http.addHeader(p.key().c_str(), p.value().as<String>().c_str());
|
||||
}
|
||||
String value = doc["value"] | "";
|
||||
String value = doc["value"] | data; // extract value if its in the command, or take the data
|
||||
String method = doc["method"] | "GET"; // default GET
|
||||
|
||||
// if there is data, force a POST
|
||||
if (value.length()) {
|
||||
if (value.startsWith("{")) {
|
||||
http.addHeader("Content-Type", "application/json"); // auto-set to JSON
|
||||
}
|
||||
httpResult = http.POST(value);
|
||||
} else if (data && data[0] != '\0') { // post
|
||||
httpResult = http.POST(String(data));
|
||||
} else {
|
||||
httpResult = http.GET();
|
||||
// no value, but check if it still a POST request
|
||||
if (method == "POST") {
|
||||
httpResult = http.POST(value);
|
||||
} else {
|
||||
httpResult = http.GET(); // normal GET
|
||||
}
|
||||
}
|
||||
|
||||
http.end();
|
||||
|
||||
// check HTTP return code
|
||||
if (httpResult != 200) {
|
||||
char error[100];
|
||||
snprintf(error, sizeof(error), "Schedule %s: URL command failed with http code %d", name, httpResult);
|
||||
emsesp::EMSESP::logger().warning(error);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return httpResult > 0;
|
||||
}
|
||||
|
||||
JsonDocument doc_input;
|
||||
JsonObject input = doc_input.to<JsonObject>();
|
||||
if (strlen(data)) { // empty data queries a value
|
||||
@@ -562,6 +584,8 @@ void WebSchedulerService::test() {
|
||||
// (14 - 40) * 2.8 + 5 = -67.8
|
||||
test_value = "(custom/seltemp - boiler/flowtempoffset) * 2.8 + 5";
|
||||
command("test", test_cmd.c_str(), compute(test_value).c_str());
|
||||
|
||||
// TODO add some HTTP/URI tests
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user