@camstack/addon-provider-unifi 0.2.59 → 0.2.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addon.js +428 -25
- package/dist/addon.mjs +428 -25
- package/package.json +1 -1
package/dist/addon.js
CHANGED
|
@@ -12756,7 +12756,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12756
12756
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12757
12757
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12758
12758
|
*/
|
|
12759
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12759
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12760
|
+
/**
|
|
12761
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12762
|
+
*
|
|
12763
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12764
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12765
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12766
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12767
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12768
|
+
* three child cameras offline for four hours.
|
|
12769
|
+
*
|
|
12770
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12771
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12772
|
+
* that cannot tell simply never sets it.
|
|
12773
|
+
*/
|
|
12774
|
+
alreadyOnboarded: boolean().optional(),
|
|
12775
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12776
|
+
onboardedDeviceId: number().optional(),
|
|
12777
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12778
|
+
onboardedName: string().optional()
|
|
12760
12779
|
});
|
|
12761
12780
|
/**
|
|
12762
12781
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12812,6 +12831,35 @@ var deviceProviderCapability = {
|
|
|
12812
12831
|
name: string(),
|
|
12813
12832
|
type: string()
|
|
12814
12833
|
}))),
|
|
12834
|
+
/**
|
|
12835
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12836
|
+
* touching no other device this provider owns.
|
|
12837
|
+
*
|
|
12838
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12839
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12840
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12841
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12842
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12843
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12844
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12845
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12846
|
+
*
|
|
12847
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12848
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12849
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12850
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12851
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12852
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12853
|
+
* against the wrong camera (D8).
|
|
12854
|
+
*
|
|
12855
|
+
* Construction can dial hardware, and the migrated source is
|
|
12856
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12857
|
+
* rather than the 60 s default.
|
|
12858
|
+
*/
|
|
12859
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12860
|
+
kind: "mutation",
|
|
12861
|
+
timeoutMs: 3 * 6e4
|
|
12862
|
+
}),
|
|
12815
12863
|
supportsDiscovery: method(object({}), boolean()),
|
|
12816
12864
|
/**
|
|
12817
12865
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13139,7 +13187,8 @@ method(object({
|
|
|
13139
13187
|
targetId: number()
|
|
13140
13188
|
}), MigrateDeviceResultSchema, {
|
|
13141
13189
|
kind: "mutation",
|
|
13142
|
-
auth: "admin"
|
|
13190
|
+
auth: "admin",
|
|
13191
|
+
timeoutMs: 12 * 6e4
|
|
13143
13192
|
}), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
|
|
13144
13193
|
deviceId: number(),
|
|
13145
13194
|
name: string()
|
|
@@ -32512,6 +32561,147 @@ var BaseDevice = class {
|
|
|
32512
32561
|
}
|
|
32513
32562
|
};
|
|
32514
32563
|
/**
|
|
32564
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
32565
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
32566
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
32567
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
32568
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
32569
|
+
*/
|
|
32570
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
32571
|
+
1e4,
|
|
32572
|
+
3e4,
|
|
32573
|
+
9e4
|
|
32574
|
+
];
|
|
32575
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
32576
|
+
function sleep$1(ms, signal) {
|
|
32577
|
+
return new Promise((resolve) => {
|
|
32578
|
+
if (signal.aborted) {
|
|
32579
|
+
resolve();
|
|
32580
|
+
return;
|
|
32581
|
+
}
|
|
32582
|
+
const onAbort = () => {
|
|
32583
|
+
clearTimeout(timer);
|
|
32584
|
+
resolve();
|
|
32585
|
+
};
|
|
32586
|
+
const timer = setTimeout(() => {
|
|
32587
|
+
signal.removeEventListener("abort", onAbort);
|
|
32588
|
+
resolve();
|
|
32589
|
+
}, ms);
|
|
32590
|
+
timer.unref?.();
|
|
32591
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
32592
|
+
});
|
|
32593
|
+
}
|
|
32594
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
32595
|
+
* not reject (callers wrap their own try/catch). */
|
|
32596
|
+
async function runWithConcurrency(items, width, fn) {
|
|
32597
|
+
const queue = [...items];
|
|
32598
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
32599
|
+
const lane = async () => {
|
|
32600
|
+
for (;;) {
|
|
32601
|
+
const item = queue.shift();
|
|
32602
|
+
if (item === void 0) return;
|
|
32603
|
+
await fn(item);
|
|
32604
|
+
}
|
|
32605
|
+
};
|
|
32606
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
32607
|
+
}
|
|
32608
|
+
var DeviceRestoreRetryScheduler = class {
|
|
32609
|
+
#logger;
|
|
32610
|
+
#attempt;
|
|
32611
|
+
#onPermanentFailure;
|
|
32612
|
+
#delaysMs;
|
|
32613
|
+
#concurrency;
|
|
32614
|
+
#now;
|
|
32615
|
+
#abort = new AbortController();
|
|
32616
|
+
constructor(options) {
|
|
32617
|
+
this.#logger = options.logger;
|
|
32618
|
+
this.#attempt = options.attempt;
|
|
32619
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
32620
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32621
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
32622
|
+
this.#now = options.now ?? Date.now;
|
|
32623
|
+
}
|
|
32624
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
32625
|
+
* permanently failed — the next boot restores them from disk. */
|
|
32626
|
+
cancel() {
|
|
32627
|
+
this.#abort.abort();
|
|
32628
|
+
}
|
|
32629
|
+
/**
|
|
32630
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
32631
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
32632
|
+
* cancelled. Never rejects.
|
|
32633
|
+
*/
|
|
32634
|
+
async run(initialFailures) {
|
|
32635
|
+
let pending = initialFailures.map((failure) => ({
|
|
32636
|
+
saved: failure.saved,
|
|
32637
|
+
lastError: failure.error,
|
|
32638
|
+
attempts: 1
|
|
32639
|
+
}));
|
|
32640
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
32641
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
32642
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
32643
|
+
if (this.#abort.signal.aborted) break;
|
|
32644
|
+
pending = await this.#runRound(pending, round);
|
|
32645
|
+
}
|
|
32646
|
+
if (this.#abort.signal.aborted) return [];
|
|
32647
|
+
const terminal = pending.map((entry) => ({
|
|
32648
|
+
deviceId: entry.saved.id,
|
|
32649
|
+
stableId: entry.saved.stableId,
|
|
32650
|
+
type: String(entry.saved.type),
|
|
32651
|
+
attempts: entry.attempts,
|
|
32652
|
+
lastError: entry.lastError,
|
|
32653
|
+
failedAt: this.#now()
|
|
32654
|
+
}));
|
|
32655
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
32656
|
+
return terminal;
|
|
32657
|
+
}
|
|
32658
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
32659
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
32660
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
32661
|
+
async #runRound(pending, round) {
|
|
32662
|
+
const next = [];
|
|
32663
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
32664
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
32665
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
32666
|
+
if (this.#abort.signal.aborted) {
|
|
32667
|
+
next.push(entry);
|
|
32668
|
+
return;
|
|
32669
|
+
}
|
|
32670
|
+
const attemptNo = entry.attempts + 1;
|
|
32671
|
+
try {
|
|
32672
|
+
await this.#attempt(entry.saved);
|
|
32673
|
+
this.#logger.info("Device restored on retry", {
|
|
32674
|
+
tags: {
|
|
32675
|
+
deviceId: entry.saved.id,
|
|
32676
|
+
stableId: entry.saved.stableId
|
|
32677
|
+
},
|
|
32678
|
+
meta: { attempt: attemptNo }
|
|
32679
|
+
});
|
|
32680
|
+
} catch (err) {
|
|
32681
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
32682
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
32683
|
+
this.#logger.warn("Device restore retry failed", {
|
|
32684
|
+
tags: {
|
|
32685
|
+
deviceId: entry.saved.id,
|
|
32686
|
+
stableId: entry.saved.stableId
|
|
32687
|
+
},
|
|
32688
|
+
meta: {
|
|
32689
|
+
attempt: attemptNo,
|
|
32690
|
+
remainingRetries,
|
|
32691
|
+
error: lastError
|
|
32692
|
+
}
|
|
32693
|
+
});
|
|
32694
|
+
next.push({
|
|
32695
|
+
saved: entry.saved,
|
|
32696
|
+
lastError,
|
|
32697
|
+
attempts: attemptNo
|
|
32698
|
+
});
|
|
32699
|
+
}
|
|
32700
|
+
});
|
|
32701
|
+
return next;
|
|
32702
|
+
}
|
|
32703
|
+
};
|
|
32704
|
+
/**
|
|
32515
32705
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
32516
32706
|
* device-provider cap router. Shared across all providers.
|
|
32517
32707
|
*/
|
|
@@ -32560,6 +32750,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32560
32750
|
}];
|
|
32561
32751
|
}
|
|
32562
32752
|
async onShutdown() {
|
|
32753
|
+
this.cancelRestoreRetries();
|
|
32563
32754
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32564
32755
|
for (const device of devices) try {
|
|
32565
32756
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -32577,9 +32768,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32577
32768
|
async start() {}
|
|
32578
32769
|
async stop() {}
|
|
32579
32770
|
async getStatus() {
|
|
32771
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32772
|
+
const summary = this.restoreFailureSummary();
|
|
32773
|
+
if (summary === null) return {
|
|
32774
|
+
connected: true,
|
|
32775
|
+
deviceCount: all.length
|
|
32776
|
+
};
|
|
32580
32777
|
return {
|
|
32581
32778
|
connected: true,
|
|
32582
|
-
deviceCount:
|
|
32779
|
+
deviceCount: all.length,
|
|
32780
|
+
error: summary
|
|
32583
32781
|
};
|
|
32584
32782
|
}
|
|
32585
32783
|
async getDevices() {
|
|
@@ -32669,8 +32867,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32669
32867
|
};
|
|
32670
32868
|
}
|
|
32671
32869
|
async restoreDevices(savedDevices) {
|
|
32672
|
-
await this.onRestoreDevices(savedDevices);
|
|
32673
|
-
if (savedDevices.length
|
|
32870
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
32871
|
+
if (savedDevices.length === 0) return;
|
|
32872
|
+
if (report && report.failedCount > 0) {
|
|
32873
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
32874
|
+
return;
|
|
32875
|
+
}
|
|
32876
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
32877
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
32878
|
+
}
|
|
32879
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
32880
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32881
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
32882
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
32883
|
+
restoreRetryConcurrency = 4;
|
|
32884
|
+
_restoreRetryScheduler = null;
|
|
32885
|
+
_restoreRetryCompletion = null;
|
|
32886
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
32887
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
32888
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
32889
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
32890
|
+
* with the devices that restored, and a late success is announced
|
|
32891
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
32892
|
+
get restoreRetryCompletion() {
|
|
32893
|
+
return this._restoreRetryCompletion;
|
|
32894
|
+
}
|
|
32895
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
32896
|
+
get permanentRestoreFailures() {
|
|
32897
|
+
return [...this._permanentRestoreFailures.values()];
|
|
32898
|
+
}
|
|
32899
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
32900
|
+
* `null` when every device restored. */
|
|
32901
|
+
restoreFailureSummary() {
|
|
32902
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
32903
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
32904
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
32905
|
+
}
|
|
32906
|
+
cancelRestoreRetries() {
|
|
32907
|
+
this._restoreRetryScheduler?.cancel();
|
|
32908
|
+
this._restoreRetryScheduler = null;
|
|
32909
|
+
}
|
|
32910
|
+
recordPermanentRestoreFailure(failure) {
|
|
32911
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
32912
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
32913
|
+
tags: {
|
|
32914
|
+
deviceId: failure.deviceId,
|
|
32915
|
+
stableId: failure.stableId
|
|
32916
|
+
},
|
|
32917
|
+
meta: {
|
|
32918
|
+
type: failure.type,
|
|
32919
|
+
attempts: failure.attempts,
|
|
32920
|
+
error: failure.lastError
|
|
32921
|
+
}
|
|
32922
|
+
});
|
|
32923
|
+
}
|
|
32924
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
32925
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
32926
|
+
logger: this.ctx.logger,
|
|
32927
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
32928
|
+
concurrency: this.restoreRetryConcurrency,
|
|
32929
|
+
attempt,
|
|
32930
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
32931
|
+
});
|
|
32932
|
+
this._restoreRetryScheduler = scheduler;
|
|
32933
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
32934
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
32935
|
+
});
|
|
32936
|
+
}
|
|
32937
|
+
/**
|
|
32938
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
32939
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
32940
|
+
* and no other device this provider owns is disturbed.
|
|
32941
|
+
*
|
|
32942
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
32943
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
32944
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
32945
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
32946
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
32947
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
32948
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
32949
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
32950
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
32951
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
32952
|
+
*
|
|
32953
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
32954
|
+
* reload its parent instead.
|
|
32955
|
+
*/
|
|
32956
|
+
async reloadDevice(input) {
|
|
32957
|
+
const { stableId } = input;
|
|
32958
|
+
const devices = this.ctx.kernel.devices;
|
|
32959
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
32960
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
32961
|
+
if (live) await devices.decommission(live.id);
|
|
32962
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
32963
|
+
addonId: this.addonId,
|
|
32964
|
+
stableId
|
|
32965
|
+
});
|
|
32966
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
32967
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
32968
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
32969
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
32970
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
32971
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
32972
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
32973
|
+
for (const row of rows) {
|
|
32974
|
+
if (row.parentDeviceId !== id) continue;
|
|
32975
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
32976
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
32977
|
+
if (!ChildClass) continue;
|
|
32978
|
+
try {
|
|
32979
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
32980
|
+
} catch (err) {
|
|
32981
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
32982
|
+
tags: {
|
|
32983
|
+
deviceId: row.id,
|
|
32984
|
+
stableId: row.stableId
|
|
32985
|
+
},
|
|
32986
|
+
meta: {
|
|
32987
|
+
parentDeviceId: id,
|
|
32988
|
+
error: err instanceof Error ? err.message : String(err)
|
|
32989
|
+
}
|
|
32990
|
+
});
|
|
32991
|
+
}
|
|
32992
|
+
}
|
|
32993
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
32994
|
+
tags: { deviceId: id },
|
|
32995
|
+
meta: {
|
|
32996
|
+
stableId,
|
|
32997
|
+
type: meta.type
|
|
32998
|
+
}
|
|
32999
|
+
});
|
|
33000
|
+
return { deviceId: id };
|
|
32674
33001
|
}
|
|
32675
33002
|
/**
|
|
32676
33003
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -32696,55 +33023,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32696
33023
|
* accessory-spawn flow handles via the parent's
|
|
32697
33024
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
32698
33025
|
* fit.
|
|
33026
|
+
*
|
|
33027
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33028
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33029
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33030
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33031
|
+
* `getStatus().error`.
|
|
32699
33032
|
*/
|
|
33033
|
+
/**
|
|
33034
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33035
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33036
|
+
*
|
|
33037
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33038
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33039
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33040
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33041
|
+
* been emptied failed all four bounded attempts against fields
|
|
33042
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33043
|
+
*
|
|
33044
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33045
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33046
|
+
* the bound, then reported — never swallowed.
|
|
33047
|
+
*/
|
|
33048
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
32700
33049
|
async onRestoreDevices(savedDevices) {
|
|
32701
33050
|
const restored = /* @__PURE__ */ new Set();
|
|
33051
|
+
const failures = [];
|
|
33052
|
+
const attemptRestore = async (saved) => {
|
|
33053
|
+
if (restored.has(saved.id)) return;
|
|
33054
|
+
const Class = this.deviceClasses[saved.type];
|
|
33055
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33056
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33057
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33058
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33059
|
+
restored.add(saved.id);
|
|
33060
|
+
};
|
|
32702
33061
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
32703
33062
|
const restoreOne = async (saved) => {
|
|
32704
|
-
|
|
32705
|
-
if (!Class) {
|
|
33063
|
+
if (!this.deviceClasses[saved.type]) {
|
|
32706
33064
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
32707
|
-
tags: {
|
|
33065
|
+
tags: {
|
|
33066
|
+
deviceId: saved.id,
|
|
33067
|
+
stableId: saved.stableId
|
|
33068
|
+
},
|
|
32708
33069
|
meta: { type: saved.type }
|
|
32709
33070
|
});
|
|
32710
33071
|
return;
|
|
32711
33072
|
}
|
|
32712
33073
|
try {
|
|
32713
|
-
await
|
|
32714
|
-
restored.add(saved.id);
|
|
33074
|
+
await attemptRestore(saved);
|
|
32715
33075
|
} catch (err) {
|
|
32716
|
-
|
|
32717
|
-
|
|
33076
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33077
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33078
|
+
tags: {
|
|
33079
|
+
deviceId: saved.id,
|
|
33080
|
+
stableId: saved.stableId
|
|
33081
|
+
},
|
|
32718
33082
|
meta: {
|
|
32719
33083
|
type: saved.type,
|
|
32720
|
-
|
|
33084
|
+
attempt: 1,
|
|
33085
|
+
error
|
|
32721
33086
|
}
|
|
32722
33087
|
});
|
|
33088
|
+
failures.push({
|
|
33089
|
+
saved,
|
|
33090
|
+
error
|
|
33091
|
+
});
|
|
32723
33092
|
}
|
|
32724
33093
|
};
|
|
32725
33094
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33095
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
32726
33096
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
32727
33097
|
for (const saved of childRows) {
|
|
32728
|
-
|
|
32729
|
-
if (!Class) continue;
|
|
33098
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
32730
33099
|
if (saved.parentDeviceId === null) continue;
|
|
32731
|
-
if (
|
|
32732
|
-
|
|
32733
|
-
|
|
32734
|
-
|
|
32735
|
-
|
|
32736
|
-
|
|
33100
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33101
|
+
try {
|
|
33102
|
+
await attemptRestore(saved);
|
|
33103
|
+
} catch (err) {
|
|
33104
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33105
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33106
|
+
tags: {
|
|
33107
|
+
deviceId: saved.id,
|
|
33108
|
+
stableId: saved.stableId,
|
|
33109
|
+
parentDeviceId: saved.parentDeviceId
|
|
33110
|
+
},
|
|
33111
|
+
meta: {
|
|
33112
|
+
type: saved.type,
|
|
33113
|
+
attempt: 1,
|
|
33114
|
+
error
|
|
33115
|
+
}
|
|
33116
|
+
});
|
|
33117
|
+
failures.push({
|
|
33118
|
+
saved,
|
|
33119
|
+
error
|
|
33120
|
+
});
|
|
33121
|
+
}
|
|
33122
|
+
continue;
|
|
33123
|
+
}
|
|
33124
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33125
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
32737
33126
|
tags: {
|
|
33127
|
+
deviceId: saved.id,
|
|
32738
33128
|
stableId: saved.stableId,
|
|
32739
33129
|
parentDeviceId: saved.parentDeviceId
|
|
32740
33130
|
},
|
|
32741
|
-
meta: {
|
|
32742
|
-
|
|
32743
|
-
|
|
32744
|
-
|
|
33131
|
+
meta: { type: saved.type }
|
|
33132
|
+
});
|
|
33133
|
+
failures.push({
|
|
33134
|
+
saved,
|
|
33135
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
32745
33136
|
});
|
|
33137
|
+
continue;
|
|
32746
33138
|
}
|
|
32747
33139
|
}
|
|
33140
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33141
|
+
return {
|
|
33142
|
+
restoredCount: restored.size,
|
|
33143
|
+
failedCount: failures.length
|
|
33144
|
+
};
|
|
32748
33145
|
}
|
|
32749
33146
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
32750
33147
|
toSummary(device) {
|
|
@@ -34507,6 +34904,12 @@ Object.freeze({
|
|
|
34507
34904
|
addonId: null,
|
|
34508
34905
|
access: "view"
|
|
34509
34906
|
},
|
|
34907
|
+
"deviceProvider.reloadDevice": {
|
|
34908
|
+
capName: "device-provider",
|
|
34909
|
+
capScope: "system",
|
|
34910
|
+
addonId: null,
|
|
34911
|
+
access: "create"
|
|
34912
|
+
},
|
|
34510
34913
|
"deviceProvider.start": {
|
|
34511
34914
|
capName: "device-provider",
|
|
34512
34915
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -12755,7 +12755,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12755
12755
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12756
12756
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12757
12757
|
*/
|
|
12758
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12758
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12759
|
+
/**
|
|
12760
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12761
|
+
*
|
|
12762
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12763
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12764
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12765
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12766
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12767
|
+
* three child cameras offline for four hours.
|
|
12768
|
+
*
|
|
12769
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12770
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12771
|
+
* that cannot tell simply never sets it.
|
|
12772
|
+
*/
|
|
12773
|
+
alreadyOnboarded: boolean().optional(),
|
|
12774
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12775
|
+
onboardedDeviceId: number().optional(),
|
|
12776
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12777
|
+
onboardedName: string().optional()
|
|
12759
12778
|
});
|
|
12760
12779
|
/**
|
|
12761
12780
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12811,6 +12830,35 @@ var deviceProviderCapability = {
|
|
|
12811
12830
|
name: string(),
|
|
12812
12831
|
type: string()
|
|
12813
12832
|
}))),
|
|
12833
|
+
/**
|
|
12834
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12835
|
+
* touching no other device this provider owns.
|
|
12836
|
+
*
|
|
12837
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12838
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12839
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12840
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12841
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12842
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12843
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12844
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12845
|
+
*
|
|
12846
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12847
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12848
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12849
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12850
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12851
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12852
|
+
* against the wrong camera (D8).
|
|
12853
|
+
*
|
|
12854
|
+
* Construction can dial hardware, and the migrated source is
|
|
12855
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12856
|
+
* rather than the 60 s default.
|
|
12857
|
+
*/
|
|
12858
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12859
|
+
kind: "mutation",
|
|
12860
|
+
timeoutMs: 3 * 6e4
|
|
12861
|
+
}),
|
|
12814
12862
|
supportsDiscovery: method(object({}), boolean()),
|
|
12815
12863
|
/**
|
|
12816
12864
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13138,7 +13186,8 @@ method(object({
|
|
|
13138
13186
|
targetId: number()
|
|
13139
13187
|
}), MigrateDeviceResultSchema, {
|
|
13140
13188
|
kind: "mutation",
|
|
13141
|
-
auth: "admin"
|
|
13189
|
+
auth: "admin",
|
|
13190
|
+
timeoutMs: 12 * 6e4
|
|
13142
13191
|
}), method(DeviceRegisterPayloadSchema, _void(), { kind: "mutation" }), method(DeviceRemovePayloadSchema, _void(), { kind: "mutation" }), method(DevicePersistConfigPayloadSchema, _void(), { kind: "mutation" }), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), record(string(), unknown())), method(object({ deviceId: number() }), DeviceMetaSchema.nullable()), method(object({
|
|
13143
13192
|
deviceId: number(),
|
|
13144
13193
|
name: string()
|
|
@@ -32511,6 +32560,147 @@ var BaseDevice = class {
|
|
|
32511
32560
|
}
|
|
32512
32561
|
};
|
|
32513
32562
|
/**
|
|
32563
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
32564
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
32565
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
32566
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
32567
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
32568
|
+
*/
|
|
32569
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
32570
|
+
1e4,
|
|
32571
|
+
3e4,
|
|
32572
|
+
9e4
|
|
32573
|
+
];
|
|
32574
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
32575
|
+
function sleep$1(ms, signal) {
|
|
32576
|
+
return new Promise((resolve) => {
|
|
32577
|
+
if (signal.aborted) {
|
|
32578
|
+
resolve();
|
|
32579
|
+
return;
|
|
32580
|
+
}
|
|
32581
|
+
const onAbort = () => {
|
|
32582
|
+
clearTimeout(timer);
|
|
32583
|
+
resolve();
|
|
32584
|
+
};
|
|
32585
|
+
const timer = setTimeout(() => {
|
|
32586
|
+
signal.removeEventListener("abort", onAbort);
|
|
32587
|
+
resolve();
|
|
32588
|
+
}, ms);
|
|
32589
|
+
timer.unref?.();
|
|
32590
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
32591
|
+
});
|
|
32592
|
+
}
|
|
32593
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
32594
|
+
* not reject (callers wrap their own try/catch). */
|
|
32595
|
+
async function runWithConcurrency(items, width, fn) {
|
|
32596
|
+
const queue = [...items];
|
|
32597
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
32598
|
+
const lane = async () => {
|
|
32599
|
+
for (;;) {
|
|
32600
|
+
const item = queue.shift();
|
|
32601
|
+
if (item === void 0) return;
|
|
32602
|
+
await fn(item);
|
|
32603
|
+
}
|
|
32604
|
+
};
|
|
32605
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
32606
|
+
}
|
|
32607
|
+
var DeviceRestoreRetryScheduler = class {
|
|
32608
|
+
#logger;
|
|
32609
|
+
#attempt;
|
|
32610
|
+
#onPermanentFailure;
|
|
32611
|
+
#delaysMs;
|
|
32612
|
+
#concurrency;
|
|
32613
|
+
#now;
|
|
32614
|
+
#abort = new AbortController();
|
|
32615
|
+
constructor(options) {
|
|
32616
|
+
this.#logger = options.logger;
|
|
32617
|
+
this.#attempt = options.attempt;
|
|
32618
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
32619
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32620
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
32621
|
+
this.#now = options.now ?? Date.now;
|
|
32622
|
+
}
|
|
32623
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
32624
|
+
* permanently failed — the next boot restores them from disk. */
|
|
32625
|
+
cancel() {
|
|
32626
|
+
this.#abort.abort();
|
|
32627
|
+
}
|
|
32628
|
+
/**
|
|
32629
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
32630
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
32631
|
+
* cancelled. Never rejects.
|
|
32632
|
+
*/
|
|
32633
|
+
async run(initialFailures) {
|
|
32634
|
+
let pending = initialFailures.map((failure) => ({
|
|
32635
|
+
saved: failure.saved,
|
|
32636
|
+
lastError: failure.error,
|
|
32637
|
+
attempts: 1
|
|
32638
|
+
}));
|
|
32639
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
32640
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
32641
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
32642
|
+
if (this.#abort.signal.aborted) break;
|
|
32643
|
+
pending = await this.#runRound(pending, round);
|
|
32644
|
+
}
|
|
32645
|
+
if (this.#abort.signal.aborted) return [];
|
|
32646
|
+
const terminal = pending.map((entry) => ({
|
|
32647
|
+
deviceId: entry.saved.id,
|
|
32648
|
+
stableId: entry.saved.stableId,
|
|
32649
|
+
type: String(entry.saved.type),
|
|
32650
|
+
attempts: entry.attempts,
|
|
32651
|
+
lastError: entry.lastError,
|
|
32652
|
+
failedAt: this.#now()
|
|
32653
|
+
}));
|
|
32654
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
32655
|
+
return terminal;
|
|
32656
|
+
}
|
|
32657
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
32658
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
32659
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
32660
|
+
async #runRound(pending, round) {
|
|
32661
|
+
const next = [];
|
|
32662
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
32663
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
32664
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
32665
|
+
if (this.#abort.signal.aborted) {
|
|
32666
|
+
next.push(entry);
|
|
32667
|
+
return;
|
|
32668
|
+
}
|
|
32669
|
+
const attemptNo = entry.attempts + 1;
|
|
32670
|
+
try {
|
|
32671
|
+
await this.#attempt(entry.saved);
|
|
32672
|
+
this.#logger.info("Device restored on retry", {
|
|
32673
|
+
tags: {
|
|
32674
|
+
deviceId: entry.saved.id,
|
|
32675
|
+
stableId: entry.saved.stableId
|
|
32676
|
+
},
|
|
32677
|
+
meta: { attempt: attemptNo }
|
|
32678
|
+
});
|
|
32679
|
+
} catch (err) {
|
|
32680
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
32681
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
32682
|
+
this.#logger.warn("Device restore retry failed", {
|
|
32683
|
+
tags: {
|
|
32684
|
+
deviceId: entry.saved.id,
|
|
32685
|
+
stableId: entry.saved.stableId
|
|
32686
|
+
},
|
|
32687
|
+
meta: {
|
|
32688
|
+
attempt: attemptNo,
|
|
32689
|
+
remainingRetries,
|
|
32690
|
+
error: lastError
|
|
32691
|
+
}
|
|
32692
|
+
});
|
|
32693
|
+
next.push({
|
|
32694
|
+
saved: entry.saved,
|
|
32695
|
+
lastError,
|
|
32696
|
+
attempts: attemptNo
|
|
32697
|
+
});
|
|
32698
|
+
}
|
|
32699
|
+
});
|
|
32700
|
+
return next;
|
|
32701
|
+
}
|
|
32702
|
+
};
|
|
32703
|
+
/**
|
|
32514
32704
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
32515
32705
|
* device-provider cap router. Shared across all providers.
|
|
32516
32706
|
*/
|
|
@@ -32559,6 +32749,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32559
32749
|
}];
|
|
32560
32750
|
}
|
|
32561
32751
|
async onShutdown() {
|
|
32752
|
+
this.cancelRestoreRetries();
|
|
32562
32753
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32563
32754
|
for (const device of devices) try {
|
|
32564
32755
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -32576,9 +32767,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32576
32767
|
async start() {}
|
|
32577
32768
|
async stop() {}
|
|
32578
32769
|
async getStatus() {
|
|
32770
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32771
|
+
const summary = this.restoreFailureSummary();
|
|
32772
|
+
if (summary === null) return {
|
|
32773
|
+
connected: true,
|
|
32774
|
+
deviceCount: all.length
|
|
32775
|
+
};
|
|
32579
32776
|
return {
|
|
32580
32777
|
connected: true,
|
|
32581
|
-
deviceCount:
|
|
32778
|
+
deviceCount: all.length,
|
|
32779
|
+
error: summary
|
|
32582
32780
|
};
|
|
32583
32781
|
}
|
|
32584
32782
|
async getDevices() {
|
|
@@ -32668,8 +32866,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32668
32866
|
};
|
|
32669
32867
|
}
|
|
32670
32868
|
async restoreDevices(savedDevices) {
|
|
32671
|
-
await this.onRestoreDevices(savedDevices);
|
|
32672
|
-
if (savedDevices.length
|
|
32869
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
32870
|
+
if (savedDevices.length === 0) return;
|
|
32871
|
+
if (report && report.failedCount > 0) {
|
|
32872
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
32873
|
+
return;
|
|
32874
|
+
}
|
|
32875
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
32876
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
32877
|
+
}
|
|
32878
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
32879
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32880
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
32881
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
32882
|
+
restoreRetryConcurrency = 4;
|
|
32883
|
+
_restoreRetryScheduler = null;
|
|
32884
|
+
_restoreRetryCompletion = null;
|
|
32885
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
32886
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
32887
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
32888
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
32889
|
+
* with the devices that restored, and a late success is announced
|
|
32890
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
32891
|
+
get restoreRetryCompletion() {
|
|
32892
|
+
return this._restoreRetryCompletion;
|
|
32893
|
+
}
|
|
32894
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
32895
|
+
get permanentRestoreFailures() {
|
|
32896
|
+
return [...this._permanentRestoreFailures.values()];
|
|
32897
|
+
}
|
|
32898
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
32899
|
+
* `null` when every device restored. */
|
|
32900
|
+
restoreFailureSummary() {
|
|
32901
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
32902
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
32903
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
32904
|
+
}
|
|
32905
|
+
cancelRestoreRetries() {
|
|
32906
|
+
this._restoreRetryScheduler?.cancel();
|
|
32907
|
+
this._restoreRetryScheduler = null;
|
|
32908
|
+
}
|
|
32909
|
+
recordPermanentRestoreFailure(failure) {
|
|
32910
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
32911
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
32912
|
+
tags: {
|
|
32913
|
+
deviceId: failure.deviceId,
|
|
32914
|
+
stableId: failure.stableId
|
|
32915
|
+
},
|
|
32916
|
+
meta: {
|
|
32917
|
+
type: failure.type,
|
|
32918
|
+
attempts: failure.attempts,
|
|
32919
|
+
error: failure.lastError
|
|
32920
|
+
}
|
|
32921
|
+
});
|
|
32922
|
+
}
|
|
32923
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
32924
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
32925
|
+
logger: this.ctx.logger,
|
|
32926
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
32927
|
+
concurrency: this.restoreRetryConcurrency,
|
|
32928
|
+
attempt,
|
|
32929
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
32930
|
+
});
|
|
32931
|
+
this._restoreRetryScheduler = scheduler;
|
|
32932
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
32933
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
32934
|
+
});
|
|
32935
|
+
}
|
|
32936
|
+
/**
|
|
32937
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
32938
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
32939
|
+
* and no other device this provider owns is disturbed.
|
|
32940
|
+
*
|
|
32941
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
32942
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
32943
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
32944
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
32945
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
32946
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
32947
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
32948
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
32949
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
32950
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
32951
|
+
*
|
|
32952
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
32953
|
+
* reload its parent instead.
|
|
32954
|
+
*/
|
|
32955
|
+
async reloadDevice(input) {
|
|
32956
|
+
const { stableId } = input;
|
|
32957
|
+
const devices = this.ctx.kernel.devices;
|
|
32958
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
32959
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
32960
|
+
if (live) await devices.decommission(live.id);
|
|
32961
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
32962
|
+
addonId: this.addonId,
|
|
32963
|
+
stableId
|
|
32964
|
+
});
|
|
32965
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
32966
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
32967
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
32968
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
32969
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
32970
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
32971
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
32972
|
+
for (const row of rows) {
|
|
32973
|
+
if (row.parentDeviceId !== id) continue;
|
|
32974
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
32975
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
32976
|
+
if (!ChildClass) continue;
|
|
32977
|
+
try {
|
|
32978
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
32979
|
+
} catch (err) {
|
|
32980
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
32981
|
+
tags: {
|
|
32982
|
+
deviceId: row.id,
|
|
32983
|
+
stableId: row.stableId
|
|
32984
|
+
},
|
|
32985
|
+
meta: {
|
|
32986
|
+
parentDeviceId: id,
|
|
32987
|
+
error: err instanceof Error ? err.message : String(err)
|
|
32988
|
+
}
|
|
32989
|
+
});
|
|
32990
|
+
}
|
|
32991
|
+
}
|
|
32992
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
32993
|
+
tags: { deviceId: id },
|
|
32994
|
+
meta: {
|
|
32995
|
+
stableId,
|
|
32996
|
+
type: meta.type
|
|
32997
|
+
}
|
|
32998
|
+
});
|
|
32999
|
+
return { deviceId: id };
|
|
32673
33000
|
}
|
|
32674
33001
|
/**
|
|
32675
33002
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -32695,55 +33022,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32695
33022
|
* accessory-spawn flow handles via the parent's
|
|
32696
33023
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
32697
33024
|
* fit.
|
|
33025
|
+
*
|
|
33026
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33027
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33028
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33029
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33030
|
+
* `getStatus().error`.
|
|
32698
33031
|
*/
|
|
33032
|
+
/**
|
|
33033
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33034
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33035
|
+
*
|
|
33036
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33037
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33038
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33039
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33040
|
+
* been emptied failed all four bounded attempts against fields
|
|
33041
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33042
|
+
*
|
|
33043
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33044
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33045
|
+
* the bound, then reported — never swallowed.
|
|
33046
|
+
*/
|
|
33047
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
32699
33048
|
async onRestoreDevices(savedDevices) {
|
|
32700
33049
|
const restored = /* @__PURE__ */ new Set();
|
|
33050
|
+
const failures = [];
|
|
33051
|
+
const attemptRestore = async (saved) => {
|
|
33052
|
+
if (restored.has(saved.id)) return;
|
|
33053
|
+
const Class = this.deviceClasses[saved.type];
|
|
33054
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33055
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33056
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33057
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33058
|
+
restored.add(saved.id);
|
|
33059
|
+
};
|
|
32701
33060
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
32702
33061
|
const restoreOne = async (saved) => {
|
|
32703
|
-
|
|
32704
|
-
if (!Class) {
|
|
33062
|
+
if (!this.deviceClasses[saved.type]) {
|
|
32705
33063
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
32706
|
-
tags: {
|
|
33064
|
+
tags: {
|
|
33065
|
+
deviceId: saved.id,
|
|
33066
|
+
stableId: saved.stableId
|
|
33067
|
+
},
|
|
32707
33068
|
meta: { type: saved.type }
|
|
32708
33069
|
});
|
|
32709
33070
|
return;
|
|
32710
33071
|
}
|
|
32711
33072
|
try {
|
|
32712
|
-
await
|
|
32713
|
-
restored.add(saved.id);
|
|
33073
|
+
await attemptRestore(saved);
|
|
32714
33074
|
} catch (err) {
|
|
32715
|
-
|
|
32716
|
-
|
|
33075
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33076
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33077
|
+
tags: {
|
|
33078
|
+
deviceId: saved.id,
|
|
33079
|
+
stableId: saved.stableId
|
|
33080
|
+
},
|
|
32717
33081
|
meta: {
|
|
32718
33082
|
type: saved.type,
|
|
32719
|
-
|
|
33083
|
+
attempt: 1,
|
|
33084
|
+
error
|
|
32720
33085
|
}
|
|
32721
33086
|
});
|
|
33087
|
+
failures.push({
|
|
33088
|
+
saved,
|
|
33089
|
+
error
|
|
33090
|
+
});
|
|
32722
33091
|
}
|
|
32723
33092
|
};
|
|
32724
33093
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33094
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
32725
33095
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
32726
33096
|
for (const saved of childRows) {
|
|
32727
|
-
|
|
32728
|
-
if (!Class) continue;
|
|
33097
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
32729
33098
|
if (saved.parentDeviceId === null) continue;
|
|
32730
|
-
if (
|
|
32731
|
-
|
|
32732
|
-
|
|
32733
|
-
|
|
32734
|
-
|
|
32735
|
-
|
|
33099
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33100
|
+
try {
|
|
33101
|
+
await attemptRestore(saved);
|
|
33102
|
+
} catch (err) {
|
|
33103
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33104
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33105
|
+
tags: {
|
|
33106
|
+
deviceId: saved.id,
|
|
33107
|
+
stableId: saved.stableId,
|
|
33108
|
+
parentDeviceId: saved.parentDeviceId
|
|
33109
|
+
},
|
|
33110
|
+
meta: {
|
|
33111
|
+
type: saved.type,
|
|
33112
|
+
attempt: 1,
|
|
33113
|
+
error
|
|
33114
|
+
}
|
|
33115
|
+
});
|
|
33116
|
+
failures.push({
|
|
33117
|
+
saved,
|
|
33118
|
+
error
|
|
33119
|
+
});
|
|
33120
|
+
}
|
|
33121
|
+
continue;
|
|
33122
|
+
}
|
|
33123
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33124
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
32736
33125
|
tags: {
|
|
33126
|
+
deviceId: saved.id,
|
|
32737
33127
|
stableId: saved.stableId,
|
|
32738
33128
|
parentDeviceId: saved.parentDeviceId
|
|
32739
33129
|
},
|
|
32740
|
-
meta: {
|
|
32741
|
-
|
|
32742
|
-
|
|
32743
|
-
|
|
33130
|
+
meta: { type: saved.type }
|
|
33131
|
+
});
|
|
33132
|
+
failures.push({
|
|
33133
|
+
saved,
|
|
33134
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
32744
33135
|
});
|
|
33136
|
+
continue;
|
|
32745
33137
|
}
|
|
32746
33138
|
}
|
|
33139
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33140
|
+
return {
|
|
33141
|
+
restoredCount: restored.size,
|
|
33142
|
+
failedCount: failures.length
|
|
33143
|
+
};
|
|
32747
33144
|
}
|
|
32748
33145
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
32749
33146
|
toSummary(device) {
|
|
@@ -34506,6 +34903,12 @@ Object.freeze({
|
|
|
34506
34903
|
addonId: null,
|
|
34507
34904
|
access: "view"
|
|
34508
34905
|
},
|
|
34906
|
+
"deviceProvider.reloadDevice": {
|
|
34907
|
+
capName: "device-provider",
|
|
34908
|
+
capScope: "system",
|
|
34909
|
+
addonId: null,
|
|
34910
|
+
access: "create"
|
|
34911
|
+
},
|
|
34509
34912
|
"deviceProvider.start": {
|
|
34510
34913
|
capName: "device-provider",
|
|
34511
34914
|
capScope: "system",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camstack/addon-provider-unifi",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.61",
|
|
4
4
|
"description": "UniFi Network controller device-provider addon for CamStack — local-controller infra switches/APs (as containers) + network-client presence. NO cameras/Protect.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"camstack",
|