File size: 9,065 Bytes
fbc565a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
981516e
fbc565a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Reachy Mini Finder</title>
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}

		body {
			font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			min-height: 100vh;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 20px;
		}

		.container {
			background: white;
			border-radius: 20px;
			padding: 40px;
			max-width: 500px;
			width: 100%;
			box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
		}

		h1 {
			color: #333;
			margin-bottom: 10px;
			font-size: 28px;
		}

		.subtitle {
			color: #666;
			margin-bottom: 30px;
			font-size: 14px;
		}

		.status {
			background: #f5f5f5;
			padding: 20px;
			border-radius: 10px;
			margin-bottom: 20px;
			min-height: 120px;
		}

		.status-text {
			color: #333;
			line-height: 1.6;
		}

		.progress-bar {
			width: 100%;
			height: 6px;
			background: #e0e0e0;
			border-radius: 3px;
			margin-top: 15px;
			overflow: hidden;
		}

		.progress-fill {
			height: 100%;
			background: linear-gradient(90deg, #667eea, #764ba2);
			width: 0%;
			transition: width 0.3s ease;
		}

		button {
			width: 100%;
			padding: 15px;
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			color: white;
			border: none;
			border-radius: 10px;
			font-size: 16px;
			font-weight: 600;
			cursor: pointer;
			transition: transform 0.2s;
		}

		button:hover {
			transform: translateY(-2px);
		}

		button:active {
			transform: translateY(0);
		}

		button:disabled {
			opacity: 0.6;
			cursor: not-allowed;
			transform: none;
		}

		.manual-input {
			margin-top: 20px;
			padding-top: 20px;
			border-top: 1px solid #e0e0e0;
		}

		.manual-input input {
			width: 100%;
			padding: 12px;
			border: 2px solid #e0e0e0;
			border-radius: 8px;
			font-size: 14px;
			margin-bottom: 10px;
		}

		.manual-input input:focus {
			outline: none;
			border-color: #667eea;
		}

		.ip-list {
			max-height: 200px;
			overflow-y: auto;
			margin-top: 10px;
		}

		.ip-item {
			padding: 8px;
			background: #f9f9f9;
			margin-bottom: 5px;
			border-radius: 5px;
			font-size: 12px;
			color: #666;
			font-family: monospace;
		}

		.ip-item.checking {
			background: #fff3cd;
		}

		.ip-item.found {
			background: #d4edda;
			color: #155724;
			font-weight: 600;
		}

		.robot-info {
			background: #e7f3ff;
			padding: 15px;
			border-radius: 8px;
			margin-top: 15px;
			font-size: 13px;
			color: #004085;
		}

		.robot-info strong {
			display: block;
			margin-bottom: 5px;
		}
	</style>
</head>

<body>
	<div class="container">
		<h1>🤖 Reachy Mini Finder</h1>
		<p class="subtitle">Automatically detect your Reachy Mini robot on the network</p>

		<div class="status">
			<div class="status-text" id="statusText">
				Click "Find Reachy" to start scanning your network.
			</div>
			<div class="progress-bar">
				<div class="progress-fill" id="progressBar"></div>
			</div>
		</div>

		<button id="scanBtn" onclick="startScan()">Find Reachy</button>

		<div class="manual-input">
			<input type="text" id="manualIP" placeholder="Or enter IP manually (e.g., 192.168.1.100)" />
			<button onclick="connectManual()">Connect</button>
		</div>

		<div class="ip-list" id="ipList"></div>
		<div id="robotInfo"></div>
	</div>

	<script>
		const PORT = 8000;
		const TIMEOUT_MS = 500;
		let scanning = false;

		async function getLocalIP() {
			return new Promise((resolve, reject) => {
				const pc = new RTCPeerConnection({ iceServers: [] });
				pc.createDataChannel('');
				pc.createOffer().then(offer => pc.setLocalDescription(offer));

				pc.onicecandidate = (ice) => {
					if (!ice || !ice.candidate || !ice.candidate.candidate) return;

					const match = /([0-9]{1,3}\.){3}[0-9]{1,3}/.exec(ice.candidate.candidate);
					if (match) {
						pc.close();
						resolve(match[0]);
					}
				};

				setTimeout(() => {
					pc.close();
					reject(new Error('Could not detect local IP'));
				}, 5000);
			});
		}

		async function tryIP(ip) {
			const controller = new AbortController();
			const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);

			try {
				const response = await fetch(`http://${ip}:${PORT}/api/daemon/status`, {
					signal: controller.signal,
					mode: 'cors'
				});
				clearTimeout(timeoutId);

				if (response.ok) {
					const data = await response.json();
					return { ip, data };
				}
			} catch (err) {
				// Timeout or connection refused
			}
			clearTimeout(timeoutId);
			return null;
		}

		function updateStatus(text, progress = null) {
			document.getElementById('statusText').innerHTML = text;
			if (progress !== null) {
				document.getElementById('progressBar').style.width = progress + '%';
			}
		}

		function addIPToList(ip, status = 'checking') {
			const ipList = document.getElementById('ipList');
			const existing = document.getElementById('ip-' + ip.replace(/\./g, '-'));

			if (existing) {
				existing.className = 'ip-item ' + status;
			} else {
				const item = document.createElement('div');
				item.id = 'ip-' + ip.replace(/\./g, '-');
				item.className = 'ip-item ' + status;
				item.textContent = status === 'found' ? `✓ Found: ${ip}` : `Checking: ${ip}`;
				ipList.appendChild(item);
			}
		}

		function showRobotInfo(ip, data) {
			const infoDiv = document.getElementById('robotInfo');
			infoDiv.innerHTML = `
                <div class="robot-info">
                    <strong>✅ Reachy Mini Found!</strong>
                    IP: ${ip}<br>
                    Server ID: ${data.server_id || 'N/A'}<br>
                    Version: ${data.version || 'N/A'}
                </div>
            `;
		}

		async function startScan() {
			if (scanning) return;
			scanning = true;

			const btn = document.getElementById('scanBtn');
			btn.disabled = true;
			btn.textContent = 'Scanning...';
			document.getElementById('ipList').innerHTML = '';
			document.getElementById('robotInfo').innerHTML = '';

			try {
				updateStatus('🔍 Detecting your phone\'s IP address...', 5);
				const phoneIP = await getLocalIP();
				const subnet = phoneIP.split('.').slice(0, 3).join('.');

				updateStatus(`📡 Phone IP: ${phoneIP}<br>Scanning subnet: ${subnet}.0/24`, 10);

				// Priority IPs to check first
				const priorityIPs = [
					`${subnet}.1`,   // Router
					`${subnet}.100`, // Common DHCP start
				];

				// Build full scan list, excluding phone's IP
				const allIPs = [];
				for (let i = 1; i < 255; i++) {
					const ip = `${subnet}.${i}`;
					if (ip !== phoneIP) {
						allIPs.push(ip);
					}
				}

				// Put priority IPs first
				const scanList = [
					...priorityIPs.filter(ip => allIPs.includes(ip)),
					...allIPs.filter(ip => !priorityIPs.includes(ip))
				];

				// Scan in batches of 20 for better responsiveness
				const BATCH_SIZE = 20;
				let scanned = 0;

				for (let i = 0; i < scanList.length; i += BATCH_SIZE) {
					const batch = scanList.slice(i, i + BATCH_SIZE);

					// Show which IPs we're checking
					batch.forEach(ip => addIPToList(ip, 'checking'));

					const results = await Promise.all(batch.map(tryIP));
					const found = results.find(r => r !== null);

					scanned += batch.length;
					const progress = 10 + (scanned / scanList.length) * 90;
					updateStatus(
						`🔍 Scanning: ${scanned}/${scanList.length} IPs checked...`,
						progress
					);

					if (found) {
						addIPToList(found.ip, 'found');
						showRobotInfo(found.ip, found.data);
						updateStatus(
							`✅ Reachy Mini found at ${found.ip}!<br>Redirecting...`,
							100
						);
						setTimeout(() => {
							window.location.href = `http://${found.ip}:${PORT}`;
						}, 2000);
						return;
					}
				}

				updateStatus(
					`❌ No Reachy Mini found on ${subnet}.0/24<br>Please check:<br>
                    • Robot is powered on<br>
                    • Connected to same WiFi<br>
                    • Daemon running on port ${PORT}<br>
                    • Try manual IP below`,
					100
				);

			} catch (err) {
				updateStatus(
					`⚠️ Error: ${err.message}<br>Try entering IP manually below`,
					0
				);
			} finally {
				scanning = false;
				btn.disabled = false;
				btn.textContent = 'Scan Again';
			}
		}

		function connectManual() {
			const ip = document.getElementById('manualIP').value.trim();
			if (!ip) {
				alert('Please enter an IP address');
				return;
			}

			// Validate IP format
			const ipRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
			if (!ipRegex.test(ip)) {
				alert('Invalid IP format. Example: 192.168.1.100');
				return;
			}

			updateStatus(`🔗 Connecting to ${ip}...`, 50);
			window.location.href = `http://${ip}:${PORT}`;
		}

		// Allow Enter key in manual input
		document.getElementById('manualIP').addEventListener('keypress', (e) => {
			if (e.key === 'Enter') {
				connectManual();
			}
		});
	</script>
</body>

</html>