forked from Blendman974/faceclaw
Merge branch 'main' into glanceboard-tilt-angle-and-glance-down
This commit is contained in:
commit
37d81d0308
416 files changed
+34629
-3885
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import type { GlassesType } from "../ui/dashboard-settings";
|
||||
import type { RingInput } from "../g2/ring-input";
|
||||
import { File, knownFolders, path, type ImageSource } from "@nativescript/core";
|
||||
import { REQUIRED_FACECLAW_FIRMWARE_VERSION, type FirmwareInfo } from "../g2/firmware-compat";
|
||||
@@ -18,6 +19,7 @@ declare const FaceclawKitFaceclawCompassListener: any;
|
||||
declare const FaceclawKitFaceclawImuListener: any;
|
||||
declare const FaceclawKitIosAmbientLightListener: any;
|
||||
declare const FaceclawKitIosMicStatusListener: any;
|
||||
declare const FaceclawKitIosKeyboardEventListener: any;
|
||||
|
||||
|
||||
export type CommunicatorPhase =
|
||||
@@ -197,6 +199,13 @@ class MicStatusListener extends NSObject {
|
||||
onNsDataMicStatusBodyArm(body: NSData, arm: string): void { this.handler(fromData(body), String(arm ?? "")); }
|
||||
}
|
||||
|
||||
@NativeClass()
|
||||
class KeyboardEventListener extends NSObject {
|
||||
static ObjCProtocols = [FaceclawKitIosKeyboardEventListener];
|
||||
handler!: (body: Uint8Array) => void;
|
||||
onNsDataKeyboardEventBody(body: NSData): void { this.handler(fromData(body)); }
|
||||
}
|
||||
|
||||
/**
|
||||
* The iOS twin of the Android communicator bridge: the same public API over
|
||||
* the shared Kotlin session (FaceclawKitIosGlassesSession, CoreBluetooth
|
||||
@@ -248,7 +257,10 @@ export class FaceclawCommunicatorBridge {
|
||||
return FaceclawCommunicatorBridge.active;
|
||||
}
|
||||
|
||||
constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string }) {
|
||||
constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string; glassesType?: GlassesType }) {
|
||||
if (addresses.glassesType === "rayneo-io") {
|
||||
throw new Error("RayNeo iO glasses are not supported on iOS yet.");
|
||||
}
|
||||
this.communicator = FaceclawKitIosGlassesSession.alloc().initWithRightAddressLeftAddressRingAddress(
|
||||
String(addresses.right ?? "").toUpperCase(),
|
||||
String(addresses.left ?? "").toUpperCase(),
|
||||
@@ -256,6 +268,7 @@ export class FaceclawCommunicatorBridge {
|
||||
);
|
||||
// The shared session halts on any other firmware ("incompatible-firmware").
|
||||
this.communicator.setRequiredFirmwareRevisionRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
|
||||
if (addresses.glassesType === "g1") this.communicator.useG1();
|
||||
// Guarded: an older framework build may not export it yet.
|
||||
if (addresses.ringRole && typeof this.communicator.setDirectRingRoleRole === "function") {
|
||||
this.communicator.setDirectRingRoleRole(addresses.ringRole);
|
||||
@@ -696,6 +709,9 @@ export class FaceclawCommunicatorBridge {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.setFaceclawWakeLeaseEnabledEnabled(Boolean(enabled))));
|
||||
}
|
||||
|
||||
/** The RayNeo iO's wake word; iOS doesn't drive an iO, so nothing to do. */
|
||||
async setVoiceWakeEnabled(_enabled: boolean): Promise<void> {}
|
||||
|
||||
async awaitEvenHubSessionReady(timeoutMs: number): Promise<boolean> {
|
||||
return this.enqueueNativeCall(() => Boolean(this.communicator.awaitEvenHubSessionReadyTimeoutMs(Math.round(nonNegativeNumber(timeoutMs)))));
|
||||
}
|
||||
@@ -795,6 +811,19 @@ export class FaceclawCommunicatorBridge {
|
||||
toData(new Uint8Array(record)), label, Boolean(rightTemple), Boolean(leftTemple)));
|
||||
}
|
||||
|
||||
/** Raw field-131 keyboard-bridge records, in arrival order, on the main queue. */
|
||||
addKeyboardEventListener(listener: (body: Uint8Array) => void): () => void {
|
||||
const proxy = KeyboardEventListener.new() as KeyboardEventListener;
|
||||
proxy.handler = listener;
|
||||
const handle = this.communicator.addKeyboardEventListenerNsDataListener(proxy);
|
||||
return () => { this.communicator.removeKeyboardEventListenerListener(handle); };
|
||||
}
|
||||
|
||||
/** Queue a CFW keyboard-bridge control for the right temple; false when nothing was queued. */
|
||||
sendFaceclawKeyboardControl(record: Uint8Array, label: string): boolean {
|
||||
return Boolean(this.communicator.sendFaceclawKeyboardControlRecordLabel(toData(new Uint8Array(record)), label));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every audio-characteristic packet, whatever enabled the mics, on the
|
||||
* main queue. Passive: sends nothing and doesn't affect capture.
|
||||
@@ -849,6 +878,11 @@ export class FaceclawCommunicatorBridge {
|
||||
return Boolean(this.communicator.isAncsAuthorized());
|
||||
}
|
||||
|
||||
/** False on a G1 whose firmware has no relay; a G2's depends on its firmware revision. */
|
||||
ancsRelayAvailable(): boolean {
|
||||
return Boolean(this.communicator.ancsRelayAvailable());
|
||||
}
|
||||
|
||||
onAncsAuthorization(listener: (authorized: boolean) => void): () => void {
|
||||
this.ancsAuthorizationListeners.add(listener);
|
||||
return () => this.ancsAuthorizationListeners.delete(listener);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { GlassesType } from "../ui/dashboard-settings";
|
||||
import type { RingInput } from "../g2/ring-input";
|
||||
import { ImageSource, Utils } from "@nativescript/core";
|
||||
import * as frameTimings from "./frame-timings";
|
||||
@@ -169,7 +170,7 @@ export class FaceclawCommunicatorBridge {
|
||||
private readonly firmwareInfoListeners = new Set<(info: FirmwareInfo) => void>();
|
||||
private readonly previewAnimationListeners = new Set<() => void>();
|
||||
|
||||
constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string }) {
|
||||
constructor(addresses: { right: string; left: string; ring?: string; ringRole?: string; glassesType?: GlassesType }) {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) throw new Error("Android application context unavailable");
|
||||
|
||||
@@ -181,6 +182,10 @@ export class FaceclawCommunicatorBridge {
|
||||
);
|
||||
// The shared session halts on any other firmware ("incompatible-firmware").
|
||||
this.communicator.setRequiredFirmwareRevision(REQUIRED_FACECLAW_FIRMWARE_VERSION);
|
||||
// RayNeo iO: one link (its address in both arm slots), its own handshake and page lease.
|
||||
if (addresses.glassesType === "rayneo-io") this.communicator.useRayNeoIo();
|
||||
// Even G1 on g1v0: two links, like a G2's arms, but frames composed here and sent to each arm.
|
||||
if (addresses.glassesType === "g1") this.communicator.useG1();
|
||||
// Which R1 role the direct ring link claims; only matters with a ring address.
|
||||
if (addresses.ringRole) this.communicator.setDirectRingRole(addresses.ringRole);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawBleCommunicatorListener({
|
||||
@@ -756,6 +761,14 @@ export class FaceclawCommunicatorBridge {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the glasses' own wake word reaches Faceclaw: on a RayNeo iO, its
|
||||
* "Hey RayNeo" switch (G2 glasses use the wake lease above instead).
|
||||
*/
|
||||
async setVoiceWakeEnabled(enabled: boolean): Promise<void> {
|
||||
await this.enqueueJavaCall(() => this.communicator.setVoiceWakeEnabled(enabled));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve only once the recreated layout and retained frame
|
||||
* are visible. CFW's deferred-dashboard READY is emitted from this barrier.
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { Application } from '@nativescript/core'
|
||||
import { parseG1FlashResult, type G1FlashResult } from '../g1/flash-plan'
|
||||
import { iosBluetooth } from './ios-bluetooth'
|
||||
import { kotlinListener } from './kotlin-listener.ios'
|
||||
import type { G1FlasherOptions } from './g1-flasher'
|
||||
export type { G1FlashMode, G1FlasherOptions } from './g1-flasher'
|
||||
|
||||
declare const FaceclawKitIosG1Flasher: any, FaceclawKitFaceclawFirmwareFlasherListener: any
|
||||
|
||||
/** CoreBluetooth has no way to remove a pairing; iOS Settings does it. */
|
||||
export function forgetG1Pairing(_address: string): boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* iOS twin of the Android G1Flasher: the shared Kotlin G1ArmFlashFlow over a CoreBluetooth central. As with the G2's
|
||||
* flasher, the phone must stay in the foreground with the screen awake, and addresses are mapped to peripheral
|
||||
* identifiers first.
|
||||
*/
|
||||
export class G1Flasher {
|
||||
private flasher: any = null
|
||||
private listenerProxy: object | null = null
|
||||
private logListener: ((line: string) => void) | null = null
|
||||
private progressListener: ((bytesSent: number, bytesTotal: number) => void) | null = null
|
||||
private stateListener: ((state: string) => void) | null = null
|
||||
private previousIdle = false
|
||||
private cancelled = false
|
||||
private foregroundError = ''
|
||||
|
||||
constructor(private readonly options: G1FlasherOptions) {}
|
||||
|
||||
onLog(listener: (line: string) => void): void { this.logListener = listener }
|
||||
onProgress(listener: (bytesSent: number, bytesTotal: number) => void): void { this.progressListener = listener }
|
||||
onState(listener: (state: string) => void): void { this.stateListener = listener }
|
||||
|
||||
private readonly backgrounded = () => {
|
||||
this.foregroundError = 'The install stopped because Faceclaw left the foreground. Return to Faceclaw and try again.'
|
||||
this.cancel()
|
||||
}
|
||||
|
||||
run(): Promise<G1FlashResult> {
|
||||
this.previousIdle = UIApplication.sharedApplication.idleTimerDisabled
|
||||
UIApplication.sharedApplication.idleTimerDisabled = true
|
||||
Application.on(Application.suspendEvent, this.backgrounded)
|
||||
const restore = () => {
|
||||
Application.off(Application.suspendEvent, this.backgrounded)
|
||||
UIApplication.sharedApplication.idleTimerDisabled = this.previousIdle
|
||||
}
|
||||
return new Promise<G1FlashResult>((resolve, reject) => {
|
||||
const identifier = iosBluetooth().peripheralIdentifier(this.options.address, this.options.side)
|
||||
if (!identifier) throw new Error(`Could not find the ${this.options.side} arm. Keep the glasses in the case with the lid open.`)
|
||||
this.flasher = FaceclawKitIosG1Flasher.alloc().initWithAddressSideFirmwarePathNetCorePathModeConnectWindowMs(
|
||||
identifier, this.options.side, this.options.firmwarePath, this.options.netCorePath ?? '', this.options.mode, this.options.connectWindowMs)
|
||||
this.listenerProxy = kotlinListener(FaceclawKitFaceclawFirmwareFlasherListener, {
|
||||
onLogLine: (line: string) => this.logListener?.(String(line ?? '')),
|
||||
onProgressLensComponentIndexComponentCountBlockIndexBlockCountBytesSentBytesTotal: (
|
||||
_lens: string, _componentIndex: number, _componentCount: number, _blockIndex: number, _blockCount: number, sent: number, total: number,
|
||||
) => this.progressListener?.(Number(sent), Number(total)),
|
||||
onStateStateDetail: (state: string) => this.stateListener?.(String(state)),
|
||||
onCompleteSuccessDetail: (success: boolean, detail: string) => {
|
||||
if (success) resolve(parseG1FlashResult(String(detail ?? '')))
|
||||
else reject(new Error(this.foregroundError || String(detail ?? '') || 'The install failed.'))
|
||||
},
|
||||
})
|
||||
this.flasher.setListenerListener(this.listenerProxy)
|
||||
if (this.cancelled) this.flasher.cancel()
|
||||
this.flasher.start()
|
||||
}).finally(restore)
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.cancelled = true
|
||||
try { this.flasher?.cancel() } catch { /* ignore */ }
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.cancel()
|
||||
try { this.flasher?.close() } catch { /* ignore */ }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
import { parseG1FlashResult, type G1FlashResult, type G1Side } from "../g1/flash-plan";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/** "restore": Even's firmware back on an arm running g1v0, both of its images. */
|
||||
export type G1FlashMode = "stock" | "update" | "probe" | "restore";
|
||||
|
||||
export type G1FlasherOptions = {
|
||||
address: string;
|
||||
side: G1Side;
|
||||
/** The application core's image. */
|
||||
firmwarePath: string;
|
||||
/** A restore's network-core image. */
|
||||
netCorePath?: string;
|
||||
mode: G1FlashMode;
|
||||
/** How long to keep trying to reach the arm and pair with it. */
|
||||
connectWindowMs: number;
|
||||
};
|
||||
|
||||
/** Removes the phone's pairing with an address, which the glasses no longer use or no longer share. False: this phone can't. */
|
||||
export function forgetG1Pairing(address: string): boolean {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) return false;
|
||||
com.faceclaw.app.FaceclawG1Flasher.forgetPairing(context, address);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** TS wrapper around FaceclawG1Flasher: one arm's install, its restore to Even's firmware, or a question about what it runs. */
|
||||
export class G1Flasher {
|
||||
private readonly flasher: any;
|
||||
private readonly listenerProxy: any;
|
||||
private logListener: ((line: string) => void) | null = null;
|
||||
private progressListener: ((bytesSent: number, bytesTotal: number) => void) | null = null;
|
||||
private stateListener: ((state: string) => void) | null = null;
|
||||
private completion: ((success: boolean, detail: string) => void) | null = null;
|
||||
|
||||
constructor(options: G1FlasherOptions) {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) throw new Error("Android application context unavailable");
|
||||
this.flasher = new com.faceclaw.app.FaceclawG1Flasher(
|
||||
context, options.address, options.side, options.firmwarePath, options.netCorePath ?? "", options.mode, options.connectWindowMs,
|
||||
);
|
||||
this.listenerProxy = new com.faceclaw.app.FaceclawFirmwareFlasherListener({
|
||||
onLog: (line: string) => this.logListener?.(String(line)),
|
||||
onProgress: (_lens: string, _component: number, _components: number, _block: number, _blocks: number, sent: number, total: number) =>
|
||||
this.progressListener?.(Number(sent), Number(total)),
|
||||
onState: (state: string) => this.stateListener?.(String(state)),
|
||||
onComplete: (success: boolean, detail: string) => this.completion?.(Boolean(success), String(detail ?? "")),
|
||||
});
|
||||
this.flasher.setListener(this.listenerProxy);
|
||||
}
|
||||
|
||||
onLog(listener: (line: string) => void): void {
|
||||
this.logListener = listener;
|
||||
}
|
||||
|
||||
onProgress(listener: (bytesSent: number, bytesTotal: number) => void): void {
|
||||
this.progressListener = listener;
|
||||
}
|
||||
|
||||
onState(listener: (state: string) => void): void {
|
||||
this.stateListener = listener;
|
||||
}
|
||||
|
||||
run(): Promise<G1FlashResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.completion = (success, detail) => {
|
||||
this.completion = null;
|
||||
if (success) resolve(parseG1FlashResult(detail));
|
||||
else reject(new Error(detail || "The install failed."));
|
||||
};
|
||||
this.flasher.start();
|
||||
});
|
||||
}
|
||||
|
||||
cancel(): void {
|
||||
this.flasher.cancel();
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.flasher.close();
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
|
||||
|
||||
/**
|
||||
* A glasses icon as a white-on-transparent phone image (gray becomes alpha),
|
||||
* for the dark header.
|
||||
* which the phone UI tints to suit its background.
|
||||
*/
|
||||
export function grayImageToWhiteIconSource(image: GrayImage): ImageSource | null {
|
||||
const baked = image.withDrawsBaked()
|
||||
|
||||
@@ -30,7 +30,7 @@ export function grayImageToPreviewSource(image: GrayImage): ImageSource | null {
|
||||
|
||||
/**
|
||||
* A glasses icon as a white-on-transparent phone image (gray becomes alpha),
|
||||
* for the dark action bar.
|
||||
* which the phone UI tints to suit its background.
|
||||
*/
|
||||
export function grayImageToWhiteIconSource(image: GrayImage): ImageSource | null {
|
||||
if (!global.isAndroid) {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import { toData, fromData } from './kotlin-data'
|
||||
import { rgbaFromPacket } from './image-rgba'
|
||||
declare const FaceclawGraphics: any
|
||||
|
||||
/** Decoded RGBA of encoded image bytes, or null when undecodable (transparent pixels come out black). */
|
||||
export function decodeImageRgba(bytes: Uint8Array): { width: number; height: number; rgba: Uint8Array } | null {
|
||||
const data = FaceclawGraphics.rgbaPacketFromImageData(toData(bytes))
|
||||
return data ? rgbaFromPacket(fromData(data)) : null
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { toUint8Array } from '../util/array-util'
|
||||
import { copyToJavaByteBuffer } from './java-direct-buffer'
|
||||
declare const com: any
|
||||
|
||||
/** Decoded RGBA (not premultiplied) of encoded image bytes, or null when undecodable. */
|
||||
export function decodeImageRgba(bytes: Uint8Array): { width: number; height: number; rgba: Uint8Array } | null {
|
||||
try {
|
||||
return rgbaFromPacket(toUint8Array(com.faceclaw.app.ImageFileLoader.decodeRgbaFromBytes(copyToJavaByteBuffer(bytes))))
|
||||
} catch (error) {
|
||||
console.warn(`decodeImageRgba failed: ${error}`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** [widthLo, widthHi, heightLo, heightHi, RGBA...] as an image, or null when short. */
|
||||
export function rgbaFromPacket(packet: Uint8Array): { width: number; height: number; rgba: Uint8Array } | null {
|
||||
if (packet.length < 4) return null
|
||||
const width = packet[0]! | (packet[1]! << 8)
|
||||
const height = packet[2]! | (packet[3]! << 8)
|
||||
if (width <= 0 || height <= 0 || packet.length < 4 + width * height * 4) return null
|
||||
return { width, height, rgba: packet.slice(4, 4 + width * height * 4) }
|
||||
}
|
||||
@@ -7,6 +7,8 @@ declare const FaceclawKitIosBleCentral: any, FaceclawKitIosProtocolPlatform: any
|
||||
|
||||
/** CBManagerState raw values reported by the Kotlin central. */
|
||||
export const BLE_STATE_UNSUPPORTED = 2, BLE_STATE_UNAUTHORIZED = 3, BLE_STATE_POWERED_OFF = 4, BLE_STATE_POWERED_ON = 5
|
||||
/** CBManagerAuthorization.allowedAlways. */
|
||||
const BLE_AUTHORIZATION_ALLOWED = 3
|
||||
|
||||
export type BluetoothEvent =
|
||||
| { kind: 'state'; state: number; authorization: number }
|
||||
@@ -50,17 +52,21 @@ export class IosBluetooth {
|
||||
}
|
||||
},
|
||||
})
|
||||
this.native.setScanListenerListener(this.scanListener)
|
||||
}
|
||||
onEvent(listener: (event: BluetoothEvent) => void): () => void {
|
||||
this.listeners.add(listener); return () => { this.listeners.delete(listener) }
|
||||
}
|
||||
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
|
||||
/**
|
||||
* Whether the user has allowed Bluetooth. Unlike `state` this is accurate before the
|
||||
* central exists, and reading it never shows the permission prompt.
|
||||
*/
|
||||
isAuthorized(): boolean { return Number(this.native.authorization()) === BLE_AUTHORIZATION_ALLOWED }
|
||||
/** Creates the central (first use shows the Bluetooth permission prompt) without blocking the JS thread. */
|
||||
private ensureCentral(): void {
|
||||
// A zero timeout returns immediately after creating the central; the state arrives
|
||||
// through the scan listener on the main queue.
|
||||
this.native.waitForPoweredOnTimeoutMs(0)
|
||||
// Registering the listener creates the central, or has an existing one re-report its
|
||||
// state; the state arrives through the listener on the main queue.
|
||||
this.native.setScanListenerListener(this.scanListener)
|
||||
}
|
||||
ensureReady(): Promise<void> {
|
||||
if (this.state === BLE_STATE_POWERED_ON) return Promise.resolve()
|
||||
@@ -129,6 +135,15 @@ export class IosBluetooth {
|
||||
})
|
||||
return result
|
||||
}
|
||||
/** The peripheral last heard at this address, if it was on this side. */
|
||||
peripheralIdentifier(address: string, role: string): string | null {
|
||||
try {
|
||||
const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}'))
|
||||
return saved.role === role && typeof saved.identifier === 'string' ? saved.identifier : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
|
||||
}
|
||||
let instance: IosBluetooth | null = null
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { FaceclawCommunicatorBridge } from "./faceclaw-communicator.ios";
|
||||
|
||||
/** The iOS twin of keyboard-link.ts, over the running shared-Kotlin session. */
|
||||
|
||||
export function keyboardLinkSession(): unknown {
|
||||
return FaceclawCommunicatorBridge.getActive();
|
||||
}
|
||||
|
||||
/** Queue a control record for the right temple; false when the session wasn't ready and nothing was queued. */
|
||||
export function sendKeyboardControlRecord(session: unknown, record: Uint8Array, label: string): boolean {
|
||||
return (session as FaceclawCommunicatorBridge).sendFaceclawKeyboardControl(record, label);
|
||||
}
|
||||
|
||||
/** Raw keyboard-bridge records (undecoded, possibly fragments), in arrival order. Returns an unsubscribe function. */
|
||||
export function addKeyboardEventListener(session: unknown, listener: (body: Uint8Array) => void): () => void {
|
||||
const unsubscribe = (session as FaceclawCommunicatorBridge).addKeyboardEventListener(listener);
|
||||
return () => {
|
||||
try {
|
||||
unsubscribe();
|
||||
} catch {
|
||||
// The session may have been closed during a reconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { toJavaBytes } from "./cloud-stt";
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/**
|
||||
* Platform plumbing for the CFW keyboard bridge (app/g2/keyboard/): the running
|
||||
* session to send field-130 controls on and to hear field-131 records from.
|
||||
* The session is opaque and is a different object after a reconnect.
|
||||
*/
|
||||
|
||||
export function keyboardLinkSession(): unknown {
|
||||
if (!global.isAndroid) return null;
|
||||
try {
|
||||
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Queue a control record for the right temple; false when the session wasn't ready and nothing was queued. */
|
||||
export function sendKeyboardControlRecord(session: unknown, record: Uint8Array, label: string): boolean {
|
||||
return (session as any).sendFaceclawKeyboardControl(toJavaBytes(record), label) !== false;
|
||||
}
|
||||
|
||||
/** Raw keyboard-bridge records (undecoded, possibly fragments), in arrival order. Returns an unsubscribe function. */
|
||||
export function addKeyboardEventListener(session: unknown, listener: (body: Uint8Array) => void): () => void {
|
||||
const communicator = session as any;
|
||||
const proxy = new com.faceclaw.app.FaceclawKeyboardEventListener({
|
||||
onKeyboardEvent: (body: any) => listener(toUint8Array(body)),
|
||||
});
|
||||
communicator.addKeyboardEventListener(proxy);
|
||||
return () => {
|
||||
try {
|
||||
communicator.removeKeyboardEventListener(proxy);
|
||||
} catch {
|
||||
// The old communicator instance may be gone after a reconnect.
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { knownFolders, path as paths } from '@nativescript/core'
|
||||
|
||||
// Kotlin/Native facade exported by FaceclawKit (native/kotlin/shared/src/iosMain/.../IosMicRecorder.kt).
|
||||
declare const FaceclawKitIosMicRecordings: any
|
||||
|
||||
/**
|
||||
* Records 16 kHz mono PCM16 to a Matroska audio file (.mka) in
|
||||
* Documents/Downloads while it is captured, through the shared Kotlin writer
|
||||
* that Android's captures are recorded with. The file is written about a
|
||||
* second at a time, so a crash leaves everything up to the last second.
|
||||
*/
|
||||
export class MicRecorder {
|
||||
private constructor(private recorder: any, readonly path: string) {}
|
||||
|
||||
/**
|
||||
* A recorder for Downloads/<name>.mka (a -2, -3... suffix if taken), or null.
|
||||
* `info` describes the capture and is stored in the file.
|
||||
*/
|
||||
static open(name: string, info: Record<string, unknown> = {}): MicRecorder | null {
|
||||
const fm = NSFileManager.defaultManager
|
||||
const dir = paths.join(knownFolders.documents().path, 'Downloads')
|
||||
try {
|
||||
fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(dir, true, null)
|
||||
let path = paths.join(dir, `${name}.mka`)
|
||||
for (let n = 2; fm.fileExistsAtPath(path); n++) path = paths.join(dir, `${name}-${n}.mka`)
|
||||
const recorder = FaceclawKitIosMicRecordings.shared.openPathConfigJson(path, JSON.stringify(info))
|
||||
return recorder ? new MicRecorder(recorder, path) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
append(pcm: Uint8Array): void {
|
||||
if (!this.recorder || !pcm.length) return
|
||||
// The handle is the buffer's start: a view into a larger buffer is copied out first.
|
||||
const bytes = pcm.byteOffset === 0 && pcm.byteLength === pcm.buffer.byteLength ? pcm : pcm.slice()
|
||||
try {
|
||||
// Copied by the recorder before this returns.
|
||||
this.recorder.appendPcm16le(NSData.dataWithBytesLength(interop.handleof(bytes.buffer), bytes.byteLength))
|
||||
} catch {
|
||||
// Stop recording rather than retry.
|
||||
this.close()
|
||||
}
|
||||
}
|
||||
|
||||
close(): void {
|
||||
try { this.recorder?.finish() } catch { /* already closed */ }
|
||||
this.recorder = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
declare const android: any;
|
||||
declare const java: any;
|
||||
|
||||
/** Extension of the recordings Save Recording writes, and Transcribe replays (Matroska audio). */
|
||||
export const RECORDING_EXTENSION = ".mka";
|
||||
|
||||
export function isMicRecordingFile(name: string): boolean {
|
||||
return name.toLowerCase().endsWith(RECORDING_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* The path for a new recording in the public Downloads directory:
|
||||
* Downloads/<name>.mka, with a -2, -3... suffix if that is taken. Null when
|
||||
* Downloads isn't available. The file itself is created by whoever records.
|
||||
*/
|
||||
export function newRecordingPath(name: string): string | null {
|
||||
try {
|
||||
const downloads = android.os.Environment.getExternalStoragePublicDirectory(
|
||||
android.os.Environment.DIRECTORY_DOWNLOADS,
|
||||
);
|
||||
downloads.mkdirs();
|
||||
let file = new java.io.File(downloads, `${name}${RECORDING_EXTENSION}`);
|
||||
for (let n = 2; file.exists(); n++) file = new java.io.File(downloads, `${name}-${n}${RECORDING_EXTENSION}`);
|
||||
return String(file.getAbsolutePath());
|
||||
} catch (error) {
|
||||
console.warn(`Voice: no Downloads directory for ${name}${RECORDING_EXTENSION}: ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import type { CloudSttTranscriptEvent } from './cloud-stt'
|
||||
import type { ReconnectingSttClient } from './reconnecting-stt'
|
||||
import { createCloudSttClient, usesCloudStt, type VoiceProviderSelection } from './cloud-stt-provider'
|
||||
import { createSpeechAnalyzerClient, speechAnalyzerSupported } from './speech-analyzer-stt.ios'
|
||||
import { WavRecorder } from './wav-recorder.ios'
|
||||
import { MicRecorder } from './mic-recorder.ios'
|
||||
import { pcmLevel } from '../util/audio-level'
|
||||
|
||||
declare const FaceclawSpeech: any
|
||||
@@ -57,11 +57,14 @@ export class IosVoiceControlBridge {
|
||||
private continuous = false
|
||||
// Shown instead of the native "ended" status once a drain the bridge started completes.
|
||||
private drainStatus: string | null = null
|
||||
// Continuous captures' audio goes to Downloads/<recordingName>.wav while it is
|
||||
// Continuous captures' audio goes to Downloads/<recordingName>.mka while it is
|
||||
// set. The file outlives a capture, so a restarted one keeps appending to it,
|
||||
// and it is created by the first audio, so a failed start leaves no file.
|
||||
private recordingName = ''
|
||||
private recorder: WavRecorder | null = null
|
||||
private recorder: MicRecorder | null = null
|
||||
// The file could not be created: one attempt per recording, not one per packet.
|
||||
private recordingFailure = false
|
||||
private readonly recordingListeners = new Set<() => void>()
|
||||
// Raw-PCM tap (EvenHub mic apps): decode-only glasses audio, broadcast to
|
||||
// rawListeners while any owner wants it, from the session setRawSession
|
||||
// bound. It shares the one glasses mic with speech capture, which preempts
|
||||
@@ -206,22 +209,37 @@ export class IosVoiceControlBridge {
|
||||
/** Where a listening continuous capture gets its audio; null when none is listening. */
|
||||
get continuousSource(): 'glasses' | 'phone' | null { return this.continuous && this.capturing ? this.source : null }
|
||||
/**
|
||||
* Record continuous captures' audio to Downloads/<name>.wav, across restarts,
|
||||
* Record continuous captures' audio to Downloads/<name>.mka, across restarts,
|
||||
* until called with another name or ''. Only cloud captures supply PCM.
|
||||
*/
|
||||
recordTo(name: string): void {
|
||||
if (name === this.recordingName) return
|
||||
this.recorder?.close(); this.recorder = null
|
||||
this.recordingName = name
|
||||
this.setRecording(name, false)
|
||||
}
|
||||
/** Whether continuous captures' audio is being saved (see recordTo). */
|
||||
get recording(): boolean { return this.recordingName !== '' && !this.recordingFailure }
|
||||
/** Whether the recording asked for could not be saved (its file could not be created). */
|
||||
get recordingFailed(): boolean { return this.recordingFailure }
|
||||
/** `recording` or `recordingFailed` changed. */
|
||||
onRecordingChange(listener: () => void): () => void { this.recordingListeners.add(listener); return () => { this.recordingListeners.delete(listener) } }
|
||||
private setRecording(name: string, failed: boolean): void {
|
||||
const wasRecording = this.recording, hadFailed = this.recordingFailure
|
||||
this.recordingName = name; this.recordingFailure = failed
|
||||
if (wasRecording === this.recording && hadFailed === failed) return
|
||||
for (const fn of [...this.recordingListeners]) fn()
|
||||
}
|
||||
private record(pcm: Uint8Array): void {
|
||||
if (!this.recorder && this.recordingName) {
|
||||
this.recorder = WavRecorder.open(this.recordingName)
|
||||
if (!this.recording) return
|
||||
if (!this.recorder) {
|
||||
this.recorder = MicRecorder.open(this.recordingName, {
|
||||
recording: this.recordingName, startedAt: new Date().toISOString(), platform: 'ios',
|
||||
source: this.source === 'phone' ? 'phone' : 'glasses', mics: 1,
|
||||
})
|
||||
this.log(this.recorder ? `Voice: recording audio to ${this.recorder.path}` : 'Voice: could not create the audio recording')
|
||||
// One attempt per recording, not one per packet.
|
||||
if (!this.recorder) this.recordingName = ''
|
||||
if (!this.recorder) { this.setRecording(this.recordingName, true); return }
|
||||
}
|
||||
this.recorder?.append(pcm)
|
||||
this.recorder.append(pcm)
|
||||
}
|
||||
/** End continuous capture, transcribing the audio since the last pause. */
|
||||
stopContinuousCapture(): Promise<void> {
|
||||
|
||||
+360
-10
@@ -1,7 +1,8 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
import { type CloudSttTranscriptEvent, CloudSttClient } from "./cloud-stt";
|
||||
import { type CloudSttTranscriptEvent, CloudSttClient, toJavaBytes } from "./cloud-stt";
|
||||
import { createCloudSttClient, transcriptionKey } from "./cloud-stt-provider";
|
||||
import { newRecordingPath } from "./mic-recording";
|
||||
import { SpeechPauseDetector } from "./speech-pause";
|
||||
import { toUint8Array } from "../util/array-util";
|
||||
import { pcmLevel } from "../util/audio-level";
|
||||
@@ -60,7 +61,6 @@ export type PushToTalkOptions = {
|
||||
whisperServerApiKey?: string;
|
||||
/** Spoken language for cloud providers (see VoiceProviderSelection.language). */
|
||||
language?: string;
|
||||
saveRecording: boolean;
|
||||
/**
|
||||
* Watch the mic and fire onSpeechEnd when the speaker stops. For hands-free
|
||||
* ("Hey Even") capture, which has no button release to end the utterance.
|
||||
@@ -84,6 +84,49 @@ export type PushToTalkOptions = {
|
||||
* frame). Null/absent leaves the mic omnidirectional.
|
||||
*/
|
||||
beamFilter?: { centerDeg: number; halfWidthDeg: number } | null;
|
||||
/**
|
||||
* Capture from the temples' own microphones (the custom firmware's
|
||||
* mic_control stream) instead of the glasses' mixed stream. Each temple
|
||||
* streams its microphones on its own link and clock; the phone mixes them
|
||||
* into the mono feed that is transcribed. Null/absent keeps the mixed
|
||||
* stream; ignored with usePhoneMic.
|
||||
*/
|
||||
micArray?: MicArrayCapture | null;
|
||||
/**
|
||||
* Play this recording (a .mka file from Save Recording) in real time in
|
||||
* place of any microphone. The capture needs no glasses, and ends by
|
||||
* itself when the recording does.
|
||||
*/
|
||||
replay?: { path: string; startMs: number } | null;
|
||||
/** What the caller knows of the capture, for the description stored in a recording of it. */
|
||||
captureInfo?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type MicArrayCapture = {
|
||||
/** The mic_control CONFIGURE record both temples are armed with (see mic-protocol.ts). */
|
||||
configure: Uint8Array;
|
||||
};
|
||||
|
||||
/** Progress of a capture that replays a recording; positions are ms into it. */
|
||||
export type VoiceReplayEvent = {
|
||||
/**
|
||||
* `start` once the file is open, `position` about once per second played,
|
||||
* `seek` when a jump took effect, `end` when the recording ran out, and
|
||||
* `error` when it could not be opened (the status says why).
|
||||
*/
|
||||
kind: "start" | "position" | "seek" | "end" | "error";
|
||||
positionMs: number;
|
||||
durationMs: number;
|
||||
};
|
||||
|
||||
/** What a listening view (Transcribe, Translate) takes from a capture: the microphone's, or a replay's. */
|
||||
export type VoiceEventSource = {
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void;
|
||||
onTranscript(listener: (event: VoiceTranscriptEvent) => void): () => void;
|
||||
onSpeechPause(listener: () => void): () => void;
|
||||
onMicLevel(listener: (level: number) => void): () => void;
|
||||
readonly recording: boolean;
|
||||
readonly recordingFailed: boolean;
|
||||
};
|
||||
|
||||
/** Who currently wants the mic running. */
|
||||
@@ -130,9 +173,13 @@ export class FaceclawVoiceControlBridge {
|
||||
private captureEndpointing = false;
|
||||
// Non-null while a cloud provider owns the transcript; Java only decodes PCM.
|
||||
private cloudClient: CloudSttClient | null = null;
|
||||
// What the running STT stream transcribes with (transcriptionKey); a holder
|
||||
// asking for something else restarts the stream rather than sharing it.
|
||||
// What the running STT stream captures from and transcribes with
|
||||
// (streamKey); a holder asking for something else restarts the stream
|
||||
// rather than sharing it.
|
||||
private streamProvider: string | null = null;
|
||||
// The options of the running stream, for the description of a recording of it.
|
||||
private streamOptions: PushToTalkOptions | null = null;
|
||||
private readonly replayListeners = new Set<(event: VoiceReplayEvent) => void>();
|
||||
// Raw-PCM tap (EvenHub mic apps, the Microphones app): when active, the
|
||||
// controller runs in the decode-only "cloud" mode and every decoded PCM
|
||||
// frame is broadcast to these listeners. STT capture preempts it — a live
|
||||
@@ -150,6 +197,17 @@ export class FaceclawVoiceControlBridge {
|
||||
// the bridge swallow it.
|
||||
private verificationActive = false;
|
||||
private verificationRejected = false;
|
||||
// Continuous captures' audio goes to Downloads/<recordingName>.mka while it is
|
||||
// set: every source's chunks as they arrive, before any processing, written
|
||||
// by the controller. The file outlives a capture, so a restarted one keeps
|
||||
// adding to it, and it is created by the first audio, so a failed start
|
||||
// leaves no file.
|
||||
private recordingName = "";
|
||||
// The file is open in the controller.
|
||||
private recordingOpen = false;
|
||||
// The file could not be created: one attempt per recording, not one per packet.
|
||||
private recordingFailure = false;
|
||||
private readonly recordingListeners = new Set<() => void>();
|
||||
|
||||
onStatus(listener: (state: VoiceControlState) => void): () => void {
|
||||
this.statusListeners.add(listener);
|
||||
@@ -242,6 +300,125 @@ export class FaceclawVoiceControlBridge {
|
||||
this.acquireCapture("continuous", options);
|
||||
}
|
||||
|
||||
/**
|
||||
* End the continuous capture as an utterance: stop it and have the
|
||||
* transcript of its last audio come as a final. For a capture whose audio
|
||||
* ran out by itself (a replay).
|
||||
*/
|
||||
finishContinuousCapture(): void {
|
||||
this.releaseCapture("continuous", true);
|
||||
}
|
||||
|
||||
/** Progress of a capture that replays a recording (PushToTalkOptions.replay). */
|
||||
onReplay(listener: (event: VoiceReplayEvent) => void): () => void {
|
||||
this.replayListeners.add(listener);
|
||||
return () => this.replayListeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Continue the running replay from `positionMs` into its recording. */
|
||||
seekReplay(positionMs: number): void {
|
||||
this.controller?.seekReplay(Math.max(0, Math.round(positionMs)));
|
||||
}
|
||||
|
||||
/** Hold the running replay where it is, or let it go on. */
|
||||
setReplayPaused(paused: boolean): void {
|
||||
this.controller?.setReplayPaused(paused);
|
||||
}
|
||||
|
||||
/** Whether a capture is running and still has audio coming (a replay stops being so at its end). */
|
||||
isCaptureLive(): boolean {
|
||||
return this.micIsLive();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record continuous captures' audio to Downloads/<name>.mka, across restarts,
|
||||
* until called with another name or "".
|
||||
*/
|
||||
recordTo(name: string): void {
|
||||
if (name === this.recordingName) return;
|
||||
this.closeRecording();
|
||||
this.setRecording(name, false);
|
||||
}
|
||||
|
||||
private closeRecording(): void {
|
||||
if (!this.recordingOpen) return;
|
||||
this.recordingOpen = false;
|
||||
try {
|
||||
this.controller?.stopRecording();
|
||||
} catch (error) {
|
||||
console.warn(`Voice: closing the recording failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether continuous captures' audio is being saved (see recordTo). */
|
||||
get recording(): boolean {
|
||||
return this.recordingName !== "" && !this.recordingFailure;
|
||||
}
|
||||
|
||||
/** Whether the recording asked for could not be saved (its file could not be created). */
|
||||
get recordingFailed(): boolean {
|
||||
return this.recordingFailure;
|
||||
}
|
||||
|
||||
/** `recording` or `recordingFailed` changed. */
|
||||
onRecordingChange(listener: () => void): () => void {
|
||||
this.recordingListeners.add(listener);
|
||||
return () => this.recordingListeners.delete(listener);
|
||||
}
|
||||
|
||||
private setRecording(name: string, failed: boolean): void {
|
||||
const wasRecording = this.recording;
|
||||
const hadFailed = this.recordingFailure;
|
||||
this.recordingName = name;
|
||||
this.recordingFailure = failed;
|
||||
if (wasRecording === this.recording && hadFailed === failed) return;
|
||||
for (const listener of Array.from(this.recordingListeners)) listener();
|
||||
}
|
||||
|
||||
/**
|
||||
* Audio arrived for a continuous capture: open the recording asked for, if
|
||||
* it isn't open yet. The controller writes it from then on, as the audio
|
||||
* arrives on its own thread.
|
||||
*/
|
||||
private ensureRecording(): void {
|
||||
if (!this.recording || this.recordingOpen) return;
|
||||
const path = newRecordingPath(this.recordingName);
|
||||
let opened = false;
|
||||
try {
|
||||
opened = path !== null && Boolean(this.controller?.startRecording(path, JSON.stringify(this.describeCapture())));
|
||||
} catch (error) {
|
||||
console.warn(`Voice: could not create the recording: ${error}`);
|
||||
}
|
||||
if (!opened) {
|
||||
this.setRecording(this.recordingName, true);
|
||||
return;
|
||||
}
|
||||
this.recordingOpen = true;
|
||||
console.log(`Voice: recording audio to ${path}`);
|
||||
}
|
||||
|
||||
/** Only captures a continuous holder has a part in are recorded: push-to-talk on its own is not. */
|
||||
private syncRecordingEnabled(): void {
|
||||
this.controller?.setRecordingEnabled(this.captureHolders.has("continuous"));
|
||||
}
|
||||
|
||||
/** The running capture, as the JSON stored in a recording of it. */
|
||||
private describeCapture(): Record<string, unknown> {
|
||||
const options = this.streamOptions;
|
||||
return {
|
||||
recording: this.recordingName,
|
||||
startedAt: new Date().toISOString(),
|
||||
source: options?.usePhoneMic ? "phone" : "glasses",
|
||||
micArray: Boolean(options?.micArray) && !options?.usePhoneMic,
|
||||
transcription: { provider: options?.provider ?? "", language: options?.language ?? "" },
|
||||
processing: {
|
||||
noiseSuppression: Boolean(options?.noiseSuppression),
|
||||
beamFilter: options?.beamFilter ?? null,
|
||||
},
|
||||
...options?.captureInfo,
|
||||
};
|
||||
}
|
||||
|
||||
/** Subscribe to decoded raw mic PCM (16 kHz mono S16LE). */
|
||||
onRawPcm(listener: RawPcmListener): () => void {
|
||||
this.rawPcmListeners.add(listener);
|
||||
@@ -305,8 +482,9 @@ export class FaceclawVoiceControlBridge {
|
||||
// The raw tap is strictly the G2 stream; clear any phone-mic flag a
|
||||
// preview-mode capture may have left on the shared controller.
|
||||
this.controller?.setUsePhoneMic(false);
|
||||
this.controller?.setSaveRecordings(false);
|
||||
this.controller?.setEndpointing(false);
|
||||
this.controller?.setMicArray(null);
|
||||
this.controller?.setReplay(null, 0);
|
||||
// Raw means raw: the Microphones session (and EvenHub apps) get the
|
||||
// unprocessed stream and run their own beam/suppression processing.
|
||||
this.controller?.setNoiseSuppression(false);
|
||||
@@ -342,19 +520,33 @@ export class FaceclawVoiceControlBridge {
|
||||
const micLive = this.micIsLive();
|
||||
this.suspendedHolders.delete(holder);
|
||||
this.captureHolders.add(holder);
|
||||
if (micLive && this.streamProvider === transcriptionKey(options)) {
|
||||
if (micLive && this.streamProvider === streamKey(options)) {
|
||||
// Mic already running; the new holder just shares the existing stream
|
||||
// (transcripts are already broadcast to its listeners).
|
||||
this.syncRecordingEnabled();
|
||||
return;
|
||||
}
|
||||
// Any leftover capture is dead, or transcribes with another provider than
|
||||
// Any leftover capture is dead, or captures or transcribes otherwise than
|
||||
// this holder needs; drop it before starting the new one.
|
||||
if (this.started) this.teardownCapture();
|
||||
this.streamProvider = transcriptionKey(options);
|
||||
this.streamProvider = streamKey(options);
|
||||
this.streamOptions = options;
|
||||
this.ensureController();
|
||||
this.syncRecordingEnabled();
|
||||
// A recording under way carries on into this capture: note what changed.
|
||||
if (this.recordingOpen) {
|
||||
try {
|
||||
this.controller?.noteRecordingEvent(JSON.stringify({ event: "capture", ...this.describeCapture() }));
|
||||
} catch (error) {
|
||||
console.warn(`Voice: recording event failed: ${error}`);
|
||||
}
|
||||
}
|
||||
this.controller?.setCommunicator(options.communicator);
|
||||
this.controller?.setUsePhoneMic(Boolean(options.usePhoneMic));
|
||||
this.controller?.setSaveRecordings(options.saveRecording);
|
||||
this.controller?.setMicArray(
|
||||
options.micArray && !options.usePhoneMic && !options.replay ? toJavaBytes(options.micArray.configure) : null,
|
||||
);
|
||||
this.controller?.setReplay(options.replay?.path ?? null, Math.max(0, Math.round(options.replay?.startMs ?? 0)));
|
||||
this.controller?.setEndpointing(Boolean(options.endpointing));
|
||||
this.captureEndpointing = Boolean(options.endpointing);
|
||||
this.controller?.setNoiseSuppression(Boolean(options.noiseSuppression));
|
||||
@@ -379,7 +571,7 @@ export class FaceclawVoiceControlBridge {
|
||||
this.speechPause.reset();
|
||||
// Replaces whatever the last capture left behind ("Listening...", an
|
||||
// error, the permission prompt) until the controller reports progress.
|
||||
this.setStatus("Starting microphone...");
|
||||
this.setStatus(options.replay ? "Opening the recording..." : "Starting microphone...");
|
||||
// A previous push-to-talk commit may still be awaiting its final result.
|
||||
this.cloudClient?.stop();
|
||||
const cloudClient = this.createCloudClient(options);
|
||||
@@ -424,6 +616,7 @@ export class FaceclawVoiceControlBridge {
|
||||
if (commit) this.cloudClient?.finish();
|
||||
return;
|
||||
}
|
||||
this.syncRecordingEnabled();
|
||||
if (this.captureHolders.size > 0) {
|
||||
// Another holder still wants the mic; keep it running.
|
||||
return;
|
||||
@@ -448,6 +641,7 @@ export class FaceclawVoiceControlBridge {
|
||||
this.captureHolders.clear();
|
||||
this.suspendedHolders.clear();
|
||||
this.suspendedRaw = false;
|
||||
this.syncRecordingEnabled();
|
||||
this.teardownCapture();
|
||||
this.setStatus("Voice control stopped.");
|
||||
}
|
||||
@@ -465,6 +659,7 @@ export class FaceclawVoiceControlBridge {
|
||||
this.suspendedHolders.add(holder);
|
||||
}
|
||||
this.captureHolders.clear();
|
||||
this.syncRecordingEnabled();
|
||||
this.suspendedRaw = this.rawActive;
|
||||
this.teardownCapture();
|
||||
if (this.suspendedHolders.size > 0) {
|
||||
@@ -522,6 +717,7 @@ export class FaceclawVoiceControlBridge {
|
||||
private teardownCapture(): void {
|
||||
this.rawActive = false;
|
||||
this.streamProvider = null;
|
||||
this.streamOptions = null;
|
||||
if (global.isAndroid) {
|
||||
this.controller?.stop();
|
||||
}
|
||||
@@ -554,6 +750,8 @@ export class FaceclawVoiceControlBridge {
|
||||
onPcm: (pcm: any) => {
|
||||
const bytes = toUint8Array(pcm);
|
||||
this.cloudClient?.acceptPcm(bytes);
|
||||
// Push-to-talk on a capture of its own is not recorded.
|
||||
if (this.captureHolders.has("continuous")) this.ensureRecording();
|
||||
if (!this.cloudClient && this.captureHolders.has("continuous") && this.speechPause.accept(bytes)) {
|
||||
for (const listener of this.speechPauseListeners) listener();
|
||||
}
|
||||
@@ -584,6 +782,10 @@ export class FaceclawVoiceControlBridge {
|
||||
);
|
||||
}
|
||||
},
|
||||
onReplay: (kind: string, positionMs: number, durationMs: number) => {
|
||||
const event = { kind: String(kind) as VoiceReplayEvent["kind"], positionMs: Number(positionMs), durationMs: Number(durationMs) };
|
||||
for (const listener of Array.from(this.replayListeners)) listener(event);
|
||||
},
|
||||
});
|
||||
this.controller.setListener(this.listenerProxy);
|
||||
}
|
||||
@@ -620,6 +822,21 @@ export class FaceclawVoiceControlBridge {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What a capture reads and transcribes with. A running capture is shared only
|
||||
* by holders asking for the same: the provider and language, the microphones
|
||||
* (the mixed stream, or the temples' own under a given configuration), or the
|
||||
* recording it replays.
|
||||
*/
|
||||
function streamKey(options: PushToTalkOptions): string {
|
||||
const source = options.replay
|
||||
? `replay:${options.replay.path}`
|
||||
: options.micArray && !options.usePhoneMic
|
||||
? `array:${Array.from(options.micArray.configure).join(",")}`
|
||||
: "mixed";
|
||||
return `${transcriptionKey(options)}|${source}`;
|
||||
}
|
||||
|
||||
function toJavaFloats(values: Float32Array): any {
|
||||
const javaFloats = Array.create("float", values.length);
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
@@ -629,3 +846,136 @@ function toJavaFloats(values: Float32Array): any {
|
||||
}
|
||||
|
||||
export const voiceControlBridge = new FaceclawVoiceControlBridge();
|
||||
|
||||
export type VoiceReplayState = "opening" | "playing" | "paused" | "ended" | "failed";
|
||||
|
||||
/**
|
||||
* A recording played back as a capture of its own: transcribed by the same
|
||||
* pipeline as the microphone, with the same settings, on a bridge and native
|
||||
* controller separate from the microphone's. Nothing here touches the mic,
|
||||
* so voice input and a live Transcribe window carry on beside a replay.
|
||||
*/
|
||||
export class VoiceReplay {
|
||||
private readonly bridge = new FaceclawVoiceControlBridge();
|
||||
private readonly listeners = new Set<(jumpedToMs?: number) => void>();
|
||||
private readonly offReplay: () => void;
|
||||
private position = 0;
|
||||
private duration = 0;
|
||||
private current: VoiceReplayState = "opening";
|
||||
private closed = false;
|
||||
|
||||
/**
|
||||
* `options` gives the capture options (provider, keys, processing) each
|
||||
* time the replay starts, so a restart picks up changed settings.
|
||||
*/
|
||||
constructor(readonly path: string, private readonly options: () => PushToTalkOptions) {
|
||||
this.offReplay = this.bridge.onReplay((event) => this.handle(event));
|
||||
}
|
||||
|
||||
/** The replay's status, transcript and level events. */
|
||||
get events(): VoiceEventSource {
|
||||
return this.bridge;
|
||||
}
|
||||
|
||||
/** Milliseconds into the recording. */
|
||||
get positionMs(): number {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
/** The recording's length in ms; 0 until it has been opened. */
|
||||
get durationMs(): number {
|
||||
return this.duration;
|
||||
}
|
||||
|
||||
get state(): VoiceReplayState {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
/**
|
||||
* The position, duration or state changed. `jumpedToMs` is given when
|
||||
* playback moved there other than by playing on: a jump, or a start
|
||||
* part-way in. It comes after the transcript of what played before.
|
||||
*/
|
||||
onChange(listener: (jumpedToMs?: number) => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
/** Play from `positionMs` into the recording. */
|
||||
start(positionMs = 0): void {
|
||||
if (this.closed) return;
|
||||
this.position = Math.max(0, positionMs);
|
||||
this.set("opening");
|
||||
this.bridge.startContinuousCapture({
|
||||
...this.options(),
|
||||
communicator: null,
|
||||
usePhoneMic: false,
|
||||
micArray: null,
|
||||
replay: { path: this.path, startMs: this.position },
|
||||
});
|
||||
}
|
||||
|
||||
/** Continue from `positionMs`: a jump while playing, a fresh start once the replay has ended. */
|
||||
jumpTo(positionMs: number): void {
|
||||
if (this.closed) return;
|
||||
const target = Math.max(0, this.duration > 0 ? Math.min(positionMs, this.duration) : positionMs);
|
||||
if ((this.current === "playing" || this.current === "paused") && this.bridge.isCaptureLive()) {
|
||||
this.bridge.seekReplay(target);
|
||||
this.position = target;
|
||||
this.notify();
|
||||
return;
|
||||
}
|
||||
this.start(target);
|
||||
}
|
||||
|
||||
setPaused(paused: boolean): void {
|
||||
if (this.closed || (this.current !== "playing" && this.current !== "paused")) return;
|
||||
this.bridge.setReplayPaused(paused);
|
||||
this.set(paused ? "paused" : "playing");
|
||||
}
|
||||
|
||||
/** Stop for good and release the capture. */
|
||||
close(): void {
|
||||
if (this.closed) return;
|
||||
this.closed = true;
|
||||
this.offReplay();
|
||||
this.listeners.clear();
|
||||
this.bridge.stop();
|
||||
}
|
||||
|
||||
private handle(event: VoiceReplayEvent): void {
|
||||
this.duration = event.durationMs;
|
||||
switch (event.kind) {
|
||||
case "start":
|
||||
this.position = event.positionMs;
|
||||
this.current = "playing";
|
||||
this.notify(event.positionMs > 0 ? event.positionMs : undefined);
|
||||
return;
|
||||
case "seek":
|
||||
this.position = event.positionMs;
|
||||
this.notify(event.positionMs);
|
||||
return;
|
||||
case "end":
|
||||
this.position = event.durationMs;
|
||||
// Its last audio is still to be transcribed (a cloud provider's final).
|
||||
this.bridge.finishContinuousCapture();
|
||||
this.set("ended");
|
||||
return;
|
||||
case "error":
|
||||
this.set("failed");
|
||||
return;
|
||||
default:
|
||||
this.position = event.positionMs;
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
private set(state: VoiceReplayState): void {
|
||||
this.current = state;
|
||||
this.notify();
|
||||
}
|
||||
|
||||
private notify(jumpedToMs?: number): void {
|
||||
for (const listener of Array.from(this.listeners)) listener(jumpedToMs);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/** The 44-byte header of a 16 kHz mono PCM16 WAV file holding `dataBytes` of samples. */
|
||||
export function wavHeader(dataBytes: number): Uint8Array {
|
||||
const header = new Uint8Array(44);
|
||||
const view = new DataView(header.buffer);
|
||||
const ascii = (offset: number, text: string) => {
|
||||
for (let i = 0; i < text.length; i++) header[offset + i] = text.charCodeAt(i);
|
||||
};
|
||||
ascii(0, "RIFF");
|
||||
view.setUint32(4, 36 + dataBytes, true);
|
||||
ascii(8, "WAVE");
|
||||
ascii(12, "fmt ");
|
||||
view.setUint32(16, 16, true);
|
||||
view.setUint16(20, 1, true); // PCM
|
||||
view.setUint16(22, 1, true); // mono
|
||||
view.setUint32(24, 16000, true);
|
||||
view.setUint32(28, 32000, true); // byte rate
|
||||
view.setUint16(32, 2, true); // block align
|
||||
view.setUint16(34, 16, true); // bits per sample
|
||||
ascii(36, "data");
|
||||
view.setUint32(40, dataBytes, true);
|
||||
return header;
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
import { knownFolders, path as paths } from '@nativescript/core'
|
||||
import { wavHeader } from './wav-header'
|
||||
|
||||
// About a second of 16 kHz mono PCM16: written in batches, not per 50 ms packet.
|
||||
const FLUSH_BYTES = 32000
|
||||
|
||||
/**
|
||||
* Appends 16 kHz mono PCM16 to a WAV file in Documents/Downloads while it is
|
||||
* captured. The header's sizes are rewritten on every flush, so a crash leaves a
|
||||
* playable file of everything up to the last second.
|
||||
*/
|
||||
export class WavRecorder {
|
||||
private pending: Uint8Array[] = []
|
||||
private pendingBytes = 0
|
||||
private dataBytes = 0
|
||||
|
||||
private constructor(private handle: NSFileHandle | null, readonly path: string) {}
|
||||
|
||||
/** A recorder for Downloads/<name>.wav (a -2, -3... suffix if taken), or null. */
|
||||
static open(name: string): WavRecorder | null {
|
||||
const fm = NSFileManager.defaultManager
|
||||
const dir = paths.join(knownFolders.documents().path, 'Downloads')
|
||||
try {
|
||||
fm.createDirectoryAtPathWithIntermediateDirectoriesAttributesError(dir, true, null)
|
||||
let path = paths.join(dir, `${name}.wav`)
|
||||
for (let n = 2; fm.fileExistsAtPath(path); n++) path = paths.join(dir, `${name}-${n}.wav`)
|
||||
if (!fm.createFileAtPathContentsAttributes(path, toData(wavHeader(0)), null)) return null
|
||||
const handle = NSFileHandle.fileHandleForWritingAtPath(path)
|
||||
if (!handle || !handle.seekToEndReturningOffsetError(null)) return null
|
||||
return new WavRecorder(handle, path)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
append(pcm: Uint8Array): void {
|
||||
if (!this.handle || !pcm.length) return
|
||||
this.pending.push(pcm.slice())
|
||||
this.pendingBytes += pcm.length
|
||||
if (this.pendingBytes >= FLUSH_BYTES) this.flush()
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.flush()
|
||||
try { this.handle?.closeAndReturnError() } catch { /* already closed */ }
|
||||
this.handle = null
|
||||
}
|
||||
|
||||
private flush(): void {
|
||||
const handle = this.handle
|
||||
if (!handle || !this.pendingBytes) return
|
||||
const chunk = new Uint8Array(this.pendingBytes)
|
||||
let offset = 0
|
||||
for (const part of this.pending) { chunk.set(part, offset); offset += part.length }
|
||||
this.pending = []
|
||||
this.pendingBytes = 0
|
||||
try {
|
||||
// NativeScript throws a failed call's NSError (storage full, file removed).
|
||||
handle.writeDataError(toData(chunk))
|
||||
handle.seekToOffsetError(0)
|
||||
handle.writeDataError(toData(wavHeader(this.dataBytes + chunk.length)))
|
||||
handle.seekToEndReturningOffsetError(null)
|
||||
this.dataBytes += chunk.length
|
||||
return
|
||||
} catch {
|
||||
// Stop recording rather than retry.
|
||||
}
|
||||
try { handle.closeAndReturnError() } catch { /* ignore */ }
|
||||
this.handle = null
|
||||
}
|
||||
}
|
||||
|
||||
function toData(bytes: Uint8Array): NSData {
|
||||
return NSData.dataWithBytesLength(interop.handleof(bytes.buffer), bytes.byteLength)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* The watch transport's wire names and shapes, shared by the Wear OS bridge
|
||||
* (wear-bridge.ts) and the Apple Watch one (wear-bridge.ios.ts). See
|
||||
* wear/PROTOCOL.md for the messages themselves.
|
||||
*/
|
||||
|
||||
/** Message paths shared with the watch apps (wear/app/.../Protocol.kt, apple-watch/Faceclaw/Protocol.swift). */
|
||||
export const WEAR_PATHS = {
|
||||
/** Watch -> phone: a ring-style gesture. */
|
||||
input: "/faceclaw/input",
|
||||
/** Watch -> phone: a finger landed on the pad (the ring-press ahead of a gesture); never acked. */
|
||||
press: "/faceclaw/press",
|
||||
/** Watch -> phone: a shell command (launch app, wake, lock, ...). */
|
||||
command: "/faceclaw/command",
|
||||
/** Watch -> phone: a text query for the assistant. */
|
||||
assistant: "/faceclaw/assistant",
|
||||
/** Watch -> phone: text to type into the foreground app. */
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
event: "/faceclaw/event",
|
||||
/** Phone -> watch Data Layer item: the mirrored dashboard state. */
|
||||
state: "/faceclaw/state",
|
||||
} as const;
|
||||
|
||||
export type WearMessage = {
|
||||
path: string;
|
||||
/** Parsed JSON payload ({} when the watch sent none or it was malformed). */
|
||||
payload: Record<string, unknown>;
|
||||
nodeId: string;
|
||||
};
|
||||
|
||||
export type WearWatchConnection = {
|
||||
reachable: boolean;
|
||||
watchName: string;
|
||||
};
|
||||
|
||||
export function parseWearPayload(json: string): Record<string, unknown> {
|
||||
try {
|
||||
const parsed = JSON.parse(json);
|
||||
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* iOS twin of wear-bridge.ts: the phone side of the Apple Watch remote, over
|
||||
* WatchConnectivity (App_Resources/iOS/src/FaceclawWatchBridge). It has the
|
||||
* same surface and carries the same (path, JSON) messages, so
|
||||
* app/g2/wear-remote.ts gives them their meaning on both platforms.
|
||||
*
|
||||
* "Reachable" here means an Apple Watch with the Faceclaw app is paired, not
|
||||
* that the app is running: iOS can't wake a closed watch app, and it is
|
||||
* suspended whenever the wrist drops, so WCSession's live reachability flips
|
||||
* constantly. The native side sends live while it can and otherwise leaves
|
||||
* the latest state in the application context for the app's next wake.
|
||||
*/
|
||||
import { WEAR_PATHS, parseWearPayload, type WearMessage, type WearWatchConnection } from './wear-bridge-types'
|
||||
|
||||
export { WEAR_PATHS, type WearMessage, type WearWatchConnection }
|
||||
|
||||
declare const FaceclawWatchBridge: any
|
||||
|
||||
/** WatchConnectivity doesn't expose the watch's own name. */
|
||||
const WATCH_NAME = 'Apple Watch'
|
||||
/** Replies go to the session's one counterpart; this only fills WearMessage.nodeId. */
|
||||
const WATCH_NODE_ID = 'apple-watch'
|
||||
|
||||
type MessageListener = (message: WearMessage) => void
|
||||
type ConnectionListener = (connection: WearWatchConnection) => void
|
||||
|
||||
class WearBridge {
|
||||
private nativeBridge: any = undefined
|
||||
private listening = false
|
||||
private readonly messageListeners = new Set<MessageListener>()
|
||||
private readonly connectionListeners = new Set<ConnectionListener>()
|
||||
|
||||
private native(): any {
|
||||
if (this.nativeBridge !== undefined) return this.nativeBridge
|
||||
try {
|
||||
const bridge = FaceclawWatchBridge.shared()
|
||||
this.nativeBridge = bridge.isAvailable() ? bridge : null
|
||||
} catch (error) {
|
||||
console.warn('watch bridge unavailable', error)
|
||||
this.nativeBridge = null
|
||||
}
|
||||
return this.nativeBridge
|
||||
}
|
||||
|
||||
/** Whether WatchConnectivity works on this phone (it does on every iPhone). */
|
||||
isAvailable(): boolean {
|
||||
return this.native() !== null
|
||||
}
|
||||
|
||||
private ensureListener(): void {
|
||||
if (this.listening) return
|
||||
const native = this.native()
|
||||
if (!native) return
|
||||
this.listening = true
|
||||
native.setMessageHandlerConnectionHandler((path: string, json: string) => {
|
||||
const message: WearMessage = { path: String(path), payload: parseWearPayload(String(json)), nodeId: WATCH_NODE_ID }
|
||||
for (const listener of Array.from(this.messageListeners)) {
|
||||
try { listener(message) }
|
||||
catch (error) { console.warn('watch message listener failed', error) }
|
||||
}
|
||||
}, () => {
|
||||
const connection = this.getWatchConnection()
|
||||
for (const listener of Array.from(this.connectionListeners)) {
|
||||
try { listener(connection) }
|
||||
catch (error) { console.warn('watch connection listener failed', error) }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Subscribe to watch messages. Registers the native listener on first use. */
|
||||
onMessage(listener: MessageListener): () => void {
|
||||
this.messageListeners.add(listener)
|
||||
this.ensureListener()
|
||||
return () => { this.messageListeners.delete(listener) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to the watch's availability; the current value is delivered
|
||||
* immediately. Also fires when the watch app comes to the front with the
|
||||
* value unchanged, which is what prompts WearRemote to push it fresh state.
|
||||
*/
|
||||
onWatchConnection(listener: ConnectionListener): () => void {
|
||||
this.connectionListeners.add(listener)
|
||||
this.ensureListener()
|
||||
listener(this.getWatchConnection())
|
||||
return () => { this.connectionListeners.delete(listener) }
|
||||
}
|
||||
|
||||
getWatchConnection(): WearWatchConnection {
|
||||
const reachable = Boolean(this.native()?.isWatchAppAvailable())
|
||||
return { reachable, watchName: reachable ? WATCH_NAME : '' }
|
||||
}
|
||||
|
||||
/** WatchConnectivity pushes changes; there is nothing to re-query. */
|
||||
refreshWatchNodes(): void {}
|
||||
|
||||
/** Mirror the dashboard state; unchanged states are dropped natively unless `force`. */
|
||||
publishState(state: Record<string, unknown>, force = false): void {
|
||||
this.native()?.publishStateForce(JSON.stringify(state), force)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an event to the watch. A suspended app misses live messages, so all
|
||||
* but streaming snapshots also stay behind for its next wake: that is how a
|
||||
* reply that finished with the wrist down still reaches the Assistant screen.
|
||||
*/
|
||||
sendEvent(payload: Record<string, unknown>): void {
|
||||
const sticky = !(payload.type === 'assistant' && payload.phase === 'streaming')
|
||||
this.native()?.sendEventSticky(JSON.stringify(payload), sticky)
|
||||
}
|
||||
|
||||
/** Send a live message on any phone -> watch path; dropped if the app isn't in front. */
|
||||
sendToWatch(path: string, payload: Record<string, unknown>): void {
|
||||
this.native()?.sendToWatchJson(path, JSON.stringify(payload))
|
||||
}
|
||||
|
||||
/** Acknowledge a watch message; `seq` echoes the watch's sequence number. */
|
||||
sendAck(_nodeId: string, seq: number, ok: boolean, message = ''): void {
|
||||
this.native()?.sendAckOkJsReadyMessage(seq, ok, true, message)
|
||||
}
|
||||
}
|
||||
|
||||
export const wearBridge = new WearBridge()
|
||||
@@ -9,47 +9,12 @@
|
||||
* Data Layer is part of it), so the rest of the app never has to care.
|
||||
*/
|
||||
import { Utils } from "@nativescript/core";
|
||||
import { WEAR_PATHS, parseWearPayload, type WearMessage, type WearWatchConnection } from "./wear-bridge-types";
|
||||
|
||||
export { WEAR_PATHS, type WearMessage, type WearWatchConnection };
|
||||
|
||||
declare const com: any;
|
||||
|
||||
/** Message paths shared with the watch app (wear/app/.../Protocol.kt). */
|
||||
export const WEAR_PATHS = {
|
||||
/** Watch -> phone: a ring-style gesture. */
|
||||
input: "/faceclaw/input",
|
||||
/** Watch -> phone: a finger landed on the pad (the ring-press ahead of a gesture); never acked. */
|
||||
press: "/faceclaw/press",
|
||||
/** Watch -> phone: a shell command (launch app, wake, lock, ...). */
|
||||
command: "/faceclaw/command",
|
||||
/** Watch -> phone: a text query for the assistant. */
|
||||
assistant: "/faceclaw/assistant",
|
||||
/** Watch -> phone: text to type into the foreground app. */
|
||||
text: "/faceclaw/text",
|
||||
/** Watch -> phone: please re-publish the state item. */
|
||||
stateRequest: "/faceclaw/state/request",
|
||||
/** Watch -> phone: the watch's own battery ({battery, charging}); never acked. */
|
||||
battery: "/faceclaw/battery",
|
||||
/** Phone -> watch: please report the watch battery (answered even while the watch app is closed). */
|
||||
batteryRequest: "/faceclaw/battery/request",
|
||||
/** Phone -> watch: reply to any of the above. */
|
||||
ack: "/faceclaw/ack",
|
||||
/** Phone -> watch: assistant activity, alerts, voice-dialog transcripts. */
|
||||
event: "/faceclaw/event",
|
||||
/** Phone -> watch Data Layer item: the mirrored dashboard state. */
|
||||
state: "/faceclaw/state",
|
||||
} as const;
|
||||
|
||||
export type WearMessage = {
|
||||
path: string;
|
||||
/** Parsed JSON payload ({} when the watch sent none or it was malformed). */
|
||||
payload: Record<string, unknown>;
|
||||
nodeId: string;
|
||||
};
|
||||
|
||||
export type WearWatchConnection = {
|
||||
reachable: boolean;
|
||||
watchName: string;
|
||||
};
|
||||
|
||||
type MessageListener = (message: WearMessage) => void;
|
||||
type ConnectionListener = (connection: WearWatchConnection) => void;
|
||||
|
||||
@@ -91,7 +56,7 @@ class WearBridge {
|
||||
onMessage: (path: string, json: string, nodeId: string) => {
|
||||
const message: WearMessage = {
|
||||
path: String(path),
|
||||
payload: parsePayload(String(json)),
|
||||
payload: parseWearPayload(String(json)),
|
||||
nodeId: String(nodeId),
|
||||
};
|
||||
for (const listener of Array.from(this.messageListeners)) {
|
||||
@@ -177,13 +142,4 @@ class WearBridge {
|
||||
}
|
||||
}
|
||||
|
||||
function parsePayload(json: string): Record<string, unknown> {
|
||||
try {
|
||||
const parsed = JSON.parse(json);
|
||||
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
export const wearBridge = new WearBridge();
|
||||
Reference in new issue
Block a user