diff options
author | Maxim Devaev <[email protected]> | 2024-10-19 08:59:52 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2024-10-19 08:59:52 +0300 |
commit | 90d8e745e39b5e49fcb1d50c1efa95148593496b (patch) | |
tree | f098894e16275d8fe80b8cb16bb615ff3a106e5e /web | |
parent | 3852d0a4568c3726d0182f3b00a9a3e8bb7e8af9 (diff) |
gpio diff events mode
Diffstat (limited to 'web')
-rw-r--r-- | web/share/js/kvm/gpio.js | 60 | ||||
-rw-r--r-- | web/share/js/kvm/session.js | 3 |
2 files changed, 37 insertions, 26 deletions
diff --git a/web/share/js/kvm/gpio.js b/web/share/js/kvm/gpio.js index fea1af32..9e237a54 100644 --- a/web/share/js/kvm/gpio.js +++ b/web/share/js/kvm/gpio.js @@ -32,17 +32,41 @@ export function Gpio(__recorder) { /************************************************************************/ - var __state = null; + var __has_model = false; /************************************************************************/ self.setState = function(state) { if (state) { + if (state.model) { + __applyModel(state.model); + __has_model = true; + } + if (__has_model && state.state) { + __applyState(state.state); + } + } else { + for (let el of $$("__gpio-led")) { + __setLedState(el, false); + } + for (let selector of ["__gpio-switch", "__gpio-button"]) { + for (let el of $$(selector)) { + tools.el.setEnabled(el, false); + } + } + __has_model = false; + } + }; + + var __applyState = function(state) { + if (state.inputs) { for (let ch in state.inputs) { for (let el of $$(`__gpio-led-${ch}`)) { __setLedState(el, state.inputs[ch].state); } } + } + if (state.outputs) { for (let ch in state.outputs) { for (let type of ["switch", "button"]) { for (let el of $$(`__gpio-${type}-${ch}`)) { @@ -53,20 +77,10 @@ export function Gpio(__recorder) { el.checked = state.outputs[ch].state; } } - } else { - for (let el of $$("__gpio-led")) { - __setLedState(el, false); - } - for (let selector of ["__gpio-switch", "__gpio-button"]) { - for (let el of $$(selector)) { - tools.el.setEnabled(el, false); - } - } } - __state = state; }; - self.setModel = function(model) { + var __applyModel = function(model) { tools.feature.setEnabled($("gpio-dropdown"), model.view.table.length); if (model.view.table.length) { let title = []; @@ -81,23 +95,23 @@ export function Gpio(__recorder) { $("gpio-menu-button").innerHTML = title.join(" "); } - let content = "<table class=\"kv\">"; + let html = "<table class=\"kv\">"; for (let row of model.view.table) { if (row === null) { - content += "</table><hr><table class=\"kv\">"; + html += "</table><hr><table class=\"kv\">"; } else { - content += "<tr>"; + html += "<tr>"; for (let item of row) { if (item.type === "output") { item.scheme = model.scheme.outputs[item.channel]; } - content += `<td align="center">${__createItem(item)}</td>`; + html += `<td align="center">${__createItem(item)}</td>`; } - content += "</tr>"; + html += "</tr>"; } } - content += "</table>"; - $("gpio-menu").innerHTML = content; + html += "</table>"; + $("gpio-menu").innerHTML = html; for (let ch in model.scheme.outputs) { for (let el of $$(`__gpio-switch-${ch}`)) { @@ -111,8 +125,6 @@ export function Gpio(__recorder) { tools.feature.setEnabled($("v3-usb-breaker"), ("__v3_usb_breaker__" in model.scheme.outputs)); tools.feature.setEnabled($("v4-locator"), ("__v4_locator__" in model.scheme.outputs)); tools.feature.setEnabled($("system-tool-wol"), ("__wol__" in model.scheme.outputs)); - - self.setState(__state); }; var __createItem = function(item) { @@ -167,9 +179,9 @@ export function Gpio(__recorder) { } }; - var __setLedState = function(el, state) { + var __setLedState = function(el, on) { let color = el.getAttribute("data-color"); - if (state) { + if (on) { el.classList.add(`led-${color}`); el.classList.remove("led-gray"); } else { @@ -221,7 +233,7 @@ export function Gpio(__recorder) { var __sendPost = function(url, params) { tools.httpPost(url, params, function(http) { if (http.status === 409) { - wm.error("Performing another operation for this GPIO channel.<br>Please try again later"); + wm.error("Performing another operation for this GPIO channel.<br>Please try again later."); } else if (http.status !== 200) { wm.error("GPIO error", http.responseText); } diff --git a/web/share/js/kvm/session.js b/web/share/js/kvm/session.js index 8100f50c..353815da 100644 --- a/web/share/js/kvm/session.js +++ b/web/share/js/kvm/session.js @@ -282,7 +282,7 @@ export function Session() { tools.httpGet("/api/auth/check", null, function(http) { if (http.status === 200) { - __ws = new WebSocket(`${tools.is_https ? "wss" : "ws"}://${location.host}/api/ws`); + __ws = new WebSocket(`${tools.is_https ? "wss" : "ws"}://${location.host}/api/ws?legacy=0`); __ws.sendHidEvent = (event) => __sendHidEvent(__ws, event.event_type, event.event); __ws.onopen = __wsOpenHandler; __ws.onmessage = __wsMessageHandler; @@ -359,7 +359,6 @@ export function Session() { case "info_fan_state": __setAboutInfoFan(data.event); break; case "info_system_state": __setAboutInfoSystem(data.event); break; case "info_extras_state": __setExtras(data.event); break; - case "gpio_model_state": __gpio.setModel(data.event); break; case "gpio_state": __gpio.setState(data.event); break; case "hid_keymaps_state": __hid.setKeymaps(data.event); break; case "hid_state": __hid.setState(data.event); break; |