-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathperipherals.ts
More file actions
62 lines (54 loc) · 1.96 KB
/
peripherals.ts
File metadata and controls
62 lines (54 loc) · 1.96 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
import { createPeripheralClient } from '@screenly/edge-apps'
import type { PeripheralStateMessage } from '@screenly/edge-apps'
import {
getState,
setSensorReadings,
setLastPeripheralState,
} from '../core/state'
import { showWelcomeThenSwitch } from '../core/screen'
import { restartLogoutTimer } from '../core/timer'
import { authenticate } from './authenticate'
export function initPeripherals() {
const client = createPeripheralClient()
// TODO: Replace with the actual Edge App ID once the app is published.
const edgeAppId = '01JZQK8VW3MXNP4RSDTHF6CY2B'
client.register(edgeAppId)
client.watchState((msg: PeripheralStateMessage) => {
setLastPeripheralState(msg.request.edge_app_source_state)
const readings = msg.request.edge_app_source_state.states
const tempReading = readings.find((r) => 'ambient_temperature' in r)
const humidityReading = readings.find((r) => 'humidity' in r)
const pressureReading = readings.find((r) => 'air_pressure' in r)
setSensorReadings({
temperature: tempReading
? (tempReading.ambient_temperature as number)
: undefined,
humidity: humidityReading
? (humidityReading.humidity as number)
: undefined,
airPressure: pressureReading
? (pressureReading.air_pressure as number)
: undefined,
})
const cardReading = readings.find((r) => 'secure_card' in r)
if (cardReading) {
const MAX_CARD_AGE_MS = 60_000
const ageMs = Date.now() - (cardReading.timestamp as number)
if (ageMs > MAX_CARD_AGE_MS) {
if (getState().currentScreen !== 'public') {
showWelcomeThenSwitch('public')
}
} else {
const uid = (cardReading.secure_card as { uid: string }).uid
const role = authenticate(uid)
if (role) {
if (role !== getState().currentScreen) {
showWelcomeThenSwitch(role)
} else {
restartLogoutTimer()
}
}
}
}
})
}