From 487c44ef54de021a46bd6f113afefc6c5c0268c3 Mon Sep 17 00:00:00 2001 From: IoT Manager <67171972+IoTManagerProject@users.noreply.github.com> Date: Fri, 6 Oct 2023 01:07:49 +0200 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rollup.config.js | 2 +- src/App.svelte | 131 +++++++++++++++++---------------------- src/pages/Profile.svelte | 11 ++-- src/pages/System.svelte | 12 ++-- 4 files changed, 71 insertions(+), 85 deletions(-) diff --git a/rollup.config.js b/rollup.config.js index 2e43462..a832614 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -49,7 +49,7 @@ export default { module: true, toplevel: true, unsafe_arrows: true, - drop_console: true, + drop_console: false, drop_debugger: true, }, output: { quote_style: 1 }, diff --git a/src/App.svelte b/src/App.svelte index f109c47..6092a0d 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -42,7 +42,10 @@ //******************************************************************************************************************************/ const debug = true; const LOG_MAX_MESSAGES = 100; - const reconnectTimeout = 60000; //период проверки соединения с устройством + let reconnectTimeout = 60; //период проверки соединения с устройством + let remainingTimeout = reconnectTimeout; + let connecting = true; + let tickerTask; let preventReconnect = false; const waitingAckTimeout = 18000; //время ожидания ответа от устройства const rebootingTimeout = 30000; @@ -54,13 +57,12 @@ const blobDebug = false; const devMode = true; - let timeout = reconnectTimeout / 1000; let percent; //****************************************************variable section**********************************************************/ //******************************************************************************************************************************/ let myip = document.location.hostname; - if (devMode) myip = "192.168.87.242"; + if (devMode) myip = "192.168.1.232"; //Flags let firstDevListRequest = true; @@ -107,6 +109,10 @@ }, ]; + var ackTimeoutsArr = []; + var startMillis = []; + var ping = []; + let incDeviceList = []; let layoutJson = []; let paramsJson = {}; @@ -184,7 +190,6 @@ console.log("[i]", "mounted"); await getUser(); onCheck(); - opened = screenSize > 900 ? true : false; selectedDeviceDataRefresh(); //флаг первого запроса списка устройств @@ -236,32 +241,6 @@ } } - var ackTimeoutsArr = []; - var startMillis = []; - var ping = []; - - function ack(ws, st) { - if (!st) { - startMillis[ws] = Date.now(); - ackTimeoutsArr[ws] = setTimeout(function () { - markDeviceStatus(ws, false); - }, waitingAckTimeout); - } else { - if (ackTimeoutsArr[ws]) clearTimeout(ackTimeoutsArr[ws]); - if (startMillis[ws]) { - ping[ws] = Date.now() - startMillis[ws]; - } - - for (let i = 0; i < deviceList.length; i++) { - if (deviceList[i].ws === ws) { - deviceList[i].ping = ping[ws]; - } - } - - deviceList = deviceList; - } - } - function markDeviceStatus(ws, status) { deviceList.forEach((device) => { if (device.ws === ws) { @@ -310,6 +289,7 @@ let ip = getIP(ws); socket[ws].addEventListener("open", function (event) { //if (debug) console.log("[i]", ip, ws, "completed connecting"); + markDeviceStatus(ws, true); //при первом подключении запросим список устройств if (firstDevListRequest && ws === 0) wsSendMsg(ws, "/devlist|"); @@ -331,6 +311,7 @@ let data = event.data; if (data === "/tstr|") { //прилетело подтверждение значит устройство онлайн + ack(ws, true); } } @@ -436,14 +417,14 @@ errorsJson = out.json; parsed.errorsJson = true; //когда запустили обновление предотвращаем попытки проверки связи - if (errorsJson.upd === 1) { - preventReconnect = true; - } + //if (errorsJson.upd === 1) { + // preventReconnect = true; + //} //когда устройство обновилось переподключимся через 5ть секунд - if (errorsJson.upd === 5) { - preventReconnect = false; - timeout = 20; - } + //if (errorsJson.upd === 5) { + // preventReconnect = false; + // reconnectTimeout = 20; + //} if (blobDebug) console.log("[✔]", "errorsJson: ", errorsJson); } else { parsed.errorsJson = false; @@ -993,19 +974,24 @@ //тикер который тикает каждую секунду и на каждую 0 секунду запускает проверку соединения function wsTestMsgTask() { - setTimeout(wsTestMsgTask, 1000); + tickerTask = setTimeout(wsTestMsgTask, 1000); if (!preventReconnect) { - timeout--; - percent = scale(timeout, reconnectTimeout / 1000, 0, 0, 100); - if (timeout <= 0) { + remainingTimeout--; + if (socketConnected) showAwaitingCircle = false; + percent = scale(remainingTimeout, reconnectTimeout, 0, 0, 100); + if (remainingTimeout <= 0) { if (debug) console.log("[i]", "----timer tick----"); printAllCreatedWs(); - timeout = reconnectTimeout / 1000; + remainingTimeout = reconnectTimeout; deviceList.forEach((device) => { if (device.status === false || device.status === undefined) { wsConnect(device.ws); wsEventAdd(device.ws); } else { + if (device.ws === selectedWs) { + //clearInterval(tickerTask); + connecting = true; + } wsSendMsg(device.ws, "/tst|"); ack(device.ws, false); } @@ -1014,6 +1000,26 @@ } } + function ack(ws, st) { + if (!st) { + startMillis[ws] = Date.now(); + ackTimeoutsArr[ws] = setTimeout(function () { + markDeviceStatus(ws, false); + }, waitingAckTimeout); + } else { + if (ackTimeoutsArr[ws]) clearTimeout(ackTimeoutsArr[ws]); + if (startMillis[ws]) { + ping[ws] = Date.now() - startMillis[ws]; + } + for (let i = 0; i < deviceList.length; i++) { + if (deviceList[i].ws === ws) { + deviceList[i].ping = ping[ws]; + } + } + deviceList = deviceList; + } + } + function wsSendMsg(ws, msg) { if (socket[ws] && socket[ws].readyState === 1) { socket[ws].send(msg); @@ -1147,7 +1153,9 @@ wsSendMsg(selectedWs, "/reboot|"); markDeviceStatus(selectedWs, false); showAwaitingCircle = true; - rebootTimer = setTimeout(rebootingTask, rebootingTimeout); + socketConnected = false; + reconnectTimeout = 10; + remainingTimeout = reconnectTimeout; } function applicationReboot() { @@ -1162,8 +1170,6 @@ } function rebootingTask() { - //перезапуск приложения - //location.reload(); clearTimeout(rebootTimer); clearData(); connectToAllDevices(); @@ -1208,33 +1214,12 @@ } function updateBuild(path) { + console.log(path); wsSendMsg(selectedWs, "/update|" + path); - } - - //depricated - function startUpdate(all) { - if (choosingVersion !== undefined) { - //if (choosingVersion === errorsJson.bver) { - // window.alert("Эта версия уже установленна"); - //} else { - if (confirm("После обновления устройство перезагрузится. Запустить обновление?")) { - console.log("start update..."); - if (all) { - sendToAllDevices('/rorre|{"chver":' + choosingVersion + "}"); - sendToAllDevices("/update|"); - } else { - wsSendMsg(selectedWs, '/rorre|{"chver":' + choosingVersion + "}"); - wsSendMsg(selectedWs, "/update|"); - } - //showAwaitingCircle = true; - //rebootTimer = setTimeout(rebootingTask, updatingTimeout); - } else { - console.log("update canceled"); - } - //} - } else { - window.alert("Версия не выбрана или сервер недоступен"); - } + showAwaitingCircle = true; + socketConnected = false; + reconnectTimeout = 10; + //rebootTimer = setTimeout(rebootingTask, updatingTimeout); } function moduleOrder(id, key, value) { @@ -1329,7 +1314,7 @@