Format test using real data

This commit is contained in:
Magnus Persson 2022-03-13 10:37:21 +01:00
parent 2f8a324bfc
commit a2aaeb3f84
5 changed files with 70 additions and 30 deletions

View File

@ -147,38 +147,71 @@
// Test the calibration
$("#test-btn").click(function(e) {
var doc = $("#format").val();
doc = doc.replaceAll("${mdns}", "testing");
doc = doc.replaceAll("${id}", "e4a344");
doc = doc.replaceAll("${sleep-interval}", "300");
doc = doc.replaceAll("${temp}", "21.1");
doc = doc.replaceAll("${token}", "a-token");
doc = doc.replaceAll("${temp-c}", "21.1");
doc = doc.replaceAll("${temp-f}", "51.3");
doc = doc.replaceAll("${temp-unit}", "C");
doc = doc.replaceAll("${battery}", "3.86");
doc = doc.replaceAll("${rssi}", "-76");
doc = doc.replaceAll("${run-time}", "4.32");
doc = doc.replaceAll("${gravity}", "1.044");
doc = doc.replaceAll("${gravity-sg}", "1.044");
doc = doc.replaceAll("${gravity-plato}", "9.5");
doc = doc.replaceAll("${gravity-unit}", "G");
doc = doc.replaceAll("${corr-gravity}", "1.044");
doc = doc.replaceAll("${corr-gravity-sg}", "1.044");
doc = doc.replaceAll("${corr-gravity-plato}", "9.5");
doc = doc.replaceAll("${angle}", "54.5");
doc = doc.replaceAll("${tilt}", "54.5");
var url = "/api/status";
//var url = "/test/status.json";
$('#spinner').show();
$.getJSON(url, function (cfg) {
console.log( cfg );
// Format in a readable json string.
try {
var json = JSON.parse(doc);
doc = JSON.stringify(json, null, 2);
} catch(e) {
console.log("Not a javascript object!")
}
var doc = $("#format").val();
$("#preview").text(doc);
if (cfg["temp-format"]=="C")
doc = doc.replaceAll("${temp}", cfg["temp-c"]);
else
doc = doc.replaceAll("${temp}", cfg["temp-f"]);
if (cfg["gravity-format"]=="G") {
var sg = cfg["gravity"];
doc = doc.replaceAll("${gravity-sg}", sg);
doc = doc.replaceAll("${corr-gravity-sg}", sg);
var plato = 259 - (259 - sg);
doc = doc.replaceAll("${gravity-plato}", plato);
doc = doc.replaceAll("${corr-gravity-plato}", plato);
}
else {
var plato = cfg["gravity"];
doc = doc.replaceAll("${gravity-plato}", plato);
doc = doc.replaceAll("${corr-gravity-plato}", plato);
var sg = 259 / (259 - plato);
doc = doc.replaceAll("${gravity-sg}", sg);
doc = doc.replaceAll("${corr-gravity-sg}", sg);
}
doc = doc.replaceAll("${mdns}", cfg["mdns"]);
doc = doc.replaceAll("${id}", cfg["id"]);
doc = doc.replaceAll("${sleep-interval}", cfg["sleep-interval"]);
doc = doc.replaceAll("${token}", cfg["token"]);
doc = doc.replaceAll("${temp-c}", cfg["temp-c"]);
doc = doc.replaceAll("${temp-f}", cfg["temp-f"]);
doc = doc.replaceAll("${temp-unit}", cfg["temp-format"]);
doc = doc.replaceAll("${battery}", cfg["battery"]);
doc = doc.replaceAll("${rssi}", cfg["rssi"]);
doc = doc.replaceAll("${run-time}", cfg["runtime-average"]);
doc = doc.replaceAll("${gravity}", cfg["gravity"]);
doc = doc.replaceAll("${gravity-unit}", cfg["gravity-format"]);
doc = doc.replaceAll("${corr-gravity}", cfg["gravity"]);
doc = doc.replaceAll("${angle}", cfg["angle"]);
doc = doc.replaceAll("${tilt}", cfg["angle"]);
// Format in a readable json string.
try {
var json = JSON.parse(doc);
doc = JSON.stringify(json, null, 2);
} catch(e) {
console.log("Not a javascript object!")
}
$("#preview").text(doc);
})
.fail(function () {
showError('Unable to get data from the device.');
})
.always(function() {
$('#spinner').hide();
});
});
function setButtonDisabled( b ) {
$("#format-btn").prop("disabled", b);

View File

@ -1,2 +1,2 @@
<!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#collapseTwo">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">&times;</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 class="accordion" id="accordion"><div class="card"><div class="card-header" id="headingOne"><h2 class="mb-0"><button class="btn btn-link btn-block text-left" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Push Format Templates</button></h2></div><div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion"><div class="card-body"><input type="text" name="id" id="id" hidden> <input type="text" name="http-1" id="http-1" hidden> <input type="text" name="http-2" id="http-2" hidden><!--<input type="text" name="brewfather" id="brewfather" hidden>--> <input type="text" name="influxdb" id="influxdb" hidden> <input type="text" name="mqtt" id="mqtt" hidden><div class="form-group row"><label for="push-target" class="col-sm-2 col-form-label">Push target:</label> <select class="custom-select col-sm-4" required name="push-target" id="push-target"><option value="http-1">HTTP option 1</option><option value="http-2">HTTP option 2</option><!--<option value="brewfather">Brewfather</option>--><option value="influxdb">Influx DB</option><option value="mqtt">MQTT</option></select></div><div class="form-group row"><div class="col-sm-12"><textarea rows="5" class="form-control" name="format" id="format">
</textarea></div></div><div class="form-group row"><div class="col-sm-8 offset-sm-2"><button class="btn btn-primary" id="format-btn">Save</button> <button class="btn btn-secondary" id="test-btn">Test</button></div></div><pre class="card-preview" id="preview" name="preview"></pre></div></div></div><hr class="my-4"></div><script type="text/javascript">function setButtonDisabled(l){$("#format-btn").prop("disabled",l),$("#test-btn").prop("disabled",l)}function selectFormat(){var l="#"+$("#push-target").val();console.log(l),l=decodeURIComponent($(l).val()),console.log(l),l=l.replaceAll("|","|\n"),console.log(l),$("#format").val(l),$("#preview").text("")}function getConfig(){setButtonDisabled(!0);var l="/api/config/format";$("#spinner").show(),$.getJSON(l,function(l){console.log(l),$("#id").val(l.id),$("#http-1").val(l["http-1"]),$("#http-2").val(l["http-2"]),$("#influxdb").val(l.influxdb),$("#mqtt").val(l.mqtt),selectFormat()}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide(),setButtonDisabled(!1)})}window.onload=getConfig,setButtonDisabled(!0),$(document).ready(function(){null!=location.hash&&""!=location.hash&&($(".collapse").removeClass("in"),$(location.hash+".collapse").collapse("show"))}),$("#push-target").change(function(l){console.log(l),selectFormat()}),$("#format-btn").click(function(l){var e=$("#format").val();e=e.replaceAll("\n","");var t="id="+$("#id").val()+"&"+$("#push-target").val()+"="+encodeURIComponent(e);console.log(t),$.ajax({type:"POST",url:"/api/config/format",data:t,success:function(l){showSuccess("Format stored successfully."),getConfig()},error:function(l){showError("Unable to store format.")}})}),$("#test-btn").click(function(l){var e=$("#format").val();e=e.replaceAll("${mdns}","testing"),e=e.replaceAll("${id}","e4a344"),e=e.replaceAll("${sleep-interval}","300"),e=e.replaceAll("${temp}","21.1"),e=e.replaceAll("${token}","a-token"),e=e.replaceAll("${temp-c}","21.1"),e=e.replaceAll("${temp-f}","51.3"),e=e.replaceAll("${temp-unit}","C"),e=e.replaceAll("${battery}","3.86"),e=e.replaceAll("${rssi}","-76"),e=e.replaceAll("${run-time}","4.32"),e=e.replaceAll("${gravity}","1.044"),e=e.replaceAll("${gravity-sg}","1.044"),e=e.replaceAll("${gravity-plato}","9.5"),e=e.replaceAll("${gravity-unit}","G"),e=e.replaceAll("${corr-gravity}","1.044"),e=e.replaceAll("${corr-gravity-sg}","1.044"),e=e.replaceAll("${corr-gravity-plato}","9.5"),e=e.replaceAll("${angle}","54.5"),e=e.replaceAll("${tilt}","54.5");try{var t=JSON.parse(e);e=JSON.stringify(t,null,2)}catch(l){console.log("Not a javascript object!")}$("#preview").text(e)})</script><!-- START FOOTER --><div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021-22 Magnus Persson</div></div></body></html>
</textarea></div></div><div class="form-group row"><div class="col-sm-8 offset-sm-2"><button class="btn btn-primary" id="format-btn">Save</button> <button class="btn btn-secondary" id="test-btn">Test</button></div></div><pre class="card-preview" id="preview" name="preview"></pre></div></div></div><hr class="my-4"></div><script type="text/javascript">function setButtonDisabled(e){$("#format-btn").prop("disabled",e),$("#test-btn").prop("disabled",e)}function selectFormat(){var e="#"+$("#push-target").val();console.log(e),e=decodeURIComponent($(e).val()),console.log(e),e=e.replaceAll("|","|\n"),console.log(e),$("#format").val(e),$("#preview").text("")}function getConfig(){setButtonDisabled(!0);var e="/api/config/format";$("#spinner").show(),$.getJSON(e,function(e){console.log(e),$("#id").val(e.id),$("#http-1").val(e["http-1"]),$("#http-2").val(e["http-2"]),$("#influxdb").val(e.influxdb),$("#mqtt").val(e.mqtt),selectFormat()}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide(),setButtonDisabled(!1)})}window.onload=getConfig,setButtonDisabled(!0),$(document).ready(function(){null!=location.hash&&""!=location.hash&&($(".collapse").removeClass("in"),$(location.hash+".collapse").collapse("show"))}),$("#push-target").change(function(e){console.log(e),selectFormat()}),$("#format-btn").click(function(e){var l=$("#format").val();l=l.replaceAll("\n","");var t="id="+$("#id").val()+"&"+$("#push-target").val()+"="+encodeURIComponent(l);console.log(t),$.ajax({type:"POST",url:"/api/config/format",data:t,success:function(e){showSuccess("Format stored successfully."),getConfig()},error:function(e){showError("Unable to store format.")}})}),$("#test-btn").click(function(e){var l="/api/status";$("#spinner").show(),$.getJSON(l,function(e){console.log(e);var l=$("#format").val();if(l="C"==e["temp-format"]?l.replaceAll("${temp}",e["temp-c"]):l.replaceAll("${temp}",e["temp-f"]),"G"==e["gravity-format"]){var t=e.gravity;l=l.replaceAll("${gravity-sg}",t),l=l.replaceAll("${corr-gravity-sg}",t);var a=259-(259-t);l=l.replaceAll("${gravity-plato}",a),l=l.replaceAll("${corr-gravity-plato}",a)}else{a=e.gravity;l=l.replaceAll("${gravity-plato}",a),l=l.replaceAll("${corr-gravity-plato}",a);t=259/(259-a);l=l.replaceAll("${gravity-sg}",t),l=l.replaceAll("${corr-gravity-sg}",t)}l=l.replaceAll("${mdns}",e.mdns),l=l.replaceAll("${id}",e.id),l=l.replaceAll("${sleep-interval}",e["sleep-interval"]),l=l.replaceAll("${token}",e.token),l=l.replaceAll("${temp-c}",e["temp-c"]),l=l.replaceAll("${temp-f}",e["temp-f"]),l=l.replaceAll("${temp-unit}",e["temp-format"]),l=l.replaceAll("${battery}",e.battery),l=l.replaceAll("${rssi}",e.rssi),l=l.replaceAll("${run-time}",e["runtime-average"]),l=l.replaceAll("${gravity}",e.gravity),l=l.replaceAll("${gravity-unit}",e["gravity-format"]),l=l.replaceAll("${corr-gravity}",e.gravity),l=l.replaceAll("${angle}",e.angle),l=l.replaceAll("${tilt}",e.angle);try{var r=JSON.parse(l);l=JSON.stringify(r,null,2)}catch(e){console.log("Not a javascript object!")}$("#preview").text(l)}).fail(function(){showError("Unable to get data from the device.")}).always(function(){$("#spinner").hide()})})</script><!-- START FOOTER --><div class="container-fluid themed-container bg-primary text-light">(C) Copyright 2021-22 Magnus Persson</div></div></body></html>

View File

@ -283,6 +283,8 @@ void WebServerHandler::webHandleStatus() {
doc[PARAM_GRAVITY_FORMAT] = String(myConfig.getGravityFormat());
doc[PARAM_SLEEP_MODE] = sleepModeAlwaysSkip;
doc[PARAM_RSSI] = WiFi.RSSI();
doc[PARAM_SLEEP_INTERVAL] = myConfig.getSleepInterval();
doc[PARAM_TOKEN] = myConfig.getToken();
doc[PARAM_APP_VER] = CFG_APPVER;
doc[PARAM_MDNS] = myConfig.getMDNS();

View File

@ -102,9 +102,11 @@ Other parameters are the same as in the configuration guide.
"battery": 0,
"temp-format": "C",
"sleep-mode": false,
"token": "token",
"rssi": -56,
"app-ver": "0.0.0",
"mdns": "gravmon",
"sleep-interval": 30,
"platform": "esp8266",
"runtime-average": 3.12
}

View File

@ -8,6 +8,7 @@ v0.9.0
* Added function to test push targets from configuration page. It will send data and show the return code as a first step.
* Merged index and device pages into one so that all the needed information is available on the index page.
* Removed api for device (/api/device), it's now merged into the /api/status api.
* Test function in format editor now uses real data and not fake.
* Updated WifiManager and DoubleReset libraries
* Experimental release of firmware using an esp32 instead of esp8266
* Updated esp32 target with littlefs support
@ -15,6 +16,8 @@ v0.9.0
* Corrected PIN for voltage read on ESP32
* Mounted esp32 d1 mini mounted to a iSpindle PCB 4.0 (CherryPhilip) which worked fine.
* BUG: If using plato and not gravity formula was defined the value was set to null.
v0.8.0
------