From 99e1fd0522180fbc021f4b56577ea8e87ff30417 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <67171972+IoTManagerProject@users.noreply.github.com> Date: Sun, 30 Jan 2022 17:44:53 +0100 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D1=81?= =?UTF-8?q?=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=BD=D1=8B=D0=BC=D0=B8=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=BF=D1=80=D0=BE=D1=81=D0=B0=D0=BC=D0=B8=20api=20?= =?UTF-8?q?=D0=B2=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4=D0=B5=D0=B9=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=B8=D1=8F=20=D0=B2=D0=B5=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 139 +++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 61 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index 3485acf..e80d945 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -158,14 +158,10 @@ //configuration let configJson = []; - let configJsonBuf = []; let configJsonFlag = false; - let configJsonStart = "/st/config.json"; let widgetsJson = []; - let widgetsJsonBuf = []; let widgetsJsonFlag = false; - let widgetsJsonStart = "/st/widgets.json"; //web sockets let socket = []; @@ -186,13 +182,45 @@ }, ]; - //navigation + //***********************************************************blob**************************************************************/ + var MyBlobBuilder = function () { + this.parts = []; + }; + + MyBlobBuilder.prototype.append = function (part) { + this.parts.push(part); + this.blob = undefined; // Invalidate the blob + }; + + MyBlobBuilder.prototype.getBlob = function () { + if (!this.blob) { + this.blob = new Blob(this.parts, { type: "binary" }); + } + return this.blob; + }; + + MyBlobBuilder.prototype.clear = function () { + this.parts = []; + }; + + //***********************************************************navigation********************************************************/ let currentPageName = undefined; - let dataReceivingInProgress = false; + var configJsonBlob = new MyBlobBuilder(); + var widgetsJsonBlob = new MyBlobBuilder(); router.subscribe(handleNavigation); - //****************************************************functions section********************************************************/ - //*****************************************************************************************************************************/ + function handleNavigation() { + clearData(); + currentPageName = $router.path.toString(); + console.log("[i]", "user on page:", currentPageName); + sendCurrentPageName(); + } + + function sendCurrentPageName() { + if (wsSelected !== undefined) { + wsSendMsg(wsSelected, currentPageName); + } + } //****************************************************web sockets section******************************************************/ function connectToAllDevices() { @@ -281,49 +309,50 @@ socket[ws].addEventListener("message", function (event) { if (typeof event.data === "string") { let data = event.data; - } - if (event.data instanceof Blob) { - let reader = new FileReader(); - reader.readAsText(event.data); - reader.onload = () => { - let result = reader.result; - //сборщик configJson пакетов======================================== - if (result === "/st/config.json") { - configJsonFlag = true; - if (debug) console.log("[i]", "configJson start!"); - } - if (configJsonFlag && result != "/st/config.json" && result != "/end/config.json") { - configJsonBuf = configJsonBuf + result; - } - if (result === "/end/config.json") { - configJsonFlag = false; - if (debug) console.log("[i]", "configJson end!"); - //if (debug) console.log("[i]", configJsonBuf); - if (IsJsonParse(configJsonBuf)) { - configJson = JSON.parse(configJsonBuf); + //сборщик configJson пакетов======================================== + if (data === "/st/config.json") { + if (debug) console.log("[i]", "configJson start!"); + configJsonFlag = true; + } + if (data === "/end/config.json") { + if (debug) console.log("[i]", "configJson end!"); + configJsonFlag = false; + var bb = configJsonBlob.getBlob(); + let configJsonReader = new FileReader(); + configJsonReader.readAsText(bb); + configJsonReader.onload = () => { + let configJsonResult = configJsonReader.result; + if (IsJsonParse(configJsonResult)) { + configJson = JSON.parse(configJsonResult); configJson = configJson; if (debug) console.log("[i]", "configJson parced!"); } - } - //сборщик widgetsJson пакетов======================================== - if (result === "/st/widgets.json") { - widgetsJsonFlag = true; - if (debug) console.log("[i]", "widgetsJson start!"); - } - if (widgetsJsonFlag && result != "/st/widgets.json" && result != "/end/widgets.json") { - widgetsJsonBuf = widgetsJsonBuf + result; - } - if (result === "/end/widgets.json") { - widgetsJsonFlag = false; - if (debug) console.log("[i]", "widgetsJson end!"); - //if (debug) console.log("[i]", widgetsJsonBuf); - if (IsJsonParse(widgetsJsonBuf)) { - widgetsJson = JSON.parse(widgetsJsonBuf); + }; + } + //сборщик widgetsJson пакетов======================================== + if (data === "/st/widgets.json") { + if (debug) console.log("[i]", "widgetsJson start!"); + widgetsJsonFlag = true; + } + if (data === "/end/widgets.json") { + if (debug) console.log("[i]", "widgetsJson end!"); + widgetsJsonFlag = false; + var bb = widgetsJsonBlob.getBlob(); + let widgetsJsonReader = new FileReader(); + widgetsJsonReader.readAsText(bb); + widgetsJsonReader.onload = () => { + let widgetsJsonResult = widgetsJsonReader.result; + if (IsJsonParse(widgetsJsonResult)) { + widgetsJson = JSON.parse(widgetsJsonResult); widgetsJson = widgetsJson; if (debug) console.log("[i]", "widgetsJson parced!"); } - } - }; + }; + } + } + if (event.data instanceof Blob) { + if (configJsonFlag) configJsonBlob.append(event.data); + if (widgetsJsonFlag) widgetsJsonBlob.append(event.data); } }); socket[ws].addEventListener("close", (event) => { @@ -347,9 +376,11 @@ function clearData() { configJson = []; - configJsonBuf = []; + configJsonBlob.clear(); widgetsJson = []; - widgetsJsonBuf = []; + widgetsJsonBlob.clear(); + + if (debug) console.log("[i]", "all app data cleared"); } function wsPush(ws, topic, status) { @@ -453,20 +484,6 @@ } } - //***********************************************************navigation************************************************************/ - function handleNavigation() { - clearData(); - currentPageName = $router.path.toString(); - console.log("[i]", "user on page:", currentPageName); - sendCurrentPageName(); - } - - function sendCurrentPageName() { - if (wsSelected !== undefined) { - wsSendMsg(wsSelected, currentPageName); - } - } - //****************************************************************json************************************************************/ function getJsonObject(array, number) { let num = 0;