Added test option for push targets
This commit is contained in:
parent
07e7cbee1c
commit
d9c467d54f
@ -277,6 +277,7 @@
|
||||
<div class="form-group row">
|
||||
<div class="col-sm-8 offset-sm-2">
|
||||
<button class="btn btn-info" id="format-btn">Format editor</button>
|
||||
<button class="btn btn-info" id="test-btn">Test Push</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -491,6 +492,10 @@ function checkHeader(input) {
|
||||
window.location.href = "/format.htm";
|
||||
});
|
||||
|
||||
$("#test-btn").click(function(e){
|
||||
window.location.href = "/test.htm";
|
||||
});
|
||||
|
||||
function estimateBatteryLife(interval) {
|
||||
// ESP8266 consumes between 140-170mA when WIFI is on. Deep sleep is 20uA.
|
||||
// MPU-6050 consumes 4mA
|
||||
|
File diff suppressed because one or more lines are too long
218
html/test.htm
Normal file
218
html/test.htm
Normal file
@ -0,0 +1,218 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<meta name="description" content="">
|
||||
<title>Beer Gravity Monitor</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body class="py-4">
|
||||
|
||||
<!-- START MENU -->
|
||||
|
||||
<nav class="navbar navbar-expand-sm navbar-dark bg-primary">
|
||||
|
||||
<a class="navbar-brand" href="/index.htm">Beer Gravity Monitor</a>
|
||||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
|
||||
<div class="collapse navbar-collapse" id="navbar">
|
||||
<ul class="navbar-nav mr-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="/config.htm">Back to configuration</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="spinner-border text-light" id="spinner" role="status"></div>
|
||||
</nav>
|
||||
|
||||
<!-- START MAIN INDEX -->
|
||||
|
||||
<div class="container">
|
||||
|
||||
<hr class="my-2">
|
||||
|
||||
<div class="alert alert-success alert-dismissible fade hide show d-none" role="alert" id="alert">
|
||||
<div id="alert-msg">...</div>
|
||||
<button type="button" id="alert-btn" class="close" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
function showError( msg ) {
|
||||
$('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('d-none').addClass('show');
|
||||
$('#alert-msg').text( msg );
|
||||
}
|
||||
|
||||
function showSuccess( msg ) {
|
||||
$('#alert').addClass('alert-success').removeClass('alert-danger').removeClass('d-none').addClass('show');
|
||||
$('#alert-msg').text( msg );
|
||||
}
|
||||
|
||||
$("#alert-btn").click(function(e){
|
||||
$('#alert').addClass('d-none').removeClass('show');
|
||||
});
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<pre class="card-preview" id="preview" name="preview"></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr class="my-4">
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
window.onload = getConfig;
|
||||
|
||||
function appendLog(log) {
|
||||
doc = $("#preview").text();
|
||||
doc += log + "\n";
|
||||
$("#preview").text(doc);
|
||||
}
|
||||
|
||||
// Get the configuration values from the API
|
||||
function getConfig() {
|
||||
appendLog( "Starting test of push targets" );
|
||||
|
||||
var url = "/api/device";
|
||||
//var url = "/test/device.json";
|
||||
$('#spinner').show();
|
||||
$.getJSON(url, function (cfg) {
|
||||
var id = cfg["id"];
|
||||
console.log( id );
|
||||
|
||||
appendLog( "Testing push target http-1" );
|
||||
testHttp( id, "http-1" );
|
||||
appendLog( "Testing push target http-2" );
|
||||
testHttp( id, "http-2" );
|
||||
appendLog( "Testing push target brewfather" );
|
||||
testHttp( id, "brewfather" );
|
||||
appendLog( "Testing push target influxdb" );
|
||||
testInfluxdb( id );
|
||||
appendLog( "Testing push target mqtt" );
|
||||
testMqtt( id );
|
||||
})
|
||||
.fail(function () {
|
||||
showError('Unable to get data from the device.');
|
||||
})
|
||||
.always(function() {
|
||||
$('#spinner').hide();
|
||||
});
|
||||
}
|
||||
|
||||
function testMqtt(id) {
|
||||
var url = "/api/test/push";
|
||||
url += "?id=" + id + "&format=mqtt";
|
||||
//var url = "/test/push.json";
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: url,
|
||||
async: false,
|
||||
success: function(cfg) {
|
||||
var code = cfg["code"];
|
||||
var success = cfg["success"];
|
||||
var enabled = cfg["enabled"];
|
||||
|
||||
if(!enabled) {
|
||||
appendLog( "Push target 'mqtt' is not configured/used" );
|
||||
} else if(success) {
|
||||
appendLog( "Push target 'mqtt' successful" );
|
||||
} else{
|
||||
if(code==-3)
|
||||
appendLog( "Push target 'mqtt' failed to connect" );
|
||||
else if(code==-4)
|
||||
appendLog( "Push target 'mqtt' failed with error timeout" );
|
||||
else if(code==-10)
|
||||
appendLog( "Push target 'mqtt' failed with error denied" );
|
||||
else
|
||||
appendLog( "Push target 'mqtt' failed with error code " + code );
|
||||
}
|
||||
}
|
||||
})
|
||||
.fail(function () {
|
||||
appendLog( "Failed to test push target 'influxdb'");
|
||||
})
|
||||
}
|
||||
|
||||
function testInfluxdb(id) {
|
||||
var url = "/api/test/push";
|
||||
url += "?id=" + id + "&format=influxdb";
|
||||
//var url = "/test/push.json";
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: url,
|
||||
async: false,
|
||||
success: function(cfg) {
|
||||
var code = cfg["code"];
|
||||
var success = cfg["success"];
|
||||
var enabled = cfg["enabled"];
|
||||
|
||||
if(!enabled) {
|
||||
appendLog( "Push target 'influxdb' is not configured/used" );
|
||||
} else if(success) {
|
||||
appendLog( "Push target 'influxdb' successful" );
|
||||
} else{
|
||||
if(code==400)
|
||||
appendLog( "Push target 'influxdb' failed with error code 400, bad request" );
|
||||
else if(code==401)
|
||||
appendLog( "Push target 'influxdb' failed with error code 401, unauthorized" );
|
||||
else if(code==404)
|
||||
appendLog( "Push target 'influxdb' failed with error code 404, url not found" );
|
||||
else
|
||||
appendLog( "Push target 'influxdb' failed with error code " + code );
|
||||
}
|
||||
}
|
||||
})
|
||||
.fail(function () {
|
||||
appendLog( "Failed to test push target 'influxdb'");
|
||||
})
|
||||
}
|
||||
|
||||
function testHttp(id, target) {
|
||||
var url = "/api/test/push";
|
||||
url += "?id=" + id + "&format=" + target;
|
||||
//var url = "/test/push.json";
|
||||
$.ajax({
|
||||
type: 'GET',
|
||||
url: url,
|
||||
async: false,
|
||||
success: function(cfg) {
|
||||
var code = cfg["code"];
|
||||
var success = cfg["success"];
|
||||
var enabled = cfg["enabled"];
|
||||
|
||||
if(!enabled) {
|
||||
appendLog( "Push target '" + target + "' is not configured/used" );
|
||||
} else if(success) {
|
||||
appendLog( "Push target '" + target + "' successful" );
|
||||
} else{
|
||||
if(code==400)
|
||||
appendLog( "Push target '" + target + "' failed with error code 400, bad request" );
|
||||
else if(code==401)
|
||||
appendLog( "Push target '" + target + "' failed with error code 401, unauthorized" );
|
||||
else if(code==404)
|
||||
appendLog( "Push target '" + target + "' failed with error code 404, url not found" );
|
||||
else
|
||||
appendLog( "Push target '" + target + "' failed with error code " + code );
|
||||
}
|
||||
}
|
||||
})
|
||||
.fail(function () {
|
||||
appendLog( "Failed to test push target '" + target + "'");
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- START FOOTER -->
|
||||
|
||||
<div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021-22 Magnus Persson</div>
|
||||
</body>
|
||||
</html>
|
1
html/test.min.htm
Normal file
1
html/test.min.htm
Normal file
@ -0,0 +1 @@
|
||||
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name="description" content=""><title>Beer Gravity Monitor</title><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous"><script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script></head><body class="py-4"><!-- START MENU --><nav class="navbar navbar-expand-sm navbar-dark bg-primary"><a class="navbar-brand" href="/index.htm">Beer Gravity Monitor</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navbar"><ul class="navbar-nav mr-auto"><li class="nav-item"><a class="nav-link" href="/config.htm">Back to configuration</a></li></ul></div><div class="spinner-border text-light" id="spinner" role="status"></div></nav><!-- START MAIN INDEX --><div class="container"><hr class="my-2"><div class="alert alert-success alert-dismissible fade hide show d-none" role="alert" id="alert"><div id="alert-msg">...</div><button type="button" id="alert-btn" class="close" aria-label="Close"><span aria-hidden="true">×</span></button></div><script type="text/javascript">function showError(s){$("#alert").removeClass("alert-success").addClass("alert-danger").removeClass("d-none").addClass("show"),$("#alert-msg").text(s)}function showSuccess(s){$("#alert").addClass("alert-success").removeClass("alert-danger").removeClass("d-none").addClass("show"),$("#alert-msg").text(s)}$("#alert-btn").click(function(s){$("#alert").addClass("d-none").removeClass("show")})</script><div><div class="card"><div class="card-body"><pre class="card-preview" id="preview" name="preview"></pre></div></div><hr class="my-4"></div><script type="text/javascript">function appendLog(t){doc=$("#preview").text(),doc+=t+"\n",$("#preview").text(doc)}function getConfig(){appendLog("Starting test of push targets");var t="/api/device";$("#spinner").show(),$.getJSON(t,function(t){var e=t.id;console.log(e),appendLog("Testing push target http-1"),testHttp(e,"http-1"),appendLog("Testing push target http-2"),testHttp(e,"http-2"),appendLog("Testing push target brewfather"),testHttp(e,"brewfather"),appendLog("Testing push target influxdb"),testInfluxdb(e),appendLog("Testing push target mqtt"),testMqtt(e)}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide()})}function testMqtt(t){var e="/api/test/push";e+="?id="+t+"&format=mqtt",$.ajax({type:"GET",url:e,async:!1,success:function(t){var e=t.code,a=t.success,r=t.enabled;appendLog(r?a?"Push target 'mqtt' successful":-3==e?"Push target 'mqtt' failed to connect":-4==e?"Push target 'mqtt' failed with error timeout":-10==e?"Push target 'mqtt' failed with error denied":"Push target 'mqtt' failed with error code "+e:"Push target 'mqtt' is not configured/used")}}).fail(function(){appendLog("Failed to test push target 'influxdb'")})}function testInfluxdb(t){var e="/api/test/push";e+="?id="+t+"&format=influxdb",$.ajax({type:"GET",url:e,async:!1,success:function(t){var e=t.code,a=t.success,r=t.enabled;appendLog(r?a?"Push target 'influxdb' successful":400==e?"Push target 'influxdb' failed with error code 400, bad request":401==e?"Push target 'influxdb' failed with error code 401, unauthorized":404==e?"Push target 'influxdb' failed with error code 404, url not found":"Push target 'influxdb' failed with error code "+e:"Push target 'influxdb' is not configured/used")}}).fail(function(){appendLog("Failed to test push target 'influxdb'")})}function testHttp(t,s){var e="/api/test/push";e+="?id="+t+"&format="+s,$.ajax({type:"GET",url:e,async:!1,success:function(t){var e=t.code,a=t.success,r=t.enabled;appendLog(r?a?"Push target '"+s+"' successful":400==e?"Push target '"+s+"' failed with error code 400, bad request":401==e?"Push target '"+s+"' failed with error code 401, unauthorized":404==e?"Push target '"+s+"' failed with error code 404, url not found":"Push target '"+s+"' failed with error code "+e:"Push target '"+s+"' is not configured/used")}}).fail(function(){appendLog("Failed to test push target '"+s+"'")})}window.onload=getConfig</script><!-- START FOOTER --><div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021-22 Magnus Persson</div></div></body></html>
|
@ -82,6 +82,10 @@
|
||||
<div class="col-md-2 themed-grid-col bg-light">format.min.htm</div>
|
||||
<div class="col-md-6 themed-grid-col bg-light" id="format">Checking...</div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-2 themed-grid-col bg-light">test.min.htm</div>
|
||||
<div class="col-md-6 themed-grid-col bg-light" id="test">Checking...</div>
|
||||
</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-md-2 themed-grid-col bg-light">about.min.htm</div>
|
||||
<div class="col-md-6 themed-grid-col bg-light" id="about">Checking...</div>
|
||||
@ -131,11 +135,16 @@ function getUpload() {
|
||||
else
|
||||
$("#config").text("File is missing.");
|
||||
|
||||
if( cfg["calibration"] )
|
||||
if( cfg["calibration"] )
|
||||
$("#calibration").text("Completed.");
|
||||
else
|
||||
$("#calibration").text("File is missing.");
|
||||
|
||||
if( cfg["test"] )
|
||||
$("#test").text("Completed.");
|
||||
else
|
||||
$("#test").text("File is missing.");
|
||||
|
||||
if( cfg["format"] )
|
||||
$("#format").text("Completed.");
|
||||
else
|
||||
|
File diff suppressed because one or more lines are too long
@ -29,3 +29,6 @@ shutil.copyfile( source + file, target + file )
|
||||
file = "format.min.htm"
|
||||
#print( "Copy file: " + source + file + "->" + target + file)
|
||||
shutil.copyfile( source + file, target + file )
|
||||
file = "test.min.htm"
|
||||
#print( "Copy file: " + source + file + "->" + target + file)
|
||||
shutil.copyfile( source + file, target + file )
|
||||
|
@ -48,13 +48,19 @@ def after_build(source, target, env):
|
||||
print( "Copy file : " + source + " -> " + target )
|
||||
shutil.copyfile( source, target )
|
||||
|
||||
# Copy file 7
|
||||
source = dir + "/data/test.min.htm"
|
||||
target = dir + "/bin/test.min.htm"
|
||||
print( "Copy file : " + source + " -> " + target )
|
||||
shutil.copyfile( source, target )
|
||||
|
||||
target = dir + "/bin/version.json"
|
||||
ver = get_build_flag_value("CFG_APPVER")
|
||||
|
||||
print( "Creating version.json" )
|
||||
f = open( target, "w" )
|
||||
f.write( "{ \"project\":\"gravmon\", \"version\":" + ver + ", " )
|
||||
f.write( " \"html\": [ \"index.min.htm\", \"device.min.htm\", \"config.min.htm\", \"calibration.min.htm\", \"format.min.htm\", \"about.min.htm\" ] }" )
|
||||
f.write( " \"html\": [ \"index.min.htm\", \"device.min.htm\", \"config.min.htm\", \"calibration.min.htm\", \"test.min.htm\", \"format.min.htm\", \"about.min.htm\" ] }" )
|
||||
f.close()
|
||||
|
||||
|
||||
|
@ -264,7 +264,7 @@ bool loopReadGravity() {
|
||||
pushMillis = millis();
|
||||
LOG_PERF_START("loop-push");
|
||||
PushTarget push;
|
||||
push.send(angle, gravitySG, corrGravitySG, tempC,
|
||||
push.sendAll(angle, gravitySG, corrGravitySG, tempC,
|
||||
(millis() - runtimeMillis) / 1000);
|
||||
LOG_PERF_STOP("loop-push");
|
||||
// Send stats to influx after each push run.
|
||||
|
@ -35,11 +35,11 @@ SOFTWARE.
|
||||
//
|
||||
// Send the data to targets
|
||||
//
|
||||
void PushTarget::send(float angle, float gravitySG, float corrGravitySG,
|
||||
void PushTarget::sendAll(float angle, float gravitySG, float corrGravitySG,
|
||||
float tempC, float runTime) {
|
||||
printHeap("PUSH");
|
||||
http.setReuse(false);
|
||||
httpSecure.setReuse(false);
|
||||
_http.setReuse(false);
|
||||
_httpSecure.setReuse(false);
|
||||
|
||||
TemplatingEngine engine;
|
||||
engine.initialize(angle, gravitySG, corrGravitySG, tempC, runTime);
|
||||
@ -82,6 +82,8 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine) {
|
||||
#if !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.notice(F("PUSH: Sending values to influxdb2." CR));
|
||||
#endif
|
||||
_lastCode = 0;
|
||||
_lastSuccess = false;
|
||||
|
||||
String serverPath =
|
||||
String(myConfig.getInfluxDb2PushUrl()) +
|
||||
@ -89,8 +91,8 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine) {
|
||||
"&bucket=" + String(myConfig.getInfluxDb2PushBucket());
|
||||
String doc = engine.create(TemplatingEngine::TEMPLATE_INFLUX);
|
||||
|
||||
http.begin(wifi, serverPath);
|
||||
http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
_http.begin(_wifi, serverPath);
|
||||
_http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
|
||||
#if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.verbose(F("PUSH: url %s." CR), serverPath.c_str());
|
||||
@ -98,20 +100,20 @@ void PushTarget::sendInfluxDb2(TemplatingEngine& engine) {
|
||||
#endif
|
||||
|
||||
String auth = "Token " + String(myConfig.getInfluxDb2PushToken());
|
||||
http.addHeader(F("Authorization"), auth.c_str());
|
||||
int httpResponseCode = http.POST(doc);
|
||||
_http.addHeader(F("Authorization"), auth.c_str());
|
||||
_lastCode = _http.POST(doc);
|
||||
|
||||
if (httpResponseCode == 204) {
|
||||
if (_lastCode == 204) {
|
||||
Log.notice(F("PUSH: InfluxDB2 push successful, response=%d" CR),
|
||||
httpResponseCode);
|
||||
_lastCode);
|
||||
} else {
|
||||
ErrorFileLog errLog;
|
||||
errLog.addEntry("PUSH: Influxdb push failed response=" +
|
||||
String(httpResponseCode));
|
||||
String(_lastCode));
|
||||
}
|
||||
|
||||
http.end();
|
||||
wifi.stop();
|
||||
_http.end();
|
||||
_wifi.stop();
|
||||
tcp_cleanup();
|
||||
}
|
||||
|
||||
@ -122,32 +124,35 @@ void PushTarget::sendBrewfather(TemplatingEngine& engine) {
|
||||
#if !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.notice(F("PUSH: Sending values to brewfather" CR));
|
||||
#endif
|
||||
_lastCode = 0;
|
||||
_lastSuccess = false;
|
||||
|
||||
String serverPath = myConfig.getBrewfatherPushUrl();
|
||||
String doc = engine.create(TemplatingEngine::TEMPLATE_BREWFATHER);
|
||||
|
||||
http.begin(wifi, serverPath);
|
||||
http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
_http.begin(_wifi, serverPath);
|
||||
_http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
|
||||
#if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.verbose(F("PUSH: url %s." CR), serverPath.c_str());
|
||||
Log.verbose(F("PUSH: json %s." CR), doc.c_str());
|
||||
#endif
|
||||
|
||||
http.addHeader(F("Content-Type"), F("application/json"));
|
||||
int httpResponseCode = http.POST(doc);
|
||||
_http.addHeader(F("Content-Type"), F("application/json"));
|
||||
_lastCode = _http.POST(doc);
|
||||
|
||||
if (httpResponseCode == 200) {
|
||||
if (_lastCode == 200) {
|
||||
_lastSuccess = true;
|
||||
Log.notice(F("PUSH: Brewfather push successful, response=%d" CR),
|
||||
httpResponseCode);
|
||||
_lastCode);
|
||||
} else {
|
||||
ErrorFileLog errLog;
|
||||
errLog.addEntry("PUSH: Brewfather push failed response=" +
|
||||
String(httpResponseCode));
|
||||
String(_lastCode));
|
||||
}
|
||||
|
||||
http.end();
|
||||
wifi.stop();
|
||||
_http.end();
|
||||
_wifi.stop();
|
||||
tcp_cleanup();
|
||||
}
|
||||
|
||||
@ -179,6 +184,9 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
|
||||
Log.notice(F("PUSH: Sending values to http (%s)" CR),
|
||||
index ? "http2" : "http");
|
||||
#endif
|
||||
_lastCode = 0;
|
||||
_lastSuccess = false;
|
||||
|
||||
String serverPath, doc;
|
||||
|
||||
if (index == 0) {
|
||||
@ -189,8 +197,6 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
|
||||
doc = engine.create(TemplatingEngine::TEMPLATE_HTTP2);
|
||||
}
|
||||
|
||||
int httpResponseCode;
|
||||
|
||||
#if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.verbose(F("PUSH: url %s." CR), serverPath.c_str());
|
||||
Log.verbose(F("PUSH: json %s." CR), doc.c_str());
|
||||
@ -198,7 +204,7 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
|
||||
|
||||
if (isSecure) {
|
||||
Log.notice(F("PUSH: HTTP, SSL enabled without validation." CR));
|
||||
wifiSecure.setInsecure();
|
||||
_wifiSecure.setInsecure();
|
||||
|
||||
#if defined (ESP8266)
|
||||
String host = serverPath.substring(8); // remove the prefix or the probe will fail, it needs a pure host name.
|
||||
@ -206,55 +212,56 @@ void PushTarget::sendHttp(TemplatingEngine& engine, bool isSecure, int index) {
|
||||
if (idx!=-1)
|
||||
host = host.substring(0, idx);
|
||||
|
||||
if (wifiSecure.probeMaxFragmentLength(host, 443, 512)) {
|
||||
if (_wifiSecure.probeMaxFragmentLength(host, 443, 512)) {
|
||||
Log.notice(F("PUSH: HTTP server supports smaller SSL buffer." CR));
|
||||
wifiSecure.setBufferSizes(512, 512);
|
||||
_wifiSecure.setBufferSizes(512, 512);
|
||||
}
|
||||
#endif
|
||||
|
||||
httpSecure.begin(wifiSecure, serverPath);
|
||||
httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
_httpSecure.begin(_wifiSecure, serverPath);
|
||||
_httpSecure.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
|
||||
if (index == 0) {
|
||||
addHttpHeader(httpSecure, myConfig.getHttpHeader(0));
|
||||
addHttpHeader(httpSecure, myConfig.getHttpHeader(1));
|
||||
addHttpHeader(_httpSecure, myConfig.getHttpHeader(0));
|
||||
addHttpHeader(_httpSecure, myConfig.getHttpHeader(1));
|
||||
} else {
|
||||
addHttpHeader(httpSecure, myConfig.getHttp2Header(0));
|
||||
addHttpHeader(httpSecure, myConfig.getHttp2Header(1));
|
||||
addHttpHeader(_httpSecure, myConfig.getHttp2Header(0));
|
||||
addHttpHeader(_httpSecure, myConfig.getHttp2Header(1));
|
||||
}
|
||||
|
||||
httpResponseCode = httpSecure.POST(doc);
|
||||
_lastCode = _httpSecure.POST(doc);
|
||||
} else {
|
||||
http.begin(wifi, serverPath);
|
||||
http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
_http.begin(_wifi, serverPath);
|
||||
_http.setTimeout(myHardwareConfig.getPushTimeout() * 1000);
|
||||
|
||||
if (index == 0) {
|
||||
addHttpHeader(http, myConfig.getHttpHeader(0));
|
||||
addHttpHeader(http, myConfig.getHttpHeader(1));
|
||||
addHttpHeader(_http, myConfig.getHttpHeader(0));
|
||||
addHttpHeader(_http, myConfig.getHttpHeader(1));
|
||||
} else {
|
||||
addHttpHeader(http, myConfig.getHttp2Header(0));
|
||||
addHttpHeader(http, myConfig.getHttp2Header(1));
|
||||
addHttpHeader(_http, myConfig.getHttp2Header(0));
|
||||
addHttpHeader(_http, myConfig.getHttp2Header(1));
|
||||
}
|
||||
|
||||
httpResponseCode = http.POST(doc);
|
||||
_lastCode = _http.POST(doc);
|
||||
}
|
||||
|
||||
if (httpResponseCode == 200) {
|
||||
if (_lastCode == 200) {
|
||||
_lastSuccess = true;
|
||||
Log.notice(F("PUSH: HTTP push successful, response=%d" CR),
|
||||
httpResponseCode);
|
||||
_lastCode);
|
||||
} else {
|
||||
ErrorFileLog errLog;
|
||||
errLog.addEntry(
|
||||
"PUSH: HTTP push failed response=" + String(httpResponseCode) +
|
||||
"PUSH: HTTP push failed response=" + String(_lastCode) +
|
||||
String(index == 0 ? " (http)" : " (http2)"));
|
||||
}
|
||||
|
||||
if (isSecure) {
|
||||
httpSecure.end();
|
||||
wifiSecure.stop();
|
||||
_httpSecure.end();
|
||||
_wifiSecure.stop();
|
||||
} else {
|
||||
http.end();
|
||||
wifi.stop();
|
||||
_http.end();
|
||||
_wifi.stop();
|
||||
}
|
||||
tcp_cleanup();
|
||||
}
|
||||
@ -266,6 +273,8 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
|
||||
#if !defined(PUSH_DISABLE_LOGGING)
|
||||
Log.notice(F("PUSH: Sending values to mqtt." CR));
|
||||
#endif
|
||||
_lastCode = 0;
|
||||
_lastSuccess = false;
|
||||
|
||||
MQTTClient mqtt(512);
|
||||
String host = myConfig.getMqttUrl();
|
||||
@ -274,18 +283,18 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
|
||||
|
||||
if (myConfig.isMqttSSL()) {
|
||||
Log.notice(F("PUSH: MQTT, SSL enabled without validation." CR));
|
||||
wifiSecure.setInsecure();
|
||||
_wifiSecure.setInsecure();
|
||||
|
||||
#if defined (ESP8266)
|
||||
if (wifiSecure.probeMaxFragmentLength(host, port, 512)) {
|
||||
if (_wifiSecure.probeMaxFragmentLength(host, port, 512)) {
|
||||
Log.notice(F("PUSH: MQTT server supports smaller SSL buffer." CR));
|
||||
wifiSecure.setBufferSizes(512, 512);
|
||||
_wifiSecure.setBufferSizes(512, 512);
|
||||
}
|
||||
#endif
|
||||
|
||||
mqtt.begin(host.c_str(), port, wifiSecure);
|
||||
mqtt.begin(host.c_str(), port, _wifiSecure);
|
||||
} else {
|
||||
mqtt.begin(host.c_str(), port, wifi);
|
||||
mqtt.begin(host.c_str(), port, _wifi);
|
||||
}
|
||||
|
||||
mqtt.connect(myConfig.getMDNS(), myConfig.getMqttUser(),
|
||||
@ -319,8 +328,11 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
|
||||
value.c_str());
|
||||
#endif
|
||||
if (mqtt.publish(topic, value)) {
|
||||
_lastSuccess = true;
|
||||
Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str());
|
||||
_lastCode = 0;
|
||||
} else {
|
||||
_lastCode = mqtt.lastError();
|
||||
ErrorFileLog errLog;
|
||||
errLog.addEntry("PUSH: MQTT push on " + topic +
|
||||
" failed error=" + String(mqtt.lastError()));
|
||||
@ -332,9 +344,9 @@ void PushTarget::sendMqtt(TemplatingEngine& engine, bool isSecure) {
|
||||
|
||||
mqtt.disconnect();
|
||||
if (isSecure) {
|
||||
wifiSecure.stop();
|
||||
_wifiSecure.stop();
|
||||
} else {
|
||||
wifi.stop();
|
||||
_wifi.stop();
|
||||
}
|
||||
tcp_cleanup();
|
||||
}
|
||||
|
@ -35,20 +35,27 @@ SOFTWARE.
|
||||
|
||||
class PushTarget {
|
||||
private:
|
||||
WiFiClient wifi;
|
||||
WiFiClientSecure wifiSecure;
|
||||
HTTPClient http;
|
||||
HTTPClient httpSecure;
|
||||
WiFiClient _wifi;
|
||||
WiFiClientSecure _wifiSecure;
|
||||
HTTPClient _http;
|
||||
HTTPClient _httpSecure;
|
||||
int _lastCode;
|
||||
bool _lastSuccess;
|
||||
|
||||
void sendBrewfather(TemplatingEngine& engine);
|
||||
void sendHttp(TemplatingEngine& engine, bool isSecure, int index);
|
||||
void sendInfluxDb2(TemplatingEngine& engine);
|
||||
void sendMqtt(TemplatingEngine& engine, bool isSecure);
|
||||
void addHttpHeader(HTTPClient& http, String header);
|
||||
|
||||
public:
|
||||
void send(float angle, float gravitySG, float corrGravitySG, float tempC,
|
||||
void sendAll(float angle, float gravitySG, float corrGravitySG, float tempC,
|
||||
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 sendInfluxDb2(TemplatingEngine& engine);
|
||||
void sendMqtt(TemplatingEngine& engine, bool isSecure);
|
||||
int getLastCode() { return _lastCode; }
|
||||
bool getLastSuccess() { return _lastSuccess; }
|
||||
};
|
||||
|
||||
#endif // SRC_PUSHTARGET_HPP_
|
||||
|
@ -35,6 +35,7 @@ INCBIN(DeviceHtm, "data/device.min.htm");
|
||||
INCBIN(ConfigHtm, "data/config.min.htm");
|
||||
INCBIN(CalibrationHtm, "data/calibration.min.htm");
|
||||
INCBIN(FormatHtm, "data/format.min.htm");
|
||||
INCBIN(TestHtm, "data/test.min.htm");
|
||||
INCBIN(AboutHtm, "data/about.min.htm");
|
||||
#else
|
||||
// Minium web interface for uploading htm files
|
||||
|
@ -85,5 +85,9 @@ SOFTWARE.
|
||||
#define PARAM_FORMAT_BREWFATHER "brewfather"
|
||||
#define PARAM_FORMAT_INFLUXDB "influxdb"
|
||||
#define PARAM_FORMAT_MQTT "mqtt"
|
||||
#define PARAM_PUSH_FORMAT "format"
|
||||
#define PARAM_PUSH_SUCCESS "success"
|
||||
#define PARAM_PUSH_CODE "code"
|
||||
#define PARAM_PUSH_ENABLED "enabled"
|
||||
|
||||
#endif // SRC_RESOURCES_HPP_
|
||||
|
@ -29,6 +29,7 @@ SOFTWARE.
|
||||
#include <resources.hpp>
|
||||
#include <templating.hpp>
|
||||
#include <tempsensor.hpp>
|
||||
#include <pushtarget.hpp>
|
||||
#include <webserver.hpp>
|
||||
#include <wifi.hpp>
|
||||
|
||||
@ -144,6 +145,7 @@ void WebServerHandler::webHandleUpload() {
|
||||
doc["calibration"] = checkHtmlFile(WebServerHandler::HTML_CALIBRATION);
|
||||
doc["format"] = checkHtmlFile(WebServerHandler::HTML_FORMAT);
|
||||
doc["about"] = checkHtmlFile(WebServerHandler::HTML_ABOUT);
|
||||
doc["test"] = checkHtmlFile(WebServerHandler::HTML_TEST);
|
||||
|
||||
#if defined(ESP8266)
|
||||
JsonArray files = doc.createNestedArray(PARAM_FILES);
|
||||
@ -748,6 +750,67 @@ void WebServerHandler::webHandleConfigFormatWrite() {
|
||||
LOG_PERF_STOP("webserver-api-config-format-write");
|
||||
}
|
||||
|
||||
//
|
||||
// Get format with real data
|
||||
//
|
||||
void WebServerHandler::webHandleTestPush() {
|
||||
LOG_PERF_START("webserver-api-test-push");
|
||||
String id = _server->arg(PARAM_ID);
|
||||
Log.notice(F("WEB : webServer callback for /api/test/push." CR));
|
||||
|
||||
if (!id.equalsIgnoreCase(myConfig.getID())) {
|
||||
Log.error(F("WEB : Wrong ID received %s, expected %s" CR), id.c_str(),
|
||||
myConfig.getID());
|
||||
_server->send(400, "text/plain", "Invalid ID.");
|
||||
LOG_PERF_STOP("webserver-api-test-push");
|
||||
return;
|
||||
}
|
||||
|
||||
#if LOG_LEVEL == 6 && !defined(WEB_DISABLE_LOGGING)
|
||||
Log.verbose(F("WEB : %s." CR), getRequestArguments().c_str());
|
||||
#endif
|
||||
|
||||
float angle = myGyro.getAngle();
|
||||
float tempC = myTempSensor.getTempC(myConfig.isGyroTemp());
|
||||
float gravitySG = calculateGravity(angle, tempC);
|
||||
float corrGravitySG = gravityTemperatureCorrectionC(gravitySG, tempC);
|
||||
|
||||
TemplatingEngine engine;
|
||||
engine.initialize(angle, gravitySG, corrGravitySG, tempC, 2.1);
|
||||
|
||||
const String& type = _server->arg(PARAM_PUSH_FORMAT);
|
||||
PushTarget push;
|
||||
bool enabled = false;
|
||||
|
||||
if (!type.compareTo(PARAM_FORMAT_BREWFATHER) && myConfig.isBrewfatherActive()) {
|
||||
push.sendBrewfather(engine);
|
||||
enabled = true;
|
||||
} else if (!type.compareTo(PARAM_FORMAT_HTTP1) && myConfig.isHttpActive()) {
|
||||
push.sendHttp1(engine, myConfig.isHttpSSL());
|
||||
enabled = true;
|
||||
} else if (!type.compareTo(PARAM_FORMAT_HTTP2) && myConfig.isHttp2Active()) {
|
||||
push.sendHttp2(engine, myConfig.isHttp2SSL());
|
||||
enabled = true;
|
||||
} else if (!type.compareTo(PARAM_FORMAT_INFLUXDB) && myConfig.isInfluxDb2Active()) {
|
||||
push.sendInfluxDb2(engine);
|
||||
enabled = true;
|
||||
} else if (!type.compareTo(PARAM_FORMAT_MQTT) && myConfig.isMqttActive()) {
|
||||
push.sendMqtt(engine, myConfig.isMqttSSL());
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(20);
|
||||
doc[PARAM_PUSH_ENABLED] = enabled;
|
||||
doc[PARAM_PUSH_SUCCESS] = push.getLastSuccess();
|
||||
doc[PARAM_PUSH_CODE] = push.getLastCode();
|
||||
|
||||
String out;
|
||||
out.reserve(50);
|
||||
serializeJson(doc, out);
|
||||
_server->send(200, "application/json", out.c_str());
|
||||
LOG_PERF_STOP("webserver-api-test-push");
|
||||
}
|
||||
|
||||
//
|
||||
// Write file to disk, if there is no data then delete the current file (if it
|
||||
// exists) = reset to default.
|
||||
@ -944,6 +1007,8 @@ const char* WebServerHandler::getHtmlFileName(HtmlFile item) {
|
||||
return "format.min.htm";
|
||||
case HTML_ABOUT:
|
||||
return "about.min.htm";
|
||||
case HTML_TEST:
|
||||
return "test.min.htm";
|
||||
}
|
||||
|
||||
return "";
|
||||
@ -999,6 +1064,8 @@ bool WebServerHandler::setupWebServer() {
|
||||
std::bind(&WebServerHandler::webReturnFormatHtm, this));
|
||||
_server->on("/about.htm",
|
||||
std::bind(&WebServerHandler::webReturnAboutHtm, this));
|
||||
_server->on("/test.htm",
|
||||
std::bind(&WebServerHandler::webReturnTestHtm, this));
|
||||
#else
|
||||
// Show files in the filessytem at startup
|
||||
|
||||
@ -1016,7 +1083,7 @@ bool WebServerHandler::setupWebServer() {
|
||||
// upload page.
|
||||
if (checkHtmlFile(HTML_INDEX) && checkHtmlFile(HTML_DEVICE) &&
|
||||
checkHtmlFile(HTML_CONFIG) && checkHtmlFile(HTML_CALIBRATION) &&
|
||||
checkHtmlFile(HTML_FORMAT) && checkHtmlFile(HTML_ABOUT)) {
|
||||
checkHtmlFile(HTML_FORMAT) && checkHtmlFile(HTML_ABOUT) && checkHtmlFile(HTML_TEST) ) {
|
||||
Log.notice(F("WEB : All html files exist, starting in normal mode." CR));
|
||||
|
||||
_server->serveStatic("/", LittleFS, "/index.min.htm");
|
||||
@ -1024,6 +1091,7 @@ bool WebServerHandler::setupWebServer() {
|
||||
_server->serveStatic("/device.htm", LittleFS, "/device.min.htm");
|
||||
_server->serveStatic("/config.htm", LittleFS, "/config.min.htm");
|
||||
_server->serveStatic("/about.htm", LittleFS, "/about.min.htm");
|
||||
_server->serveStatic("/test.htm", LittleFS, "/test.min.htm");
|
||||
_server->serveStatic("/calibration.htm", LittleFS, "/calibration.min.htm");
|
||||
_server->serveStatic("/format.htm", LittleFS, "/format.min.htm");
|
||||
|
||||
@ -1096,6 +1164,9 @@ bool WebServerHandler::setupWebServer() {
|
||||
_server->on("/api/device/param", HTTP_GET,
|
||||
std::bind(&WebServerHandler::webHandleDeviceParam,
|
||||
this)); // Change device params
|
||||
_server->on("/api/test/push", HTTP_GET,
|
||||
std::bind(&WebServerHandler::webHandleTestPush,
|
||||
this)); //
|
||||
|
||||
_server->onNotFound(
|
||||
std::bind(&WebServerHandler::webHandlePageNotFound, this));
|
||||
|
@ -41,6 +41,7 @@ INCBIN_EXTERN(DeviceHtm);
|
||||
INCBIN_EXTERN(ConfigHtm);
|
||||
INCBIN_EXTERN(CalibrationHtm);
|
||||
INCBIN_EXTERN(FormatHtm);
|
||||
INCBIN_EXTERN(TestHtm);
|
||||
INCBIN_EXTERN(AboutHtm);
|
||||
#else
|
||||
INCBIN_EXTERN(UploadHtm);
|
||||
@ -61,6 +62,7 @@ class WebServerHandler {
|
||||
void webHandleConfigDevice();
|
||||
void webHandleConfigFormatRead();
|
||||
void webHandleConfigFormatWrite();
|
||||
void webHandleTestPush();
|
||||
void webHandleStatusSleepmode();
|
||||
void webHandleClearWIFI();
|
||||
void webHandleStatus();
|
||||
@ -104,6 +106,10 @@ class WebServerHandler {
|
||||
_server->send_P(200, "text/html", (const char*)gAboutHtmData,
|
||||
gAboutHtmSize);
|
||||
}
|
||||
void webReturnTestHtm() {
|
||||
_server->send_P(200, "text/html", (const char*)gTestHtmData,
|
||||
gTestHtmSize);
|
||||
}
|
||||
#else
|
||||
void webReturnUploadHtm() {
|
||||
_server->send_P(200, "text/html", (const char*)gUploadHtmData,
|
||||
@ -118,7 +124,8 @@ class WebServerHandler {
|
||||
HTML_CONFIG = 2,
|
||||
HTML_ABOUT = 3,
|
||||
HTML_CALIBRATION = 4,
|
||||
HTML_FORMAT = 5
|
||||
HTML_FORMAT = 5,
|
||||
HTML_TEST = 6
|
||||
};
|
||||
|
||||
bool setupWebServer();
|
||||
|
5
test/push.json
Normal file
5
test/push.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"success": false,
|
||||
"enabled": true,
|
||||
"code": -3
|
||||
}
|
@ -4,5 +4,6 @@
|
||||
"config": false,
|
||||
"calibration": false,
|
||||
"format": false,
|
||||
"test": false,
|
||||
"about": true
|
||||
}
|
Loading…
Reference in New Issue
Block a user