-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebPage.h
More file actions
327 lines (278 loc) · 7.93 KB
/
WebPage.h
File metadata and controls
327 lines (278 loc) · 7.93 KB
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
#ifndef WebPage_h
#define WebPage_h
// Info about websockets
// https://javascript.info/websocket
const char HTML_REMOTE_SERIAL_PAGE[] PROGMEM = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Remote Serial</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<style>
body{
background: lightgray;
}
.flex-container {
height: 100vh;
background: black;
}
#content{
overflow-y: auto;
font-family: monospace;
color: lightgreen;
}
#form-controls{
background: darkgray;
}
</style>
</head>
<body>
<div class="container">
<div class="d-flex align-items-start flex-column flex-container" style="">
<div class="mb-auto p-2 w-100" id="content"></div>
<div class="p-2 w-100" id="form-controls">
<div class="form-row align-items-center">
<div class="col-sm-1">
<button id="btnClear" class="btn btn-secondary">Clear</button>
</div>
<div class="col-sm-1">
<button id="btnConnection" class="btn btn-success">Connect</button>
</div>
<div class="col-sm-2">
<div class="form-check text-center">
<input type="checkbox" class="form-check-input connected-only" id="chkAutoScroll" checked>
<label class="form-check-label connected-only" for="chkAutoScroll">Auto scroll</label>
</div>
</div>
<div class="col-sm-7">
<input type="text" id="txtCommand" class="form-control connected-only" placeholder="Enter command..." autocomplete="false">
</div>
<div class="col-sm-1">
<button id="btnSend" class="btn btn-primary connected-only" disabled>Send</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="modal-message" tabindex="-1" role="dialog" aria-labelledby="form-title" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="form-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
<script type="module">
// Ansi colours
const AnsiColour = {
YELLOW: "\x1B[0;93m",
RED: "\x1B[0;91m",
GREEN: "\x1B[0;92m"
}
// import ansi_up from CDN for displaying ansi formatting
import { AnsiUp } from "https://cdn.jsdelivr.net/npm/ansi_up@6.0.0/ansi_up.min.js"
// Ansi colour coding library
const ansi_up = new AnsiUp();
// Id for this session
const sessionId = getSessionGuid();
// IIFE for handling text animation
const textAnimation = (function() {
let timer;
let frames = ["|", "/", "-", "\\"];
let index = 0;
function getFrame() {
let frame = frames[index++];
if (index == frames.length) {
index = 0;
}
return frame;
}
function start(){
var $span = $("<span>").attr("id", "connect-animation");
$("#content").append($span);
timer = setInterval(function(){
$("#connect-animation").text(getFrame());
}, 100);
}
function stop(){
clearTimeout(timer);
$("#connect-animation").remove();
}
return {
Start: start,
Stop: stop
}
})();
// IIFE for handing web socket connection
const client = (function(){
var url = "";
var socket;
function init(webSocketUrl){
url = webSocketUrl;
}
function connect(){
socket = new WebSocket(url);
socket.onopen = client.OnConnect;
socket.onmessage = client.OnMessage;
socket.onclose = client.OnDisconnect;
socket.onerror = client.OnError;
}
function disconnect(){
socket.close();
}
function send(data){
if (socket && socket.readyState == 1){
socket.send(data);
return true;
}
return false;
}
return {
Init: init,
Connect: connect,
Disconnect: disconnect,
Send: send,
OnError: null,
OnMessage: null,
OnConnect: null,
OnDisconnect: null
}
})();
// Connect to host
function connect(){
client.OnConnect = function(e) {
textAnimation.Stop();
$("#btnConnection")
.removeClass("btn-success")
.addClass("btn-danger")
.text("Disconnect")
.data("connected", true)
.attr("disabled", false);
$(".connected-only").attr("disabled", false);
outputMessage(`${AnsiColour.GREEN}${getTimeStamp()} Connected!\n`);
}
client.OnMessage = function(event) {
outputMessage(event.data);
}
client.OnDisconnect = function(event) {
textAnimation.Stop();
$("#btnConnection")
.removeClass("btn-danger")
.addClass("btn-success")
.text("Connect")
.data("connected", false)
.attr("disabled", false);
$(".connected-only").attr("disabled", true);
outputMessage(`${AnsiColour.YELLOW}${getTimeStamp()} Disconnected!\n`);
}
client.OnError = function(error) {
const message = "There was a problem with the connection.";
outputMessage(`${AnsiColour.RED}${getTimeStamp()} ${message}\n`);
showPopup("Communication error", message);
}
outputMessage(`${AnsiColour.GREEN}${getTimeStamp()} Trying to connect...`);
textAnimation.Start();
outputMessage("\n");
client.Connect();
$("#btnConnection").attr("disabled", true);
}
// Output a message to the console div
function outputMessage(message){
const ansi = ansi_up.ansi_to_html(message).replace(/(?:\r\n|\r|\n)/g, "<br>");
const $content = $("#content");
$content.append(ansi);
const autoScroll = $("#chkAutoScroll").is(":checked");
if (autoScroll){
$content.scrollTop($content.prop("scrollHeight"));
}
}
// Close socket
function disconnect(){
client.Disconnect();
}
// Send a message to the server
function sendMessage(){
const $textbox = $("#txtCommand");
const data = $textbox.val();
if (client.Send(data)){
$textbox.val("");
}
else{
console.log(`[send] could not send not connected!`);
showPopup("Error sending message", "Could not send message because you are not connected");
}
}
// Generate a guid useful for handling sessions
// (an guid can be sent to the server to handle which client sent a message)
function getSessionGuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c === "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Show a popup message
function showPopup(title, body){
$("#modal-message").modal("show");
$(".modal-title").text(title);
$(".modal-body").text(body);
setTimeout(
function(){
$("#modal-message").fadeOut("slow", function(){ $("#modal-message").modal("hide"); });
},
5000
);
}
// Generate a timestamp
function getTimeStamp(){
function padZeros(val){
var str = val.toString();
return (str.length < 2 ? "0" : "") + str;
}
var now = new Date();
var ret = padZeros(now.getDate()) + "/" + padZeros(now.getMonth()+1) + "/" + now.getFullYear() + " @ "
+ padZeros(now.getHours()) + ":" + padZeros(now.getMinutes()) + ":" + padZeros(now.getSeconds());
return ret;
}
// Startup stuff
$(function() {
$("#btnSend").click(function(){
sendMessage();
});
$("#txtCommand").on("keypress", function(e) {
if (e.which == 13) {
sendMessage();
}
});
$("#btnConnection").click(function(){
if ($(this).data("connected")){
disconnect();
return;
}
connect();
});
$("#btnClear").click(function(){
$("#content span").remove();
});
$(".connected-only").attr("disabled", true);
client.Init(`ws://${document.location.host}/remoteserialws`);
$("#txtCommand").focus();
connect();
});
</script>
</html>
)rawliteral";
#endif