импорт

This commit is contained in:
Dmitry Borisenko
2022-09-30 23:48:56 +02:00
parent c3d503d8b2
commit 6ef976187c
3 changed files with 66 additions and 23 deletions

View File

@@ -4,12 +4,12 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" /> <meta name="viewport" content="width=device-width,initial-scale=1" />
<title>IoT Manager 4.3.8</title> <title>IoT Manager 4.3.9</title>
<link rel="icon" type="image/png" href="/favicon.ico" /> <link rel="icon" type="image/png" href="/favicon.ico" />
<link rel="stylesheet" href="/build/bundle.css?438" /> <link rel="stylesheet" href="/build/bundle.css?439" />
<script defer src="/build/bundle.js?438"></script> <script defer src="/build/bundle.js?439"></script>
</head> </head>
<body></body> <body></body>

View File

@@ -37,7 +37,7 @@
let updatingTimeout = 120000; let updatingTimeout = 120000;
let opened = false; let opened = false;
let preventMove = false; let preventMove = false;
let devMode = false; let devMode = true;
//****************************************************variable section**********************************************************/ //****************************************************variable section**********************************************************/
//******************************************************************************************************************************/ //******************************************************************************************************************************/

View File

@@ -60,11 +60,11 @@
function windowHeight() { function windowHeight() {
console.log("test", scenarioTxt); console.log("test", scenarioTxt);
let scenStr = scenarioTxt; let scenStr = scenarioTxt;
height = scenStr.split("\n").length; height = scenStr.split("\n").length + 1;
} }
// Function to download data to a file // Function to download data to a file
function download(data, filename, type) { function saveFile2(data, filename, type) {
var file = new Blob([data], { type: type }); var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob) if (window.navigator.msSaveOrOpenBlob)
// IE10+ // IE10+
@@ -77,6 +77,7 @@
a.download = filename; a.download = filename;
document.body.appendChild(a); document.body.appendChild(a);
a.click(); a.click();
setTimeout(function () { setTimeout(function () {
document.body.removeChild(a); document.body.removeChild(a);
window.URL.revokeObjectURL(url); window.URL.revokeObjectURL(url);
@@ -84,6 +85,24 @@
} }
} }
function saveFile(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, filename);
} else {
const a = document.createElement("a");
document.body.appendChild(a);
const url = window.URL.createObjectURL(file);
a.href = url;
a.download = filename;
a.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}, 0);
}
}
const syntaxHighlight = (json) => { const syntaxHighlight = (json) => {
try { try {
json = JSON.stringify(JSON.parse(json), null, 4); json = JSON.stringify(JSON.parse(json), null, 4);
@@ -100,7 +119,11 @@
function createExportFile() { function createExportFile() {
exportJson.mark = "iotm"; exportJson.mark = "iotm";
exportJson.config = configJson; exportJson.config = configJson;
exportJson.scenario = scenarioTxt;
let exportAsText = syntaxHighlight(JSON.stringify(exportJson));
exportAsText = exportAsText + "\n\nscenario=>" + scenarioTxt;
saveFile(exportAsText, "export.json", "application/json");
} }
let template = null; let template = null;
@@ -113,24 +136,34 @@
const fileText = files[0].text(); const fileText = files[0].text();
fileText.then((text) => { fileText.then((text) => {
template = text; template = text;
if (IsJsonParse(template)) {
let json = JSON.parse(template); if (!template.includes("scenario=>")) {
if (json.mark === "iotm") { window.alert(alertErr);
return;
}
let jsonPart = selectToMarker(template, "scenario=>");
let txtPart = deleteBeforeDelimiter(template, "scenario=>");
if (!IsJsonParse(jsonPart)) {
window.alert(alertErr);
return;
}
let json = JSON.parse(jsonPart);
if (json.mark !== "iotm") {
window.alert(alertErr);
return;
}
if (window.confirm(alertOk)) { if (window.confirm(alertOk)) {
configJson = []; configJson = [];
scenarioTxt = ""; scenarioTxt = "";
configJson = json.config; configJson = json.config;
scenarioTxt = json.scenario; scenarioTxt = txtPart;
console.log("config updated"); console.log("config updated");
} }
} else {
window.alert(alertErr);
}
} else {
window.alert(alertErr);
}
}); });
files = null; files = null;
} }
@@ -149,6 +182,16 @@
} }
return true; return true;
} }
function selectToMarker(str, found) {
let p = str.indexOf(found);
return str.substring(0, p);
}
function deleteBeforeDelimiter(str, found) {
let p = str.indexOf(found) + found.length;
return str.substring(p);
}
</script> </script>
{#if show} {#if show}
@@ -231,7 +274,7 @@
<div class="grd-2col1"> <div class="grd-2col1">
<button class="btn-lg" on:click={() => saveConfig()}>{"Сохранить на устройстве"}</button> <button class="btn-lg" on:click={() => saveConfig()}>{"Сохранить на устройстве"}</button>
<button class="btn-lg" on:click={() => rebootEsp()}>{"Перезагрузить устройство"}</button> <button class="btn-lg" on:click={() => rebootEsp()}>{"Перезагрузить устройство"}</button>
<button class="btn-lg" on:click={() => (createExportFile(), download(syntaxHighlight(JSON.stringify(exportJson)), "export.json", "application/json"))}>{"Экспорт конфигурации"}</button> <button class="btn-lg" on:click={() => (createExportFile(), createExportFile())}>{"Экспорт конфигурации"}</button>
<label on:click={() => reset()} class="btn-lg cursor-pointer select-none"> <label on:click={() => reset()} class="btn-lg cursor-pointer select-none">
<input bind:files accept="application/JSON" type="file" id="formFile" /> <input bind:files accept="application/JSON" type="file" id="formFile" />
{"Импорт конфигурации"} {"Импорт конфигурации"}