浏览代码

Make configuration editor network requests asynchronous

SybexXx 1 天之前
父节点
当前提交
a74df0cdba
共有 1 个文件被更改,包括 29 次插入 和 29 次删除
  1. 29 29
      sd-card/html/edit_config_template.html

+ 29 - 29
sd-card/html/edit_config_template.html

@@ -1749,8 +1749,8 @@ function cameraParameterChanged() {
     }
 }
 
-function LoadConfigNeu() {
-    if (!loadConfig(domainname)) {
+async function LoadConfigNeu() {
+    if (!await loadConfig(domainname)) {
         firework.launch('Configuration could not be loaded! Please reload the page!', 'danger', 30000);
         return;
     }
@@ -1760,7 +1760,7 @@ function LoadConfigNeu() {
     category = getConfigCategory();
     InitIndivParameter();
 
-    UpdateInput();
+    await UpdateInput();
     var sel = document.getElementById("Numbers_value1");
     UpdateInputIndividual(sel);
 	
@@ -1881,7 +1881,7 @@ function UpdateInputIndividual(sel) {
     WriteParameter(param, category, "MQTT", "DomoticzIDX", true, NUNBERSAkt);
 }
 
-function UpdateInput() {
+async function UpdateInput() {
     document.getElementById("Category_Digits_enabled").checked = category["Digits"]["enabled"];
     setVisible("DigitItem", category["Digits"]["enabled"]);
 
@@ -2011,11 +2011,11 @@ function UpdateInput() {
     WriteParameter(param, category, "System", "RSSIThreshold", true);
     WriteParameter(param, category, "System", "CPUFrequency", true);
 
-    WriteModelFiles();
+    await WriteModelFiles();
 }
 
-function WriteModelFiles() {
-    list_tflite = getTFLITEList();
+async function WriteModelFiles() {
+    list_tflite = await getTFLITEList();
 
     var _indexDig = document.getElementById("Digits_Model_value1");
     var _indexAna = document.getElementById("Analog_Model_value1");
@@ -2256,7 +2256,7 @@ function UpdateTooltipModus() {
     }
 }
 
-function saveTextAsFile() {
+async function saveTextAsFile() {
     if (window.GPIO_PROFILE_LOADED !== true) {
         firework.launch("The GPIO board profile is not available. Reload the page before saving configuration.", "danger", 10000);
         return;
@@ -2280,7 +2280,10 @@ function saveTextAsFile() {
 
 	ReadParameterAll();
 	WriteConfigININew();
-	SaveConfigToServer(domainname);
+	if (!await SaveConfigToServer(domainname)) {
+        firework.launch('Configuration could not be saved!', 'danger', 10000);
+        return;
+    }
 
     if(window.location.hash) {
         var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
@@ -2297,12 +2300,12 @@ function saveTextAsFile() {
 	}
 
 	if (changeCamValue == 1) {
-		camSettingsSet();
+		await camSettingsSet();
 		firework.launch('You have changed the camera settings, creating a new reference image and updating the alignment marks is mandatory!', 'success', 5000);
 	}
 }
 
-function camSettingsSet(){
+async function camSettingsSet(){
     document.getElementById("overlay").style.display = "block";
     document.getElementById("overlaytext").innerHTML = "Save Cam Settings...";
 	
@@ -2474,46 +2477,41 @@ function camSettingsSet(){
 	
     async function task() {
         while (true) {
-            var xhttp = new XMLHttpRequest();
-			
             if (durchlaufe > 10) {
                 document.getElementById("overlay").style.display = "none";
                 firework.launch('Save Cam Settings aborted, timeout!', 'danger', 5000);
                 return;
             }			
 
+            var responseText = "";
             try {
-                xhttp.open("GET", url, false);
-                xhttp.send();			
-            } catch (error){}				
+                const response = await fetch(url);
+                responseText = await response.text();
+            } catch (error) {}				
             
-            if (xhttp.responseText == "CamSettingsSet") {
+            if (responseText == "CamSettingsSet") {
                 document.getElementById("overlay").style.display = "none";
                 firework.launch('Cam Settings saved', 'success', 2000);
                 return;
             }
             else {
                 // Get status
-                var _xhttp = new XMLHttpRequest();
-                durchlaufe = durchlaufe + 1;				
-				
+                var statusText = "";
+                durchlaufe = durchlaufe + 1;
                 try {
-                    _xhttp.open("GET", domainname + "/statusflow", false);
-                    _xhttp.send();
+                    const statusResponse = await fetch(domainname + "/statusflow");
+                    statusText = await statusResponse.text();
                 }
-                catch (error){}
+                catch (error) {}
 
-                document.getElementById("overlaytext").innerHTML = "Device is busy, please wait.<br><br>Current step: " + _xhttp.responseText;
+                document.getElementById("overlaytext").innerHTML = "Device is busy, please wait.<br><br>Current step: " + _responseText;
                 console.log("Device is busy, waiting 2s then checking again...");
                 await sleep(2000);
             }
         }   
     }
 
-    setTimeout(function() { 
-        // Delay so the overlay gets shown
-        task();
-    }, 1);
+    await task();
 }
 	
 function doReboot() {
@@ -2965,7 +2963,9 @@ function openDescription() {
 }
 
 openDescription();
-LoadConfigNeu();
+LoadConfigNeu().catch(function (error) {
+    console.error('Could not initialize configuration editor:', error);
+});
 
 /*
  * Generate the GPIO editor from the active, audited board whitelist.