-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
336 lines (303 loc) · 10.6 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Gobot-Pion</title>
<link
href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet"
/>
</head>
<body class="text-center bg-gray-800 min-h-screen flex flex-col">
<div class="flex-auto flex justify-center max-h-screen">
<div class="flex-shrink-0 flex flex-col">
<h3 class="text-white font-bold text-xl mt-4">Steuerung</h3>
<div
class="grid grid-cols-3 gap-1 my-4 mx-1 font-bold leading-none text-lg select-none"
>
<button
class="bg-gray-400 rounded p-2"
onmousedown="startCmd(event, '-clock')"
ontouchstart="startCmd(event, '-clock')"
onmouseup="stopCmd(event, '-clock')"
ontouchend="stopCmd(event, '-clock')"
>
↶<br /><span class="text-gray-600">Q</span>
</button>
<button
class="bg-gray-100 rounded p-2"
onmousedown="startCmd(event, '+forwa')"
ontouchstart="startCmd(event, '+forwa')"
onmouseup="stopCmd(event, '+forwa')"
ontouchend="stopCmd(event, '+forwa')"
>
⇑<br /><span class="text-gray-600">W</span>
</button>
<button
class="bg-gray-400 rounded p-2"
onmousedown="startCmd(event, '+clock')"
ontouchstart="startCmd(event, '+clock')"
onmouseup="stopCmd(event, '+clock')"
ontouchend="stopCmd(event, '+clock')"
>
↷<br /><span class="text-gray-600">E</span>
</button>
<button
class="bg-gray-100 rounded p-2"
onmousedown="startCmd(event, '-right')"
ontouchstart="startCmd(event, '-right')"
onmouseup="stopCmd(event, '-right')"
ontouchend="stopCmd(event, '-right')"
>
<span class="text-gray-600">A</span><br />⇐
</button>
<button
class="bg-gray-100 rounded p-2"
onmousedown="startCmd(event, '-forwa')"
ontouchstart="startCmd(event, '-forwa')"
onmouseup="stopCmd(event, '-forwa')"
ontouchend="stopCmd(event, '-forwa')"
>
<span class="text-gray-600">S</span><br />⇓
</button>
<button
class="bg-gray-100 rounded p-2"
onmousedown="startCmd(event, '+right')"
ontouchstart="startCmd(event, '+right')"
onmouseup="stopCmd(event, '+right')"
ontouchend="stopCmd(event, '+right')"
>
<span class="text-gray-600">D</span><br />⇒
</button>
</div>
<div
class="transition-all duration-200"
style="flex-grow: 1"
id="batteryBarGone"
></div>
<div
class="mx-auto w-8 transition-all duration-200"
style="flex-grow: 0"
id="batteryBarRemaining"
></div>
<div class="text-white font-bold" id="batteryBarLabel">
<span id="BatteryPercentage"></span>% Akku
</div>
</div>
<div
id="remoteVideos"
class="flex-auto flex flex-col"
style="max-width: 133.334vh"
></div>
<div class="flex-shrink-0 flex flex-col">
<h3 class="text-white font-bold text-xl mt-4">Flughöhe</h3>
<div
class="grid grid-cols-3 gap-1 my-4 ml-1 leading-none text-lg select-none"
>
<button
class="row-span-2 bg-gray-400 rounded px-1 cursor-pointer"
onmousedown="startCmd(event, '=takeoff')"
ontouchstart="startCmd(event, '=takeoff')"
>
<strong class="text-gray-600">←</strong>
<span class="block">abheben</span>
</button>
<button
class="bg-gray-100 rounded py-2"
onmousedown="startCmd(event, '+up')"
ontouchstart="startCmd(event, '+up')"
onmouseup="stopCmd(event, '+up')"
ontouchend="stopCmd(event, '+up')"
>
hoch<br /><strong class="text-gray-600">↑</strong>
</button>
<button
class="row-span-2 bg-gray-400 rounded px-1 cursor-pointer"
onmousedown="startCmd(event, '=land')"
ontouchstart="startCmd(event, '=land')"
>
<strong class="text-gray-600">→</strong>
<span class="block">landen</span>
</button>
<button
class="bg-gray-100 rounded py-2"
onmousedown="startCmd(event, '-up')"
ontouchstart="startCmd(event, '-up')"
onmouseup="stopCmd(event, '-up')"
ontouchend="stopCmd(event, '-up')"
>
<strong class="text-gray-600">↓</strong><br />runter
</button>
</div>
<div id="log" class="text-gray-500 flex-grow"></div>
<div class="text-white font-bold" title="Height">
<span id="Height"></span>
</div>
</div>
</div>
<script>
/* eslint-env browser */
const configuration = {
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
};
let pc = new RTCPeerConnection(configuration);
let log = (msg) => {
console.log(msg);
document.getElementById("log").innerHTML += msg + "<br>";
};
pc.ontrack = function (event) {
var el = document.createElement(event.track.kind);
el.srcObject = event.streams[0];
el.muted = true;
el.autoplay = true;
el.controls = false;
el.className = "flex-auto max-h-screen";
document.getElementById("remoteVideos").appendChild(el);
};
pc.onconnectionstatechange = (e) => log(pc.iceConnectionState);
pc.oniceconnectionstatechange = (e) => log(pc.iceConnectionState);
pc.onicegatheringstatechange = (e) => log(pc.iceGatheringState);
// Offer to receive 1 audio, and 2 video tracks
pc.addTransceiver("video", { direction: "sendrecv" });
// add data channel
let sendChannel = pc.createDataChannel("pilot");
sendChannel.binaryType = "arraybuffer";
sendChannel.onclose = () => console.log("sendChannel has closed");
sendChannel.onopen = () => console.log("sendChannel has opened");
sendChannel.onmessage = (e) => {
let dataView = new DataView(e.data);
let decoder = new TextDecoder("utf8");
let { Height, BatteryPercentage } = JSON.parse(
decoder.decode(dataView)
);
document.getElementById("Height").innerText = Height;
document.getElementById("BatteryPercentage").innerText =
BatteryPercentage;
document.getElementById("BatteryPercentage").innerText =
BatteryPercentage;
const remainingElt = document.getElementById("batteryBarRemaining");
const labelElt = document.getElementById("batteryBarLabel");
remainingElt.style.flexGrow = BatteryPercentage;
document.getElementById("batteryBarGone").style.flexGrow =
100 - BatteryPercentage;
if (BatteryPercentage > 30) {
remainingElt.classList.remove("bg-orange-500", "bg-red-500");
remainingElt.classList.add("bg-green-500");
labelElt.classList.remove("text-orange-400", "text-red-400");
labelElt.classList.add("text-green-400");
} else if (BatteryPercentage > 15) {
remainingElt.classList.remove("bg-green-500", "bg-red-500");
remainingElt.classList.add("bg-orange-500");
labelElt.classList.remove("text-green-400", "text-red-400");
labelElt.classList.add("text-orange-400");
} else {
remainingElt.classList.remove("bg-green-500", "bg-orange-500");
remainingElt.classList.add("bg-red-500");
labelElt.classList.remove("text-green-400", "text-orange-400");
labelElt.classList.add("text-red-400");
}
};
// trickle-ice
let wsProto = "ws";
if (window.location.protocol != "http:") {
wsProto += "s";
}
const socket = new WebSocket(
wsProto + `://${window.location.host}/websocket`
);
socket.onmessage = (e) => {
let msg = JSON.parse(e.data);
if (!msg) {
return console.log("failed to parse msg");
}
if (msg.candidate) {
pc.addIceCandidate(msg);
} else {
pc.setRemoteDescription(msg);
}
};
pc.onicecandidate = (e) => {
if (e.candidate && e.candidate.candidate !== "") {
socket.send(JSON.stringify(e.candidate));
}
};
socket.onopen = () => {
pc.createOffer().then((offer) => {
pc.setLocalDescription(offer);
socket.send(JSON.stringify(offer));
});
};
let controls = {
KeyQ: "-clock",
KeyE: "+clock",
KeyS: "-forwa",
KeyW: "+forwa",
KeyA: "-right",
KeyD: "+right",
PageUp: "+up",
ArrowUp: "+up",
KeyU: "+up",
PageDown: "-up",
ArrowDown: "-up",
KeyJ: "-up",
Home: "=takeoff",
ArrowLeft: "=takeoff",
KeyH: "=takeoff",
End: "=land",
ArrowRight: "=land",
KeyK: "=land",
};
let shiftControls = {
KeyS: "2flip",
KeyW: "0flip",
KeyQ: "1flip",
KeyE: "3flip",
KeyA: "1flip",
KeyD: "3flip",
};
document.addEventListener("keyup", function (event) {
let e = controls[event.code];
if (!e) {
return;
}
stopCmd(event, e);
});
document.addEventListener("keydown", function (event) {
const currentControls = event.shiftKey ? shiftControls : controls;
let e = currentControls[event.code];
if (!e) {
console.log(event.code);
return;
}
startCmd(event, e);
});
var cmdIntervalIds = {};
function startCmd(e, cmd) {
e.preventDefault();
if (cmdIntervalIds[cmd]) {
// already running
return;
}
sendChannel.send(cmd);
if (cmd.substr(0, 1) != "+" && cmd.substr(0, 1) != "-") {
return;
}
cmdIntervalIds[cmd] = setInterval(() => {
sendChannel.send(cmd);
}, 100);
}
function stopCmd(e, cmd) {
e.preventDefault();
if (cmd.substr(0, 1) == "=") {
return;
}
if (cmdIntervalIds[cmd]) {
clearInterval(cmdIntervalIds[cmd]);
cmdIntervalIds[cmd] = null;
}
sendChannel.send("=hover");
}
</script>
</body>
</html>