настроил общение через web sockets

This commit is contained in:
Dmitry Borisenko
2022-01-22 00:15:22 +01:00
parent 40acb5570f
commit b249897162

View File

@@ -159,9 +159,15 @@
//configuration //configuration
let configJson = []; let configJson = [];
let configJsonBuf = []; let configJsonBuf = [];
let configJsonFlag = false;
let configJsonStart = "/st/config.json";
let configJsonEnd = "/end/config.json";
let widgetCollection = []; let widgetsJson = [];
let widgetCollectionBuf = []; let widgetsJsonBuf = [];
let widgetsJsonFlag = false;
let widgetsJsonStart = "/st/widgets.json";
let widgetsJsonEnd = "/end/widgets.json";
//web sockets //web sockets
let socket = []; let socket = [];
@@ -180,12 +186,6 @@
ip: "192.168.88.235", ip: "192.168.88.235",
status: false, status: false,
}, },
{
name: "Устройство 2",
id: "987654321",
ip: "192.168.88.233",
status: false,
},
]; ];
//navigation //navigation
@@ -282,39 +282,52 @@
socket[ws].addEventListener("message", function (event) { socket[ws].addEventListener("message", function (event) {
let data = event.data.toString(); let data = event.data.toString();
//if (debug) console.log("[i]", "data:", data); //if (debug) console.log("[i]", "data:", data);
if (data.includes("[log]")) { //if (data.includes("[log]")) {
data = data.replace("[log]", ""); // data = data.replace("[log]", "");
addCoreMsg(data); // addCoreMsg(data);
//if (debug) console.log("[i]", "log data:", data); //}
} else if (data.includes("/config.json")) { //сборщик configJson пакетов========================================
dataReceivingInProgress = true; if (data === configJsonStart) {
data = data.replace("/config.json", ""); if (debug) console.log("[i]", "start receiving configJson");
configJsonBuf = configJsonBuf + data; configJsonFlag = true;
if (data.includes("]}")) { configJsonBuf = [];
configJsonBuf = configJsonBuf.replace("]}", "]"); configJson = [];
if (IsJsonParse(configJsonBuf)) {
configJson = JSON.parse(configJsonBuf);
configJsonBuf = [];
configJson = configJson;
dataReceivingInProgress = false;
if (debug) console.log("[i]", "configJson parsed");
}
}
} else if (data.includes("/widgets.json")) {
dataReceivingInProgress = true;
data = data.replace("/widgets.json", "");
widgetCollectionBuf = widgetCollectionBuf + data;
if (data.includes("]}")) {
widgetCollectionBuf = widgetCollectionBuf.replace("]}", "]");
if (IsJsonParse(widgetCollectionBuf)) {
widgetCollection = JSON.parse(widgetCollectionBuf);
widgetCollectionBuf = [];
widgetCollection = widgetCollection;
dataReceivingInProgress = false;
if (debug) console.log("[i]", "widgetCollection parsed");
}
}
} }
if (configJsonFlag && data != configJsonStart && data != configJsonEnd) {
//data = strdecode(data);
configJsonBuf = configJsonBuf + data;
}
if (data === configJsonEnd) {
if (debug) console.log("[i]", "comleted receiving configJson");
configJsonFlag = false;
if (IsJsonParse(configJsonBuf)) {
configJson = JSON.parse(configJsonBuf);
configJson = configJson;
if (debug) console.log("[i]", "configJson parced!");
}
configJsonBuf = [];
}
//сборщик widgetsJson пакетов========================================
if (data === widgetsJsonStart) {
if (debug) console.log("[i]", "start receiving widgetsJson");
widgetsJsonFlag = true;
widgetsJsonBuf = [];
widgetsJson = [];
}
if (widgetsJsonFlag && data != widgetsJsonStart && data != widgetsJsonEnd) {
widgetsJsonBuf = widgetsJsonBuf + data;
}
if (data === widgetsJsonEnd) {
if (debug) console.log("[i]", "comleted receiving widgetsJson");
widgetsJsonFlag = false;
if (IsJsonParse(widgetsJsonBuf)) {
widgetsJson = JSON.parse(widgetsJsonBuf);
widgetsJson = widgetsJson;
if (debug) console.log("[i]", "widgetsJson parced!");
}
widgetsJsonBuf = [];
}
//====================================================================
}); });
socket[ws].addEventListener("close", (event) => { socket[ws].addEventListener("close", (event) => {
if (debug) console.log("[e]", ip, "connection closed"); if (debug) console.log("[e]", ip, "connection closed");
@@ -331,15 +344,24 @@
function sendConfigJson() { function sendConfigJson() {
wsSendMsg(wsSelected, "/gifnoc.json" + JSON.stringify(configJson)); wsSendMsg(wsSelected, "/gifnoc.json" + JSON.stringify(configJson));
//wsSendMsg(wsSelected, "/gifnoc.json" + strencode(configJson));
clearData(); clearData();
sendCurrentPageName(); sendCurrentPageName();
} }
function strencode(data) {
return unescape(encodeURIComponent(JSON.stringify(data)));
}
function strdecode(data) {
return JSON.parse(decodeURIComponent(escape(data)));
}
function clearData() { function clearData() {
configJson = []; configJson = [];
configJsonBuf = []; configJsonBuf = [];
widgetCollection = []; widgetsJson = [];
widgetCollectionBuf = []; widgetsJsonBuf = [];
} }
function wsPush(ws, topic, status) { function wsPush(ws, topic, status) {
@@ -495,7 +517,7 @@
//пример как формировать массив json //пример как формировать массив json
function createWidgetsDropdown() { function createWidgetsDropdown() {
let widgetsDropdown = []; let widgetsDropdown = [];
widgetCollection.forEach((widget) => { widgetsJson.forEach((widget) => {
widgetsDropdown.push({ widgetsDropdown.push({
id: widget.name, id: widget.name,
val: widget.rus, val: widget.rus,
@@ -670,7 +692,7 @@
<td class="tbl-bdy"><input bind:value={element.id} class="tbl-ipt w-full" type="text" /></td> <td class="tbl-bdy"><input bind:value={element.id} class="tbl-ipt w-full" type="text" /></td>
<td class="tbl-bdy" <td class="tbl-bdy"
><select bind:value={element.widget} class="tbl-ipt w-full"> ><select bind:value={element.widget} class="tbl-ipt w-full">
{#each widgetCollection as select} {#each widgetsJson as select}
<option value={select.name}> <option value={select.name}>
{select.label} {select.label}
</option> </option>