версия с решенными вопросами api взаимодействия веба

This commit is contained in:
Dmitry Borisenko
2022-01-30 17:44:53 +01:00
parent 37c9e9b169
commit 99e1fd0522

View File

@@ -158,14 +158,10 @@
//configuration //configuration
let configJson = []; let configJson = [];
let configJsonBuf = [];
let configJsonFlag = false; let configJsonFlag = false;
let configJsonStart = "/st/config.json";
let widgetsJson = []; let widgetsJson = [];
let widgetsJsonBuf = [];
let widgetsJsonFlag = false; let widgetsJsonFlag = false;
let widgetsJsonStart = "/st/widgets.json";
//web sockets //web sockets
let socket = []; 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 currentPageName = undefined;
let dataReceivingInProgress = false; var configJsonBlob = new MyBlobBuilder();
var widgetsJsonBlob = new MyBlobBuilder();
router.subscribe(handleNavigation); 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******************************************************/ //****************************************************web sockets section******************************************************/
function connectToAllDevices() { function connectToAllDevices() {
@@ -281,49 +309,50 @@
socket[ws].addEventListener("message", function (event) { socket[ws].addEventListener("message", function (event) {
if (typeof event.data === "string") { if (typeof event.data === "string") {
let data = event.data; let data = event.data;
} //сборщик configJson пакетов========================================
if (event.data instanceof Blob) { if (data === "/st/config.json") {
let reader = new FileReader(); if (debug) console.log("[i]", "configJson start!");
reader.readAsText(event.data); configJsonFlag = true;
reader.onload = () => { }
let result = reader.result; if (data === "/end/config.json") {
//сборщик configJson пакетов======================================== if (debug) console.log("[i]", "configJson end!");
if (result === "/st/config.json") { configJsonFlag = false;
configJsonFlag = true; var bb = configJsonBlob.getBlob();
if (debug) console.log("[i]", "configJson start!"); let configJsonReader = new FileReader();
} configJsonReader.readAsText(bb);
if (configJsonFlag && result != "/st/config.json" && result != "/end/config.json") { configJsonReader.onload = () => {
configJsonBuf = configJsonBuf + result; let configJsonResult = configJsonReader.result;
} if (IsJsonParse(configJsonResult)) {
if (result === "/end/config.json") { configJson = JSON.parse(configJsonResult);
configJsonFlag = false;
if (debug) console.log("[i]", "configJson end!");
//if (debug) console.log("[i]", configJsonBuf);
if (IsJsonParse(configJsonBuf)) {
configJson = JSON.parse(configJsonBuf);
configJson = configJson; configJson = configJson;
if (debug) console.log("[i]", "configJson parced!"); if (debug) console.log("[i]", "configJson parced!");
} }
} };
//сборщик widgetsJson пакетов======================================== }
if (result === "/st/widgets.json") { //сборщик widgetsJson пакетов========================================
widgetsJsonFlag = true; if (data === "/st/widgets.json") {
if (debug) console.log("[i]", "widgetsJson start!"); if (debug) console.log("[i]", "widgetsJson start!");
} widgetsJsonFlag = true;
if (widgetsJsonFlag && result != "/st/widgets.json" && result != "/end/widgets.json") { }
widgetsJsonBuf = widgetsJsonBuf + result; if (data === "/end/widgets.json") {
} if (debug) console.log("[i]", "widgetsJson end!");
if (result === "/end/widgets.json") { widgetsJsonFlag = false;
widgetsJsonFlag = false; var bb = widgetsJsonBlob.getBlob();
if (debug) console.log("[i]", "widgetsJson end!"); let widgetsJsonReader = new FileReader();
//if (debug) console.log("[i]", widgetsJsonBuf); widgetsJsonReader.readAsText(bb);
if (IsJsonParse(widgetsJsonBuf)) { widgetsJsonReader.onload = () => {
widgetsJson = JSON.parse(widgetsJsonBuf); let widgetsJsonResult = widgetsJsonReader.result;
if (IsJsonParse(widgetsJsonResult)) {
widgetsJson = JSON.parse(widgetsJsonResult);
widgetsJson = widgetsJson; widgetsJson = widgetsJson;
if (debug) console.log("[i]", "widgetsJson parced!"); 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) => { socket[ws].addEventListener("close", (event) => {
@@ -347,9 +376,11 @@
function clearData() { function clearData() {
configJson = []; configJson = [];
configJsonBuf = []; configJsonBlob.clear();
widgetsJson = []; widgetsJson = [];
widgetsJsonBuf = []; widgetsJsonBlob.clear();
if (debug) console.log("[i]", "all app data cleared");
} }
function wsPush(ws, topic, status) { 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************************************************************/ //****************************************************************json************************************************************/
function getJsonObject(array, number) { function getJsonObject(array, number) {
let num = 0; let num = 0;