1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/*****************************************************************************
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2024 Maxim Devaev <[email protected]> #
# #
# 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 <https://www.gnu.org/licenses/>. #
# #
*****************************************************************************/
"use strict";
import {tools, $, $$} from "../tools.js";
import {wm} from "../wm.js";
export function Gpio(__recorder) {
var self = this;
/************************************************************************/
var __state = null;
/************************************************************************/
self.setState = function(state) {
if (state) {
for (let ch in state.inputs) {
for (let el of $$(`__gpio-led-${ch}`)) {
__setLedState(el, state.inputs[ch].state);
}
}
for (let ch in state.outputs) {
for (let type of ["switch", "button"]) {
for (let el of $$(`__gpio-${type}-${ch}`)) {
tools.el.setEnabled(el, state.outputs[ch].online && !state.outputs[ch].busy);
}
}
for (let el of $$(`__gpio-switch-${ch}`)) {
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) {
tools.feature.setEnabled($("gpio-dropdown"), model.view.table.length);
if (model.view.table.length) {
let title = [];
let last_is_label = false;
for (let item of model.view.header.title) {
if (last_is_label && item.type === "label") {
title.push("<span></span>");
}
last_is_label = (item.type === "label");
title.push(__createItem(item));
}
$("gpio-menu-button").innerHTML = title.join(" ");
}
let content = "<table class=\"kv\">";
for (let row of model.view.table) {
if (row === null) {
content += "</table><hr><table class=\"kv\">";
} else {
content += "<tr>";
for (let item of row) {
if (item.type === "output") {
item.scheme = model.scheme.outputs[item.channel];
}
content += `<td align="center">${__createItem(item)}</td>`;
}
content += "</tr>";
}
}
content += "</table>";
$("gpio-menu").innerHTML = content;
for (let ch in model.scheme.outputs) {
for (let el of $$(`__gpio-switch-${ch}`)) {
tools.el.setOnClick(el, tools.partial(__switchChannel, el));
}
for (let el of $$(`__gpio-button-${ch}`)) {
tools.el.setOnClick(el, tools.partial(__pulseChannel, el));
}
}
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) {
if (item.type === "label") {
return item.text;
} else if (item.type === "input") {
return `
<img
class="__gpio-led __gpio-led-${item.channel} inline-lamp-big led-gray"
src="/share/svg/led-circle.svg"
data-color="${item.color}"
/>
`;
} else if (item.type === "output") {
let controls = [];
let confirm = (item.confirm ? "Are you sure you want to perform this action?" : "");
if (item.scheme["switch"]) {
let id = tools.makeId();
controls.push(`
<td><div class="switch-box">
<input
disabled
type="checkbox"
id="__gpio-switch-${id}"
class="__gpio-switch __gpio-switch-${item.channel}"
data-channel="${item.channel}"
data-confirm="${confirm}"
/>
<label for="__gpio-switch-${id}">
<span class="switch-inner"></span>
<span class="switch"></span>
</label>
</div></td>
`);
}
if (item.scheme.pulse.delay) {
controls.push(`
<td><button
disabled
class="__gpio-button __gpio-button-${item.channel}"
${item.hide ? "data-force-hide-menu" : ""}
data-channel="${item.channel}"
data-confirm="${confirm}"
>
${(item.hide ? "• " : "") + item.text}
</button></td>
`);
}
return `<table><tr>${controls.join("<td> </td>")}</tr></table>`;
} else {
return "";
}
};
var __setLedState = function(el, state) {
let color = el.getAttribute("data-color");
if (state) {
el.classList.add(`led-${color}`);
el.classList.remove("led-gray");
} else {
el.classList.add("led-gray");
el.classList.remove(`led-${color}`);
}
};
var __switchChannel = function(el) {
let ch = el.getAttribute("data-channel");
let confirm = el.getAttribute("data-confirm");
let to = (el.checked ? "1" : "0");
if (to === "0" && el.hasAttribute("data-confirm-off")) {
confirm = el.getAttribute("data-confirm-off");
}
let act = () => {
__sendPost("/api/gpio/switch", {"channel": ch, "state": to});
__recorder.recordGpioSwitchEvent(ch, to);
};
if (confirm) {
wm.confirm(tools.escape(confirm)).then(function(ok) {
if (ok) {
act();
}
});
} else {
act();
}
};
var __pulseChannel = function(el) {
let ch = el.getAttribute("data-channel");
let confirm = el.getAttribute("data-confirm");
let act = () => {
__sendPost("/api/gpio/pulse", {"channel": ch});
__recorder.recordGpioPulseEvent(ch);
};
if (confirm) {
wm.confirm(tools.escape(confirm)).then(function(ok) {
if (ok) {
act();
}
});
} else {
act();
}
};
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");
} else if (http.status !== 200) {
wm.error("GPIO error", http.responseText);
}
});
};
}
|