/
test.html
246 lines (246 loc) · 6.7 KB
/
test.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
<html>
<head>
<meta charset=koi8-r http-equiv="Content-Language" content="ru"/>
<title>A test</title>
<style type="text/css">
.content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; border-radius:10px; }
button { display: block; width: 70px; text-align: center; }
.container {border: solid 1px; border-radius:10px; padding:12px; }
</style>
<script>
Global = function(){
const framepause = 50; // minimal pause between subsequent frames
var socket = null;
var imsocket = null;
var connected = 0;
var globSpeed = 0;
var imType = "im";
var img = new Image();
var iterator = 1;
var frames = 0;
var T0 = gettime();
var wdTmout, faulttmout;
function $(nm){return document.getElementById(nm);}
function gettime(){
var d = new Date();
var T = d.getTime();
delete d;
return T;
}
function get_appropriate_ws_url(){
var pcol;
var u = document.URL;
if (u.substring(0, 5) == "https") {
pcol = "wss://";
u = u.substr(8);
} else {
pcol = "ws://";
if (u.substring(0, 4) == "http")
u = u.substr(7);
}
u = u.split('/');
return pcol + u[0] + ":9999";
}
function send(){
imsocket.send("get");
}
function close_imsock(){
if(imsocket){
imsocket.onclose = function(){};
imsocket.onmessage = function(){};
imsocket.close();
delete imsocket;
imsocket = null;
}
}
function update_fps(){
var T1 = gettime();
var F = (++frames)/(T1-T0)*1000;
$("cntr").innerHTML = Math.round(F*10)/10;
}
function reload_stream(){
frames = 0;
T0 = gettime();
stream_next();
}
function stream_next(){
clearTimeout(faulttmout);
faulttmout = setTimeout(reload_stream, 1000); // try to reload if fault
img.src = "http://localhost:54321/" + (iterator++) + ".jpg";
}
img.onload = function(){
$("ws_image").src = img.src;
update_fps();
clearTimeout(wdTmout);
wdTmout = setTimeout(stream_next, framepause);
}
function TryImsock(){
clearTimeout(wdTmout);
console.log("Try to connect to image socket");
close_imsock();
apprURL = get_appropriate_ws_url();
if (typeof MozWebSocket != "undefined") {
imsocket = new MozWebSocket(apprURL, "image-protocol");
} else {
imsocket = new WebSocket(apprURL, "image-protocol");
}
try {
imsocket.onopen = function(){
frames = 0; T0 = gettime();
send();
}
imsocket.onmessage = function(msg){
clearTimeout(wdTmout);
$("ws_image").src = "data:image/jpeg;base64," + msg.data;
update_fps();
wdTmout = setTimeout(TryImsock, 3000);
setTimeout(send, framepause);
}
imsocket.onclose = function(){
$("connected").textContent = "Broken connection to image streamer";
clearTimeout(wdTmout);
wdTmout = setTimeout(TryImsock, 1000);
}
} catch(exception) {
alert('Error' + exception);
}
wdTmout = setTimeout(TryImsock, 3000);
}
function TryConnect(){
if(connected) return;
if(socket) delete socket;
apprURL = get_appropriate_ws_url();
if (typeof MozWebSocket != "undefined") {
socket = new MozWebSocket(apprURL, "XY-protocol");
} else {
socket = new WebSocket(apprURL, "XY-protocol");
}
if(imType == "ws")
TryImsock();
else
reload_stream();
if(!socket){
alert("Error: can't create websocket!\nMake sure that your browser supports websockets");
return;
}
try {
socket.onopen = function(){
$("connected").style.backgroundColor = "#40ff40";
$("connected").textContent = "Connection opened";
connected = 1;
socket.send("G"); // get speed
}
socket.onmessage = function(msg){
var answ = msg.data
if(answ.substring(0,7) == "curspd="){ // server sent current motors' speed
globSpeed = Number(answ.substring(7));
$("speed").value = globSpeed;
$("curspeed").innerHTML = globSpeed;
}else
$("answer").innerHTML = answ;
}
socket.onclose = function(){
$("connected").style.backgroundColor = "#ff4040";
$("connected").textContent = "Connection closed";
$("answer").innerHTML = "";
connected = 0;
setTimeout(TryConnect, 1000);
}
} catch(exception) {
alert('Error' + exception);
}
}
function ch_imaging_type(){
var lbl;
clearTimeout(faulttmout);
clearTimeout(wdTmout);
if(imType == "ws"){
imType = "stream";
lbl = "websock";
close_imsock();
reload_stream();
}else{
imType = "ws";
lbl = "stream";
TryImsock();
}
console.log("ch type to " + imType);
$("imaging").innerHTML = "Imaging: " + lbl;
}
function init(){
console.log("init");
$("cont").style.width = $("tab").clientWidth;
//var Buttons = document.getElementsByTagName("button");
var Buttons = document.getElementsByClassName("MotorButton");
for(var i = 0; i < Buttons.length; i++){
Buttons[i].addEventListener("mousedown", btnmousedown);
Buttons[i].addEventListener("mouseup", btnmouseup);
Buttons[i].addEventListener("mouseout", btnmouseup);
Buttons[i].pressed = 0;
}
var el = $("imaging");
el.onclick = ch_imaging_type;
el.innerHTML = "Imaging: " + ((imType == "ws") ? "stream" : "websock");
el = $("speed");
el.value = globSpeed;
el.addEventListener("input", ChSpd);
el.addEventListener("change", SetSpd);
TryConnect();
}
function btnmouseup(){
if(this.pressed == 0) return; // this function calls also from "mouseout", so we must prevent stopping twice
this.pressed = 0;
if(connected) socket.send("U"+this.id);
}
function btnmousedown(){
this.pressed = 1;
if(connected) socket.send("D"+this.id);
}
function ChSpd(){
if(globSpeed == this.value) return;
globSpeed = this.value;
$("curspeed").textContent = globSpeed;
$("answer").textContent = "";
}
function SetSpd(){
if(connected) socket.send("S"+globSpeed);
}
return{
init: init,
send: send
}
}();
</script>
</head>
<body onload="Global.init()">
<table class="content"><tr><td valign="top">
<div style="height: 1.5em;"></div>
<div class="container" id="cont">
<table class="content" id="tab">
<tr><td></td>
<td align='center'><button class='MotorButton' id='Y+'>Y+</button></td>
<td></td></tr>
<tr>
<td align='center'><button class='MotorButton' id='X-'>X-</button></td>
<td><button class='MotorButton' id='0'>0</button></td>
<td align='center'><button class='MotorButton' id='X+'>X+</button></td>
</tr>
<tr><td></td>
<td align='center'><button class='MotorButton' id='Y-'>Y-</button></td>
<td></td></tr>
</table>
<div class="content">
Speed: <input type="range" min="1" max="499" step="1" id="speed">
<span id="curspeed">000</span>
</div>
<p></p>
<div id = "connected">No connection</div>
<div style="height: 2.5em;"><span id = "answer"></span></div>
<div align="center"><button id='imaging' style="width: 80%;">Imaging</button></div>
</div>
</td><td>
<div id="cntr" style="height: 1.5em;"></div>
<div><img id="ws_image"></div></td></tr>
</table>
</body>
</html>