/***************************************************************************** # # # KVMD - The main Pi-KVM daemon. # # # # Copyright (C) 2018 Maxim Devaev # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # *****************************************************************************/ "use strict"; import {tools, $, $$$} from "../tools.js"; import {wm} from "../wm.js"; export function Gpio() { var self = this; /************************************************************************/ var __state = null; /************************************************************************/ self.setState = function(state) { if (state) { for (let channel in state.inputs) { let el = $(`gpio-led-${channel}`); if (el) { __setLedState(el, state.inputs[channel].state); } } for (let channel in state.outputs) { for (let type of ["switch", "button"]) { let el = $(`gpio-${type}-${channel}`); if (el) { wm.switchEnabled(el, state.outputs[channel].online && !state.outputs[channel].busy); } } let el = $(`gpio-switch-${channel}`); if (el) { el.checked = state.outputs[channel].state; } } } else { for (let el of $$$(".gpio-led")) { __setLedState(el, false); } for (let selector of [".gpio-switch", ".gpio-button"]) { for (let el of $$$(selector)) { wm.switchEnabled(el, false); } } } __state = state; }; self.setModel = function(model) { tools.featureSetEnabled($("gpio-dropdown"), model.view.table.length); if (model.view.table.length) { $("gpio-menu-button").innerHTML = `${model.view.header.title} ↴`; } let switches = []; let buttons = []; let content = ""; for (let row of model.view.table) { if (row === null) { content += "

"; } else { content += ""; for (let item of row) { if (item.type === "output") { item.scheme = model.scheme.outputs[item.channel]; } content += ``; } content += ""; } } content += "
${__createItem(item, switches, buttons)}
"; $("gpio-menu").innerHTML = content; for (let channel of switches) { tools.setOnClick($(`gpio-switch-${channel}`), () => __switchChannel(channel)); } for (let channel of buttons) { tools.setOnClick($(`gpio-button-${channel}`), () => __pulseChannel(channel)); } self.setState(__state); }; var __createItem = function(item, switches, buttons) { if (item.type === "label") { return item.text; } else if (item.type === "input") { return ``; } else if (item.type === "output") { let controls = []; if (item.scheme["switch"]) { switches.push(item.channel); controls.push(`
`); } if (item.scheme.pulse.delay) { buttons.push(item.channel); controls.push(``); } return `${controls.join("")}
   
`; } else { return ""; } }; var __setLedState = function(el, state) { if (state) { el.classList.add("led-green"); el.classList.remove("led-gray"); } else { el.classList.add("led-gray"); el.classList.remove("led-green"); } }; var __switchChannel = function(channel) { let to = ($(`gpio-switch-${channel}`).checked ? "1" : "0"); __sendPost(`/api/gpio/switch?channel=${channel}&state=${to}`); }; var __pulseChannel = function(channel) { __sendPost(`/api/gpio/pulse?channel=${channel}`); }; var __sendPost = function(url) { let http = tools.makeRequest("POST", url, function() { if (http.readyState === 4) { if (http.status === 409) { wm.error("Performing another operation for this GPIO channel.
Please try again later"); } else if (http.status !== 200) { wm.error("GPIO error:
", http.responseText); } } }); }; }