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> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div> </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"> <script type="text/javascript">
function showError( msg ) { function showError( msg ) {
$('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none'); $('#alert').removeClass('alert-success').addClass('alert-danger').removeClass('hide').addClass('show').removeClass('d-none');
@ -85,6 +90,13 @@
function hideWarningHomeAssistant() { function hideWarningHomeAssistant() {
$('#warning-ha').addClass('d-none').removeClass('show').addClass('hide'); $('#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> </script>
<div class="accordion" id="accordion"> <div class="accordion" id="accordion">
@ -117,7 +129,7 @@
<div class="row mb-3"> <div class="row mb-3">
<div class="col-sm-12"> <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> </textarea>
</div> </div>
</div> </div>
@ -158,6 +170,9 @@
</div> </div>
</div> </div>
<div class="col-sm-8" id="status">
</div>
<hr class="my-2"> <hr class="my-2">
<pre class="card-preview" id="preview" name="preview"></pre> <pre class="card-preview" id="preview" name="preview"></pre>
@ -171,6 +186,8 @@
<script type="text/javascript"> <script type="text/javascript">
window.onload = getConfig; window.onload = getConfig;
var maxCharsInFormatTemplate = 1500;
setButtonDisabled( true ); setButtonDisabled( true );
// Opens the targetet according (if URL has #collapseOne to #collapseFour) // Opens the targetet according (if URL has #collapseOne to #collapseFour)
@ -181,15 +198,16 @@
} }
}); });
/* function updateStatusField() {
$("#format-btn").click(function(e){ var t = $("#format").val();
console.log(e) $("#status").text( t.length + " characters in template, recommended maximum is " + maxCharsInFormatTemplate );
var s = $("#push-target").val()
if (s == "mqtt") { if (t.length > maxCharsInFormatTemplate) {
console.log("Current format is mqtt, checking for HA device registration.") showWarningLength();
} else {
hideWarningLength();
} }
});*/ };
$("#push-target").change(function(e){ $("#push-target").change(function(e){
console.log(e) console.log(e)
@ -207,19 +225,29 @@
$("#format").val( decodeURIComponent(item.format) ); $("#format").val( decodeURIComponent(item.format) );
} }
}); });
updateStatusField();
}); });
// Clear the selected template // Clear the selected template
$("#clear-btn").click(function(e) { $("#clear-btn").click(function(e) {
$("#format").val( "" ); $("#format").val( "" );
updateStatusField();
}); });
// Store the format // Store the format
$("#format-btn").click(function(e) { $("#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(); $('#spinner').show();
setButtonDisabled( true ); setButtonDisabled( true );
var s = $("#format").val();
var ha = false; var ha = false;
hideWarningHomeAssistant(); hideWarningHomeAssistant();
@ -356,6 +384,7 @@
$("#test-btn").prop("disabled", b); $("#test-btn").prop("disabled", b);
$("#copy-btn").prop("disabled", b); $("#copy-btn").prop("disabled", b);
$("#clear-btn").prop("disabled", b); $("#clear-btn").prop("disabled", b);
$("#push-target").prop("disabled", b);
} }
function selectFormat() { function selectFormat() {
@ -367,6 +396,7 @@
console.log(s); console.log(s);
$("#format").val(s); $("#format").val(s);
$("#preview").text(""); $("#preview").text("");
updateStatusField();
} }
// Get the configuration values from the API // Get the configuration values from the API
@ -394,6 +424,8 @@
$('#spinner').hide(); $('#spinner').hide();
setButtonDisabled( false ); setButtonDisabled( false );
}); });
updateStatusField();
} }
</script> </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]; char buf[80];
vsnprintf(&buf[0], sizeof(buf), format, arg); vsnprintf(&buf[0], sizeof(buf), format, arg);
f.write(&buf[0], strlen(&buf[0])); f.write(&buf[0], strlen(&buf[0]));
Log.error(&buf[0]); Log.errorln(&buf[0]);
va_end(arg); va_end(arg);
f.println(); f.println();
f.close(); f.close();

View File

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

View File

@ -131,6 +131,7 @@ class TemplatingEngine {
} }
} }
freeMemory(); // In case this is reused
_output = static_cast<char *>(malloc(size + 20)); _output = static_cast<char *>(malloc(size + 20));
if (!_output) { 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() { void dumpAll() {
@ -187,7 +196,9 @@ class TemplatingEngine {
void freeMemory() { void freeMemory() {
if (_output) free(_output); if (_output) free(_output);
_output = 0; _output = 0;
_baseTemplate.clear();
} }
void initialize(float angle, float gravitySG, float corrGravitySG, void initialize(float angle, float gravitySG, float corrGravitySG,
float tempC, float runTime); float tempC, float runTime);

View File

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