Refactor iOS version to use a bunch more shared code

This commit is contained in:
James Babcock committed 2026-09-22 21:26:51 -07:00
1 parent c3a0ba9f56
commit 4a194e37bb
40 files changed
+1874 -2605

No files matched your search

+87 -77
View File
@@ -1,88 +1,98 @@
import { StockConnection } from '../g2/stock-connection'
import { createFlashPrompt, flashPromptSelection } from '../g2/flash-prompt-protocol'
import * as p from '../g2/ble-protocol'
import { iosBluetooth } from './ios-bluetooth'
import { kotlinListener } from './kotlin-listener.ios'
import type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
export type { FlashPromptBattery, FlashPromptState } from './flash-prompt-communicator'
declare const FaceclawKitIosFlashPrompt: any, FaceclawKitFaceclawFlashPromptListener: any
function normalizePercent(value: number): number | null {
const n = Number(value)
return Number.isFinite(n) && n >= 0 ? n : null
}
/**
* iOS twin of the Android FlashPromptCommunicator wrapper over the shared Kotlin
* FlashPromptFlow (FaceclawKitIosFlashPrompt): connects and authenticates both arms, shows
* the pre-flash Yes/No page on the glasses (unless skipPrompt) and reads each arm's battery.
*/
export class FlashPromptCommunicator {
private logs = new Set<(line: string) => void>()
private states = new Set<(state: FlashPromptState, detail: string) => void>()
private results = new Set<(approved: boolean) => void>()
private batteries = new Set<(battery: FlashPromptBattery) => void>()
private link = new StockConnection(iosBluetooth(), line => { for (const fn of this.logs) fn(line) })
private communicator: any = null
private listenerProxy: object | null = null
private readonly logListeners = new Set<(line: string) => void>()
private readonly stateListeners = new Set<(state: FlashPromptState, detail: string) => void>()
private readonly resultListeners = new Set<(approved: boolean) => void>()
private readonly batteryListeners = new Set<(battery: FlashPromptBattery) => void>()
private started = false
private closed = false
private started = false
private heartbeat: ReturnType<typeof setInterval> | null = null
private stopSelection: (() => void) | null = null
constructor(private addresses: { right: string; left: string }, private warning: string, private options?: { skipPrompt?: boolean }) {}
onLog(fn: (line: string) => void): () => void { this.logs.add(fn); return () => this.logs.delete(fn) }
onStateChange(fn: (state: FlashPromptState, detail: string) => void): () => void { this.states.add(fn); return () => this.states.delete(fn) }
onResult(fn: (approved: boolean) => void): () => void { this.results.add(fn); return () => this.results.delete(fn) }
onBattery(fn: (battery: FlashPromptBattery) => void): () => void { this.batteries.add(fn); return () => this.batteries.delete(fn) }
private state(state: FlashPromptState, detail = ''): void { for (const fn of this.states) fn(state, detail) }
start(): void { if (!this.started) { this.started = true; void this.run() } }
constructor(
private readonly addresses: { right: string; left: string },
private readonly warningText: string,
private readonly options?: { skipPrompt?: boolean },
) {}
onLog(listener: (line: string) => void): () => void {
this.logListeners.add(listener)
return () => this.logListeners.delete(listener)
}
onStateChange(listener: (state: FlashPromptState, detail: string) => void): () => void {
this.stateListeners.add(listener)
return () => this.stateListeners.delete(listener)
}
onBattery(listener: (battery: FlashPromptBattery) => void): () => void {
this.batteryListeners.add(listener)
return () => this.batteryListeners.delete(listener)
}
onResult(listener: (approved: boolean) => void): () => void {
this.resultListeners.add(listener)
return () => this.resultListeners.delete(listener)
}
start(): void {
if (this.started) return
this.started = true
void this.run()
}
private async run(): Promise<void> {
let identifiers: Record<string, string>
try {
this.state('connecting')
await this.link.resolve({ ...this.addresses, ring: '' })
// Complete pairing for one peripheral before starting the other.
for (const role of ['right', 'left']) {
await this.link.connect(role)
if (role === 'right') this.state('connected')
await this.link.authenticate(role)
}
await this.link.request('right', p.SID.launch, p.prelude, 3500, 156)
if (!this.options?.skipPrompt) {
// Listen before creating the page: an immediate tap may precede its ACK.
const selection = this.selection()
// Attach a rejection handler immediately, even while create-page is pending.
void selection.catch(() => {})
for (let attempt = 0; ; attempt++) {
try { await this.link.request('right', p.SID.hub, magic => createFlashPrompt(magic, this.warning)); break }
catch (error) { this.link.check(); if (attempt) throw error }
}
this.state('prompting')
this.heartbeat = setInterval(() => {
void this.link.write('right', p.SID.hub, p.heartbeat(this.link.nextMagic())).catch(error => {
if (!this.closed) this.state('error', String(error.message ?? error))
this.close()
})
}, 4000)
const approved = await selection
this.stopHeartbeat()
// Stock shutdown returns the glasses to their normal UI before OTA.
await this.link.write('right', p.SID.hub, p.concat(p.integer(1, 9), p.integer(2, this.link.nextMagic()), p.bytes(11, p.integer(1, 0))))
if (!approved) { for (const fn of this.results) fn(false); return }
}
this.state('battery')
const battery: FlashPromptBattery = { right: null, left: null }
for (const role of ['right', 'left'] as const) {
for (let attempt = 0; attempt < 2; attempt++) {
try {
const message = await this.link.request(role, p.SID.settings, p.settingsQuery)
const values = p.readBytes(message.payload, 4), value = values ? p.readInteger(values, 12, -1) : -1
if (value >= 0 && value <= 100) { battery[role] = value; break }
} catch (error) { this.link.check() }
}
}
for (const fn of this.batteries) fn(battery)
for (const fn of this.results) fn(true)
} catch (error) { if (!this.closed) this.state('error', String((error as Error).message ?? error)) }
finally { this.close() }
this.emit(this.stateListeners, 'connecting', '')
identifiers = await iosBluetooth().resolveDevices({ ...this.addresses, ring: '' })
} catch (error) {
if (!this.closed) this.emit(this.stateListeners, 'error', String((error as Error).message ?? error))
return
}
if (this.closed) return
this.communicator = FaceclawKitIosFlashPrompt.alloc().initWithRightAddressLeftAddressWarningTextSkipPrompt(
identifiers.right ?? '', identifiers.left ?? '', this.warningText, Boolean(this.options?.skipPrompt))
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFlashPromptListener, {
onLogLine: (line: string) => this.emit(this.logListeners, String(line ?? '')),
onStateStateDetail: (state: string, detail: string) =>
this.emit(this.stateListeners, String(state) as FlashPromptState, String(detail ?? '')),
onBatteryRightPercentLeftPercent: (rightPercent: number, leftPercent: number) =>
this.emit(this.batteryListeners, { right: normalizePercent(rightPercent), left: normalizePercent(leftPercent) }),
onResultApproved: (approved: boolean) => this.emit(this.resultListeners, Boolean(approved)),
})
this.communicator.setListenerListener(this.listenerProxy)
this.communicator.start()
}
cancel(): void {
this.closed = true
try { this.communicator?.cancel() } catch { /* ignore */ }
}
private selection(): Promise<boolean> {
return new Promise((resolve, reject) => {
const cleanup = () => { clearTimeout(timer); off(); this.stopSelection = null }
const off = this.link.onMessage((role, message) => {
const result = role === 'right' ? flashPromptSelection(message) : null
if (result !== null) { cleanup(); resolve(result) }
})
const timer = setTimeout(() => { cleanup(); reject(new Error('No response from the glasses.')) }, 120_000)
this.stopSelection = () => { cleanup(); reject(new Error('Cancelled.')) }
})
close(): void {
this.closed = true
try { this.communicator?.close() } catch { /* ignore */ }
}
private emit<A extends unknown[]>(listeners: Set<(...args: A) => void>, ...args: A): void {
const snapshot = Array.from(listeners)
setTimeout(() => { for (const listener of snapshot) listener(...args) }, 0)
}
private stopHeartbeat(): void { if (this.heartbeat) clearInterval(this.heartbeat); this.heartbeat = null }
cancel(): void { this.close() }
close(): void { this.closed = true; this.stopHeartbeat(); this.stopSelection?.(); this.link.close() }
}