-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile.html
More file actions
268 lines (245 loc) · 10.4 KB
/
mobile.html
File metadata and controls
268 lines (245 loc) · 10.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<title>Kuro Mobile Sensor</title>
<style>
:root { --bg: #0a0a0a; --fg: #e0e0e0; --accent: #6cf; --dim: #666; --ok: #4c6; --err: #e44; }
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, system-ui, sans-serif; background: var(--bg); color: var(--fg); padding: 16px; min-height: 100dvh; }
h1 { font-size: 1.2rem; color: var(--accent); margin-bottom: 12px; }
.status { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; font-size: 0.85rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--dim); }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.setup { background: #111; border-radius: 8px; padding: 16px; margin-bottom: 16px; }
.setup label { display: block; font-size: 0.8rem; color: var(--dim); margin-bottom: 4px; }
.setup input { width: 100%; padding: 8px; background: #1a1a1a; border: 1px solid #333; border-radius: 4px; color: var(--fg); font-size: 0.9rem; margin-bottom: 12px; }
.setup button { width: 100%; padding: 10px; background: var(--accent); color: #000; border: none; border-radius: 4px; font-weight: 600; font-size: 0.9rem; cursor: pointer; }
.setup button:disabled { opacity: 0.4; }
.data { font-family: 'SF Mono', monospace; font-size: 0.8rem; line-height: 1.6; }
.data .row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px solid #1a1a1a; }
.data .label { color: var(--dim); }
.log { margin-top: 16px; font-size: 0.75rem; color: var(--dim); max-height: 120px; overflow-y: auto; }
.hidden { display: none; }
</style>
</head>
<body>
<h1>Kuro Mobile Sensor</h1>
<div class="status">
<div class="dot" id="statusDot"></div>
<span id="statusText">Disconnected</span>
</div>
<div class="setup" id="setupPanel">
<label>Server URL</label>
<input id="serverUrl" type="url" placeholder="http://192.168.1.x:3001" />
<label>API Key</label>
<input id="apiKey" type="password" placeholder="MINI_AGENT_API_KEY" />
<button id="connectBtn" onclick="startSensors()">Connect</button>
</div>
<div class="setup hidden" id="motionPanel" style="border:2px solid #f90;">
<p style="font-size:0.85rem;color:var(--fg);margin-bottom:12px;">📱 iOS requires a separate tap to enable gyroscope & accelerometer.</p>
<button id="motionBtn" onclick="requestMotionPermission()" style="background:#f90;font-size:1rem;padding:14px;">Enable Motion Sensors</button>
</div>
<div class="data hidden" id="dataPanel">
<div class="row"><span class="label">GPS</span><span id="gps">--</span></div>
<div class="row"><span class="label">Accuracy</span><span id="accuracy">--</span></div>
<div class="row"><span class="label">Altitude</span><span id="altitude">--</span></div>
<div class="row"><span class="label">Speed</span><span id="speed">--</span></div>
<div class="row"><span class="label">Heading</span><span id="heading">--</span></div>
<div class="row"><span class="label">Orientation</span><span id="orientation">--</span></div>
<div class="row"><span class="label">Motion</span><span id="motion">--</span></div>
<div class="row"><span class="label">Last sent</span><span id="lastSent">--</span></div>
</div>
<div class="log" id="log"></div>
<script>
const $ = id => document.getElementById(id);
let sensorState = {};
let sendInterval = null;
let sending = false;
// Auto-detect server URL from current page origin, restore saved config
const saved = localStorage.getItem('kuro-mobile');
const autoUrl = window.location.origin;
if (saved) {
try {
const cfg = JSON.parse(saved);
$('serverUrl').value = cfg.url || autoUrl;
$('apiKey').value = cfg.key || '';
} catch {}
} else {
$('serverUrl').value = autoUrl;
}
function log(msg) {
const el = $('log');
const t = new Date().toLocaleTimeString();
el.textContent = `[${t}] ${msg}\n` + el.textContent;
if (el.textContent.length > 2000) el.textContent = el.textContent.slice(0, 2000);
}
function setStatus(ok, text) {
$('statusDot').className = 'dot ' + (ok ? 'ok' : ok === false ? 'err' : '');
$('statusText').textContent = text;
}
async function startSensors() {
const url = $('serverUrl').value.replace(/\/+$/, '');
const key = $('apiKey').value;
if (!url) { log('Server URL required'); return; }
localStorage.setItem('kuro-mobile', JSON.stringify({ url, key }));
$('connectBtn').disabled = true;
log('Starting sensors...');
// Test connection
try {
const res = await fetch(`${url}/health`, { signal: AbortSignal.timeout(5000) });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
log('Server reachable');
} catch (e) {
log(`Connection failed: ${e.message}`);
setStatus(false, 'Connection failed');
$('connectBtn').disabled = false;
return;
}
$('setupPanel').classList.add('hidden');
$('dataPanel').classList.remove('hidden');
setStatus(null, 'Requesting permissions...');
// GPS
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
pos => {
sensorState.latitude = pos.coords.latitude;
sensorState.longitude = pos.coords.longitude;
sensorState.accuracy = pos.coords.accuracy;
sensorState.altitude = pos.coords.altitude;
sensorState.speed = pos.coords.speed;
sensorState.heading = pos.coords.heading;
$('gps').textContent = `${pos.coords.latitude.toFixed(5)}, ${pos.coords.longitude.toFixed(5)}`;
$('accuracy').textContent = `\u00b1${Math.round(pos.coords.accuracy)}m`;
$('altitude').textContent = pos.coords.altitude != null ? `${Math.round(pos.coords.altitude)}m` : '--';
$('speed').textContent = pos.coords.speed != null ? `${pos.coords.speed.toFixed(1)} m/s` : '0 m/s';
$('heading').textContent = pos.coords.heading != null ? `${Math.round(pos.coords.heading)}\u00b0` : '--';
},
err => log(`GPS error: ${err.message}`),
{ enableHighAccuracy: true, maximumAge: 5000 }
);
log('GPS started');
} else {
log('GPS not available');
}
// Motion sensors: iOS requires user gesture → show button if needed
const needsMotionPermission = typeof DeviceMotionEvent !== 'undefined'
&& typeof DeviceMotionEvent.requestPermission === 'function';
if (needsMotionPermission) {
// iOS: show a dedicated button (must be triggered by direct user tap)
$('motionPanel').classList.remove('hidden');
$('motionPanel').scrollIntoView({ behavior: 'smooth', block: 'center' });
log('Tap "Enable Motion Sensors" to grant gyro/accel permission');
} else {
// Non-iOS: just attach listeners directly
attachMotionListeners();
}
setStatus(true, 'Connected');
// Send every 5 seconds
sendInterval = setInterval(() => sendData(url, key), 5000);
sendData(url, key); // first send immediately
// Page Visibility: pause when backgrounded, resume when foregrounded
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
if (sendInterval) { clearInterval(sendInterval); sendInterval = null; }
log('Backgrounded - paused');
setStatus(null, 'Paused (background)');
} else {
if (!sendInterval) {
sendInterval = setInterval(() => sendData(url, key), 5000);
sendData(url, key);
}
log('Foregrounded - resumed');
setStatus(true, 'Connected');
}
});
}
function attachMotionListeners() {
window.addEventListener('deviceorientation', e => {
sensorState.alpha = e.alpha;
sensorState.beta = e.beta;
sensorState.gamma = e.gamma;
const a = e.alpha != null ? Math.round(e.alpha) : '?';
const b = e.beta != null ? Math.round(e.beta) : '?';
const g = e.gamma != null ? Math.round(e.gamma) : '?';
$('orientation').textContent = `\u03b1=${a}\u00b0 \u03b2=${b}\u00b0 \u03b3=${g}\u00b0`;
});
window.addEventListener('devicemotion', e => {
const a = e.accelerationIncludingGravity;
if (a) {
sensorState.accelX = a.x;
sensorState.accelY = a.y;
sensorState.accelZ = a.z;
$('motion').textContent = `x=${a.x?.toFixed(1)} y=${a.y?.toFixed(1)} z=${a.z?.toFixed(1)}`;
}
});
log('Motion & orientation listeners active');
}
async function requestMotionPermission() {
try {
// Request both permissions from the same user gesture
const results = await Promise.all([
DeviceOrientationEvent.requestPermission().catch(e => 'error:' + e.message),
DeviceMotionEvent.requestPermission().catch(e => 'error:' + e.message),
]);
const [orientPerm, motionPerm] = results;
log(`Permission: orientation=${orientPerm}, motion=${motionPerm}`);
// Attach listeners for whichever was granted
if (orientPerm === 'granted' || motionPerm === 'granted') {
attachMotionListeners();
$('motionPanel').classList.add('hidden');
log('Motion sensors enabled!');
} else {
log('Motion permission denied — check Settings > Safari > Motion & Orientation Access');
$('motionBtn').textContent = 'Retry Motion Permission';
}
} catch (e) {
log(`Motion permission error: ${e.message}`);
$('motionBtn').textContent = 'Retry Motion Permission';
}
}
async function sendData(url, key) {
if (sending) return;
sending = true;
try {
const payload = {
...sensorState,
deviceName: navigator.userAgent.includes('iPhone') ? "Alex's iPhone"
: navigator.userAgent.includes('iPad') ? "Alex's iPad"
: 'Mobile Device',
timestamp: new Date().toISOString(),
};
const res = await fetch(`${url}/api/mobile/sensor`, {
method: 'POST',
headers: Object.assign(
{ 'Content-Type': 'application/json' },
key ? { 'Authorization': `Bearer ${key}` } : {}
),
body: JSON.stringify(payload),
signal: AbortSignal.timeout(5000),
});
if (res.ok) {
$('lastSent').textContent = new Date().toLocaleTimeString();
setStatus(true, 'Connected');
} else if (res.status === 401) {
log('Auth failed (401) - check API key');
setStatus(false, 'Auth failed');
clearInterval(sendInterval);
} else {
log(`Send error: HTTP ${res.status}`);
setStatus(false, `Error ${res.status}`);
}
} catch (e) {
log(`Send failed: ${e.message}`);
setStatus(false, 'Send failed');
} finally {
sending = false;
}
}
</script>
</body>
</html>