944 lines
30 KiB
HTML
944 lines
30 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
<title>SecureCam Mobile Simulator</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0b1220;
|
|
--panel: #111b2e;
|
|
--panel-2: #17243a;
|
|
--line: #23324d;
|
|
--text: #e5ecf7;
|
|
--muted: #9fb0cd;
|
|
--accent: #14b8a6;
|
|
--accent-2: #0d9488;
|
|
--danger: #ef4444;
|
|
--warn: #f59e0b;
|
|
}
|
|
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
margin: 0;
|
|
font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
|
|
background: linear-gradient(160deg, #08101d 0%, #0b1220 45%, #091425 100%);
|
|
color: var(--text);
|
|
min-height: 100vh;
|
|
}
|
|
|
|
.app {
|
|
max-width: 430px;
|
|
margin: 0 auto;
|
|
min-height: 100vh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
border-left: 1px solid rgba(255, 255, 255, 0.06);
|
|
border-right: 1px solid rgba(255, 255, 255, 0.06);
|
|
background: radial-gradient(circle at 80% -30%, #1a2a49 0%, transparent 40%);
|
|
}
|
|
|
|
.topbar {
|
|
padding: 14px 14px 10px;
|
|
border-bottom: 1px solid var(--line);
|
|
background: rgba(9, 16, 30, 0.92);
|
|
position: sticky;
|
|
top: 0;
|
|
z-index: 10;
|
|
}
|
|
|
|
.toprow {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}
|
|
|
|
.brand {
|
|
font-size: 15px;
|
|
font-weight: 700;
|
|
letter-spacing: 0.2px;
|
|
}
|
|
|
|
.chip {
|
|
font-size: 11px;
|
|
border: 1px solid var(--line);
|
|
background: #102039;
|
|
color: var(--muted);
|
|
border-radius: 999px;
|
|
padding: 4px 9px;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.chip.online {
|
|
color: #22c55e;
|
|
border-color: #14532d;
|
|
background: #052e16;
|
|
}
|
|
|
|
.chip.offline {
|
|
color: #fca5a5;
|
|
border-color: #7f1d1d;
|
|
background: #3f0e16;
|
|
}
|
|
|
|
.subtitle {
|
|
margin: 6px 0 0;
|
|
color: var(--muted);
|
|
font-size: 12px;
|
|
}
|
|
|
|
.content {
|
|
flex: 1;
|
|
overflow: auto;
|
|
padding: 14px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 12px;
|
|
}
|
|
|
|
.screen {
|
|
display: none;
|
|
}
|
|
|
|
.screen.active {
|
|
display: block;
|
|
}
|
|
|
|
.card {
|
|
background: linear-gradient(180deg, var(--panel) 0%, #0f182a 100%);
|
|
border: 1px solid var(--line);
|
|
border-radius: 14px;
|
|
padding: 12px;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
h1,
|
|
h2,
|
|
h3,
|
|
p {
|
|
margin: 0;
|
|
}
|
|
|
|
h2 {
|
|
font-size: 18px;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
h3 {
|
|
font-size: 14px;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
p,
|
|
label,
|
|
small {
|
|
color: var(--muted);
|
|
font-size: 12px;
|
|
}
|
|
|
|
label {
|
|
display: block;
|
|
margin-top: 8px;
|
|
}
|
|
|
|
input,
|
|
select,
|
|
button {
|
|
width: 100%;
|
|
border: 1px solid #31476d;
|
|
background: #0a1527;
|
|
color: var(--text);
|
|
border-radius: 10px;
|
|
padding: 10px;
|
|
font-size: 13px;
|
|
margin-top: 6px;
|
|
}
|
|
|
|
button {
|
|
cursor: pointer;
|
|
font-weight: 700;
|
|
border-color: #0f766e;
|
|
background: linear-gradient(180deg, #10b7a6 0%, #0d9488 100%);
|
|
}
|
|
|
|
button.alt {
|
|
border-color: #3a4f73;
|
|
background: linear-gradient(180deg, #2c3f5f 0%, #21324f 100%);
|
|
}
|
|
|
|
button.warn {
|
|
border-color: #92400e;
|
|
background: linear-gradient(180deg, #d97706 0%, #b45309 100%);
|
|
}
|
|
|
|
button.danger {
|
|
border-color: #991b1b;
|
|
background: linear-gradient(180deg, #dc2626 0%, #b91c1c 100%);
|
|
}
|
|
|
|
.row {
|
|
display: flex;
|
|
gap: 8px;
|
|
}
|
|
|
|
.row > * {
|
|
flex: 1;
|
|
}
|
|
|
|
.muted-block {
|
|
background: #091224;
|
|
border: 1px solid #203252;
|
|
border-radius: 10px;
|
|
padding: 10px;
|
|
color: #c7d5ef;
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
font-size: 11px;
|
|
white-space: pre-wrap;
|
|
}
|
|
|
|
.nav {
|
|
border-top: 1px solid var(--line);
|
|
background: rgba(9, 16, 30, 0.95);
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
gap: 8px;
|
|
padding: 10px;
|
|
}
|
|
|
|
.nav button {
|
|
margin: 0;
|
|
font-size: 12px;
|
|
}
|
|
|
|
.hidden {
|
|
display: none !important;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="app">
|
|
<header class="topbar">
|
|
<div class="toprow">
|
|
<div class="brand">SecureCam Mobile</div>
|
|
<div class="toprow" style="gap: 6px">
|
|
<span id="authChip" class="chip offline">signed out</span>
|
|
<span id="socketChip" class="chip offline">offline</span>
|
|
</div>
|
|
</div>
|
|
<p id="topSubtitle" class="subtitle">Open app to continue</p>
|
|
</header>
|
|
|
|
<main class="content">
|
|
<section id="screen-auth" class="screen active">
|
|
<div class="card">
|
|
<h2>Welcome</h2>
|
|
<p>Sign in or create an account to use this phone as a camera or client.</p>
|
|
<label>Name</label>
|
|
<input id="authName" placeholder="Optional" />
|
|
<label>Email</label>
|
|
<input id="authEmail" placeholder="you@example.com" />
|
|
<label>Password</label>
|
|
<input id="authPassword" type="password" placeholder="password" />
|
|
<div class="row" style="margin-top: 8px">
|
|
<button id="signUpBtn" class="alt">Create Account</button>
|
|
<button id="signInBtn">Sign In</button>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section id="screen-onboarding" class="screen">
|
|
<div class="card">
|
|
<h2>Set Up This Phone</h2>
|
|
<p>Choose how this phone should behave in your home security setup.</p>
|
|
<label>Device Name</label>
|
|
<input id="deviceName" placeholder="e.g. Hallway iPhone" />
|
|
<label>Mode</label>
|
|
<select id="role">
|
|
<option value="client">Client (viewer)</option>
|
|
<option value="camera">Camera (records)</option>
|
|
</select>
|
|
<label>Push Token (optional)</label>
|
|
<input id="pushToken" placeholder="simulated push token" />
|
|
<button id="registerBtn" style="margin-top: 10px">Finish Setup</button>
|
|
<button id="loadSavedBtn" class="alt">Load Existing Device</button>
|
|
</div>
|
|
</section>
|
|
|
|
<section id="screen-home-client" class="screen">
|
|
<div class="card">
|
|
<h2>Client Home</h2>
|
|
<p>Monitor cameras and request live streams.</p>
|
|
<div class="row" style="margin-top: 10px">
|
|
<button id="connectBtn">Go Online</button>
|
|
<button id="disconnectBtn" class="alt">Go Offline</button>
|
|
</div>
|
|
<label>Camera Device ID</label>
|
|
<input id="targetCameraId" placeholder="camera uuid" />
|
|
<div class="row">
|
|
<button id="linkBtn" class="alt">Link Camera</button>
|
|
<button id="requestStreamBtn">Request Live View</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="fetchSubscribeBtn" class="alt">Subscribe Credentials</button>
|
|
<button id="fetchPlaybackBtn" class="alt">Playback Token</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="listRecordingsBtn" class="alt">My Recordings</button>
|
|
<button id="downloadLatestRecordingBtn" class="alt">Latest Download URL</button>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section id="screen-home-camera" class="screen">
|
|
<div class="card">
|
|
<h2>Camera Home</h2>
|
|
<p>Detect motion, stream live, and finalize recordings.</p>
|
|
<div class="row" style="margin-top: 10px">
|
|
<button id="connectBtnCam">Go Online</button>
|
|
<button id="disconnectBtnCam" class="alt">Go Offline</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="startMotionBtn" class="warn">Start Motion</button>
|
|
<button id="endMotionBtn" class="danger">End Motion</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="fetchPublishBtn" class="alt">Publish Credentials</button>
|
|
<button id="finalizeRecordingBtn" class="alt">Finalize Latest Recording</button>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section id="screen-activity" class="screen">
|
|
<div class="card">
|
|
<h2>Activity</h2>
|
|
<p>Realtime events and push inbox.</p>
|
|
<div class="row">
|
|
<button id="pollPushInboxBtn" class="alt">Refresh Push Inbox</button>
|
|
<button id="markLatestPushReadBtn" class="alt">Mark Latest Read</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="dispatchPushWorkerBtn" class="alt">Dispatch Push Worker</button>
|
|
<button id="fetchAuditBtn" class="alt">My Audit Logs</button>
|
|
</div>
|
|
<div id="activityState" class="muted-block" style="margin-top: 10px"></div>
|
|
<div id="eventLog" class="muted-block" style="margin-top: 10px; max-height: 280px; overflow: auto"></div>
|
|
</div>
|
|
</section>
|
|
|
|
<section id="screen-account" class="screen">
|
|
<div class="card">
|
|
<h2>Account</h2>
|
|
<div class="row">
|
|
<button id="sessionBtn" class="alt">Check Session</button>
|
|
<button id="signOutBtn" class="danger">Sign Out</button>
|
|
</div>
|
|
<div class="row">
|
|
<button id="checkLiveBtn" class="alt">Ops Live</button>
|
|
<button id="checkReadyBtn" class="alt">Ops Ready</button>
|
|
<button id="checkMetricsBtn" class="alt">Ops Metrics</button>
|
|
</div>
|
|
<div id="accountState" class="muted-block" style="margin-top: 10px"></div>
|
|
</div>
|
|
</section>
|
|
</main>
|
|
|
|
<nav id="bottomNav" class="nav hidden">
|
|
<button id="navHome">Home</button>
|
|
<button id="navActivity" class="alt">Activity</button>
|
|
<button id="navAccount" class="alt">Account</button>
|
|
</nav>
|
|
</div>
|
|
|
|
<script src="/socket.io/socket.io.js"></script>
|
|
<script>
|
|
const state = {
|
|
session: null,
|
|
device: null,
|
|
deviceToken: null,
|
|
socket: null,
|
|
currentScreen: 'auth',
|
|
lastMotionEventId: null,
|
|
lastStreamSessionId: null,
|
|
lastRecordingId: null,
|
|
latestPushNotificationId: null,
|
|
};
|
|
|
|
const screens = ['auth', 'onboarding', 'home-client', 'home-camera', 'activity', 'account'];
|
|
|
|
const $ = (id) => document.getElementById(id);
|
|
|
|
const safeJson = (value) => JSON.stringify(value, null, 2);
|
|
|
|
const log = (message, payload) => {
|
|
const ts = new Date().toISOString();
|
|
const line = `[${ts}] ${message}`;
|
|
const next = `${line}${payload ? `\n${safeJson(payload)}` : ''}\n\n${$('eventLog').textContent}`;
|
|
$('eventLog').textContent = next;
|
|
};
|
|
|
|
const saveLocal = () => {
|
|
localStorage.setItem('mobileSimDevice', JSON.stringify({ device: state.device, deviceToken: state.deviceToken }));
|
|
};
|
|
|
|
const loadLocal = () => {
|
|
const raw = localStorage.getItem('mobileSimDevice');
|
|
if (!raw) return;
|
|
try {
|
|
const parsed = JSON.parse(raw);
|
|
state.device = parsed.device ?? null;
|
|
state.deviceToken = parsed.deviceToken ?? null;
|
|
} catch {
|
|
state.device = null;
|
|
state.deviceToken = null;
|
|
}
|
|
};
|
|
|
|
const setScreen = (name) => {
|
|
state.currentScreen = name;
|
|
for (const screen of screens) {
|
|
$(`screen-${screen}`).classList.toggle('active', screen === name);
|
|
}
|
|
};
|
|
|
|
const updateTop = () => {
|
|
const signedIn = Boolean(state.session?.session);
|
|
$('authChip').textContent = signedIn ? 'signed in' : 'signed out';
|
|
$('authChip').className = `chip ${signedIn ? 'online' : 'offline'}`;
|
|
|
|
const socketConnected = Boolean(state.socket?.connected);
|
|
$('socketChip').textContent = socketConnected ? 'online' : 'offline';
|
|
$('socketChip').className = `chip ${socketConnected ? 'online' : 'offline'}`;
|
|
|
|
if (!signedIn) {
|
|
$('topSubtitle').textContent = 'Sign in to continue';
|
|
return;
|
|
}
|
|
|
|
if (!state.device) {
|
|
$('topSubtitle').textContent = 'Set up this phone as camera or client';
|
|
return;
|
|
}
|
|
|
|
$('topSubtitle').textContent = `${state.device.role} mode · ${state.device.name || state.device.id}`;
|
|
};
|
|
|
|
const renderState = () => {
|
|
$('activityState').textContent = safeJson({
|
|
device: state.device,
|
|
lastStreamSessionId: state.lastStreamSessionId,
|
|
lastRecordingId: state.lastRecordingId,
|
|
latestPushNotificationId: state.latestPushNotificationId,
|
|
});
|
|
|
|
$('accountState').textContent = safeJson({
|
|
session: state.session,
|
|
deviceTokenPresent: Boolean(state.deviceToken),
|
|
});
|
|
};
|
|
|
|
const updateFlow = () => {
|
|
const signedIn = Boolean(state.session?.session);
|
|
|
|
if (!signedIn) {
|
|
$('bottomNav').classList.add('hidden');
|
|
setScreen('auth');
|
|
updateTop();
|
|
renderState();
|
|
return;
|
|
}
|
|
|
|
if (!state.device || !state.deviceToken) {
|
|
$('bottomNav').classList.add('hidden');
|
|
setScreen('onboarding');
|
|
updateTop();
|
|
renderState();
|
|
return;
|
|
}
|
|
|
|
$('bottomNav').classList.remove('hidden');
|
|
|
|
if (!['home-client', 'home-camera', 'activity', 'account'].includes(state.currentScreen)) {
|
|
setScreen(state.device.role === 'camera' ? 'home-camera' : 'home-client');
|
|
}
|
|
|
|
updateTop();
|
|
renderState();
|
|
};
|
|
|
|
const authFetch = async (url, options = {}) => {
|
|
const response = await fetch(url, {
|
|
credentials: 'include',
|
|
...options,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...(options.headers || {}),
|
|
},
|
|
});
|
|
|
|
const payload = await response.json().catch(() => ({}));
|
|
|
|
if (!response.ok) {
|
|
throw new Error(payload.message || response.statusText);
|
|
}
|
|
|
|
return payload;
|
|
};
|
|
|
|
const deviceFetch = async (url, options = {}) => {
|
|
if (!state.deviceToken) {
|
|
throw new Error('No device token');
|
|
}
|
|
|
|
return authFetch(url, {
|
|
...options,
|
|
headers: {
|
|
Authorization: `Bearer ${state.deviceToken}`,
|
|
...(options.headers || {}),
|
|
},
|
|
});
|
|
};
|
|
|
|
const getAuthPayload = () => ({
|
|
name: $('authName').value.trim() || undefined,
|
|
email: $('authEmail').value.trim(),
|
|
password: $('authPassword').value,
|
|
});
|
|
|
|
const connectSocket = () => {
|
|
if (!state.deviceToken) throw new Error('Register device first');
|
|
|
|
if (state.socket) {
|
|
state.socket.disconnect();
|
|
}
|
|
|
|
state.socket = io({ auth: { token: state.deviceToken } });
|
|
|
|
state.socket.on('connect', () => {
|
|
updateTop();
|
|
log('socket connected');
|
|
});
|
|
|
|
state.socket.on('disconnect', () => {
|
|
updateTop();
|
|
log('socket disconnected');
|
|
});
|
|
|
|
state.socket.on('connected', (payload) => log('connected', payload));
|
|
state.socket.on('command:status', (payload) => log('command:status', payload));
|
|
state.socket.on('stream:requested', (payload) => {
|
|
state.lastStreamSessionId = payload.streamSessionId;
|
|
renderState();
|
|
log('stream:requested', payload);
|
|
});
|
|
state.socket.on('stream:started', (payload) => {
|
|
state.lastStreamSessionId = payload.streamSessionId;
|
|
renderState();
|
|
log('stream:started', payload);
|
|
if (state.device?.role === 'client') {
|
|
deviceFetch(`/streams/${payload.streamSessionId}/subscribe-credentials`)
|
|
.then((x) => log('auto subscribe credentials', x))
|
|
.catch((e) => log('auto subscribe credentials failed', { error: e.message }));
|
|
}
|
|
});
|
|
state.socket.on('stream:ended', (payload) => log('stream:ended', payload));
|
|
state.socket.on('motion:detected', (payload) => log('motion:detected', payload));
|
|
state.socket.on('motion:ended', (payload) => log('motion:ended', payload));
|
|
|
|
state.socket.on('command:received', async (payload) => {
|
|
log('command:received', payload);
|
|
|
|
try {
|
|
if (payload.commandType === 'start_stream' && payload.payload?.streamSessionId) {
|
|
await deviceFetch(`/streams/${payload.payload.streamSessionId}/accept`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({}),
|
|
});
|
|
|
|
const creds = await deviceFetch(`/streams/${payload.payload.streamSessionId}/publish-credentials`);
|
|
log('auto publish credentials', creds);
|
|
}
|
|
|
|
if (payload.commandType === 'stop_stream' && payload.payload?.streamSessionId) {
|
|
await deviceFetch(`/streams/${payload.payload.streamSessionId}/end`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ reason: 'completed' }),
|
|
});
|
|
}
|
|
|
|
state.socket.emit('command:ack', { commandId: payload.commandId, status: 'acknowledged' });
|
|
} catch (error) {
|
|
state.socket.emit('command:ack', {
|
|
commandId: payload.commandId,
|
|
status: 'rejected',
|
|
error: error.message,
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
$('signUpBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/api/auth/sign-up/email', {
|
|
method: 'POST',
|
|
body: JSON.stringify(getAuthPayload()),
|
|
});
|
|
log('sign up', payload);
|
|
$('sessionBtn').click();
|
|
} catch (error) {
|
|
log('sign up failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('signInBtn').addEventListener('click', async () => {
|
|
try {
|
|
const authPayload = getAuthPayload();
|
|
const payload = await authFetch('/api/auth/sign-in/email', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ email: authPayload.email, password: authPayload.password }),
|
|
});
|
|
log('sign in', payload);
|
|
$('sessionBtn').click();
|
|
} catch (error) {
|
|
log('sign in failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('sessionBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/api/auth/get-session');
|
|
state.session = payload?.session ? payload : null;
|
|
log('session', payload);
|
|
} catch (error) {
|
|
state.session = null;
|
|
log('session check failed', { error: error.message });
|
|
}
|
|
updateFlow();
|
|
});
|
|
|
|
$('signOutBtn').addEventListener('click', async () => {
|
|
try {
|
|
await authFetch('/api/auth/sign-out', { method: 'POST', body: JSON.stringify({}) });
|
|
} catch (error) {
|
|
log('sign out failed', { error: error.message });
|
|
}
|
|
|
|
state.session = null;
|
|
state.device = null;
|
|
state.deviceToken = null;
|
|
state.socket?.disconnect();
|
|
state.socket = null;
|
|
localStorage.removeItem('mobileSimDevice');
|
|
updateFlow();
|
|
});
|
|
|
|
$('registerBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/devices/register', {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
role: $('role').value,
|
|
name: $('deviceName').value.trim() || undefined,
|
|
platform: 'web',
|
|
appVersion: 'sim-ux-1',
|
|
pushToken: $('pushToken').value.trim() || undefined,
|
|
}),
|
|
});
|
|
|
|
state.device = payload.device;
|
|
state.deviceToken = payload.deviceToken;
|
|
saveLocal();
|
|
log('device registered', payload);
|
|
updateFlow();
|
|
} catch (error) {
|
|
log('register failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('loadSavedBtn').addEventListener('click', async () => {
|
|
loadLocal();
|
|
|
|
try {
|
|
if (state.device?.id && $('pushToken').value.trim()) {
|
|
await authFetch(`/devices/${state.device.id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify({ pushToken: $('pushToken').value.trim() }),
|
|
});
|
|
}
|
|
} catch (error) {
|
|
log('push token update failed', { error: error.message });
|
|
}
|
|
|
|
updateFlow();
|
|
});
|
|
|
|
const bindConnectButtons = (connectId, disconnectId) => {
|
|
$(connectId).addEventListener('click', () => {
|
|
try {
|
|
connectSocket();
|
|
} catch (error) {
|
|
log('connect failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$(disconnectId).addEventListener('click', () => {
|
|
state.socket?.disconnect();
|
|
});
|
|
};
|
|
|
|
bindConnectButtons('connectBtn', 'disconnectBtn');
|
|
bindConnectButtons('connectBtnCam', 'disconnectBtnCam');
|
|
|
|
$('linkBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.device?.id || state.device.role !== 'client') throw new Error('Current phone is not in client mode');
|
|
|
|
const cameraDeviceId = $('targetCameraId').value.trim();
|
|
if (!cameraDeviceId) throw new Error('Enter camera device id');
|
|
|
|
const payload = await authFetch('/device-links', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ cameraDeviceId, clientDeviceId: state.device.id }),
|
|
});
|
|
log('camera linked', payload);
|
|
} catch (error) {
|
|
log('link failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('requestStreamBtn').addEventListener('click', async () => {
|
|
try {
|
|
const cameraDeviceId = $('targetCameraId').value.trim();
|
|
if (!cameraDeviceId) throw new Error('Enter camera device id');
|
|
|
|
const payload = await deviceFetch('/streams/request', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ cameraDeviceId, reason: 'on_demand' }),
|
|
});
|
|
|
|
state.lastStreamSessionId = payload.streamSession.id;
|
|
renderState();
|
|
log('stream requested', payload);
|
|
} catch (error) {
|
|
log('stream request failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('fetchSubscribeBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastStreamSessionId) throw new Error('No stream session yet');
|
|
const payload = await deviceFetch(`/streams/${state.lastStreamSessionId}/subscribe-credentials`);
|
|
log('subscribe credentials', payload);
|
|
} catch (error) {
|
|
log('subscribe credentials failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('fetchPlaybackBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastStreamSessionId) throw new Error('No stream session yet');
|
|
const payload = await deviceFetch(`/streams/${state.lastStreamSessionId}/playback-token`);
|
|
log('playback token', payload);
|
|
} catch (error) {
|
|
log('playback token failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('listRecordingsBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await deviceFetch('/recordings/me/list');
|
|
if (payload.recordings?.length) {
|
|
state.lastRecordingId = payload.recordings[0].id;
|
|
}
|
|
renderState();
|
|
log('recordings', payload);
|
|
} catch (error) {
|
|
log('recordings list failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('downloadLatestRecordingBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastRecordingId) throw new Error('No recording selected');
|
|
const payload = await deviceFetch(`/recordings/${state.lastRecordingId}/download-url`);
|
|
log('recording download url', payload);
|
|
} catch (error) {
|
|
log('recording download failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('startMotionBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await deviceFetch('/events/motion/start', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ title: 'Motion from simulator', triggeredBy: 'motion' }),
|
|
});
|
|
state.lastMotionEventId = payload.event.id;
|
|
renderState();
|
|
log('motion started', payload);
|
|
} catch (error) {
|
|
log('motion start failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('endMotionBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastMotionEventId) throw new Error('No active motion event id');
|
|
const payload = await deviceFetch(`/events/${state.lastMotionEventId}/motion/end`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({ status: 'completed' }),
|
|
});
|
|
log('motion ended', payload);
|
|
} catch (error) {
|
|
log('motion end failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('fetchPublishBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastStreamSessionId) throw new Error('No stream session yet');
|
|
const payload = await deviceFetch(`/streams/${state.lastStreamSessionId}/publish-credentials`);
|
|
log('publish credentials', payload);
|
|
} catch (error) {
|
|
log('publish credentials failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('finalizeRecordingBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.lastStreamSessionId) throw new Error('No stream session yet');
|
|
const list = await deviceFetch('/recordings/me/list');
|
|
const target = list.recordings?.find((r) => r.streamSessionId === state.lastStreamSessionId) ?? list.recordings?.[0];
|
|
if (!target) throw new Error('No recording placeholder found');
|
|
|
|
state.lastRecordingId = target.id;
|
|
renderState();
|
|
|
|
const payload = await deviceFetch(`/recordings/${target.id}/finalize`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
objectKey: `recordings/${target.id}.mp4`,
|
|
bucket: 'videos',
|
|
durationSeconds: 12,
|
|
sizeBytes: 8 * 1024 * 1024,
|
|
}),
|
|
});
|
|
log('recording finalized', payload);
|
|
} catch (error) {
|
|
log('recording finalize failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('pollPushInboxBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await deviceFetch('/push-notifications/me');
|
|
if (payload.notifications?.length) {
|
|
state.latestPushNotificationId = payload.notifications[0].id;
|
|
}
|
|
renderState();
|
|
log('push inbox', payload);
|
|
} catch (error) {
|
|
log('push inbox failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('markLatestPushReadBtn').addEventListener('click', async () => {
|
|
try {
|
|
if (!state.latestPushNotificationId) throw new Error('No push notification selected');
|
|
const payload = await deviceFetch(`/push-notifications/${state.latestPushNotificationId}/read`, {
|
|
method: 'POST',
|
|
body: JSON.stringify({}),
|
|
});
|
|
log('push marked read', payload);
|
|
} catch (error) {
|
|
log('mark push read failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('dispatchPushWorkerBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await deviceFetch('/push-notifications/worker/dispatch', {
|
|
method: 'POST',
|
|
body: JSON.stringify({}),
|
|
});
|
|
log('push worker dispatch', payload);
|
|
} catch (error) {
|
|
log('push worker dispatch failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('fetchAuditBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await deviceFetch('/audit/device');
|
|
log('audit logs', payload);
|
|
} catch (error) {
|
|
log('audit fetch failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('checkLiveBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/ops/live');
|
|
log('ops live', payload);
|
|
} catch (error) {
|
|
log('ops live failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('checkReadyBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/ops/ready');
|
|
log('ops ready', payload);
|
|
} catch (error) {
|
|
log('ops ready failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('checkMetricsBtn').addEventListener('click', async () => {
|
|
try {
|
|
const payload = await authFetch('/ops/metrics');
|
|
log('ops metrics', payload);
|
|
} catch (error) {
|
|
log('ops metrics failed', { error: error.message });
|
|
}
|
|
});
|
|
|
|
$('navHome').addEventListener('click', () => {
|
|
setScreen(state.device?.role === 'camera' ? 'home-camera' : 'home-client');
|
|
updateFlow();
|
|
});
|
|
|
|
$('navActivity').addEventListener('click', () => {
|
|
setScreen('activity');
|
|
updateFlow();
|
|
});
|
|
|
|
$('navAccount').addEventListener('click', () => {
|
|
setScreen('account');
|
|
updateFlow();
|
|
});
|
|
|
|
const init = async () => {
|
|
loadLocal();
|
|
try {
|
|
const payload = await authFetch('/api/auth/get-session');
|
|
state.session = payload?.session ? payload : null;
|
|
} catch {
|
|
state.session = null;
|
|
}
|
|
|
|
updateFlow();
|
|
log('simulator initialized', {
|
|
hasSession: Boolean(state.session),
|
|
hasSavedDevice: Boolean(state.device),
|
|
});
|
|
};
|
|
|
|
init();
|
|
</script>
|
|
</body>
|
|
</html>
|