From 5ee3956246610b2f43150f75dfd2b17528e335fb Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Sat, 25 Dec 2021 00:20:49 +0100 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B1=D0=BE=D1=80=20json=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D1=88=D0=B5=D0=B4=D1=88=D0=B5=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=81=20esp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.svelte | 62 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index e13dd0b..22e151a 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -15,7 +15,7 @@ //==================================constants section======================================== let debug = true; let LOG_MAX_MESSAGES = 10; - let reconnectTimeout = 10000; + let reconnectTimeout = 60000; //=================================variable section========================================== let myip = document.location.hostname; @@ -137,6 +137,7 @@ //configuration let config = []; + let buf = []; //web sockets let socket = []; @@ -219,7 +220,8 @@ if (ip === "error") { if (debug) console.log("[e]", "device list wrong"); } else { - socket[ws] = new WebSocket("ws://" + ip + "/ws"); + socket[ws] = new WebSocket("ws://" + ip + ":81"); + //socket[ws] = new WebSocket("ws://" + ip + "/ws"); if (debug) console.log("[i]", ip, "started connecting..."); } } @@ -246,19 +248,24 @@ }); socket[ws].addEventListener("message", function (event) { let data = event.data.toString(); - if (debug) console.log("[i]", "data:", data); - - //if (data.includes("[log]")) { - // data = data.replace("[log]", ""); - // addCoreMsg(data); - // //if (debug) console.log("[i]", "log data:", data); - //} else if (data.includes("[/setup.json]")) { - // if (debug) console.log("[i]", "config data:", data); - // data = data.replace("[/setup.json]", ""); - // data = JSON.parse(data); - // config.push(data); - // config = config; - //} + //if (debug) console.log("[i]", "data:", data); + if (data.includes("[log]")) { + data = data.replace("[log]", ""); + addCoreMsg(data); + //if (debug) console.log("[i]", "log data:", data); + } else if (data.includes("/config.json")) { + data = data.replace("/config.json", ""); + buf = buf + data; + if (data.includes("]}")) { + buf = buf.replace("]}", "]"); + if (IsJsonParse(buf)) { + config = JSON.parse(buf); + buf = []; + config = config; + if (debug) console.log("[i]", "parsed"); + } + } + } }); socket[ws].addEventListener("close", (event) => { if (debug) console.log("[e]", ip, "connection closed"); @@ -349,6 +356,8 @@ function dropdownChange() { socketConnected = selectedDeviceData.status; wsSelected = selectedDeviceData.ws; + clearData(); + sendCurrentPageName(); if (debug) console.log("[i]", "user selected device:", selectedDeviceData.name); if (selectedDeviceData.ip === myip) { if (debug) console.log("[i]", "user selected original device", selectedDeviceData.name); @@ -372,9 +381,13 @@ } } + function clearData() { + config = []; + } + //navigation=========================================================================================== function handleNavigation() { - config = []; + clearData(); currentPageName = $router.path.toString(); console.log("[i]", "user on page:", currentPageName); sendCurrentPageName(); @@ -412,6 +425,15 @@ return json; }; + function IsJsonParse(str) { + try { + JSON.parse(str); + } catch (e) { + return false; + } + return true; + } + //post и get запросы================================================================================= //запрос с помощью которого можно отредактировать любой файл на esp //editRequest("192.168.88.235", "data data data data", "file.json") @@ -459,6 +481,12 @@ } } + function pushConfigToEsp() { + //config.forEach((element) => { + wsSendMsg(wsSelected, JSON.stringify(config)); + //}); + } + //initialisation======================================================================================= onMount(async () => { console.log("[i]", "mounted"); @@ -568,7 +596,7 @@ {/each} - +