-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
home.html
346 lines (304 loc) · 9.91 KB
/
home.html
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="en">
<head>
<title>Arduino Cloud Agent Debug Console</title>
<link
href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700&display=swap"
rel="stylesheet"
/>
<link
href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,600,700&display=swap"
rel="stylesheet"
/>
<script
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.min.js"
></script>
<script type="text/javascript">
const LOCAL_STORAGE_KEY = "ArduinoAgentListVisibility";
let messages = []; // The messages to show in the main log.
document.addEventListener("DOMContentLoaded", function () {
let listMsgVisibility = getListMsgVisibility();
updateListMsgVisibility(listMsgVisibility);
const socket = setupWebsocket();
// Handle the form submission and send the message to the websocket.
document
.getElementById("form")
.addEventListener("submit", function (event) {
event.preventDefault(); // Stop the from from actually submitting.
if (!socket) {
return false;
}
let input = document.getElementById("input");
if (!input.value) {
return false;
}
socket.emit("command", input.value);
input.value = "";
});
});
function getListMsgVisibility() {
// Check if the list visibility setting is saved in the localStorage.
let savedSetting = localStorage.getItem(LOCAL_STORAGE_KEY);
let listCommand = savedSetting != null ? parseInt(savedSetting) : 1; // Default: Show list commands inline.
document.getElementById("listShow").value = listCommand;
return listCommand;
}
function onListMsgVisibilityChange() {
let listCommand = document.getElementById("listShow").value;
localStorage.setItem(LOCAL_STORAGE_KEY, "" + listCommand); // Save the setting for future use.
// Update the rest of the UI so that it reflects the selected setting.
updateListMsgVisibility(listCommand);
}
function updateListMsgVisibility(visibility) {
const element = document.getElementById("log-list");
if (visibility == 2) {
element.innerHTML = ""; // Clear the "list" log.
element.style.display = "block"; // Make sure the "list" log UI is visible.
} else {
element.style.display = "none"; // Make sure the "list" log UI is hidden.
}
}
function setupWebsocket() {
let socket;
if (window["WebSocket"]) {
if (window.location.protocol === "https:") {
socket = io("https://{{$}}");
} else {
socket = io("http://{{$}}");
}
socket.on("disconnect", function (evt) {
appendLog("Connection closed.");
});
socket.on("message", function (evt) {
appendLog(evt);
});
} else {
appendLog("Your browser does not support WebSockets.");
}
return socket;
}
function appendLog(msg) {
const MESSAGES_MAX_COUNT = 2000;
let jsonMsg = {};
let portsListing = false;
try {
// Try to parse the received message as JSON, and then check if it contains a "Ports" property.
jsonMsg = JSON.parse(msg);
portsListing = jsonMsg.Ports !== undefined;
} catch {
// no valid json
}
// This is a "list" message if it starts with "list" or if it's valid JSON and has a "Ports" property.
let isListMessage = portsListing || msg.indexOf("list") == 0;
// Get the current setting for the "list" message visibility.
const listMsgVisibility = document.getElementById("listShow").value;
// If this is a "LIST" command and we want to hide them. Skip.
if (isListMessage && listMsgVisibility == 0) return;
let printMsg = msg;
if (jsonMsg.Ports) {
const validKeys = [
"Name",
"SerialNumber",
"IsOpen",
"VendorID",
"ProductID",
];
printMsg =
"Serial Ports:\n" + JSON.stringify(jsonMsg.Ports, validKeys, 2);
} else if (Object.keys(jsonMsg).length !== 0) {
printMsg = JSON.stringify(jsonMsg, undefined, 2);
}
// when parsing JSON we're escaping some html charaters like "&<>", we want to show their
// original value in the log
function decode(str) {
let txt = new DOMParser().parseFromString(str, "text/html");
return txt.documentElement.textContent;
}
printMsg = decode(printMsg);
const log1 = document.getElementById("log");
const log2 = document.getElementById("log-list");
const autoscroll = document.getElementById("autoscroll");
// Check if this is a "LIST" message and needs to be shown in a separate log.
if (isListMessage && listMsgVisibility == 2) {
// Show the "list" message in the specific log element. Replace any previous content.
log2.textContent = "list\n\n" + printMsg;
} else {
// Show the message in the log element.
messages.push(printMsg);
if (messages.length > MESSAGES_MAX_COUNT) {
messages.shift();
}
log1.textContent = messages.join("\n\n");
if (autoscroll.checked) {
log1.scrollTop = log1.scrollHeight - log1.clientHeight;
}
}
}
function clearLogs() {
const log1 = document.getElementById("log");
const log2 = document.getElementById("log-list");
messages = [];
log1.innerHTML = "";
log2.innerHTML = "";
}
function exportLogs() {
const link = document.createElement("a");
link.setAttribute("download", "agent-log.txt");
const text = document.getElementById("log").textContent;
link.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
);
link.click();
}
</script>
<style type="text/css">
html,
body {
overflow: hidden;
height: 100%;
}
body {
margin: 0px;
padding: 0px;
background: #f8f9f9;
font-size: 16px;
font-family: "Open Sans", "Lucida Grande", Lucida, Verdana, sans-serif;
}
#container {
display: flex;
flex-direction: column;
height: 100vh;
width: 100%;
}
.logs {
display: flex;
gap: 10px;
flex: 1;
overflow-y: auto;
margin: 15px 15px 10px;
font-family: "Roboto Mono", "Courier", "Lucida Grande", Verdana,
sans-serif;
}
.logs pre {
background-color: #dae3e3;
padding: 8px 10px;
margin: 0;
overflow-y: auto;
}
#log {
flex-basis: 65%;
flex-grow: 1;
}
#log-list {
flex-basis: 35%;
}
#footer {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
justify-content: space-between;
margin: 0px 15px 0px;
}
#form {
display: flex;
flex-grow: 1;
margin-bottom: 15px;
}
#input {
flex-grow: 1;
}
#secondary-controls div {
display: inline-block;
padding: 10px 15px;
}
#autoscroll,
#list {
vertical-align: middle;
width: 20px;
height: 20px;
}
#secondary-controls .button {
margin-bottom: 15px;
vertical-align: top;
height: 36px;
}
.button {
background-color: #b5c8c9;
border: 1px solid #b5c8c9;
border-radius: 2px 2px 0 0;
box-shadow: 0 4px #95a5a6;
margin-bottom: 4px;
color: #000;
cursor: pointer;
font-size: 14px;
letter-spacing: 1.28px;
line-height: normal;
outline: none;
padding: 9px 18px;
text-align: center;
text-transform: uppercase;
transition: box-shadow 0.1s ease-out, transform 0.1s ease-out;
}
.button:hover {
box-shadow: 0 2px #95a5a6;
outline: none;
transform: translateY(2px);
}
.button:active {
box-shadow: none;
transform: translateY(4px);
}
.textfield {
background-color: #dae3e3;
width: auto;
height: auto;
padding: 10px 8px;
margin-left: 8px;
vertical-align: top;
border: none;
font-family: "Open Sans", "Lucida Grande", Lucida, Verdana, sans-serif;
font-size: 1em;
outline: none;
}
</style>
</head>
<body>
<div id="container">
<div class="logs">
<pre id="log"></pre>
<pre id="log-list"></pre>
</div>
<div id="footer">
<form id="form">
<input type="submit" class="button" value="Send" />
<input
type="text"
id="input"
class="textfield"
autocomplete="off"
autofocus
/>
</form>
<div id="secondary-controls">
<div>
<input type="checkbox" checked id="autoscroll" />
<label for="autoscroll">Autoscroll</label>
</div>
<select
id="listShow"
class="button"
onchange="onListMsgVisibilityChange()"
>
<option value="0">Hide 'list' commands</option>
<option value="1" selected>Show 'list' commands inline</option>
<option value="2">Show 'list' commands separately</option>
</select>
<button class="button" onclick="clearLogs()">Clear Log</button>
<button class="button" onclick="exportLogs()">Export Log</button>
</div>
</div>
</div>
</body>
</html>