Added http get option to push

This commit is contained in:
Magnus Persson
2022-03-14 18:55:12 +01:00
parent 6e9d562977
commit 4cad6f888f
23 changed files with 249 additions and 39 deletions

View File

@ -74,12 +74,14 @@ void Config::createJson(DynamicJsonDocument& doc) {
doc[PARAM_TEMPFORMAT] = String(getTempFormat());
doc[PARAM_PUSH_BREWFATHER] = getBrewfatherPushUrl();
doc[PARAM_TOKEN] = getToken();
doc[PARAM_TOKEN2] = getToken2();
doc[PARAM_PUSH_HTTP] = getHttpUrl();
doc[PARAM_PUSH_HTTP_H1] = getHttpHeader(0);
doc[PARAM_PUSH_HTTP_H2] = getHttpHeader(1);
doc[PARAM_PUSH_HTTP2] = getHttp2Url();
doc[PARAM_PUSH_HTTP2_H1] = getHttp2Header(0);
doc[PARAM_PUSH_HTTP2_H2] = getHttp2Header(1);
doc[PARAM_PUSH_HTTP3] = getHttp3Url();
doc[PARAM_PUSH_INFLUXDB2] = getInfluxDb2PushUrl();
doc[PARAM_PUSH_INFLUXDB2_ORG] = getInfluxDb2PushOrg();
doc[PARAM_PUSH_INFLUXDB2_BUCKET] = getInfluxDb2PushBucket();
@ -215,6 +217,7 @@ bool Config::loadFile() {
setBrewfatherPushUrl(doc[PARAM_PUSH_BREWFATHER]);
if (!doc[PARAM_TOKEN].isNull()) setToken(doc[PARAM_TOKEN]);
if (!doc[PARAM_TOKEN2].isNull()) setToken2(doc[PARAM_TOKEN2]);
if (!doc[PARAM_PUSH_HTTP].isNull()) setHttpUrl(doc[PARAM_PUSH_HTTP]);
if (!doc[PARAM_PUSH_HTTP_H1].isNull())
setHttpHeader(doc[PARAM_PUSH_HTTP_H1], 0);
@ -225,6 +228,7 @@ bool Config::loadFile() {
setHttp2Header(doc[PARAM_PUSH_HTTP2_H1], 0);
if (!doc[PARAM_PUSH_HTTP2_H2].isNull())
setHttp2Header(doc[PARAM_PUSH_HTTP2_H2], 1);
if (!doc[PARAM_PUSH_HTTP3].isNull()) setHttp3Url(doc[PARAM_PUSH_HTTP3]);
if (!doc[PARAM_PUSH_INFLUXDB2].isNull())
setInfluxDb2PushUrl(doc[PARAM_PUSH_INFLUXDB2]);

View File

@ -109,11 +109,13 @@ class Config {
String _brewfatherPushUrl = "";
String _token = "";
String _token2 = "";
String _httpUrl = "";
String _httpHeader[2] = {"Content-Type: application/json", ""};
String _http2Url = "";
String _http2Header[2] = {"Content-Type: application/json", ""};
String _http3Url = "";
String _influxDb2Url = "";
String _influxDb2Org = "";
@ -192,6 +194,11 @@ class Config {
_token = s;
_saveNeeded = true;
}
const char* getToken2() { return _token2.c_str(); }
void setToken2(String s) {
_token2 = s;
_saveNeeded = true;
}
// Standard HTTP
const char* getHttpUrl() { return _httpUrl.c_str(); }
@ -220,6 +227,14 @@ class Config {
bool isHttp2Active() { return _http2Url.length() ? true : false; }
bool isHttp2SSL() { return _http2Url.startsWith("https://"); }
const char* getHttp3Url() { return _http3Url.c_str(); }
void setHttp3Url(String s) {
_http3Url = s;
_saveNeeded = true;
}
bool isHttp3Active() { return _http3Url.length() ? true : false; }
bool isHttp3SSL() { return _http3Url.startsWith("https://"); }
// InfluxDB2
const char* getInfluxDb2PushUrl() { return _influxDb2Url.c_str(); }
void setInfluxDb2PushUrl(String s) {
@ -345,7 +360,7 @@ class Config {
}
bool isBLEActive() { return _colorBLE.length() ? true : false; }
bool isWifiPushActive() {
return (isHttpActive() || isHttp2Active() || isBrewfatherActive() || isInfluxDb2Active() || isMqttActive()) ? true : false;
return (isHttpActive() || isHttp2Active() || isHttp3Active() || isBrewfatherActive() || isInfluxDb2Active() || isMqttActive()) ? true : false;
}
const RawGyroData& getGyroCalibration() { return _gyroCalibration; }

View File

@ -52,16 +52,22 @@ void PushTarget::sendAll(float angle, float gravitySG, float corrGravitySG,
if (myConfig.isHttpActive()) {
LOG_PERF_START("push-http");
sendHttp(engine, myConfig.isHttpSSL(), 0);
sendHttpPost(engine, myConfig.isHttpSSL(), 0);
LOG_PERF_STOP("push-http");
}
if (myConfig.isHttp2Active()) {
LOG_PERF_START("push-http2");
sendHttp(engine, myConfig.isHttp2SSL(), 1);
sendHttpPost(engine, myConfig.isHttp2SSL(), 1);
LOG_PERF_STOP("push-http2");
}
if (myConfig.isHttp3Active()) {
LOG_PERF_START("push-http3");
sendHttpGet(engine, myConfig.isHttp3SSL());
LOG_PERF_STOP("push-http3");
}
if (myConfig.isInfluxDb2Active()) {
LOG_PERF_START("push-influxdb2");
sendInfluxDb2(engine);
@ -178,9 +184,9 @@ void PushTarget::addHttpHeader(HTTPClient& http, String header) {
}
//
// Send data to http target
// Send data to http target using POST
//
void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, int index) {
#if !defined(PUSH_DISABLE_LOGGING)
Log.notice(F("PUSH: Sending values to http (%s)" CR),
index ? "http2" : "http");
@ -248,12 +254,12 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
if (_lastCode == 200) {
_lastSuccess = true;
Log.notice(F("PUSH: HTTP push successful, response=%d" CR),
Log.notice(F("PUSH: HTTP post successful, response=%d" CR),
_lastCode);
} else {
ErrorFileLog errLog;
errLog.addEntry(
"PUSH: HTTP push failed response=" + String(_lastCode) +
"PUSH: HTTP post failed response=" + String(_lastCode) +
String(index == 0 ? " (http)" : " (http2)"));
}
@ -268,7 +274,71 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
}
//
// Send data to http target
// Send data to http target using GET
//
void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) {
#if !defined(PUSH_DISABLE_LOGGING)
Log.notice(F("PUSH: Sending values to http3" CR));
#endif
_lastCode = 0;
_lastSuccess = false;
String serverPath;
serverPath = myConfig.getHttp3Url();
serverPath += engine.create(TemplatingEngine::TEMPLATE_HTTP3);
#if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING)
Log.verbose(F("PUSH: url %s." CR), serverPath.c_str());
#endif
if (isSecure) {
Log.notice(F("PUSH: HTTP, SSL enabled without validation." CR));
_wifiSecure.setInsecure();
#if defined (ESP8266)
String host = serverPath.substring(8); // remove the prefix or the probe will fail, it needs a pure host name.
int idx = host.indexOf("/");
if (idx!=-1)
host = host.substring(0, idx);
if (_wifiSecure.probeMaxFragmentLength(host, 443, 512)) {
Log.notice(F("PUSH: HTTP server supports smaller SSL buffer." CR));
_wifiSecure.setBufferSizes(512, 512);
}
#endif
_httpSecure.begin(_wifiSecure, serverPath);
_httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
_lastCode = _httpSecure.GET();
} else {
_http.begin(_wifi, serverPath);
_http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
_lastCode = _http.GET();
}
if (_lastCode == 200) {
_lastSuccess = true;
Log.notice(F("PUSH: HTTP get successful, response=%d" CR),
_lastCode);
} else {
ErrorFileLog errLog;
errLog.addEntry(
"PUSH: HTTP get failed response=" + String(_lastCode));
}
if (isSecure) {
_httpSecure.end();
_wifiSecure.stop();
} else {
_http.end();
_wifi.stop();
}
tcp_cleanup();
}
//
// Send data to mqtt target
//
void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
#if !defined(PUSH_DISABLE_LOGGING)

View File

@ -42,7 +42,8 @@ class PushTarget {
int _lastCode;
bool _lastSuccess;
void sendHttp(TemplatingEngine& engine, bool isSecure, int index);
void sendHttpPost(TemplatingEngine& engine, bool isSecure, int index);
void sendHttpGet(TemplatingEngine& engine, bool isSecure);
void addHttpHeader(HTTPClient& http, String header);
public:
@ -50,8 +51,9 @@ class PushTarget {
float runTime);
void sendBrewfather(TemplatingEngine& engine);
void sendHttp1(TemplatingEngine& engine, bool isSecure) { sendHttp(engine, isSecure, 0); }
void sendHttp2(TemplatingEngine& engine, bool isSecure) { sendHttp(engine, isSecure, 1); }
void sendHttp1(TemplatingEngine& engine, bool isSecure) { sendHttpPost(engine, isSecure, 0); }
void sendHttp2(TemplatingEngine& engine, bool isSecure) { sendHttpPost(engine, isSecure, 1); }
void sendHttp3(TemplatingEngine& engine, bool isSecure) { sendHttpGet(engine, isSecure); }
void sendInfluxDb2(TemplatingEngine& engine);
void sendMqtt(TemplatingEngine& engine, bool isSecure);
int getLastCode() { return _lastCode; }

View File

@ -34,12 +34,14 @@ SOFTWARE.
#define PARAM_RUNTIME_AVERAGE "runtime-average"
#define PARAM_PUSH_BREWFATHER "brewfather-push"
#define PARAM_TOKEN "token"
#define PARAM_TOKEN2 "token2"
#define PARAM_PUSH_HTTP "http-push"
#define PARAM_PUSH_HTTP_H1 "http-push-h1"
#define PARAM_PUSH_HTTP_H2 "http-push-h2"
#define PARAM_PUSH_HTTP2 "http-push2"
#define PARAM_PUSH_HTTP2_H1 "http-push2-h1"
#define PARAM_PUSH_HTTP2_H2 "http-push2-h2"
#define PARAM_PUSH_HTTP3 "http-push3"
#define PARAM_PUSH_INFLUXDB2 "influxdb2-push"
#define PARAM_PUSH_INFLUXDB2_ORG "influxdb2-org"
#define PARAM_PUSH_INFLUXDB2_BUCKET "influxdb2-bucket"
@ -81,6 +83,7 @@ SOFTWARE.
#define PARAM_HW_PUSH_TIMEOUT "push-timeout"
#define PARAM_FORMAT_HTTP1 "http-1"
#define PARAM_FORMAT_HTTP2 "http-2"
#define PARAM_FORMAT_HTTP3 "http-3"
#define PARAM_FORMAT_BREWFATHER "brewfather"
#define PARAM_FORMAT_INFLUXDB "influxdb"
#define PARAM_FORMAT_MQTT "mqtt"

View File

@ -30,7 +30,7 @@ SOFTWARE.
#include <WiFi.h>
#endif
// Use iSpindle format for compatibility
// Use iSpindle format for compatibility, HTTP POST
const char iSpindleFormat[] PROGMEM =
"{"
"\"name\" : \"${mdns}\", "
@ -48,6 +48,22 @@ const char iSpindleFormat[] PROGMEM =
"\"run-time\": ${run-time} "
"}";
// Format for an HTTP GET
const char iHttpGetFormat[] PROGMEM =
"?name=${mdns}"
"&id=${id}"
"&token=${token2}"
"&interval=${sleep-interval}"
"&temperature=${temp}"
"&temp-units=${temp-unit}"
"&gravity=${gravity}"
"&angle=${angle}"
"&battery=${battery}"
"&rssi=${rssi}"
"&corr-gravity=${corr-gravity}"
"&gravity-unit=${gravity-unit}"
"&run-time=${run-time}";
const char brewfatherFormat[] PROGMEM =
"{"
"\"name\": \"${mdns}\","
@ -89,6 +105,7 @@ void TemplatingEngine::initialize(float angle, float gravitySG, float corrGravit
setVal(TPL_MDNS, myConfig.getMDNS());
setVal(TPL_ID, myConfig.getID());
setVal(TPL_TOKEN, myConfig.getToken());
setVal(TPL_TOKEN2, myConfig.getToken2());
// Temperature
if (myConfig.isTempC()) {
@ -151,6 +168,10 @@ const String& TemplatingEngine::create(TemplatingEngine::Templates idx) {
baseTemplate = String(iSpindleFormat);
fname = TPL_FNAME_HTTP2;
break;
case TEMPLATE_HTTP3:
baseTemplate = String(iHttpGetFormat);
fname = TPL_FNAME_HTTP3;
break;
case TEMPLATE_BREWFATHER:
baseTemplate = String(brewfatherFormat);
//fname = TPL_FNAME_BREWFATHER;

View File

@ -35,6 +35,7 @@ SOFTWARE.
#define TPL_MDNS "${mdns}"
#define TPL_ID "${id}"
#define TPL_TOKEN "${token}"
#define TPL_TOKEN2 "${token2}"
#define TPL_SLEEP_INTERVAL "${sleep-interval}"
#define TPL_TEMP "${temp}"
#define TPL_TEMP_C "${temp-c}"
@ -55,11 +56,13 @@ SOFTWARE.
#define TPL_FNAME_HTTP1 "/http-1.tpl"
#define TPL_FNAME_HTTP2 "/http-2.tpl"
#define TPL_FNAME_HTTP3 "/http-3.tpl"
// #define TPL_FNAME_BREWFATHER "/brewfather.tpl"
#define TPL_FNAME_INFLUXDB "/influxdb.tpl"
#define TPL_FNAME_MQTT "/mqtt.tpl"
extern const char iSpindleFormat[] PROGMEM;
extern const char iHttpGetFormat[] PROGMEM;
extern const char brewfatherFormat[] PROGMEM;
extern const char influxDbFormat[] PROGMEM;
extern const char mqttFormat[] PROGMEM;
@ -72,7 +75,7 @@ class TemplatingEngine {
String val;
};
KeyVal items[20] = {{TPL_MDNS, ""}, {TPL_ID, ""},
KeyVal items[21] = {{TPL_MDNS, ""}, {TPL_ID, ""},
{TPL_SLEEP_INTERVAL, ""}, {TPL_TEMP, ""},
{TPL_TEMP_C, ""}, {TPL_TEMP_F, ""},
{TPL_TEMP_UNITS, ""}, {TPL_BATTERY, ""},
@ -81,7 +84,8 @@ class TemplatingEngine {
{TPL_GRAVITY, ""}, {TPL_GRAVITY_G, ""},
{TPL_GRAVITY_P, ""}, {TPL_GRAVITY_CORR, ""},
{TPL_GRAVITY_CORR_G, ""}, {TPL_GRAVITY_CORR_P, ""},
{TPL_GRAVITY_UNIT, ""}, {TPL_TOKEN, ""}};
{TPL_GRAVITY_UNIT, ""}, {TPL_TOKEN, ""},
{TPL_TOKEN2, ""} };
char buffer[20];
String baseTemplate;
@ -128,9 +132,10 @@ class TemplatingEngine {
enum Templates {
TEMPLATE_HTTP1 = 0,
TEMPLATE_HTTP2 = 1,
TEMPLATE_BREWFATHER = 2,
TEMPLATE_INFLUX = 3,
TEMPLATE_MQTT = 4
TEMPLATE_HTTP3 = 2,
TEMPLATE_BREWFATHER = 3,
TEMPLATE_INFLUX = 4,
TEMPLATE_MQTT = 5
};
void initialize(float angle, float gravitySG, float corrGravitySG,

View File

@ -285,6 +285,7 @@ void WebServerHandler::webHandleStatus() {
doc[PARAM_RSSI] = WiFi.RSSI();
doc[PARAM_SLEEP_INTERVAL] = myConfig.getSleepInterval();
doc[PARAM_TOKEN] = myConfig.getToken();
doc[PARAM_TOKEN2] = myConfig.getToken2();
doc[PARAM_APP_VER] = CFG_APPVER;
doc[PARAM_MDNS] = myConfig.getMDNS();
@ -413,6 +414,8 @@ void WebServerHandler::webHandleConfigPush() {
if (_server->hasArg(PARAM_TOKEN))
myConfig.setToken(_server->arg(PARAM_TOKEN).c_str());
if (_server->hasArg(PARAM_TOKEN2))
myConfig.setToken2(_server->arg(PARAM_TOKEN2).c_str());
if (_server->hasArg(PARAM_PUSH_HTTP))
myConfig.setHttpUrl(_server->arg(PARAM_PUSH_HTTP).c_str());
if (_server->hasArg(PARAM_PUSH_HTTP_H1))
@ -425,6 +428,8 @@ void WebServerHandler::webHandleConfigPush() {
myConfig.setHttp2Header(_server->arg(PARAM_PUSH_HTTP2_H1).c_str(), 0);
if (_server->hasArg(PARAM_PUSH_HTTP2_H2))
myConfig.setHttp2Header(_server->arg(PARAM_PUSH_HTTP2_H2).c_str(), 1);
if (_server->hasArg(PARAM_PUSH_HTTP3))
myConfig.setHttp3Url(_server->arg(PARAM_PUSH_HTTP3).c_str());
if (_server->hasArg(PARAM_PUSH_BREWFATHER))
myConfig.setBrewfatherPushUrl(_server->arg(PARAM_PUSH_BREWFATHER).c_str());
if (_server->hasArg(PARAM_PUSH_INFLUXDB2))
@ -710,6 +715,8 @@ void WebServerHandler::webHandleConfigFormatWrite() {
success = writeFile(TPL_FNAME_HTTP1, _server->arg(PARAM_FORMAT_HTTP1));
} else if (_server->hasArg(PARAM_FORMAT_HTTP2)) {
success = writeFile(TPL_FNAME_HTTP2, _server->arg(PARAM_FORMAT_HTTP2));
} else if (_server->hasArg(PARAM_FORMAT_HTTP3)) {
success = writeFile(TPL_FNAME_HTTP3, _server->arg(PARAM_FORMAT_HTTP3));
} else if (_server->hasArg(PARAM_FORMAT_INFLUXDB)) {
success =
writeFile(TPL_FNAME_INFLUXDB, _server->arg(PARAM_FORMAT_INFLUXDB));
@ -774,6 +781,9 @@ void WebServerHandler::webHandleTestPush() {
} else if (!type.compareTo(PARAM_FORMAT_HTTP2) && myConfig.isHttp2Active()) {
push.sendHttp2(engine, myConfig.isHttp2SSL());
enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_HTTP3) && myConfig.isHttp3Active()) {
push.sendHttp3(engine, myConfig.isHttp3SSL());
enabled = true;
} else if (!type.compareTo(PARAM_FORMAT_INFLUXDB) && myConfig.isInfluxDb2Active()) {
push.sendInfluxDb2(engine);
enabled = true;
@ -868,6 +878,12 @@ void WebServerHandler::webHandleConfigFormatRead() {
else
doc[PARAM_FORMAT_HTTP2] = urlencode(String(&iSpindleFormat[0]));
s = readFile(TPL_FNAME_HTTP3);
if (s.length())
doc[PARAM_FORMAT_HTTP3] = urlencode(s);
else
doc[PARAM_FORMAT_HTTP3] = urlencode(String(&iHttpGetFormat[0]));
/*s = readFile(TPL_FNAME_BREWFATHER);
if (s.length())
doc[PARAM_FORMAT_BREWFATHER] = urlencode(s);