Fixes for new tpl engine, optimize memory.

This commit is contained in:
Magnus Persson 2022-08-05 09:38:10 +02:00
parent 45294f6b07
commit 68d44a5846
6 changed files with 63 additions and 18 deletions

View File

@ -64,6 +64,11 @@
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<div class="alert alert-warning alert-dismissible hide fade d-none" role="alert" id="warning-length">
<div>The format template is quite large, its possible the device will not have enough memory to handle it.</div>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<script type="text/javascript">
function showError( msg ) {
$('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
@ -85,6 +90,13 @@
function hideWarningHomeAssistant() {
$('#warning-ha').addClass('d-none').removeClass('show').addClass('hide');
}
function showWarningLength() {
$('#warning-length').removeClass('d-none').addClass('show').removeClass('hide');
}
function hideWarningLength() {
$('#warning-length').addClass('d-none').removeClass('show').addClass('hide');
}
</script>
<div class="accordion" id="accordion">
@ -117,7 +129,7 @@
<div class="row mb-3">
<div class="col-sm-12">
<textarea rows="10" class="form-control" name="format" id="format">
<textarea rows="10" class="form-control" name="format" id="format" onchange="updateStatusField()" onkeydown="updateStatusField()">
</textarea>
</div>
</div>
@ -158,6 +170,9 @@
</div>
</div>
<div class="col-sm-8" id="status">
</div>
<hr class="my-2">
<pre class="card-preview" id="preview" name="preview"></pre>
@ -171,6 +186,8 @@
<script type="text/javascript">
window.onload = getConfig;
var maxCharsInFormatTemplate = 1500;
setButtonDisabled( true );
// Opens the targetet according (if URL has #collapseOne to #collapseFour)
@ -180,16 +197,17 @@
$(location.hash + '.collapse').collapse('show');
}
});
/*
$("#format-btn").click(function(e){
console.log(e)
var s = $("#push-target").val()
if (s == "mqtt") {
console.log("Current format is mqtt, checking for HA device registration.")
function updateStatusField() {
var t = $("#format").val();
$("#status").text( t.length + " characters in template, recommended maximum is " + maxCharsInFormatTemplate );
if (t.length > maxCharsInFormatTemplate) {
showWarningLength();
} else {
hideWarningLength();
}
});*/
};
$("#push-target").change(function(e){
console.log(e)
@ -207,21 +225,31 @@
$("#format").val( decodeURIComponent(item.format) );
}
});
updateStatusField();
});
// Clear the selected template
$("#clear-btn").click(function(e) {
$("#format").val( "" );
updateStatusField();
});
// Store the format
$("#format-btn").click(function(e) {
var s = $("#format").val();
/*if (s.length > maxCharsInFormatTemplate) {
showError("Format template is too large for the device to handle, unable to save.")
return;
}*/
hideError();
$('#spinner').show();
setButtonDisabled( true );
var ha = false;
var s = $("#format").val();
var ha = false;
hideWarningHomeAssistant();
if ($("#push-target").val() == "mqtt") {
if (s.search("homeassistant/sensor/") != -1) {
@ -356,6 +384,7 @@
$("#test-btn").prop("disabled", b);
$("#copy-btn").prop("disabled", b);
$("#clear-btn").prop("disabled", b);
$("#push-target").prop("disabled", b);
}
function selectFormat() {
@ -367,6 +396,7 @@
console.log(s);
$("#format").val(s);
$("#preview").text("");
updateStatusField();
}
// Get the configuration values from the API
@ -374,7 +404,7 @@
setButtonDisabled( true );
var url = "/api/config/format";
//var url = "/test/format.json";
// var url = "/test/format.json";
$('#spinner').show();
$.getJSON(url, function (cfg) {
console.log( cfg );
@ -394,6 +424,8 @@
$('#spinner').hide();
setButtonDisabled( false );
});
updateStatusField();
}
</script>

File diff suppressed because one or more lines are too long

View File

@ -63,7 +63,7 @@ void writeErrorLog(const char* format, ...) {
char buf[80];
vsnprintf(&buf[0], sizeof(buf), format, arg);
f.write(&buf[0], strlen(&buf[0]));
Log.error(&buf[0]);
Log.errorln(&buf[0]);
va_end(arg);
f.println();
f.close();

View File

@ -141,6 +141,7 @@ void PushTarget::sendAll(float angle, float gravitySG, float corrGravitySG,
LOG_PERF_STOP("push-mqtt");
}
engine.freeMemory();
intDelay.save();
}

View File

@ -131,6 +131,7 @@ class TemplatingEngine {
}
}
freeMemory(); // In case this is reused
_output = static_cast<char *>(malloc(size + 20));
if (!_output) {
@ -159,7 +160,15 @@ class TemplatingEngine {
}
}
}
strncat(_output, format + k, strlen(format + k));
strncat(_output, format + k, size - k);
Log.notice(F("TPL : Transformed template %d chars to %d chars" CR),
strlen(format), strlen(_output));
#if LOG_LEVEL == 6
printHeap("TPL ");
Log.verboseln(format);
Log.verboseln(_output);
#endif
}
void dumpAll() {
@ -187,7 +196,9 @@ class TemplatingEngine {
void freeMemory() {
if (_output) free(_output);
_output = 0;
_baseTemplate.clear();
}
void initialize(float angle, float gravitySG, float corrGravitySG,
float tempC, float runTime);

View File

@ -949,6 +949,7 @@ void WebServerHandler::webHandleTestPush() {
enabled = true;
}
engine.freeMemory();
DynamicJsonDocument doc(100);
doc[PARAM_PUSH_ENABLED] = enabled;
doc[PARAM_PUSH_SUCCESS] = push.getLastSuccess();