Skip to content

Commit

Permalink
Live Server Mode for UI
Browse files Browse the repository at this point in the history
index.js
- add fetchModel and use if 127.0.0.1
- add addModule and use by fetchModel and makeWS

model.json
- add o, fun, p and dash in model
  • Loading branch information
ewoudwijma committed Jul 24, 2024
1 parent b3829f9 commit c1cee5b
Show file tree
Hide file tree
Showing 5 changed files with 1,224 additions and 1,867 deletions.
86 changes: 54 additions & 32 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const pinTypeSpi = 3;
const pinTypeInvalid = UINT8_MAX;
let sysInfo = {};
function getPinType(pinNr) {
if (!sysInfo.pinTypes) return "🔴"; //Live Server Mode
if (sysInfo.pinTypes[pinNr] == pinTypeIO) return "🟢";
else if (sysInfo.pinTypes[pinNr] == pinTypeReadOnly) return "🟠";
else if (sysInfo.pinTypes[pinNr] == pinTypeReserved) return "🟣";
Expand All @@ -40,10 +41,26 @@ function handleVisibilityChange() {
console.log("handleVisibilityChange");
}

//Live Server Mode
async function fetchModel() {
// Mock fetch for testing while using Live Server. No error checking for brevity.
// Replace with call to server websocket
model = await (await fetch('../misc/model.json')).json()

for (let module of model) {
addModule(module)
}
}

function onLoad() {
getTheme();

makeWS();
//aaroneous: run this code if Live Server is invoked
if (window.location.href.includes("127.0.0.1")) { //Live Server Mode
fetchModel();
}
else
makeWS();

initMdlColumns();

Expand Down Expand Up @@ -72,8 +89,37 @@ function ppf() {
// }
logNode.scrollTop = logNode.scrollHeight;
}
else
console.log(arguments);
// else
// console.log(arguments);
}

//used by fetchModel and by makeWS
function addModule(module) {
// let module = json;
console.log("WS receive createHTML", module);
ppf("WS receive createHTML", module.id);
createHTML(module); //no parentNode

if (module.id == "System") {
console.log("system changes", module);
if (module.view)
savedView = module.view;
if (module.theme)
changeHTMLTheme(module.theme);
}

//rerun after each module added
if (window.location.href.includes("4.3.2.1")) //captive portal
changeHTMLView("vSetup"); //captive portal shows setup screen
else if (savedView)
changeHTMLView(savedView);
else
changeHTMLView("vApp"); //default

gId("vApp").value = appName(); //tbd: should be set by server

//send request for onUI
flushOnUICommands();
}

function makeWS() {
Expand Down Expand Up @@ -116,32 +162,8 @@ function makeWS() {
found = true;
}
if (!found) {
let module = json;
model.push((module)); //this is the model
console.log("WS receive createHTML", module);
ppf("WS receive createHTML", module.id);
createHTML(module); //no parentNode

if (module.id == "System") {
console.log("system changes", module);
if (module.view)
savedView = module.view;
if (module.theme)
changeHTMLTheme(module.theme);
}

//rerun after each module added
if (window.location.href.includes("4.3.2.1")) //captive portal
changeHTMLView("vSetup"); //captive portal shows setup screen
else if (savedView)
changeHTMLView(savedView);
else
changeHTMLView("vApp"); //default

gId("vApp").value = appName(); //tbd: should be set by server

//send request for onUI
flushOnUICommands();
model.push((json)); //this is the model
addModule(json);
}
else
console.log("html of module already generated", json);
Expand Down Expand Up @@ -999,11 +1021,11 @@ function changeHTML(variable, commandJson, rowNr = UINT8_MAX) {
//hide/show disabled/enabled modules
if (variable.id == "mdlEnabled") {
let nameVar = findVar("mdlName");
let mdlNode = gId(nameVar.value[newRowNr]);
let mdlNode = nameVar.value?gId(nameVar.value[newRowNr]):null; //Live Server Mode: no value
// console.log("mdlEnabled", variable, node, newValue, newRowNr, nameVar, mdlNode);
if (mdlNode) {
if (mdlNode.hidden && newValue) mdlNode.hidden = false;
if (!mdlNode.hidden && !newValue) mdlNode.hidden = true;
if (mdlNode.hidden && newValue) mdlNode.hidden = false;
if (!mdlNode.hidden && !newValue) mdlNode.hidden = true;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions data/newui.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class NewApp {
init() {
// Update the copyright notice in the footer
// Fetch data from the server
this.#fetchData() //async
this.#fetchModel() //async
}

async #fetchData() {
async #fetchModel() {
// Mock fetch for testing while using Live Server. No error checking for brevity.
// Replace with call to server websocket
this.#modules = await (await fetch('../misc/model.json')).json()
Expand Down Expand Up @@ -172,7 +172,7 @@ function onLoad() {

}

//used by NewApp and by makeWS
//used by fetchModel and by makeWS
function addModule(module) {
// let module = json;
model.push((module)); //this is the model
Expand Down
Loading

0 comments on commit c1cee5b

Please sign in to comment.