-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (131 loc) · 5.03 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Olli Movement Simulator controller</title>
</head>
<body>
<h1>Olli Movement Simulator controller</h1>
<div>
<button id="startBtn" type="button" onclick="toggleStart()" disabled>Start</button>
<button id="pauseBtn" type="button" onclick="togglePause()" disabled>Pause</button>
</div>
<div style="margin-top: 25px;">
<span id="msg"> </span>
</div>
<div style="margin-top: 25px;">
<textarea id="textBox" style="height: 228px; width: 95%; max-width: 1000px;"></textarea>
</div>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
var startBtn = document.getElementById("startBtn")
var pauseBtn = document.getElementById("pauseBtn")
var textBox = document.getElementById("textBox")
var msgNode = document.getElementById("msg")
var socket = null
function addText(msg) {
textBox.value += (msg.data || msg) + '\r\n\r\n'
textBox.scrollTop = textBox.scrollHeight
}
function setMsg(msg) {
msgNode.innerText = (msg.data || msg)
}
function getInfo() {
axios.get('/info')
.then(response => {
console.log(response)
var d = response.data || response
addText('config: ' + JSON.stringify(d.config))
addText('status: started=' + d.started + ', paused=' + d.paused)
startBtn.innerText = d.hasOwnProperty('started') && d.started ? 'Stop' : 'Start'
pauseBtn.innerText = d.hasOwnProperty('paused') && d.paused ? 'Continue' : 'Pause'
pauseBtn.disabled = d.hasOwnProperty('started') && !d.started ? true : false
if (d.started) {
websocketConnect()
}
else {
setTimeout(getInfo, 5000)
}
})
}
function toggleStart() {
startBtn.disabled = true
if (startBtn.innerText === 'Start') {
axios.get('/start')
.then(response => {
console.log(response)
startBtn.innerText = 'Stop'
pauseBtn.innerText = 'Pause'
startBtn.disabled = false
pauseBtn.disabled = false
setMsg(response)
if (!socket) {
websocketConnect()
}
})
} else {
axios.get('/stop')
.then(response => {
console.log(response)
startBtn.innerText = 'Start'
startBtn.disabled = false
pauseBtn.disabled = true
setMsg(response)
if (socket) {
socket.close()
socket = null
}
})
}
}
function togglePause() {
pauseBtn.disabled = true
if (pauseBtn.innerText === 'Pause') {
axios.get('/pause')
.then(response => {
console.log(response)
pauseBtn.innerText = 'Continue'
pauseBtn.disabled = false
setMsg(response)
})
} else {
axios.get('/continue')
.then(response => {
console.log(response)
pauseBtn.innerText = 'Pause'
pauseBtn.disabled = false
setMsg(response)
})
}
}
function websocketConnect() {
console.log('Creating socket')
socket = new WebSocket(window.location.protocol.replace('http', 'ws') + '//' + window.location.host)
socket.onopen = function() {
console.log('Socket opened')
}
socket.onclose = function() {
console.log('Socket closed')
socket = null
setTimeout(getInfo, 5000)
}
socket.onerror = function(err) {
console.log('Socket error', err)
socket = null
setTimeout(getInfo, 5000)
}
socket.onmessage = function(message) {
// if (pauseBtn.innerText === 'Pause') {
addText(message.data)
// }
}
}
startBtn.disabled = false;
pauseBtn.disabled = true;
textBox.value = ''
getInfo()
</script>
</body>
</html>