From e91c8af1a566eb148b9c09824ab21761f4633b5a Mon Sep 17 00:00:00 2001 From: Magnus Persson Date: Wed, 27 Apr 2022 22:15:35 +0200 Subject: [PATCH] moved maxfrag to own function --- src/helper.cpp | 2 +- src/main.cpp | 6 +++ src/pushtarget.cpp | 116 +++++++++++++++++++++------------------------ src/pushtarget.hpp | 1 + 4 files changed, 61 insertions(+), 64 deletions(-) diff --git a/src/helper.cpp b/src/helper.cpp index 7fba565..8023174 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -387,7 +387,7 @@ void PerfLogging::pushInflux() { // Send HTTP POST request String auth = "Token " + String(myConfig.getInfluxDb2PushToken()); http.addHeader(F("Authorization"), auth.c_str()); - http.setTimeout(myHardwareConfig.getPushTimeout()); + http.setTimeout(myAdvancedConfig.getPushTimeout()); int httpResponseCode = http.POST(body); if (httpResponseCode == 204) { diff --git a/src/main.cpp b/src/main.cpp index 8cbc1b8..882a76c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -60,6 +60,12 @@ void checkSleepMode(float angle, float volt) { return; #endif +#if defined( FORCE_GRAVITY_MODE ) + Log.notice( + F("MAIN: Forcing device into gravity mode for debugging" CR)); + runMode = RunMode::gravityMode; +#endif + const RawGyroData &g = myConfig.getGyroCalibration(); if (!g.ax && !g.ay && !g.az && !g.gx && !g.gy && !g.gz) { diff --git a/src/pushtarget.cpp b/src/pushtarget.cpp index 32949c2..68123c5 100644 --- a/src/pushtarget.cpp +++ b/src/pushtarget.cpp @@ -148,6 +148,36 @@ void PushTarget::sendAll(float angle, float gravitySG, float corrGravitySG, intDelay.save(); } +// +// Check if the server can reduce the buffer size to save memory (ESP8266 only) +// +void PushTarget::probeMaxFragement( String& serverPath ) { +#if defined(ESP8266) // Looks like this is feature is not supported by influxdb + // Format: http:://servername:port/path + int port = 443; + String host = + serverPath.substring(8); // remove the prefix or the probe will fail, + // it needs a pure host name. + // Remove the path if it exist + int idx = host.indexOf("/"); + if (idx != -1) host = host.substring(0, idx); + + // If a server port is defined, lets extract that part + idx = host.indexOf(":"); + if (idx != -1) { + String p = host.substring(idx+1); + port = p.toInt(); + host = host.substring(0, idx); + } + + Log.notice(F("PUSH: Probing server to max fragment %s:%d" CR), host.c_str(), port); + if (_wifiSecure.probeMaxFragmentLength(host, port, 512)) { + Log.notice(F("PUSH: Server supports smaller SSL buffer." CR)); + _wifiSecure.setBufferSizes(512, 512); + } +#endif +} + // // Send to influx db v2 // @@ -164,40 +194,27 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine, bool isSecure) { "&bucket=" + String(myConfig.getInfluxDb2PushBucket()); String doc = engine.create(TemplatingEngine::TEMPLATE_INFLUX); - if (isSecure) { - Log.notice(F("PUSH: InfluxDB, 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: InfluxDB server supports smaller SSL buffer." CR)); - _wifiSecure.setBufferSizes(512, 512); - } -#endif - - _httpSecure.begin(_wifiSecure, serverPath); - _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); - _lastCode = _httpSecure.POST(doc); - } else { - _http.begin(_wifi, serverPath); - _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); - _lastCode = _http.POST(doc); - } - #if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING) Log.verbose(F("PUSH: url %s." CR), serverPath.c_str()); Log.verbose(F("PUSH: data %s." CR), doc.c_str()); #endif String auth = "Token " + String(myConfig.getInfluxDb2PushToken()); - _http.addHeader(F("Authorization"), auth.c_str()); - _lastCode = _http.POST(doc); + + if (isSecure) { + Log.notice(F("PUSH: InfluxDB, SSL enabled without validation." CR)); + _wifiSecure.setInsecure(); + probeMaxFragement( serverPath ); + _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _httpSecure.begin(_wifiSecure, serverPath); + _httpSecure.addHeader(F("Authorization"), auth.c_str()); + _lastCode = _httpSecure.POST(doc); + } else { + _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _http.begin(_wifi, serverPath); + _http.addHeader(F("Authorization"), auth.c_str()); + _lastCode = _http.POST(doc); + } if (_lastCode == 204) { _lastSuccess = true; @@ -267,22 +284,9 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, 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); + probeMaxFragement( serverPath ); _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _httpSecure.begin(_wifiSecure, serverPath); if (index == 0) { addHttpHeader(_httpSecure, myConfig.getHttpHeader(0)); @@ -294,8 +298,8 @@ void PushTarget::sendHttpPost(TemplatingEngine& engine, bool isSecure, _lastCode = _httpSecure.POST(doc); } else { - _http.begin(_wifi, serverPath); _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _http.begin(_wifi, serverPath); if (index == 0) { addHttpHeader(_http, myConfig.getHttpHeader(0)); @@ -349,26 +353,13 @@ void PushTarget::sendHttpGet(TemplatingEngine& engine, bool isSecure) { 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); + probeMaxFragement( serverPath ); _httpSecure.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _httpSecure.begin(_wifiSecure, serverPath); _lastCode = _httpSecure.GET(); } else { - _http.begin(_wifi, serverPath); _http.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); + _http.begin(_wifi, serverPath); _lastCode = _http.GET(); } @@ -416,8 +407,10 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { } #endif + mqtt.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); mqtt.begin(host.c_str(), port, _wifiSecure); } else { + mqtt.setTimeout(myAdvancedConfig.getPushTimeout() * 1000); mqtt.begin(host.c_str(), port, _wifi); } @@ -429,9 +422,6 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) { Log.verbose(F("PUSH: data %s." CR), doc.c_str()); #endif - // Send MQQT message(s) - mqtt.setTimeout(myAdvancedConfig.getPushTimeout()); // 10 seconds timeout - int lines = 1; // Find out how many lines are in the document. Each line is one // topic/message. | is used as new line. diff --git a/src/pushtarget.hpp b/src/pushtarget.hpp index 6bfde2b..e657565 100644 --- a/src/pushtarget.hpp +++ b/src/pushtarget.hpp @@ -45,6 +45,7 @@ class PushTarget { void sendHttpPost(TemplatingEngine& engine, bool isSecure, int index); void sendHttpGet(TemplatingEngine& engine, bool isSecure); void addHttpHeader(HTTPClient& http, String header); + void probeMaxFragement( String& serverPath ); public: void sendAll(float angle, float gravitySG, float corrGravitySG, float tempC,