@camstack/addon-provider-onvif 1.2.58 → 1.2.60
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
|
@@ -12546,7 +12546,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12546
12546
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12547
12547
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12548
12548
|
*/
|
|
12549
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12549
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12550
|
+
/**
|
|
12551
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12552
|
+
*
|
|
12553
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12554
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12555
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12556
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12557
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12558
|
+
* three child cameras offline for four hours.
|
|
12559
|
+
*
|
|
12560
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12561
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12562
|
+
* that cannot tell simply never sets it.
|
|
12563
|
+
*/
|
|
12564
|
+
alreadyOnboarded: boolean().optional(),
|
|
12565
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12566
|
+
onboardedDeviceId: number().optional(),
|
|
12567
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12568
|
+
onboardedName: string().optional()
|
|
12550
12569
|
});
|
|
12551
12570
|
/**
|
|
12552
12571
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12602,6 +12621,35 @@ var deviceProviderCapability = {
|
|
|
12602
12621
|
name: string(),
|
|
12603
12622
|
type: string()
|
|
12604
12623
|
}))),
|
|
12624
|
+
/**
|
|
12625
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12626
|
+
* touching no other device this provider owns.
|
|
12627
|
+
*
|
|
12628
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12629
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12630
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12631
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12632
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12633
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12634
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12635
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12636
|
+
*
|
|
12637
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12638
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12639
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12640
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12641
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12642
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12643
|
+
* against the wrong camera (D8).
|
|
12644
|
+
*
|
|
12645
|
+
* Construction can dial hardware, and the migrated source is
|
|
12646
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12647
|
+
* rather than the 60 s default.
|
|
12648
|
+
*/
|
|
12649
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12650
|
+
kind: "mutation",
|
|
12651
|
+
timeoutMs: 3 * 6e4
|
|
12652
|
+
}),
|
|
12605
12653
|
supportsDiscovery: method(object({}), boolean()),
|
|
12606
12654
|
/**
|
|
12607
12655
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -12929,7 +12977,8 @@ method(object({
|
|
|
12929
12977
|
targetId: number()
|
|
12930
12978
|
}), MigrateDeviceResultSchema, {
|
|
12931
12979
|
kind: "mutation",
|
|
12932
|
-
auth: "admin"
|
|
12980
|
+
auth: "admin",
|
|
12981
|
+
timeoutMs: 12 * 6e4
|
|
12933
12982
|
}), 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({
|
|
12934
12983
|
deviceId: number(),
|
|
12935
12984
|
name: string()
|
|
@@ -29283,6 +29332,147 @@ var DeviceConfig = class DeviceConfig {
|
|
|
29283
29332
|
}
|
|
29284
29333
|
};
|
|
29285
29334
|
/**
|
|
29335
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
29336
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
29337
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
29338
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
29339
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
29340
|
+
*/
|
|
29341
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
29342
|
+
1e4,
|
|
29343
|
+
3e4,
|
|
29344
|
+
9e4
|
|
29345
|
+
];
|
|
29346
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
29347
|
+
function sleep$1(ms, signal) {
|
|
29348
|
+
return new Promise((resolve) => {
|
|
29349
|
+
if (signal.aborted) {
|
|
29350
|
+
resolve();
|
|
29351
|
+
return;
|
|
29352
|
+
}
|
|
29353
|
+
const onAbort = () => {
|
|
29354
|
+
clearTimeout(timer);
|
|
29355
|
+
resolve();
|
|
29356
|
+
};
|
|
29357
|
+
const timer = setTimeout(() => {
|
|
29358
|
+
signal.removeEventListener("abort", onAbort);
|
|
29359
|
+
resolve();
|
|
29360
|
+
}, ms);
|
|
29361
|
+
timer.unref?.();
|
|
29362
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
29363
|
+
});
|
|
29364
|
+
}
|
|
29365
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
29366
|
+
* not reject (callers wrap their own try/catch). */
|
|
29367
|
+
async function runWithConcurrency(items, width, fn) {
|
|
29368
|
+
const queue = [...items];
|
|
29369
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
29370
|
+
const lane = async () => {
|
|
29371
|
+
for (;;) {
|
|
29372
|
+
const item = queue.shift();
|
|
29373
|
+
if (item === void 0) return;
|
|
29374
|
+
await fn(item);
|
|
29375
|
+
}
|
|
29376
|
+
};
|
|
29377
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
29378
|
+
}
|
|
29379
|
+
var DeviceRestoreRetryScheduler = class {
|
|
29380
|
+
#logger;
|
|
29381
|
+
#attempt;
|
|
29382
|
+
#onPermanentFailure;
|
|
29383
|
+
#delaysMs;
|
|
29384
|
+
#concurrency;
|
|
29385
|
+
#now;
|
|
29386
|
+
#abort = new AbortController();
|
|
29387
|
+
constructor(options) {
|
|
29388
|
+
this.#logger = options.logger;
|
|
29389
|
+
this.#attempt = options.attempt;
|
|
29390
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
29391
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
29392
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
29393
|
+
this.#now = options.now ?? Date.now;
|
|
29394
|
+
}
|
|
29395
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
29396
|
+
* permanently failed — the next boot restores them from disk. */
|
|
29397
|
+
cancel() {
|
|
29398
|
+
this.#abort.abort();
|
|
29399
|
+
}
|
|
29400
|
+
/**
|
|
29401
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
29402
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
29403
|
+
* cancelled. Never rejects.
|
|
29404
|
+
*/
|
|
29405
|
+
async run(initialFailures) {
|
|
29406
|
+
let pending = initialFailures.map((failure) => ({
|
|
29407
|
+
saved: failure.saved,
|
|
29408
|
+
lastError: failure.error,
|
|
29409
|
+
attempts: 1
|
|
29410
|
+
}));
|
|
29411
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
29412
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
29413
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
29414
|
+
if (this.#abort.signal.aborted) break;
|
|
29415
|
+
pending = await this.#runRound(pending, round);
|
|
29416
|
+
}
|
|
29417
|
+
if (this.#abort.signal.aborted) return [];
|
|
29418
|
+
const terminal = pending.map((entry) => ({
|
|
29419
|
+
deviceId: entry.saved.id,
|
|
29420
|
+
stableId: entry.saved.stableId,
|
|
29421
|
+
type: String(entry.saved.type),
|
|
29422
|
+
attempts: entry.attempts,
|
|
29423
|
+
lastError: entry.lastError,
|
|
29424
|
+
failedAt: this.#now()
|
|
29425
|
+
}));
|
|
29426
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
29427
|
+
return terminal;
|
|
29428
|
+
}
|
|
29429
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
29430
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
29431
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
29432
|
+
async #runRound(pending, round) {
|
|
29433
|
+
const next = [];
|
|
29434
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
29435
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
29436
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
29437
|
+
if (this.#abort.signal.aborted) {
|
|
29438
|
+
next.push(entry);
|
|
29439
|
+
return;
|
|
29440
|
+
}
|
|
29441
|
+
const attemptNo = entry.attempts + 1;
|
|
29442
|
+
try {
|
|
29443
|
+
await this.#attempt(entry.saved);
|
|
29444
|
+
this.#logger.info("Device restored on retry", {
|
|
29445
|
+
tags: {
|
|
29446
|
+
deviceId: entry.saved.id,
|
|
29447
|
+
stableId: entry.saved.stableId
|
|
29448
|
+
},
|
|
29449
|
+
meta: { attempt: attemptNo }
|
|
29450
|
+
});
|
|
29451
|
+
} catch (err) {
|
|
29452
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
29453
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
29454
|
+
this.#logger.warn("Device restore retry failed", {
|
|
29455
|
+
tags: {
|
|
29456
|
+
deviceId: entry.saved.id,
|
|
29457
|
+
stableId: entry.saved.stableId
|
|
29458
|
+
},
|
|
29459
|
+
meta: {
|
|
29460
|
+
attempt: attemptNo,
|
|
29461
|
+
remainingRetries,
|
|
29462
|
+
error: lastError
|
|
29463
|
+
}
|
|
29464
|
+
});
|
|
29465
|
+
next.push({
|
|
29466
|
+
saved: entry.saved,
|
|
29467
|
+
lastError,
|
|
29468
|
+
attempts: attemptNo
|
|
29469
|
+
});
|
|
29470
|
+
}
|
|
29471
|
+
});
|
|
29472
|
+
return next;
|
|
29473
|
+
}
|
|
29474
|
+
};
|
|
29475
|
+
/**
|
|
29286
29476
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
29287
29477
|
* device-provider cap router. Shared across all providers.
|
|
29288
29478
|
*/
|
|
@@ -29331,6 +29521,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29331
29521
|
}];
|
|
29332
29522
|
}
|
|
29333
29523
|
async onShutdown() {
|
|
29524
|
+
this.cancelRestoreRetries();
|
|
29334
29525
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
29335
29526
|
for (const device of devices) try {
|
|
29336
29527
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -29348,9 +29539,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29348
29539
|
async start() {}
|
|
29349
29540
|
async stop() {}
|
|
29350
29541
|
async getStatus() {
|
|
29542
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
29543
|
+
const summary = this.restoreFailureSummary();
|
|
29544
|
+
if (summary === null) return {
|
|
29545
|
+
connected: true,
|
|
29546
|
+
deviceCount: all.length
|
|
29547
|
+
};
|
|
29351
29548
|
return {
|
|
29352
29549
|
connected: true,
|
|
29353
|
-
deviceCount:
|
|
29550
|
+
deviceCount: all.length,
|
|
29551
|
+
error: summary
|
|
29354
29552
|
};
|
|
29355
29553
|
}
|
|
29356
29554
|
async getDevices() {
|
|
@@ -29440,8 +29638,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29440
29638
|
};
|
|
29441
29639
|
}
|
|
29442
29640
|
async restoreDevices(savedDevices) {
|
|
29443
|
-
await this.onRestoreDevices(savedDevices);
|
|
29444
|
-
if (savedDevices.length
|
|
29641
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
29642
|
+
if (savedDevices.length === 0) return;
|
|
29643
|
+
if (report && report.failedCount > 0) {
|
|
29644
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
29645
|
+
return;
|
|
29646
|
+
}
|
|
29647
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
29648
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
29649
|
+
}
|
|
29650
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
29651
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
29652
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
29653
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
29654
|
+
restoreRetryConcurrency = 4;
|
|
29655
|
+
_restoreRetryScheduler = null;
|
|
29656
|
+
_restoreRetryCompletion = null;
|
|
29657
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
29658
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
29659
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
29660
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
29661
|
+
* with the devices that restored, and a late success is announced
|
|
29662
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
29663
|
+
get restoreRetryCompletion() {
|
|
29664
|
+
return this._restoreRetryCompletion;
|
|
29665
|
+
}
|
|
29666
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
29667
|
+
get permanentRestoreFailures() {
|
|
29668
|
+
return [...this._permanentRestoreFailures.values()];
|
|
29669
|
+
}
|
|
29670
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
29671
|
+
* `null` when every device restored. */
|
|
29672
|
+
restoreFailureSummary() {
|
|
29673
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
29674
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
29675
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
29676
|
+
}
|
|
29677
|
+
cancelRestoreRetries() {
|
|
29678
|
+
this._restoreRetryScheduler?.cancel();
|
|
29679
|
+
this._restoreRetryScheduler = null;
|
|
29680
|
+
}
|
|
29681
|
+
recordPermanentRestoreFailure(failure) {
|
|
29682
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
29683
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
29684
|
+
tags: {
|
|
29685
|
+
deviceId: failure.deviceId,
|
|
29686
|
+
stableId: failure.stableId
|
|
29687
|
+
},
|
|
29688
|
+
meta: {
|
|
29689
|
+
type: failure.type,
|
|
29690
|
+
attempts: failure.attempts,
|
|
29691
|
+
error: failure.lastError
|
|
29692
|
+
}
|
|
29693
|
+
});
|
|
29694
|
+
}
|
|
29695
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
29696
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
29697
|
+
logger: this.ctx.logger,
|
|
29698
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
29699
|
+
concurrency: this.restoreRetryConcurrency,
|
|
29700
|
+
attempt,
|
|
29701
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
29702
|
+
});
|
|
29703
|
+
this._restoreRetryScheduler = scheduler;
|
|
29704
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
29705
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
29706
|
+
});
|
|
29707
|
+
}
|
|
29708
|
+
/**
|
|
29709
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
29710
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
29711
|
+
* and no other device this provider owns is disturbed.
|
|
29712
|
+
*
|
|
29713
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
29714
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
29715
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
29716
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
29717
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
29718
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
29719
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
29720
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
29721
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
29722
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
29723
|
+
*
|
|
29724
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
29725
|
+
* reload its parent instead.
|
|
29726
|
+
*/
|
|
29727
|
+
async reloadDevice(input) {
|
|
29728
|
+
const { stableId } = input;
|
|
29729
|
+
const devices = this.ctx.kernel.devices;
|
|
29730
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
29731
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
29732
|
+
if (live) await devices.decommission(live.id);
|
|
29733
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
29734
|
+
addonId: this.addonId,
|
|
29735
|
+
stableId
|
|
29736
|
+
});
|
|
29737
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
29738
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
29739
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
29740
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
29741
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
29742
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
29743
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
29744
|
+
for (const row of rows) {
|
|
29745
|
+
if (row.parentDeviceId !== id) continue;
|
|
29746
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
29747
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
29748
|
+
if (!ChildClass) continue;
|
|
29749
|
+
try {
|
|
29750
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
29751
|
+
} catch (err) {
|
|
29752
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
29753
|
+
tags: {
|
|
29754
|
+
deviceId: row.id,
|
|
29755
|
+
stableId: row.stableId
|
|
29756
|
+
},
|
|
29757
|
+
meta: {
|
|
29758
|
+
parentDeviceId: id,
|
|
29759
|
+
error: err instanceof Error ? err.message : String(err)
|
|
29760
|
+
}
|
|
29761
|
+
});
|
|
29762
|
+
}
|
|
29763
|
+
}
|
|
29764
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
29765
|
+
tags: { deviceId: id },
|
|
29766
|
+
meta: {
|
|
29767
|
+
stableId,
|
|
29768
|
+
type: meta.type
|
|
29769
|
+
}
|
|
29770
|
+
});
|
|
29771
|
+
return { deviceId: id };
|
|
29445
29772
|
}
|
|
29446
29773
|
/**
|
|
29447
29774
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -29467,55 +29794,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29467
29794
|
* accessory-spawn flow handles via the parent's
|
|
29468
29795
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
29469
29796
|
* fit.
|
|
29797
|
+
*
|
|
29798
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
29799
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
29800
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
29801
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
29802
|
+
* `getStatus().error`.
|
|
29803
|
+
*/
|
|
29804
|
+
/**
|
|
29805
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
29806
|
+
* Default: no-op — most providers have nothing to heal.
|
|
29807
|
+
*
|
|
29808
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
29809
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
29810
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
29811
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
29812
|
+
* been emptied failed all four bounded attempts against fields
|
|
29813
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
29814
|
+
*
|
|
29815
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
29816
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
29817
|
+
* the bound, then reported — never swallowed.
|
|
29470
29818
|
*/
|
|
29819
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
29471
29820
|
async onRestoreDevices(savedDevices) {
|
|
29472
29821
|
const restored = /* @__PURE__ */ new Set();
|
|
29822
|
+
const failures = [];
|
|
29823
|
+
const attemptRestore = async (saved) => {
|
|
29824
|
+
if (restored.has(saved.id)) return;
|
|
29825
|
+
const Class = this.deviceClasses[saved.type];
|
|
29826
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
29827
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
29828
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
29829
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
29830
|
+
restored.add(saved.id);
|
|
29831
|
+
};
|
|
29473
29832
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
29474
29833
|
const restoreOne = async (saved) => {
|
|
29475
|
-
|
|
29476
|
-
if (!Class) {
|
|
29834
|
+
if (!this.deviceClasses[saved.type]) {
|
|
29477
29835
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
29478
|
-
tags: {
|
|
29836
|
+
tags: {
|
|
29837
|
+
deviceId: saved.id,
|
|
29838
|
+
stableId: saved.stableId
|
|
29839
|
+
},
|
|
29479
29840
|
meta: { type: saved.type }
|
|
29480
29841
|
});
|
|
29481
29842
|
return;
|
|
29482
29843
|
}
|
|
29483
29844
|
try {
|
|
29484
|
-
await
|
|
29485
|
-
restored.add(saved.id);
|
|
29845
|
+
await attemptRestore(saved);
|
|
29486
29846
|
} catch (err) {
|
|
29487
|
-
|
|
29488
|
-
|
|
29847
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
29848
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
29849
|
+
tags: {
|
|
29850
|
+
deviceId: saved.id,
|
|
29851
|
+
stableId: saved.stableId
|
|
29852
|
+
},
|
|
29489
29853
|
meta: {
|
|
29490
29854
|
type: saved.type,
|
|
29491
|
-
|
|
29855
|
+
attempt: 1,
|
|
29856
|
+
error
|
|
29492
29857
|
}
|
|
29493
29858
|
});
|
|
29859
|
+
failures.push({
|
|
29860
|
+
saved,
|
|
29861
|
+
error
|
|
29862
|
+
});
|
|
29494
29863
|
}
|
|
29495
29864
|
};
|
|
29496
29865
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
29866
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
29497
29867
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
29498
29868
|
for (const saved of childRows) {
|
|
29499
|
-
|
|
29500
|
-
if (!Class) continue;
|
|
29869
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
29501
29870
|
if (saved.parentDeviceId === null) continue;
|
|
29502
|
-
if (
|
|
29503
|
-
|
|
29504
|
-
|
|
29505
|
-
|
|
29506
|
-
|
|
29507
|
-
|
|
29871
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
29872
|
+
try {
|
|
29873
|
+
await attemptRestore(saved);
|
|
29874
|
+
} catch (err) {
|
|
29875
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
29876
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
29877
|
+
tags: {
|
|
29878
|
+
deviceId: saved.id,
|
|
29879
|
+
stableId: saved.stableId,
|
|
29880
|
+
parentDeviceId: saved.parentDeviceId
|
|
29881
|
+
},
|
|
29882
|
+
meta: {
|
|
29883
|
+
type: saved.type,
|
|
29884
|
+
attempt: 1,
|
|
29885
|
+
error
|
|
29886
|
+
}
|
|
29887
|
+
});
|
|
29888
|
+
failures.push({
|
|
29889
|
+
saved,
|
|
29890
|
+
error
|
|
29891
|
+
});
|
|
29892
|
+
}
|
|
29893
|
+
continue;
|
|
29894
|
+
}
|
|
29895
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
29896
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
29508
29897
|
tags: {
|
|
29898
|
+
deviceId: saved.id,
|
|
29509
29899
|
stableId: saved.stableId,
|
|
29510
29900
|
parentDeviceId: saved.parentDeviceId
|
|
29511
29901
|
},
|
|
29512
|
-
meta: {
|
|
29513
|
-
|
|
29514
|
-
|
|
29515
|
-
|
|
29902
|
+
meta: { type: saved.type }
|
|
29903
|
+
});
|
|
29904
|
+
failures.push({
|
|
29905
|
+
saved,
|
|
29906
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
29516
29907
|
});
|
|
29908
|
+
continue;
|
|
29517
29909
|
}
|
|
29518
29910
|
}
|
|
29911
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
29912
|
+
return {
|
|
29913
|
+
restoredCount: restored.size,
|
|
29914
|
+
failedCount: failures.length
|
|
29915
|
+
};
|
|
29519
29916
|
}
|
|
29520
29917
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
29521
29918
|
toSummary(device) {
|
|
@@ -31344,6 +31741,12 @@ Object.freeze({
|
|
|
31344
31741
|
addonId: null,
|
|
31345
31742
|
access: "view"
|
|
31346
31743
|
},
|
|
31744
|
+
"deviceProvider.reloadDevice": {
|
|
31745
|
+
capName: "device-provider",
|
|
31746
|
+
capScope: "system",
|
|
31747
|
+
addonId: null,
|
|
31748
|
+
access: "create"
|
|
31749
|
+
},
|
|
31347
31750
|
"deviceProvider.start": {
|
|
31348
31751
|
capName: "device-provider",
|
|
31349
31752
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -12547,7 +12547,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12547
12547
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12548
12548
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12549
12549
|
*/
|
|
12550
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12550
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12551
|
+
/**
|
|
12552
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12553
|
+
*
|
|
12554
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12555
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12556
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12557
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12558
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12559
|
+
* three child cameras offline for four hours.
|
|
12560
|
+
*
|
|
12561
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12562
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12563
|
+
* that cannot tell simply never sets it.
|
|
12564
|
+
*/
|
|
12565
|
+
alreadyOnboarded: boolean().optional(),
|
|
12566
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12567
|
+
onboardedDeviceId: number().optional(),
|
|
12568
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12569
|
+
onboardedName: string().optional()
|
|
12551
12570
|
});
|
|
12552
12571
|
/**
|
|
12553
12572
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12603,6 +12622,35 @@ var deviceProviderCapability = {
|
|
|
12603
12622
|
name: string(),
|
|
12604
12623
|
type: string()
|
|
12605
12624
|
}))),
|
|
12625
|
+
/**
|
|
12626
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12627
|
+
* touching no other device this provider owns.
|
|
12628
|
+
*
|
|
12629
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12630
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12631
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12632
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12633
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12634
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12635
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12636
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12637
|
+
*
|
|
12638
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12639
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12640
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12641
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12642
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12643
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12644
|
+
* against the wrong camera (D8).
|
|
12645
|
+
*
|
|
12646
|
+
* Construction can dial hardware, and the migrated source is
|
|
12647
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12648
|
+
* rather than the 60 s default.
|
|
12649
|
+
*/
|
|
12650
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12651
|
+
kind: "mutation",
|
|
12652
|
+
timeoutMs: 3 * 6e4
|
|
12653
|
+
}),
|
|
12606
12654
|
supportsDiscovery: method(object({}), boolean()),
|
|
12607
12655
|
/**
|
|
12608
12656
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -12930,7 +12978,8 @@ method(object({
|
|
|
12930
12978
|
targetId: number()
|
|
12931
12979
|
}), MigrateDeviceResultSchema, {
|
|
12932
12980
|
kind: "mutation",
|
|
12933
|
-
auth: "admin"
|
|
12981
|
+
auth: "admin",
|
|
12982
|
+
timeoutMs: 12 * 6e4
|
|
12934
12983
|
}), 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({
|
|
12935
12984
|
deviceId: number(),
|
|
12936
12985
|
name: string()
|
|
@@ -29284,6 +29333,147 @@ var DeviceConfig = class DeviceConfig {
|
|
|
29284
29333
|
}
|
|
29285
29334
|
};
|
|
29286
29335
|
/**
|
|
29336
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
29337
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
29338
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
29339
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
29340
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
29341
|
+
*/
|
|
29342
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
29343
|
+
1e4,
|
|
29344
|
+
3e4,
|
|
29345
|
+
9e4
|
|
29346
|
+
];
|
|
29347
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
29348
|
+
function sleep$1(ms, signal) {
|
|
29349
|
+
return new Promise((resolve) => {
|
|
29350
|
+
if (signal.aborted) {
|
|
29351
|
+
resolve();
|
|
29352
|
+
return;
|
|
29353
|
+
}
|
|
29354
|
+
const onAbort = () => {
|
|
29355
|
+
clearTimeout(timer);
|
|
29356
|
+
resolve();
|
|
29357
|
+
};
|
|
29358
|
+
const timer = setTimeout(() => {
|
|
29359
|
+
signal.removeEventListener("abort", onAbort);
|
|
29360
|
+
resolve();
|
|
29361
|
+
}, ms);
|
|
29362
|
+
timer.unref?.();
|
|
29363
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
29364
|
+
});
|
|
29365
|
+
}
|
|
29366
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
29367
|
+
* not reject (callers wrap their own try/catch). */
|
|
29368
|
+
async function runWithConcurrency(items, width, fn) {
|
|
29369
|
+
const queue = [...items];
|
|
29370
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
29371
|
+
const lane = async () => {
|
|
29372
|
+
for (;;) {
|
|
29373
|
+
const item = queue.shift();
|
|
29374
|
+
if (item === void 0) return;
|
|
29375
|
+
await fn(item);
|
|
29376
|
+
}
|
|
29377
|
+
};
|
|
29378
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
29379
|
+
}
|
|
29380
|
+
var DeviceRestoreRetryScheduler = class {
|
|
29381
|
+
#logger;
|
|
29382
|
+
#attempt;
|
|
29383
|
+
#onPermanentFailure;
|
|
29384
|
+
#delaysMs;
|
|
29385
|
+
#concurrency;
|
|
29386
|
+
#now;
|
|
29387
|
+
#abort = new AbortController();
|
|
29388
|
+
constructor(options) {
|
|
29389
|
+
this.#logger = options.logger;
|
|
29390
|
+
this.#attempt = options.attempt;
|
|
29391
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
29392
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
29393
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
29394
|
+
this.#now = options.now ?? Date.now;
|
|
29395
|
+
}
|
|
29396
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
29397
|
+
* permanently failed — the next boot restores them from disk. */
|
|
29398
|
+
cancel() {
|
|
29399
|
+
this.#abort.abort();
|
|
29400
|
+
}
|
|
29401
|
+
/**
|
|
29402
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
29403
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
29404
|
+
* cancelled. Never rejects.
|
|
29405
|
+
*/
|
|
29406
|
+
async run(initialFailures) {
|
|
29407
|
+
let pending = initialFailures.map((failure) => ({
|
|
29408
|
+
saved: failure.saved,
|
|
29409
|
+
lastError: failure.error,
|
|
29410
|
+
attempts: 1
|
|
29411
|
+
}));
|
|
29412
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
29413
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
29414
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
29415
|
+
if (this.#abort.signal.aborted) break;
|
|
29416
|
+
pending = await this.#runRound(pending, round);
|
|
29417
|
+
}
|
|
29418
|
+
if (this.#abort.signal.aborted) return [];
|
|
29419
|
+
const terminal = pending.map((entry) => ({
|
|
29420
|
+
deviceId: entry.saved.id,
|
|
29421
|
+
stableId: entry.saved.stableId,
|
|
29422
|
+
type: String(entry.saved.type),
|
|
29423
|
+
attempts: entry.attempts,
|
|
29424
|
+
lastError: entry.lastError,
|
|
29425
|
+
failedAt: this.#now()
|
|
29426
|
+
}));
|
|
29427
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
29428
|
+
return terminal;
|
|
29429
|
+
}
|
|
29430
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
29431
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
29432
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
29433
|
+
async #runRound(pending, round) {
|
|
29434
|
+
const next = [];
|
|
29435
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
29436
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
29437
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
29438
|
+
if (this.#abort.signal.aborted) {
|
|
29439
|
+
next.push(entry);
|
|
29440
|
+
return;
|
|
29441
|
+
}
|
|
29442
|
+
const attemptNo = entry.attempts + 1;
|
|
29443
|
+
try {
|
|
29444
|
+
await this.#attempt(entry.saved);
|
|
29445
|
+
this.#logger.info("Device restored on retry", {
|
|
29446
|
+
tags: {
|
|
29447
|
+
deviceId: entry.saved.id,
|
|
29448
|
+
stableId: entry.saved.stableId
|
|
29449
|
+
},
|
|
29450
|
+
meta: { attempt: attemptNo }
|
|
29451
|
+
});
|
|
29452
|
+
} catch (err) {
|
|
29453
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
29454
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
29455
|
+
this.#logger.warn("Device restore retry failed", {
|
|
29456
|
+
tags: {
|
|
29457
|
+
deviceId: entry.saved.id,
|
|
29458
|
+
stableId: entry.saved.stableId
|
|
29459
|
+
},
|
|
29460
|
+
meta: {
|
|
29461
|
+
attempt: attemptNo,
|
|
29462
|
+
remainingRetries,
|
|
29463
|
+
error: lastError
|
|
29464
|
+
}
|
|
29465
|
+
});
|
|
29466
|
+
next.push({
|
|
29467
|
+
saved: entry.saved,
|
|
29468
|
+
lastError,
|
|
29469
|
+
attempts: attemptNo
|
|
29470
|
+
});
|
|
29471
|
+
}
|
|
29472
|
+
});
|
|
29473
|
+
return next;
|
|
29474
|
+
}
|
|
29475
|
+
};
|
|
29476
|
+
/**
|
|
29287
29477
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
29288
29478
|
* device-provider cap router. Shared across all providers.
|
|
29289
29479
|
*/
|
|
@@ -29332,6 +29522,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29332
29522
|
}];
|
|
29333
29523
|
}
|
|
29334
29524
|
async onShutdown() {
|
|
29525
|
+
this.cancelRestoreRetries();
|
|
29335
29526
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
29336
29527
|
for (const device of devices) try {
|
|
29337
29528
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -29349,9 +29540,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29349
29540
|
async start() {}
|
|
29350
29541
|
async stop() {}
|
|
29351
29542
|
async getStatus() {
|
|
29543
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
29544
|
+
const summary = this.restoreFailureSummary();
|
|
29545
|
+
if (summary === null) return {
|
|
29546
|
+
connected: true,
|
|
29547
|
+
deviceCount: all.length
|
|
29548
|
+
};
|
|
29352
29549
|
return {
|
|
29353
29550
|
connected: true,
|
|
29354
|
-
deviceCount:
|
|
29551
|
+
deviceCount: all.length,
|
|
29552
|
+
error: summary
|
|
29355
29553
|
};
|
|
29356
29554
|
}
|
|
29357
29555
|
async getDevices() {
|
|
@@ -29441,8 +29639,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29441
29639
|
};
|
|
29442
29640
|
}
|
|
29443
29641
|
async restoreDevices(savedDevices) {
|
|
29444
|
-
await this.onRestoreDevices(savedDevices);
|
|
29445
|
-
if (savedDevices.length
|
|
29642
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
29643
|
+
if (savedDevices.length === 0) return;
|
|
29644
|
+
if (report && report.failedCount > 0) {
|
|
29645
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
29646
|
+
return;
|
|
29647
|
+
}
|
|
29648
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
29649
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
29650
|
+
}
|
|
29651
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
29652
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
29653
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
29654
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
29655
|
+
restoreRetryConcurrency = 4;
|
|
29656
|
+
_restoreRetryScheduler = null;
|
|
29657
|
+
_restoreRetryCompletion = null;
|
|
29658
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
29659
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
29660
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
29661
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
29662
|
+
* with the devices that restored, and a late success is announced
|
|
29663
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
29664
|
+
get restoreRetryCompletion() {
|
|
29665
|
+
return this._restoreRetryCompletion;
|
|
29666
|
+
}
|
|
29667
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
29668
|
+
get permanentRestoreFailures() {
|
|
29669
|
+
return [...this._permanentRestoreFailures.values()];
|
|
29670
|
+
}
|
|
29671
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
29672
|
+
* `null` when every device restored. */
|
|
29673
|
+
restoreFailureSummary() {
|
|
29674
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
29675
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
29676
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
29677
|
+
}
|
|
29678
|
+
cancelRestoreRetries() {
|
|
29679
|
+
this._restoreRetryScheduler?.cancel();
|
|
29680
|
+
this._restoreRetryScheduler = null;
|
|
29681
|
+
}
|
|
29682
|
+
recordPermanentRestoreFailure(failure) {
|
|
29683
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
29684
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
29685
|
+
tags: {
|
|
29686
|
+
deviceId: failure.deviceId,
|
|
29687
|
+
stableId: failure.stableId
|
|
29688
|
+
},
|
|
29689
|
+
meta: {
|
|
29690
|
+
type: failure.type,
|
|
29691
|
+
attempts: failure.attempts,
|
|
29692
|
+
error: failure.lastError
|
|
29693
|
+
}
|
|
29694
|
+
});
|
|
29695
|
+
}
|
|
29696
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
29697
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
29698
|
+
logger: this.ctx.logger,
|
|
29699
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
29700
|
+
concurrency: this.restoreRetryConcurrency,
|
|
29701
|
+
attempt,
|
|
29702
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
29703
|
+
});
|
|
29704
|
+
this._restoreRetryScheduler = scheduler;
|
|
29705
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
29706
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
29707
|
+
});
|
|
29708
|
+
}
|
|
29709
|
+
/**
|
|
29710
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
29711
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
29712
|
+
* and no other device this provider owns is disturbed.
|
|
29713
|
+
*
|
|
29714
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
29715
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
29716
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
29717
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
29718
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
29719
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
29720
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
29721
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
29722
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
29723
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
29724
|
+
*
|
|
29725
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
29726
|
+
* reload its parent instead.
|
|
29727
|
+
*/
|
|
29728
|
+
async reloadDevice(input) {
|
|
29729
|
+
const { stableId } = input;
|
|
29730
|
+
const devices = this.ctx.kernel.devices;
|
|
29731
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
29732
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
29733
|
+
if (live) await devices.decommission(live.id);
|
|
29734
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
29735
|
+
addonId: this.addonId,
|
|
29736
|
+
stableId
|
|
29737
|
+
});
|
|
29738
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
29739
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
29740
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
29741
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
29742
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
29743
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
29744
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
29745
|
+
for (const row of rows) {
|
|
29746
|
+
if (row.parentDeviceId !== id) continue;
|
|
29747
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
29748
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
29749
|
+
if (!ChildClass) continue;
|
|
29750
|
+
try {
|
|
29751
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
29752
|
+
} catch (err) {
|
|
29753
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
29754
|
+
tags: {
|
|
29755
|
+
deviceId: row.id,
|
|
29756
|
+
stableId: row.stableId
|
|
29757
|
+
},
|
|
29758
|
+
meta: {
|
|
29759
|
+
parentDeviceId: id,
|
|
29760
|
+
error: err instanceof Error ? err.message : String(err)
|
|
29761
|
+
}
|
|
29762
|
+
});
|
|
29763
|
+
}
|
|
29764
|
+
}
|
|
29765
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
29766
|
+
tags: { deviceId: id },
|
|
29767
|
+
meta: {
|
|
29768
|
+
stableId,
|
|
29769
|
+
type: meta.type
|
|
29770
|
+
}
|
|
29771
|
+
});
|
|
29772
|
+
return { deviceId: id };
|
|
29446
29773
|
}
|
|
29447
29774
|
/**
|
|
29448
29775
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -29468,55 +29795,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
29468
29795
|
* accessory-spawn flow handles via the parent's
|
|
29469
29796
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
29470
29797
|
* fit.
|
|
29798
|
+
*
|
|
29799
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
29800
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
29801
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
29802
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
29803
|
+
* `getStatus().error`.
|
|
29804
|
+
*/
|
|
29805
|
+
/**
|
|
29806
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
29807
|
+
* Default: no-op — most providers have nothing to heal.
|
|
29808
|
+
*
|
|
29809
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
29810
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
29811
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
29812
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
29813
|
+
* been emptied failed all four bounded attempts against fields
|
|
29814
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
29815
|
+
*
|
|
29816
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
29817
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
29818
|
+
* the bound, then reported — never swallowed.
|
|
29471
29819
|
*/
|
|
29820
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
29472
29821
|
async onRestoreDevices(savedDevices) {
|
|
29473
29822
|
const restored = /* @__PURE__ */ new Set();
|
|
29823
|
+
const failures = [];
|
|
29824
|
+
const attemptRestore = async (saved) => {
|
|
29825
|
+
if (restored.has(saved.id)) return;
|
|
29826
|
+
const Class = this.deviceClasses[saved.type];
|
|
29827
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
29828
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
29829
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
29830
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
29831
|
+
restored.add(saved.id);
|
|
29832
|
+
};
|
|
29474
29833
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
29475
29834
|
const restoreOne = async (saved) => {
|
|
29476
|
-
|
|
29477
|
-
if (!Class) {
|
|
29835
|
+
if (!this.deviceClasses[saved.type]) {
|
|
29478
29836
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
29479
|
-
tags: {
|
|
29837
|
+
tags: {
|
|
29838
|
+
deviceId: saved.id,
|
|
29839
|
+
stableId: saved.stableId
|
|
29840
|
+
},
|
|
29480
29841
|
meta: { type: saved.type }
|
|
29481
29842
|
});
|
|
29482
29843
|
return;
|
|
29483
29844
|
}
|
|
29484
29845
|
try {
|
|
29485
|
-
await
|
|
29486
|
-
restored.add(saved.id);
|
|
29846
|
+
await attemptRestore(saved);
|
|
29487
29847
|
} catch (err) {
|
|
29488
|
-
|
|
29489
|
-
|
|
29848
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
29849
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
29850
|
+
tags: {
|
|
29851
|
+
deviceId: saved.id,
|
|
29852
|
+
stableId: saved.stableId
|
|
29853
|
+
},
|
|
29490
29854
|
meta: {
|
|
29491
29855
|
type: saved.type,
|
|
29492
|
-
|
|
29856
|
+
attempt: 1,
|
|
29857
|
+
error
|
|
29493
29858
|
}
|
|
29494
29859
|
});
|
|
29860
|
+
failures.push({
|
|
29861
|
+
saved,
|
|
29862
|
+
error
|
|
29863
|
+
});
|
|
29495
29864
|
}
|
|
29496
29865
|
};
|
|
29497
29866
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
29867
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
29498
29868
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
29499
29869
|
for (const saved of childRows) {
|
|
29500
|
-
|
|
29501
|
-
if (!Class) continue;
|
|
29870
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
29502
29871
|
if (saved.parentDeviceId === null) continue;
|
|
29503
|
-
if (
|
|
29504
|
-
|
|
29505
|
-
|
|
29506
|
-
|
|
29507
|
-
|
|
29508
|
-
|
|
29872
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
29873
|
+
try {
|
|
29874
|
+
await attemptRestore(saved);
|
|
29875
|
+
} catch (err) {
|
|
29876
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
29877
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
29878
|
+
tags: {
|
|
29879
|
+
deviceId: saved.id,
|
|
29880
|
+
stableId: saved.stableId,
|
|
29881
|
+
parentDeviceId: saved.parentDeviceId
|
|
29882
|
+
},
|
|
29883
|
+
meta: {
|
|
29884
|
+
type: saved.type,
|
|
29885
|
+
attempt: 1,
|
|
29886
|
+
error
|
|
29887
|
+
}
|
|
29888
|
+
});
|
|
29889
|
+
failures.push({
|
|
29890
|
+
saved,
|
|
29891
|
+
error
|
|
29892
|
+
});
|
|
29893
|
+
}
|
|
29894
|
+
continue;
|
|
29895
|
+
}
|
|
29896
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
29897
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
29509
29898
|
tags: {
|
|
29899
|
+
deviceId: saved.id,
|
|
29510
29900
|
stableId: saved.stableId,
|
|
29511
29901
|
parentDeviceId: saved.parentDeviceId
|
|
29512
29902
|
},
|
|
29513
|
-
meta: {
|
|
29514
|
-
|
|
29515
|
-
|
|
29516
|
-
|
|
29903
|
+
meta: { type: saved.type }
|
|
29904
|
+
});
|
|
29905
|
+
failures.push({
|
|
29906
|
+
saved,
|
|
29907
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
29517
29908
|
});
|
|
29909
|
+
continue;
|
|
29518
29910
|
}
|
|
29519
29911
|
}
|
|
29912
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
29913
|
+
return {
|
|
29914
|
+
restoredCount: restored.size,
|
|
29915
|
+
failedCount: failures.length
|
|
29916
|
+
};
|
|
29520
29917
|
}
|
|
29521
29918
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
29522
29919
|
toSummary(device) {
|
|
@@ -31345,6 +31742,12 @@ Object.freeze({
|
|
|
31345
31742
|
addonId: null,
|
|
31346
31743
|
access: "view"
|
|
31347
31744
|
},
|
|
31745
|
+
"deviceProvider.reloadDevice": {
|
|
31746
|
+
capName: "device-provider",
|
|
31747
|
+
capScope: "system",
|
|
31748
|
+
addonId: null,
|
|
31749
|
+
access: "create"
|
|
31750
|
+
},
|
|
31348
31751
|
"deviceProvider.start": {
|
|
31349
31752
|
capName: "device-provider",
|
|
31350
31753
|
capScope: "system",
|