объединение разных

This commit is contained in:
Dmitry Borisenko
2022-02-26 01:09:17 +01:00
parent 4b13dea123
commit e4ee65742b

View File

@@ -81,9 +81,7 @@
let itemsJsonParced = false; let itemsJsonParced = false;
let layoutJson = []; let layoutJson = [];
let layoutJsonFlag = false; let layoutJsonArrayParced = false;
let layoutJsonParced = false;
let layoutJsonArrayParced = [];
let settingsJson = {}; let settingsJson = {};
let settingsJsonParced = false; let settingsJsonParced = false;
@@ -157,7 +155,6 @@
var configJsonBlob = new MyBlobBuilder(); var configJsonBlob = new MyBlobBuilder();
var widgetsJsonBlob = new MyBlobBuilder(); var widgetsJsonBlob = new MyBlobBuilder();
var itemsJsonBlob = new MyBlobBuilder(); var itemsJsonBlob = new MyBlobBuilder();
var layoutJsonBlob = new MyBlobBuilder();
var scenarioTxtBlob = new MyBlobBuilder(); var scenarioTxtBlob = new MyBlobBuilder();
var layoutJsonArray = []; var layoutJsonArray = [];
@@ -168,14 +165,14 @@
console.log("[i]", "handle navigation"); console.log("[i]", "handle navigation");
clearData(); clearData();
currentPageName = $router.path.toString(); currentPageName = $router.path.toString();
//название страницы служит заголовком, поэтому отметим конец заголовка "|"
currentPageName = currentPageName + "|"; currentPageName = currentPageName + "|";
console.log("[i]", "user on page:", currentPageName); console.log("[i]", "user on page:", currentPageName);
//if (currentPageName === "/|") {
// sendToAllDevices(currentPageName); if (currentPageName === "/|") {
//} else { sendToAllDevices(currentPageName);
} else {
sendCurrentPageName(); sendCurrentPageName();
//} }
} }
function sendCurrentPageName() { function sendCurrentPageName() {
@@ -191,7 +188,7 @@
firstDevListRequest = true; firstDevListRequest = true;
connectToAllDevices(); connectToAllDevices();
wsTestMsgTask(); wsTestMsgTask();
findNewPage(); sortingLayout();
}); });
//****************************************************web sockets section******************************************************/ //****************************************************web sockets section******************************************************/
@@ -275,9 +272,12 @@
if (debug) console.log("[i]", ip, ws, "completed connecting"); if (debug) console.log("[i]", ip, ws, "completed connecting");
markDeviceStatus(ws, true); markDeviceStatus(ws, true);
if (firstDevListRequest) wsSendMsg(0, "/list|"); if (firstDevListRequest) wsSendMsg(0, "/list|");
//при подключении отправляем название страницы
if (currentPageName === "/|") { if (currentPageName === "/|") {
//всем устройствам
wsSendMsg(ws, currentPageName); wsSendMsg(ws, currentPageName);
} else { } else {
//только выбранному
if (ws === selectedWs) { if (ws === selectedWs) {
sendCurrentPageName(); sendCurrentPageName();
} }
@@ -321,26 +321,7 @@
connectToAllDevices(); connectToAllDevices();
} }
} }
//сборщик statusJson сообщений======================================
if (data.includes("status")) {
if (IsJsonParse(data)) {
let statusJson = JSON.parse(data);
udateStatusOfWidget(statusJson);
wigetsUpdate();
if (debug) console.log("✔", "statusJson parced");
statusJsonParced = true;
onParced();
}
}
//сборщик paramsJson сообщений======================================
if (data.includes("params")) {
if (IsJsonParse(data)) {
paramsJson = JSON.parse(data);
if (debug) console.log("✔", "paramsJson parced", ws);
paramsJsonParced = true;
onParced();
}
}
//сборщик ssidJson сообщений====================================== //сборщик ssidJson сообщений======================================
if (data.includes("ssid")) { if (data.includes("ssid")) {
if (IsJsonParse(data)) { if (IsJsonParse(data)) {
@@ -366,7 +347,7 @@
if (IsJsonParse(data)) { if (IsJsonParse(data)) {
settingsJson = JSON.parse(data); settingsJson = JSON.parse(data);
settingsJson = settingsJson; settingsJson = settingsJson;
wigetsUpdate(); //sortingLayout();
settingsJsonParced = true; settingsJsonParced = true;
if (debug) console.log("✔", "settingsJson json parced"); if (debug) console.log("✔", "settingsJson json parced");
onParced(); onParced();
@@ -462,26 +443,51 @@
} }
//сборщик layoutJson пакетов======================================== //сборщик layoutJson пакетов========================================
if (data === "/st/layout.json") { if (data === "/st/layout.json") {
//layoutJsonArrayParced[ws] = false; deviceList[ws].lp = false;
layoutJsonFlag = true;
} }
if (data === "/end/layout.json") { if (data === "/end/layout.json") {
layoutJsonArrayParced[ws] = true; deviceList[ws].lp = true;
layoutJsonFlag = false; for (let i = 0; i < deviceList.length; i++) {
var bb = layoutJsonBlob.getBlob(); if (deviceList[i].lp === false || deviceList[i].lp === undefined) {
let layoutJsonReader = new FileReader(); layoutJsonArrayParced = false;
layoutJsonReader.readAsText(bb); break;
layoutJsonReader.onload = () => { } else {
let layoutJsonResult = layoutJsonReader.result; layoutJsonArrayParced = true;
if (IsJsonParse(layoutJsonResult)) { }
layoutJson = JSON.parse(layoutJsonResult); }
layoutJson = layoutJson; if (layoutJsonArrayParced) console.log("✔", "layoutJsonArray parced");
wigetsUpdate();
layoutJsonParced = true;
if (debug) console.log("✔", "layoutJson parced", ws);
onParced(); onParced();
} }
//сборщик paramsJson сообщений======================================
if (data.includes("params")) {
if (IsJsonParse(data)) {
paramsJson = {
...paramsJson,
...JSON.parse(data),
}; };
paramsJson = paramsJson;
deviceList[ws].pp = true;
for (let i = 0; i < deviceList.length; i++) {
if (deviceList[i].pp === false || deviceList[i].pp === undefined) {
paramsJsonParced = false;
break;
} else {
paramsJsonParced = true;
}
}
if (paramsJsonParced) console.log("✔", "paramsJson parced");
onParced();
}
}
//сборщик statusJson сообщений======================================
if (data.includes("status")) {
if (IsJsonParse(data)) {
let statusJson = JSON.parse(data);
udateStatusOfWidget(statusJson);
sortingLayout();
if (debug) console.log("[i]", statusJson);
statusJsonParced = true;
}
} }
} }
if (event.data instanceof Blob) { if (event.data instanceof Blob) {
@@ -490,14 +496,11 @@
if (configJsonFlag) configJsonBlob.append(event.data); if (configJsonFlag) configJsonBlob.append(event.data);
if (widgetsJsonFlag) widgetsJsonBlob.append(event.data); if (widgetsJsonFlag) widgetsJsonBlob.append(event.data);
if (itemsJsonFlag) itemsJsonBlob.append(event.data); if (itemsJsonFlag) itemsJsonBlob.append(event.data);
if (layoutJsonFlag) layoutJsonBlob.append(event.data);
if (scenarioTxtFlag) scenarioTxtBlob.append(event.data); if (scenarioTxtFlag) scenarioTxtBlob.append(event.data);
} }
//принимаем данные от всех устройств
if (!layoutJsonArray[ws]) layoutJsonArray[ws] = new MyBlobBuilder(); if (!layoutJsonArray[ws]) layoutJsonArray[ws] = new MyBlobBuilder();
layoutJsonArray[ws].append(event.data); layoutJsonArray[ws].append(event.data);
if (layoutJsonArrayParced.every(Boolean) === true) {
if (debug) console.log("✔", "!!! !!!");
}
} }
}); });
socket[ws].addEventListener("close", (event) => { socket[ws].addEventListener("close", (event) => {
@@ -513,24 +516,53 @@
} }
} }
function testBlob() { async function createFinalLayout() {
console.log("[i]", "create Final Layout");
for (let i = 0; i < layoutJsonArray.length; i++) { for (let i = 0; i < layoutJsonArray.length; i++) {
var bb = layoutJsonArray[i].getBlob(); var bb = layoutJsonArray[i].getBlob();
let reader = new FileReader(); let reader = new FileReader();
reader.readAsText(bb); reader.readAsText(bb);
reader.onload = () => { reader.onload = () => {
console.log(i, reader.result); layoutJson = layoutJson.concat(JSON.parse(reader.result));
}; };
} }
layoutJsonArray = []; console.log("[i]", "creating Final Layout done: ", layoutJson);
return layoutJson;
} }
function onParced() { function udateStatusOfAllWidgets() {
if (currentPageName === "/|" && layoutJsonParced && paramsJsonParced) { console.log("[i]", "udate Status Of All Widgets");
for (const [key, value] of Object.entries(paramsJson)) {
for (let i = 0; i < layoutJson.length; i++) {
let topic = layoutJson[i].topic;
topic = topic.substring(topic.lastIndexOf("/") + 1, topic.length);
if (key === topic) {
console.log("[i]", "value " + topic + " updated");
layoutJson[i].status = value;
break;
}
}
}
}
function udateStatusOfWidget(newStatusJson) {
for (let i = 0; i < layoutJson.length; i++) {
let topic = layoutJson[i].topic;
if (topic === newStatusJson.topic) {
layoutJson[i].status = newStatusJson.status;
break;
}
}
}
async function onParced() {
if (currentPageName === "/|" && layoutJsonArrayParced && paramsJsonParced) {
clearParcedFlags(); clearParcedFlags();
if (debug) console.log("✔✔", "dashboard data parced"); if (debug) console.log("✔✔", "dashboard data parced");
udateStatusOfAllWidgets(paramsJson); await createFinalLayout();
wigetsUpdate(); sortingLayout();
console.log(paramsJson);
udateStatusOfAllWidgets();
dashReady = true; dashReady = true;
} }
if (currentPageName === "/config|" && itemsJsonParced && widgetsJsonParced && configJsonParced && settingsJsonParced && scenarioTxtParced) { if (currentPageName === "/config|" && itemsJsonParced && widgetsJsonParced && configJsonParced && settingsJsonParced && scenarioTxtParced) {
@@ -617,30 +649,6 @@
return layout; return layout;
} }
function udateStatusOfWidget(newStatusJson) {
for (let i = 0; i < layoutJson.length; i++) {
let topic = layoutJson[i].topic;
if (topic === newStatusJson.topic) {
layoutJson[i].status = newStatusJson.status;
break;
}
}
}
function udateStatusOfAllWidgets(paramsJson) {
for (const [key, value] of Object.entries(paramsJson)) {
for (let i = 0; i < layoutJson.length; i++) {
let topic = layoutJson[i].topic;
topic = topic.substring(topic.lastIndexOf("/") + 1, topic.length);
if (key === topic) {
console.log("[i]", "value " + topic + " updated");
layoutJson[i].status = value;
break;
}
}
}
}
function clearData() { function clearData() {
configJson = []; configJson = [];
configJsonBlob.clear(); configJsonBlob.clear();
@@ -652,7 +660,7 @@
itemsJsonBlob.clear(); itemsJsonBlob.clear();
layoutJson = []; layoutJson = [];
layoutJsonBlob.clear(); layoutJsonArray = [];
scenarioTxt = ""; scenarioTxt = "";
scenarioTxtBlob.clear(); scenarioTxtBlob.clear();
@@ -676,7 +684,7 @@
configJsonParced = false; configJsonParced = false;
widgetsJsonParced = false; widgetsJsonParced = false;
itemsJsonParced = false; itemsJsonParced = false;
layoutJsonParced = false; layoutJsonArrayParced = false;
settingsJsonParced = false; settingsJsonParced = false;
errorsJsonParced = false; errorsJsonParced = false;
ssidJsonParced = false; ssidJsonParced = false;
@@ -684,6 +692,14 @@
statusJsonParced = false; statusJsonParced = false;
deviceListParced = false; deviceListParced = false;
scenarioTxtParced = false; scenarioTxtParced = false;
clearFlags();
}
function clearFlags() {
for (let i = 0; i < deviceList.length; i++) {
deviceList[i].pp = false;
deviceList[i].lp = false;
}
} }
function wsPush(ws, topic, status) { function wsPush(ws, topic, status) {
@@ -730,7 +746,7 @@
} }
//***********************************************************dashboard***************************************************************/ //***********************************************************dashboard***************************************************************/
function findNewPage() { function sortingLayout() {
pages = []; pages = [];
const newPage = Array.from(new Set(Array.from(layoutJson, ({ page }) => page))); const newPage = Array.from(new Set(Array.from(layoutJson, ({ page }) => page)));
newPage.forEach(function (item, i, arr) { newPage.forEach(function (item, i, arr) {
@@ -754,11 +770,6 @@
}); });
} }
function wigetsUpdate() {
findNewPage();
layoutJson = layoutJson;
}
//***********************************************************logging******************************************************************/ //***********************************************************logging******************************************************************/
const addCoreMsg = (msg) => { const addCoreMsg = (msg) => {
if (coreMessages.length >= LOG_MAX_MESSAGES) { if (coreMessages.length >= LOG_MAX_MESSAGES) {
@@ -1113,7 +1124,7 @@
{:else} {:else}
<Route path="/"> <Route path="/">
<DashboardPage show={dashReady} layoutJson={layoutJson} pages={pages} wsPush={(ws, topic, status) => wsPush(ws, topic, status)} /> <DashboardPage show={dashReady} layoutJson={layoutJson} pages={pages} wsPush={(ws, topic, status) => wsPush(ws, topic, status)} />
<button class="btn-lg" on:click={() => testBlob()}>{"Test"}</button> <!--<button class="btn-lg" on:click={() => createFinalLayout()}>{"Test"}</button>-->
</Route> </Route>
<Route path="/config"> <Route path="/config">
<ConfigPage show={configReady} configJson={configJson} widgetsJson={widgetsJson} itemsJson={itemsJson} bind:scenarioTxt saveConfig={() => saveConfig()} rebootEsp={() => rebootEsp()} /> <ConfigPage show={configReady} configJson={configJson} widgetsJson={widgetsJson} itemsJson={itemsJson} bind:scenarioTxt saveConfig={() => saveConfig()} rebootEsp={() => rebootEsp()} />