forked from micropython/webrepl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
425 lines (362 loc) · 11.6 KB
/
Copy pathindex.html
File metadata and controls
425 lines (362 loc) · 11.6 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE html>
<html>
<head>
<title>MicroPython WebREPL</title>
<script src="https://edrys-labs.github.io/module/edrys.js"></script>
<meta
name="description"
content="Allows to connect to a webrepl via a WebSocket.<br/> Form more information, visit: <a href='https://github.com/edrys-labs/module-micropython-webrepl' target='_blank'>https://github.com/edrys-labs/module-micropython-webrepl</a>"
/>
<meta name="show-in" content="station" />
<meta
name="module-config"
content="{
'stationConfig': {
'websocket': {
'type': string,
'hint': 'URL of the WebSocket server',
},
'execute': {
'type': string,
'hint': 'Message to listen for (default: execute)',
},
'self_charge_alvik': {
'type': boolean,
'hint': 'Run self charge script when all users leave the station',
}
}
}"
/>
<script
defer
src="https://edrys-labs.github.io/module/vendor/alpine.min.js"
></script>
<link
rel="stylesheet"
href="https://edrys-labs.github.io/module/vendor/water.min.css"
/>
<style>
body {
min-width: calc(100% - 20px);
overflow: hidden;
}
/* Student-specific layout */
body.student-view #term {
height: calc(100vh - 120px) !important;
}
body.student-view #url {
display: none;
}
body.student-view .file-box:not(.connection-box) {
display: none;
}
body.student-view .connection-box {
flex: 1 1 100%;
text-align: center;
}
input {
display: inline;
}
input[type='text'],
input[type='url'] {
width: calc(100% - 20px);
}
#file-boxes {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 10px;
justify-content: space-between;
width: 100%;
align-items: stretch;
}
.file-box {
margin-bottom: 15px;
flex: 1 1 calc(25% - 20px);
box-sizing: border-box;
border: 2px black solid;
padding: 5px;
padding-top: 10px;
}
.box-title {
font-weight: bold;
margin-bottom: 8px;
padding: 4px 8px;
background: linear-gradient(135deg, #013b4e, #1a5d78);
color: white;
border-radius: 4px;
font-size: 13px;
text-align: center;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin: -5px -5px 10px -5px;
border-bottom: 2px solid #001f2b;
}
#term {
display: block;
min-width: 100%;
background-color: black;
border: 1px solid;
overflow-y: auto;
}
#file-status {
transition: all 0.3s ease;
}
#file-status.highlight {
background-color: #4caf5066;
padding: 4px 8px;
transform: scale(1.05);
box-shadow: 0 2px 8px rgba(76, 175, 80, 0.4);
}
</style>
<script src="term.js"></script>
<script src="FileSaver.js"></script>
</head>
<body>
<div id="file-boxes">
<form class="file-box connection-box">
<div class="box-title">WebSocket Connection</div>
<input type="url" name="webrepl_url" id="url" value="" />
<input
type="submit"
id="button"
value="Connect"
onclick="button_click(); return false;"
/>
</form>
<div class="file-box" id="put-file-list">
<div class="box-title">Send a file</div>
<input type="file" id="put-file-select" />
<input
type="button"
value="Send to device"
id="put-file-button"
onclick="put_file(); return false;"
/>
</div>
<div class="file-box">
<div class="box-title">Get a file</div>
<input
type="text"
name="get_filename"
id="get_filename"
value=""
size="13"
/>
<input
type="button"
value="Get from device"
onclick="get_file(); return false;"
/>
</div>
<div class="file-box">
<div class="box-title">File status</div>
<span id="file-status">(file operation status)</span>
</div>
<div class="file-box">
<div class="box-title">Self Charge</div>
<input
type="button"
value="Run Self Charge"
onclick="runSelfCharge(); return false;"
/>
</div>
</div>
<div id="term"></div>
<div>
<i
>Terminal widget should be focused (text cursor visible) to accept
input. Click on it if not. To paste, press Ctrl+A, then Ctrl+V</i
>
</div>
</body>
<script>
const urlInput = document.getElementById('url')
const fileStatus = document.getElementById('file-status')
const connectionButton = document.getElementById('button')
var execute = 'execute'
let lastUsersCount = 0
Edrys.onMessage((message) => {
if (message.subject === execute && Edrys.role === 'station') {
//console.log('execute', message.body)
// Upload code as temporary file and execute
let code = message.body
let filename = 'temp_code.py'
// Convert string to binary data for file upload
let encoder = new TextEncoder()
let data = encoder.encode(code)
// Upload the file first
mimic_put_file_select(filename, data)
// Wait for upload to complete, then execute
setTimeout(() => {
window.term.emit('data', `exec(open('${filename}').read())\r`)
// Clean up temporary file
setTimeout(() => {
window.term.emit('data', `import os; os.remove('${filename}')\r`)
}, 1000)
}, 2000)
}
if (message.subject === 'run_self_charge' && Edrys.role === 'station') {
window.term.emit('data', `exec(open('self_charge.py').read())\r`)
}
if (message.from === Edrys.liveUser.name) {
return
}
if (message.subject === 'write' && Edrys.role !== 'station') {
term.write(message.body)
}
if (message.subject === 'data' && Edrys.role === 'station') {
window.termOnData(message.body)
}
if (message.subject === 'put_file' && Edrys.role === 'station') {
console.log('Put file', message.body)
mimic_put_file_select(message.body.filename, message.body.data)
}
if (message.subject === 'get_file' && Edrys.role === 'station') {
console.log('Get file', message.body)
get_file(message.body)
}
if (message.subject === 'got_file') {
//fileStatus.innerHTML = message.body
console.warn('Got file', message.body)
}
}, (promiscuous = true))
Edrys.onReady(() => {
if (Edrys.role === 'student') {
document.body.classList.add('student-view')
// Force terminal resize for student view
setTimeout(() => {
if (window.term) {
var size = calculate_size(window)
window.term.resize(size[0], size[1])
}
}, 100)
}
if (Edrys.role !== 'station') {
const clickEvent = button_click
button_click = () => {
const connect = Edrys.getState('connect', 'Value')
Edrys.getState('connect', 'Value', !connect)
clickEvent()
}
if (!Edrys.getState('connect', 'Value')) {
connect('')
}
}
if (Edrys.role === 'station') {
execute = Edrys.module.stationConfig.execute || 'execute'
}
Edrys.updateState(() => {
if (Edrys.getState('url', 'Value') == undefined) {
Edrys.getState(
'url',
'Value',
Edrys.module.stationConfig.websocket || 'ws://'
)
}
if (Edrys.getState('file_status', 'Value') == undefined) {
Edrys.getState('file_status', 'Value', fileStatus.innerHTML)
}
if (Edrys.getState('connect', 'Value') == undefined) {
Edrys.getState(
'connect',
'Value',
connectionButton.value === 'Connect'
)
} else if (Edrys.getState('connect', 'Value') === false) {
connect('')
}
urlInput.value = Edrys.getState('url', 'Value')
urlInput.oninput = (e) => {
Edrys.getState('url', 'Value', e.target.value)
}
fileStatus.innerHTML = Edrys.getState('file_status', 'Value')
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
const state = Edrys.getState('file_status', 'Value')
if (state !== fileStatus.innerHTML) {
Edrys.getState('file_status', 'Value', fileStatus.innerHTML)
}
}
}
})
observer.observe(fileStatus, { childList: true, subtree: true })
connectionButton.value =
Edrys.getState('connect', 'Value') === true ? 'Connect' : 'Disconnect'
const observer2 = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.attributeName === 'value') {
const state = Edrys.getState('connect', 'Value')
if (state !== (connectionButton.value === 'Connect')) {
Edrys.getState(
'connect',
'Value',
connectionButton.value === 'Connect'
)
}
}
}
})
// Start observing the button for attribute changes
observer2.observe(connectionButton, { attributes: true })
})
if (Edrys.module?.stationConfig?.self_charge_alvik) {
checkUsersInStation();
setInterval(checkUsersInStation, 5000);
}
})
Edrys.onUpdate(() => {
console.warn('Update')
urlInput.value = Edrys.liveRoom.url || ''
fileStatus.innerHTML = Edrys.liveRoom.file_status || ''
const connect = Edrys.liveRoom.connect === true ? 'Connect' : 'Disconnect'
if (Edrys.role === 'station') {
console.log('Role is station', connectionButton.value, connect)
if (connectionButton.value !== connect) {
console.log('Connecting')
button_click()
}
} else {
connectionButton.value = connect
}
console.dir(Edrys.liveRoom)
})
function runSelfCharge() {
if (Edrys.role === 'station') {
window.term.emit('data', `exec(open('self_charge.py').read())\r`)
} else {
Edrys.sendMessage('run_self_charge', '')
}
}
// When no users are in the station, run self charging script
function checkUsersInStation() {
if (Edrys.role !== 'station') {
return;
}
if (!Edrys.liveClass?.users) {
return;
}
const currentStationUsers = Object.values(Edrys.liveClass.users).filter(
(user) => {
return (
user &&
user.displayName &&
user.room &&
!user.displayName.startsWith("Station") &&
user.room.startsWith("Station")
);
}
);
const currentCount = currentStationUsers.length;
if (currentCount === 0 && lastUsersCount > 0) {
if (window.ws && window.ws.readyState === WebSocket.OPEN) {
console.log('No users in station, running self charge script');
runSelfCharge();
}
}
lastUsersCount = currentCount;
};
</script>
<script src="webrepl.js"></script>
</html>