@camstack/addon-provider-petkit 0.2.59 → 0.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 +391 -24
- package/dist/addon.mjs +391 -24
- package/package.json +1 -1
package/dist/addon.js
CHANGED
|
@@ -13931,6 +13931,35 @@ var deviceProviderCapability = {
|
|
|
13931
13931
|
name: string(),
|
|
13932
13932
|
type: string()
|
|
13933
13933
|
}))),
|
|
13934
|
+
/**
|
|
13935
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13936
|
+
* touching no other device this provider owns.
|
|
13937
|
+
*
|
|
13938
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13939
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13940
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13941
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13942
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13943
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13944
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13945
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13946
|
+
*
|
|
13947
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13948
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13949
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13950
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13951
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13952
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13953
|
+
* against the wrong camera (D8).
|
|
13954
|
+
*
|
|
13955
|
+
* Construction can dial hardware, and the migrated source is
|
|
13956
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13957
|
+
* rather than the 60 s default.
|
|
13958
|
+
*/
|
|
13959
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13960
|
+
kind: "mutation",
|
|
13961
|
+
timeoutMs: 3 * 6e4
|
|
13962
|
+
}),
|
|
13934
13963
|
supportsDiscovery: method(object({}), boolean()),
|
|
13935
13964
|
/**
|
|
13936
13965
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -14258,7 +14287,8 @@ method(object({
|
|
|
14258
14287
|
targetId: number()
|
|
14259
14288
|
}), MigrateDeviceResultSchema, {
|
|
14260
14289
|
kind: "mutation",
|
|
14261
|
-
auth: "admin"
|
|
14290
|
+
auth: "admin",
|
|
14291
|
+
timeoutMs: 12 * 6e4
|
|
14262
14292
|
}), 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({
|
|
14263
14293
|
deviceId: number(),
|
|
14264
14294
|
name: string()
|
|
@@ -33622,6 +33652,147 @@ var BaseDevice = class {
|
|
|
33622
33652
|
}
|
|
33623
33653
|
};
|
|
33624
33654
|
/**
|
|
33655
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33656
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33657
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33658
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33659
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33660
|
+
*/
|
|
33661
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33662
|
+
1e4,
|
|
33663
|
+
3e4,
|
|
33664
|
+
9e4
|
|
33665
|
+
];
|
|
33666
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33667
|
+
function sleep$1(ms, signal) {
|
|
33668
|
+
return new Promise((resolve) => {
|
|
33669
|
+
if (signal.aborted) {
|
|
33670
|
+
resolve();
|
|
33671
|
+
return;
|
|
33672
|
+
}
|
|
33673
|
+
const onAbort = () => {
|
|
33674
|
+
clearTimeout(timer);
|
|
33675
|
+
resolve();
|
|
33676
|
+
};
|
|
33677
|
+
const timer = setTimeout(() => {
|
|
33678
|
+
signal.removeEventListener("abort", onAbort);
|
|
33679
|
+
resolve();
|
|
33680
|
+
}, ms);
|
|
33681
|
+
timer.unref?.();
|
|
33682
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33683
|
+
});
|
|
33684
|
+
}
|
|
33685
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33686
|
+
* not reject (callers wrap their own try/catch). */
|
|
33687
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33688
|
+
const queue = [...items];
|
|
33689
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33690
|
+
const lane = async () => {
|
|
33691
|
+
for (;;) {
|
|
33692
|
+
const item = queue.shift();
|
|
33693
|
+
if (item === void 0) return;
|
|
33694
|
+
await fn(item);
|
|
33695
|
+
}
|
|
33696
|
+
};
|
|
33697
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33698
|
+
}
|
|
33699
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33700
|
+
#logger;
|
|
33701
|
+
#attempt;
|
|
33702
|
+
#onPermanentFailure;
|
|
33703
|
+
#delaysMs;
|
|
33704
|
+
#concurrency;
|
|
33705
|
+
#now;
|
|
33706
|
+
#abort = new AbortController();
|
|
33707
|
+
constructor(options) {
|
|
33708
|
+
this.#logger = options.logger;
|
|
33709
|
+
this.#attempt = options.attempt;
|
|
33710
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33711
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33712
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33713
|
+
this.#now = options.now ?? Date.now;
|
|
33714
|
+
}
|
|
33715
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33716
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33717
|
+
cancel() {
|
|
33718
|
+
this.#abort.abort();
|
|
33719
|
+
}
|
|
33720
|
+
/**
|
|
33721
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33722
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33723
|
+
* cancelled. Never rejects.
|
|
33724
|
+
*/
|
|
33725
|
+
async run(initialFailures) {
|
|
33726
|
+
let pending = initialFailures.map((failure) => ({
|
|
33727
|
+
saved: failure.saved,
|
|
33728
|
+
lastError: failure.error,
|
|
33729
|
+
attempts: 1
|
|
33730
|
+
}));
|
|
33731
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33732
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33733
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33734
|
+
if (this.#abort.signal.aborted) break;
|
|
33735
|
+
pending = await this.#runRound(pending, round);
|
|
33736
|
+
}
|
|
33737
|
+
if (this.#abort.signal.aborted) return [];
|
|
33738
|
+
const terminal = pending.map((entry) => ({
|
|
33739
|
+
deviceId: entry.saved.id,
|
|
33740
|
+
stableId: entry.saved.stableId,
|
|
33741
|
+
type: String(entry.saved.type),
|
|
33742
|
+
attempts: entry.attempts,
|
|
33743
|
+
lastError: entry.lastError,
|
|
33744
|
+
failedAt: this.#now()
|
|
33745
|
+
}));
|
|
33746
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33747
|
+
return terminal;
|
|
33748
|
+
}
|
|
33749
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33750
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33751
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33752
|
+
async #runRound(pending, round) {
|
|
33753
|
+
const next = [];
|
|
33754
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33755
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33756
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33757
|
+
if (this.#abort.signal.aborted) {
|
|
33758
|
+
next.push(entry);
|
|
33759
|
+
return;
|
|
33760
|
+
}
|
|
33761
|
+
const attemptNo = entry.attempts + 1;
|
|
33762
|
+
try {
|
|
33763
|
+
await this.#attempt(entry.saved);
|
|
33764
|
+
this.#logger.info("Device restored on retry", {
|
|
33765
|
+
tags: {
|
|
33766
|
+
deviceId: entry.saved.id,
|
|
33767
|
+
stableId: entry.saved.stableId
|
|
33768
|
+
},
|
|
33769
|
+
meta: { attempt: attemptNo }
|
|
33770
|
+
});
|
|
33771
|
+
} catch (err) {
|
|
33772
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33773
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33774
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33775
|
+
tags: {
|
|
33776
|
+
deviceId: entry.saved.id,
|
|
33777
|
+
stableId: entry.saved.stableId
|
|
33778
|
+
},
|
|
33779
|
+
meta: {
|
|
33780
|
+
attempt: attemptNo,
|
|
33781
|
+
remainingRetries,
|
|
33782
|
+
error: lastError
|
|
33783
|
+
}
|
|
33784
|
+
});
|
|
33785
|
+
next.push({
|
|
33786
|
+
saved: entry.saved,
|
|
33787
|
+
lastError,
|
|
33788
|
+
attempts: attemptNo
|
|
33789
|
+
});
|
|
33790
|
+
}
|
|
33791
|
+
});
|
|
33792
|
+
return next;
|
|
33793
|
+
}
|
|
33794
|
+
};
|
|
33795
|
+
/**
|
|
33625
33796
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33626
33797
|
* device-provider cap router. Shared across all providers.
|
|
33627
33798
|
*/
|
|
@@ -33670,6 +33841,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33670
33841
|
}];
|
|
33671
33842
|
}
|
|
33672
33843
|
async onShutdown() {
|
|
33844
|
+
this.cancelRestoreRetries();
|
|
33673
33845
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33674
33846
|
for (const device of devices) try {
|
|
33675
33847
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33687,9 +33859,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33687
33859
|
async start() {}
|
|
33688
33860
|
async stop() {}
|
|
33689
33861
|
async getStatus() {
|
|
33862
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33863
|
+
const summary = this.restoreFailureSummary();
|
|
33864
|
+
if (summary === null) return {
|
|
33865
|
+
connected: true,
|
|
33866
|
+
deviceCount: all.length
|
|
33867
|
+
};
|
|
33690
33868
|
return {
|
|
33691
33869
|
connected: true,
|
|
33692
|
-
deviceCount:
|
|
33870
|
+
deviceCount: all.length,
|
|
33871
|
+
error: summary
|
|
33693
33872
|
};
|
|
33694
33873
|
}
|
|
33695
33874
|
async getDevices() {
|
|
@@ -33779,8 +33958,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33779
33958
|
};
|
|
33780
33959
|
}
|
|
33781
33960
|
async restoreDevices(savedDevices) {
|
|
33782
|
-
await this.onRestoreDevices(savedDevices);
|
|
33783
|
-
if (savedDevices.length
|
|
33961
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33962
|
+
if (savedDevices.length === 0) return;
|
|
33963
|
+
if (report && report.failedCount > 0) {
|
|
33964
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33965
|
+
return;
|
|
33966
|
+
}
|
|
33967
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33968
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33969
|
+
}
|
|
33970
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33971
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33972
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33973
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33974
|
+
restoreRetryConcurrency = 4;
|
|
33975
|
+
_restoreRetryScheduler = null;
|
|
33976
|
+
_restoreRetryCompletion = null;
|
|
33977
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33978
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33979
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33980
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33981
|
+
* with the devices that restored, and a late success is announced
|
|
33982
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33983
|
+
get restoreRetryCompletion() {
|
|
33984
|
+
return this._restoreRetryCompletion;
|
|
33985
|
+
}
|
|
33986
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33987
|
+
get permanentRestoreFailures() {
|
|
33988
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33989
|
+
}
|
|
33990
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33991
|
+
* `null` when every device restored. */
|
|
33992
|
+
restoreFailureSummary() {
|
|
33993
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33994
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33995
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33996
|
+
}
|
|
33997
|
+
cancelRestoreRetries() {
|
|
33998
|
+
this._restoreRetryScheduler?.cancel();
|
|
33999
|
+
this._restoreRetryScheduler = null;
|
|
34000
|
+
}
|
|
34001
|
+
recordPermanentRestoreFailure(failure) {
|
|
34002
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
34003
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
34004
|
+
tags: {
|
|
34005
|
+
deviceId: failure.deviceId,
|
|
34006
|
+
stableId: failure.stableId
|
|
34007
|
+
},
|
|
34008
|
+
meta: {
|
|
34009
|
+
type: failure.type,
|
|
34010
|
+
attempts: failure.attempts,
|
|
34011
|
+
error: failure.lastError
|
|
34012
|
+
}
|
|
34013
|
+
});
|
|
34014
|
+
}
|
|
34015
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
34016
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
34017
|
+
logger: this.ctx.logger,
|
|
34018
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
34019
|
+
concurrency: this.restoreRetryConcurrency,
|
|
34020
|
+
attempt,
|
|
34021
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
34022
|
+
});
|
|
34023
|
+
this._restoreRetryScheduler = scheduler;
|
|
34024
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
34025
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
34026
|
+
});
|
|
34027
|
+
}
|
|
34028
|
+
/**
|
|
34029
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
34030
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
34031
|
+
* and no other device this provider owns is disturbed.
|
|
34032
|
+
*
|
|
34033
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
34034
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
34035
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
34036
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
34037
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
34038
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
34039
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
34040
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
34041
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
34042
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
34043
|
+
*
|
|
34044
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
34045
|
+
* reload its parent instead.
|
|
34046
|
+
*/
|
|
34047
|
+
async reloadDevice(input) {
|
|
34048
|
+
const { stableId } = input;
|
|
34049
|
+
const devices = this.ctx.kernel.devices;
|
|
34050
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
34051
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
34052
|
+
if (live) await devices.decommission(live.id);
|
|
34053
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
34054
|
+
addonId: this.addonId,
|
|
34055
|
+
stableId
|
|
34056
|
+
});
|
|
34057
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
34058
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
34059
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
34060
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
34061
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
34062
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
34063
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
34064
|
+
for (const row of rows) {
|
|
34065
|
+
if (row.parentDeviceId !== id) continue;
|
|
34066
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
34067
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
34068
|
+
if (!ChildClass) continue;
|
|
34069
|
+
try {
|
|
34070
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
34071
|
+
} catch (err) {
|
|
34072
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
34073
|
+
tags: {
|
|
34074
|
+
deviceId: row.id,
|
|
34075
|
+
stableId: row.stableId
|
|
34076
|
+
},
|
|
34077
|
+
meta: {
|
|
34078
|
+
parentDeviceId: id,
|
|
34079
|
+
error: err instanceof Error ? err.message : String(err)
|
|
34080
|
+
}
|
|
34081
|
+
});
|
|
34082
|
+
}
|
|
34083
|
+
}
|
|
34084
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
34085
|
+
tags: { deviceId: id },
|
|
34086
|
+
meta: {
|
|
34087
|
+
stableId,
|
|
34088
|
+
type: meta.type
|
|
34089
|
+
}
|
|
34090
|
+
});
|
|
34091
|
+
return { deviceId: id };
|
|
33784
34092
|
}
|
|
33785
34093
|
/**
|
|
33786
34094
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33806,55 +34114,108 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33806
34114
|
* accessory-spawn flow handles via the parent's
|
|
33807
34115
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33808
34116
|
* fit.
|
|
34117
|
+
*
|
|
34118
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
34119
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
34120
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
34121
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
34122
|
+
* `getStatus().error`.
|
|
33809
34123
|
*/
|
|
33810
34124
|
async onRestoreDevices(savedDevices) {
|
|
33811
34125
|
const restored = /* @__PURE__ */ new Set();
|
|
34126
|
+
const failures = [];
|
|
34127
|
+
const attemptRestore = async (saved) => {
|
|
34128
|
+
if (restored.has(saved.id)) return;
|
|
34129
|
+
const Class = this.deviceClasses[saved.type];
|
|
34130
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
34131
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
34132
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
34133
|
+
restored.add(saved.id);
|
|
34134
|
+
};
|
|
33812
34135
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33813
34136
|
const restoreOne = async (saved) => {
|
|
33814
|
-
|
|
33815
|
-
if (!Class) {
|
|
34137
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33816
34138
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33817
|
-
tags: {
|
|
34139
|
+
tags: {
|
|
34140
|
+
deviceId: saved.id,
|
|
34141
|
+
stableId: saved.stableId
|
|
34142
|
+
},
|
|
33818
34143
|
meta: { type: saved.type }
|
|
33819
34144
|
});
|
|
33820
34145
|
return;
|
|
33821
34146
|
}
|
|
33822
34147
|
try {
|
|
33823
|
-
await
|
|
33824
|
-
restored.add(saved.id);
|
|
34148
|
+
await attemptRestore(saved);
|
|
33825
34149
|
} catch (err) {
|
|
33826
|
-
|
|
33827
|
-
|
|
34150
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34151
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
34152
|
+
tags: {
|
|
34153
|
+
deviceId: saved.id,
|
|
34154
|
+
stableId: saved.stableId
|
|
34155
|
+
},
|
|
33828
34156
|
meta: {
|
|
33829
34157
|
type: saved.type,
|
|
33830
|
-
|
|
34158
|
+
attempt: 1,
|
|
34159
|
+
error
|
|
33831
34160
|
}
|
|
33832
34161
|
});
|
|
34162
|
+
failures.push({
|
|
34163
|
+
saved,
|
|
34164
|
+
error
|
|
34165
|
+
});
|
|
33833
34166
|
}
|
|
33834
34167
|
};
|
|
33835
34168
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
34169
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33836
34170
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33837
34171
|
for (const saved of childRows) {
|
|
33838
|
-
|
|
33839
|
-
if (!Class) continue;
|
|
34172
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33840
34173
|
if (saved.parentDeviceId === null) continue;
|
|
33841
|
-
if (
|
|
33842
|
-
|
|
33843
|
-
|
|
33844
|
-
|
|
33845
|
-
|
|
33846
|
-
|
|
34174
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
34175
|
+
try {
|
|
34176
|
+
await attemptRestore(saved);
|
|
34177
|
+
} catch (err) {
|
|
34178
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34179
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
34180
|
+
tags: {
|
|
34181
|
+
deviceId: saved.id,
|
|
34182
|
+
stableId: saved.stableId,
|
|
34183
|
+
parentDeviceId: saved.parentDeviceId
|
|
34184
|
+
},
|
|
34185
|
+
meta: {
|
|
34186
|
+
type: saved.type,
|
|
34187
|
+
attempt: 1,
|
|
34188
|
+
error
|
|
34189
|
+
}
|
|
34190
|
+
});
|
|
34191
|
+
failures.push({
|
|
34192
|
+
saved,
|
|
34193
|
+
error
|
|
34194
|
+
});
|
|
34195
|
+
}
|
|
34196
|
+
continue;
|
|
34197
|
+
}
|
|
34198
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
34199
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33847
34200
|
tags: {
|
|
34201
|
+
deviceId: saved.id,
|
|
33848
34202
|
stableId: saved.stableId,
|
|
33849
34203
|
parentDeviceId: saved.parentDeviceId
|
|
33850
34204
|
},
|
|
33851
|
-
meta: {
|
|
33852
|
-
type: saved.type,
|
|
33853
|
-
error: err instanceof Error ? err.message : String(err)
|
|
33854
|
-
}
|
|
34205
|
+
meta: { type: saved.type }
|
|
33855
34206
|
});
|
|
34207
|
+
failures.push({
|
|
34208
|
+
saved,
|
|
34209
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
34210
|
+
});
|
|
34211
|
+
continue;
|
|
33856
34212
|
}
|
|
33857
34213
|
}
|
|
34214
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
34215
|
+
return {
|
|
34216
|
+
restoredCount: restored.size,
|
|
34217
|
+
failedCount: failures.length
|
|
34218
|
+
};
|
|
33858
34219
|
}
|
|
33859
34220
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33860
34221
|
toSummary(device) {
|
|
@@ -35617,6 +35978,12 @@ Object.freeze({
|
|
|
35617
35978
|
addonId: null,
|
|
35618
35979
|
access: "view"
|
|
35619
35980
|
},
|
|
35981
|
+
"deviceProvider.reloadDevice": {
|
|
35982
|
+
capName: "device-provider",
|
|
35983
|
+
capScope: "system",
|
|
35984
|
+
addonId: null,
|
|
35985
|
+
access: "create"
|
|
35986
|
+
},
|
|
35620
35987
|
"deviceProvider.start": {
|
|
35621
35988
|
capName: "device-provider",
|
|
35622
35989
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -13930,6 +13930,35 @@ var deviceProviderCapability = {
|
|
|
13930
13930
|
name: string(),
|
|
13931
13931
|
type: string()
|
|
13932
13932
|
}))),
|
|
13933
|
+
/**
|
|
13934
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13935
|
+
* touching no other device this provider owns.
|
|
13936
|
+
*
|
|
13937
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13938
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13939
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13940
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13941
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13942
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13943
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13944
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13945
|
+
*
|
|
13946
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13947
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13948
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13949
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13950
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13951
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13952
|
+
* against the wrong camera (D8).
|
|
13953
|
+
*
|
|
13954
|
+
* Construction can dial hardware, and the migrated source is
|
|
13955
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13956
|
+
* rather than the 60 s default.
|
|
13957
|
+
*/
|
|
13958
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13959
|
+
kind: "mutation",
|
|
13960
|
+
timeoutMs: 3 * 6e4
|
|
13961
|
+
}),
|
|
13933
13962
|
supportsDiscovery: method(object({}), boolean()),
|
|
13934
13963
|
/**
|
|
13935
13964
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -14257,7 +14286,8 @@ method(object({
|
|
|
14257
14286
|
targetId: number()
|
|
14258
14287
|
}), MigrateDeviceResultSchema, {
|
|
14259
14288
|
kind: "mutation",
|
|
14260
|
-
auth: "admin"
|
|
14289
|
+
auth: "admin",
|
|
14290
|
+
timeoutMs: 12 * 6e4
|
|
14261
14291
|
}), 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({
|
|
14262
14292
|
deviceId: number(),
|
|
14263
14293
|
name: string()
|
|
@@ -33621,6 +33651,147 @@ var BaseDevice = class {
|
|
|
33621
33651
|
}
|
|
33622
33652
|
};
|
|
33623
33653
|
/**
|
|
33654
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33655
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33656
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33657
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33658
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33659
|
+
*/
|
|
33660
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33661
|
+
1e4,
|
|
33662
|
+
3e4,
|
|
33663
|
+
9e4
|
|
33664
|
+
];
|
|
33665
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33666
|
+
function sleep$1(ms, signal) {
|
|
33667
|
+
return new Promise((resolve) => {
|
|
33668
|
+
if (signal.aborted) {
|
|
33669
|
+
resolve();
|
|
33670
|
+
return;
|
|
33671
|
+
}
|
|
33672
|
+
const onAbort = () => {
|
|
33673
|
+
clearTimeout(timer);
|
|
33674
|
+
resolve();
|
|
33675
|
+
};
|
|
33676
|
+
const timer = setTimeout(() => {
|
|
33677
|
+
signal.removeEventListener("abort", onAbort);
|
|
33678
|
+
resolve();
|
|
33679
|
+
}, ms);
|
|
33680
|
+
timer.unref?.();
|
|
33681
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33682
|
+
});
|
|
33683
|
+
}
|
|
33684
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33685
|
+
* not reject (callers wrap their own try/catch). */
|
|
33686
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33687
|
+
const queue = [...items];
|
|
33688
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33689
|
+
const lane = async () => {
|
|
33690
|
+
for (;;) {
|
|
33691
|
+
const item = queue.shift();
|
|
33692
|
+
if (item === void 0) return;
|
|
33693
|
+
await fn(item);
|
|
33694
|
+
}
|
|
33695
|
+
};
|
|
33696
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33697
|
+
}
|
|
33698
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33699
|
+
#logger;
|
|
33700
|
+
#attempt;
|
|
33701
|
+
#onPermanentFailure;
|
|
33702
|
+
#delaysMs;
|
|
33703
|
+
#concurrency;
|
|
33704
|
+
#now;
|
|
33705
|
+
#abort = new AbortController();
|
|
33706
|
+
constructor(options) {
|
|
33707
|
+
this.#logger = options.logger;
|
|
33708
|
+
this.#attempt = options.attempt;
|
|
33709
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33710
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33711
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33712
|
+
this.#now = options.now ?? Date.now;
|
|
33713
|
+
}
|
|
33714
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33715
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33716
|
+
cancel() {
|
|
33717
|
+
this.#abort.abort();
|
|
33718
|
+
}
|
|
33719
|
+
/**
|
|
33720
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33721
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33722
|
+
* cancelled. Never rejects.
|
|
33723
|
+
*/
|
|
33724
|
+
async run(initialFailures) {
|
|
33725
|
+
let pending = initialFailures.map((failure) => ({
|
|
33726
|
+
saved: failure.saved,
|
|
33727
|
+
lastError: failure.error,
|
|
33728
|
+
attempts: 1
|
|
33729
|
+
}));
|
|
33730
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33731
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33732
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33733
|
+
if (this.#abort.signal.aborted) break;
|
|
33734
|
+
pending = await this.#runRound(pending, round);
|
|
33735
|
+
}
|
|
33736
|
+
if (this.#abort.signal.aborted) return [];
|
|
33737
|
+
const terminal = pending.map((entry) => ({
|
|
33738
|
+
deviceId: entry.saved.id,
|
|
33739
|
+
stableId: entry.saved.stableId,
|
|
33740
|
+
type: String(entry.saved.type),
|
|
33741
|
+
attempts: entry.attempts,
|
|
33742
|
+
lastError: entry.lastError,
|
|
33743
|
+
failedAt: this.#now()
|
|
33744
|
+
}));
|
|
33745
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33746
|
+
return terminal;
|
|
33747
|
+
}
|
|
33748
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33749
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33750
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33751
|
+
async #runRound(pending, round) {
|
|
33752
|
+
const next = [];
|
|
33753
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33754
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33755
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33756
|
+
if (this.#abort.signal.aborted) {
|
|
33757
|
+
next.push(entry);
|
|
33758
|
+
return;
|
|
33759
|
+
}
|
|
33760
|
+
const attemptNo = entry.attempts + 1;
|
|
33761
|
+
try {
|
|
33762
|
+
await this.#attempt(entry.saved);
|
|
33763
|
+
this.#logger.info("Device restored on retry", {
|
|
33764
|
+
tags: {
|
|
33765
|
+
deviceId: entry.saved.id,
|
|
33766
|
+
stableId: entry.saved.stableId
|
|
33767
|
+
},
|
|
33768
|
+
meta: { attempt: attemptNo }
|
|
33769
|
+
});
|
|
33770
|
+
} catch (err) {
|
|
33771
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33772
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33773
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33774
|
+
tags: {
|
|
33775
|
+
deviceId: entry.saved.id,
|
|
33776
|
+
stableId: entry.saved.stableId
|
|
33777
|
+
},
|
|
33778
|
+
meta: {
|
|
33779
|
+
attempt: attemptNo,
|
|
33780
|
+
remainingRetries,
|
|
33781
|
+
error: lastError
|
|
33782
|
+
}
|
|
33783
|
+
});
|
|
33784
|
+
next.push({
|
|
33785
|
+
saved: entry.saved,
|
|
33786
|
+
lastError,
|
|
33787
|
+
attempts: attemptNo
|
|
33788
|
+
});
|
|
33789
|
+
}
|
|
33790
|
+
});
|
|
33791
|
+
return next;
|
|
33792
|
+
}
|
|
33793
|
+
};
|
|
33794
|
+
/**
|
|
33624
33795
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33625
33796
|
* device-provider cap router. Shared across all providers.
|
|
33626
33797
|
*/
|
|
@@ -33669,6 +33840,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33669
33840
|
}];
|
|
33670
33841
|
}
|
|
33671
33842
|
async onShutdown() {
|
|
33843
|
+
this.cancelRestoreRetries();
|
|
33672
33844
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33673
33845
|
for (const device of devices) try {
|
|
33674
33846
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33686,9 +33858,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33686
33858
|
async start() {}
|
|
33687
33859
|
async stop() {}
|
|
33688
33860
|
async getStatus() {
|
|
33861
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33862
|
+
const summary = this.restoreFailureSummary();
|
|
33863
|
+
if (summary === null) return {
|
|
33864
|
+
connected: true,
|
|
33865
|
+
deviceCount: all.length
|
|
33866
|
+
};
|
|
33689
33867
|
return {
|
|
33690
33868
|
connected: true,
|
|
33691
|
-
deviceCount:
|
|
33869
|
+
deviceCount: all.length,
|
|
33870
|
+
error: summary
|
|
33692
33871
|
};
|
|
33693
33872
|
}
|
|
33694
33873
|
async getDevices() {
|
|
@@ -33778,8 +33957,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33778
33957
|
};
|
|
33779
33958
|
}
|
|
33780
33959
|
async restoreDevices(savedDevices) {
|
|
33781
|
-
await this.onRestoreDevices(savedDevices);
|
|
33782
|
-
if (savedDevices.length
|
|
33960
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33961
|
+
if (savedDevices.length === 0) return;
|
|
33962
|
+
if (report && report.failedCount > 0) {
|
|
33963
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33964
|
+
return;
|
|
33965
|
+
}
|
|
33966
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33967
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33968
|
+
}
|
|
33969
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33970
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33971
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33972
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33973
|
+
restoreRetryConcurrency = 4;
|
|
33974
|
+
_restoreRetryScheduler = null;
|
|
33975
|
+
_restoreRetryCompletion = null;
|
|
33976
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33977
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33978
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33979
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33980
|
+
* with the devices that restored, and a late success is announced
|
|
33981
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33982
|
+
get restoreRetryCompletion() {
|
|
33983
|
+
return this._restoreRetryCompletion;
|
|
33984
|
+
}
|
|
33985
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33986
|
+
get permanentRestoreFailures() {
|
|
33987
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33988
|
+
}
|
|
33989
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33990
|
+
* `null` when every device restored. */
|
|
33991
|
+
restoreFailureSummary() {
|
|
33992
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33993
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33994
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33995
|
+
}
|
|
33996
|
+
cancelRestoreRetries() {
|
|
33997
|
+
this._restoreRetryScheduler?.cancel();
|
|
33998
|
+
this._restoreRetryScheduler = null;
|
|
33999
|
+
}
|
|
34000
|
+
recordPermanentRestoreFailure(failure) {
|
|
34001
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
34002
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
34003
|
+
tags: {
|
|
34004
|
+
deviceId: failure.deviceId,
|
|
34005
|
+
stableId: failure.stableId
|
|
34006
|
+
},
|
|
34007
|
+
meta: {
|
|
34008
|
+
type: failure.type,
|
|
34009
|
+
attempts: failure.attempts,
|
|
34010
|
+
error: failure.lastError
|
|
34011
|
+
}
|
|
34012
|
+
});
|
|
34013
|
+
}
|
|
34014
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
34015
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
34016
|
+
logger: this.ctx.logger,
|
|
34017
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
34018
|
+
concurrency: this.restoreRetryConcurrency,
|
|
34019
|
+
attempt,
|
|
34020
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
34021
|
+
});
|
|
34022
|
+
this._restoreRetryScheduler = scheduler;
|
|
34023
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
34024
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
34025
|
+
});
|
|
34026
|
+
}
|
|
34027
|
+
/**
|
|
34028
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
34029
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
34030
|
+
* and no other device this provider owns is disturbed.
|
|
34031
|
+
*
|
|
34032
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
34033
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
34034
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
34035
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
34036
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
34037
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
34038
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
34039
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
34040
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
34041
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
34042
|
+
*
|
|
34043
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
34044
|
+
* reload its parent instead.
|
|
34045
|
+
*/
|
|
34046
|
+
async reloadDevice(input) {
|
|
34047
|
+
const { stableId } = input;
|
|
34048
|
+
const devices = this.ctx.kernel.devices;
|
|
34049
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
34050
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
34051
|
+
if (live) await devices.decommission(live.id);
|
|
34052
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
34053
|
+
addonId: this.addonId,
|
|
34054
|
+
stableId
|
|
34055
|
+
});
|
|
34056
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
34057
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
34058
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
34059
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
34060
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
34061
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
34062
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
34063
|
+
for (const row of rows) {
|
|
34064
|
+
if (row.parentDeviceId !== id) continue;
|
|
34065
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
34066
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
34067
|
+
if (!ChildClass) continue;
|
|
34068
|
+
try {
|
|
34069
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
34070
|
+
} catch (err) {
|
|
34071
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
34072
|
+
tags: {
|
|
34073
|
+
deviceId: row.id,
|
|
34074
|
+
stableId: row.stableId
|
|
34075
|
+
},
|
|
34076
|
+
meta: {
|
|
34077
|
+
parentDeviceId: id,
|
|
34078
|
+
error: err instanceof Error ? err.message : String(err)
|
|
34079
|
+
}
|
|
34080
|
+
});
|
|
34081
|
+
}
|
|
34082
|
+
}
|
|
34083
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
34084
|
+
tags: { deviceId: id },
|
|
34085
|
+
meta: {
|
|
34086
|
+
stableId,
|
|
34087
|
+
type: meta.type
|
|
34088
|
+
}
|
|
34089
|
+
});
|
|
34090
|
+
return { deviceId: id };
|
|
33783
34091
|
}
|
|
33784
34092
|
/**
|
|
33785
34093
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33805,55 +34113,108 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33805
34113
|
* accessory-spawn flow handles via the parent's
|
|
33806
34114
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33807
34115
|
* fit.
|
|
34116
|
+
*
|
|
34117
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
34118
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
34119
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
34120
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
34121
|
+
* `getStatus().error`.
|
|
33808
34122
|
*/
|
|
33809
34123
|
async onRestoreDevices(savedDevices) {
|
|
33810
34124
|
const restored = /* @__PURE__ */ new Set();
|
|
34125
|
+
const failures = [];
|
|
34126
|
+
const attemptRestore = async (saved) => {
|
|
34127
|
+
if (restored.has(saved.id)) return;
|
|
34128
|
+
const Class = this.deviceClasses[saved.type];
|
|
34129
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
34130
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
34131
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
34132
|
+
restored.add(saved.id);
|
|
34133
|
+
};
|
|
33811
34134
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33812
34135
|
const restoreOne = async (saved) => {
|
|
33813
|
-
|
|
33814
|
-
if (!Class) {
|
|
34136
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33815
34137
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33816
|
-
tags: {
|
|
34138
|
+
tags: {
|
|
34139
|
+
deviceId: saved.id,
|
|
34140
|
+
stableId: saved.stableId
|
|
34141
|
+
},
|
|
33817
34142
|
meta: { type: saved.type }
|
|
33818
34143
|
});
|
|
33819
34144
|
return;
|
|
33820
34145
|
}
|
|
33821
34146
|
try {
|
|
33822
|
-
await
|
|
33823
|
-
restored.add(saved.id);
|
|
34147
|
+
await attemptRestore(saved);
|
|
33824
34148
|
} catch (err) {
|
|
33825
|
-
|
|
33826
|
-
|
|
34149
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34150
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
34151
|
+
tags: {
|
|
34152
|
+
deviceId: saved.id,
|
|
34153
|
+
stableId: saved.stableId
|
|
34154
|
+
},
|
|
33827
34155
|
meta: {
|
|
33828
34156
|
type: saved.type,
|
|
33829
|
-
|
|
34157
|
+
attempt: 1,
|
|
34158
|
+
error
|
|
33830
34159
|
}
|
|
33831
34160
|
});
|
|
34161
|
+
failures.push({
|
|
34162
|
+
saved,
|
|
34163
|
+
error
|
|
34164
|
+
});
|
|
33832
34165
|
}
|
|
33833
34166
|
};
|
|
33834
34167
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
34168
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33835
34169
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33836
34170
|
for (const saved of childRows) {
|
|
33837
|
-
|
|
33838
|
-
if (!Class) continue;
|
|
34171
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33839
34172
|
if (saved.parentDeviceId === null) continue;
|
|
33840
|
-
if (
|
|
33841
|
-
|
|
33842
|
-
|
|
33843
|
-
|
|
33844
|
-
|
|
33845
|
-
|
|
34173
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
34174
|
+
try {
|
|
34175
|
+
await attemptRestore(saved);
|
|
34176
|
+
} catch (err) {
|
|
34177
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34178
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
34179
|
+
tags: {
|
|
34180
|
+
deviceId: saved.id,
|
|
34181
|
+
stableId: saved.stableId,
|
|
34182
|
+
parentDeviceId: saved.parentDeviceId
|
|
34183
|
+
},
|
|
34184
|
+
meta: {
|
|
34185
|
+
type: saved.type,
|
|
34186
|
+
attempt: 1,
|
|
34187
|
+
error
|
|
34188
|
+
}
|
|
34189
|
+
});
|
|
34190
|
+
failures.push({
|
|
34191
|
+
saved,
|
|
34192
|
+
error
|
|
34193
|
+
});
|
|
34194
|
+
}
|
|
34195
|
+
continue;
|
|
34196
|
+
}
|
|
34197
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
34198
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33846
34199
|
tags: {
|
|
34200
|
+
deviceId: saved.id,
|
|
33847
34201
|
stableId: saved.stableId,
|
|
33848
34202
|
parentDeviceId: saved.parentDeviceId
|
|
33849
34203
|
},
|
|
33850
|
-
meta: {
|
|
33851
|
-
type: saved.type,
|
|
33852
|
-
error: err instanceof Error ? err.message : String(err)
|
|
33853
|
-
}
|
|
34204
|
+
meta: { type: saved.type }
|
|
33854
34205
|
});
|
|
34206
|
+
failures.push({
|
|
34207
|
+
saved,
|
|
34208
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
34209
|
+
});
|
|
34210
|
+
continue;
|
|
33855
34211
|
}
|
|
33856
34212
|
}
|
|
34213
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
34214
|
+
return {
|
|
34215
|
+
restoredCount: restored.size,
|
|
34216
|
+
failedCount: failures.length
|
|
34217
|
+
};
|
|
33857
34218
|
}
|
|
33858
34219
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33859
34220
|
toSummary(device) {
|
|
@@ -35616,6 +35977,12 @@ Object.freeze({
|
|
|
35616
35977
|
addonId: null,
|
|
35617
35978
|
access: "view"
|
|
35618
35979
|
},
|
|
35980
|
+
"deviceProvider.reloadDevice": {
|
|
35981
|
+
capName: "device-provider",
|
|
35982
|
+
capScope: "system",
|
|
35983
|
+
addonId: null,
|
|
35984
|
+
access: "create"
|
|
35985
|
+
},
|
|
35619
35986
|
"deviceProvider.start": {
|
|
35620
35987
|
capName: "device-provider",
|
|
35621
35988
|
capScope: "system",
|
package/package.json
CHANGED