Files
faceclaw/app/g2/device-addresses.ts
T
James BabcockandClaude Fable 5 17182532f4 Address review findings for the proximity-pairing PR
Correctness:
- Pair-glasses flows no longer strand the app in the manual-disconnected
  state: the pre-scan disconnect is followed by resumeAutoReconnect(), and
  the config page's scan hand-off drops the live link too.
- The mixed-arms serial warning is reachable now: pairs() groups by the
  canonical serial key, and a lone left + lone right with different serials
  get the mismatch warning on both rows (it previously required two
  different serials inside one serial-keyed group, which cannot happen).
- A selected pair that degrades to one arm (prune while the page is open)
  is deselected, and Save never pads a missing arm from stored addresses.
- DiscoveryAggregator.ingest no longer lets a bonded re-emit or split
  advertisement overwrite the note/name/flag learned from a full
  advertisement, and reclassifies split reports using remembered fields so
  a nameless half doesn't drop the arm.
- estimateProximity ignores the advertised TX Power Level (radiated power,
  not measured-power-at-1m); the calibration table is the only reference.
- Saving a ring without a glasses pair updates the stored identity's ring
  name/address instead of leaving the previous ring on record.
- Ring detection requires the stock "EVEN R1" name prefix in both the TS
  classifier and the Java pre-filter; a bare "R1" substring matched
  unrelated devices, and ring rows are always selectable.
- One-shot scans keep every sample (buildAddressSet merges them through
  DiscoveryAggregator), and flash-flow address resolution takes left/right
  from one serial-joined complete pair instead of latest-per-role.

Lifecycle:
- The pairing scan and its refresh timer pause on app suspend and resume
  with the app, and the model's app-event hooks are detached when the page
  is left for good.

Cleanup:
- One MAC normalizer (even-advertisement.normalizeMacAddress, re-exported
  by device-addresses), used for the ring "Yours" comparison too.
- One serial-equality definition (serialKey/serialsMatch); the containment
  fallback in isPreviouslyPaired is gone.
- Shared formatErrorMessage replaces five per-view-model copies; shared
  hex-util replaces the duplicate TS hex codecs; one Java bytesToHex in
  FaceclawFirmwareUtil replaces three hand-rolled encoders.
- glassesRow/ringRow share a presentation-base helper; dead row address
  fields, the constant glassesSectionVisibility, the identical-branch
  status ternary, and zoneRank/ZONE_ORDER are removed.
- .gitignore now actually covers .test-build/ as tests/README claims.

UI:
- Dark-mode styles for the pairing page: readable instructions, dark-gray
  cards with consistent light text; light mode keeps its current look.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ZqzwmRQ3HQxasBz5rYyZV
2026-08-23 15:28:59 -07:00

124 lines
4.9 KiB
TypeScript

import { ApplicationSettings } from "@nativescript/core";
import { normalizeMacAddress } from "./even-advertisement";
export { normalizeMacAddress };
export type DeviceAddresses = {
right: string;
left: string;
ring: string;
};
const ADDRESS_KEYS = {
right: "deviceAddress.right",
left: "deviceAddress.left",
ring: "deviceAddress.ring",
} as const;
const DEFAULT_DEVICE_ADDRESSES: DeviceAddresses = {
right: "",
left: "",
ring: "",
};
export function loadDeviceAddresses(): DeviceAddresses {
return {
right: normalizeMacAddress(ApplicationSettings.getString(ADDRESS_KEYS.right, DEFAULT_DEVICE_ADDRESSES.right)),
left: normalizeMacAddress(ApplicationSettings.getString(ADDRESS_KEYS.left, DEFAULT_DEVICE_ADDRESSES.left)),
ring: normalizeMacAddress(ApplicationSettings.getString(ADDRESS_KEYS.ring, DEFAULT_DEVICE_ADDRESSES.ring)),
};
}
export function saveDeviceAddresses(addresses: DeviceAddresses): void {
ApplicationSettings.setString(ADDRESS_KEYS.right, normalizeMacAddress(addresses.right));
ApplicationSettings.setString(ADDRESS_KEYS.left, normalizeMacAddress(addresses.left));
ApplicationSettings.setString(ADDRESS_KEYS.ring, normalizeMacAddress(addresses.ring));
}
export function isValidMacAddress(value: string, allowEmpty = false): boolean {
const trimmed = value.trim();
if (!trimmed) {
return allowEmpty;
}
return /^[0-9A-F]{2}(?::[0-9A-F]{2}){5}$/.test(normalizeMacAddress(trimmed));
}
// ---------------------------------------------------------------------------
// Paired-glasses identity
// ---------------------------------------------------------------------------
/**
* What the pairing scan learned about the pair whose addresses were saved, so
* later screens can name and picture the exact glasses without a connection.
* Kept separate from the addresses so hand-entered MACs keep working.
*/
export type PairedGlassesIdentity = {
/** The 14-character serial both arms advertised (pair identity). */
serial: string;
leftName: string;
rightName: string;
leftAddress: string;
rightAddress: string;
/** Ring local name ("EVEN R1_B56EE2"), when a ring was chosen from the scan. */
ringName: string;
ringAddress: string;
pairedAtMs: number;
};
const IDENTITY_KEYS = {
serial: "deviceIdentity.serial",
leftName: "deviceIdentity.leftName",
rightName: "deviceIdentity.rightName",
leftAddress: "deviceIdentity.leftAddress",
rightAddress: "deviceIdentity.rightAddress",
ringName: "deviceIdentity.ringName",
ringAddress: "deviceIdentity.ringAddress",
pairedAtMs: "deviceIdentity.pairedAtMs",
} as const;
export function loadPairedGlassesIdentity(): PairedGlassesIdentity | null {
const serial = ApplicationSettings.getString(IDENTITY_KEYS.serial, "").trim();
if (!serial) return null;
return {
serial,
leftName: ApplicationSettings.getString(IDENTITY_KEYS.leftName, ""),
rightName: ApplicationSettings.getString(IDENTITY_KEYS.rightName, ""),
leftAddress: normalizeMacAddress(ApplicationSettings.getString(IDENTITY_KEYS.leftAddress, "")),
rightAddress: normalizeMacAddress(ApplicationSettings.getString(IDENTITY_KEYS.rightAddress, "")),
ringName: ApplicationSettings.getString(IDENTITY_KEYS.ringName, ""),
ringAddress: normalizeMacAddress(ApplicationSettings.getString(IDENTITY_KEYS.ringAddress, "")),
pairedAtMs: ApplicationSettings.getNumber(IDENTITY_KEYS.pairedAtMs, 0),
};
}
export function savePairedGlassesIdentity(identity: PairedGlassesIdentity): void {
ApplicationSettings.setString(IDENTITY_KEYS.serial, identity.serial.trim().toUpperCase());
ApplicationSettings.setString(IDENTITY_KEYS.leftName, identity.leftName);
ApplicationSettings.setString(IDENTITY_KEYS.rightName, identity.rightName);
ApplicationSettings.setString(IDENTITY_KEYS.leftAddress, normalizeMacAddress(identity.leftAddress));
ApplicationSettings.setString(IDENTITY_KEYS.rightAddress, normalizeMacAddress(identity.rightAddress));
ApplicationSettings.setString(IDENTITY_KEYS.ringName, identity.ringName);
ApplicationSettings.setString(IDENTITY_KEYS.ringAddress, normalizeMacAddress(identity.ringAddress));
ApplicationSettings.setNumber(IDENTITY_KEYS.pairedAtMs, identity.pairedAtMs);
}
export function clearPairedGlassesIdentity(): void {
for (const key of Object.values(IDENTITY_KEYS)) {
ApplicationSettings.remove(key);
}
}
/**
* The saved identity only describes the saved addresses. If the user later
* hand-edits an arm's MAC to something else, the serial no longer applies —
* report null rather than a stale name for a different pair.
*/
export function loadPairedGlassesIdentityForAddresses(addresses: DeviceAddresses): PairedGlassesIdentity | null {
const identity = loadPairedGlassesIdentity();
if (!identity) return null;
if (identity.leftAddress !== normalizeMacAddress(addresses.left)) return null;
if (identity.rightAddress !== normalizeMacAddress(addresses.right)) return null;
return identity;
}