-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocol-decode.ts
More file actions
More file actions
Latest commit
53 lines (47 loc) · 1.66 KB
/
Copy pathprotocol-decode.ts
File metadata and controls
53 lines (47 loc) · 1.66 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
import { milliCToCelsius } from './protocol';
export interface JuneSnapshot {
url: string;
contentType: string;
}
export interface ProbeTelemetry {
probeC?: number;
probePresent?: boolean;
}
const CAMERA_HOSTS = new Set(['api.junelife.com', 'june-api.s3.amazonaws.com']);
function isTrustedCameraUrl(candidate: unknown): candidate is string {
if (typeof candidate !== 'string') {
return false;
}
try {
const url = new URL(candidate);
return url.protocol === 'https:' && CAMERA_HOSTS.has(url.hostname);
} catch {
return false;
}
}
export function parseCameraFrame(data: unknown): JuneSnapshot | null {
const record = data && typeof data === 'object' ? data as Record<string, unknown> : {};
const url = [record.image_url, record.signed_url].find(isTrustedCameraUrl);
if (!url) {
return null;
}
return { url, contentType: typeof record.content_type === 'string' ? record.content_type : 'image/jpeg' };
}
export function parseProbeTelemetry(data: unknown): ProbeTelemetry {
const record = data && typeof data === 'object' ? data as { sensor_data?: unknown } : {};
const sensorData = record.sensor_data && typeof record.sensor_data === 'object'
? record.sensor_data as { probe?: unknown }
: {};
const probes = Array.isArray(sensorData.probe) ? sensorData.probe : undefined;
const out: ProbeTelemetry = {};
if (!probes) {
return out;
}
const entry = probes.find((p: unknown): p is { value: number } =>
Boolean(p) && typeof p === 'object' && typeof (p as { value?: unknown }).value === 'number');
if (entry) {
out.probeC = milliCToCelsius(entry.value);
}
out.probePresent = probes.length > 0;
return out;
}