@camstack/addon-provider-rtsp 1.2.59 → 1.2.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addon.js +428 -25
- package/dist/addon.mjs +428 -25
- package/package.json +1 -1
package/dist/addon.js
CHANGED
|
@@ -12812,7 +12812,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12812
12812
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12813
12813
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12814
12814
|
*/
|
|
12815
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12815
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12816
|
+
/**
|
|
12817
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12818
|
+
*
|
|
12819
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12820
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12821
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12822
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12823
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12824
|
+
* three child cameras offline for four hours.
|
|
12825
|
+
*
|
|
12826
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12827
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12828
|
+
* that cannot tell simply never sets it.
|
|
12829
|
+
*/
|
|
12830
|
+
alreadyOnboarded: boolean().optional(),
|
|
12831
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12832
|
+
onboardedDeviceId: number().optional(),
|
|
12833
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12834
|
+
onboardedName: string().optional()
|
|
12816
12835
|
});
|
|
12817
12836
|
/**
|
|
12818
12837
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12868,6 +12887,35 @@ var deviceProviderCapability = {
|
|
|
12868
12887
|
name: string(),
|
|
12869
12888
|
type: string()
|
|
12870
12889
|
}))),
|
|
12890
|
+
/**
|
|
12891
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12892
|
+
* touching no other device this provider owns.
|
|
12893
|
+
*
|
|
12894
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12895
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12896
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12897
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12898
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12899
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12900
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12901
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12902
|
+
*
|
|
12903
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12904
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12905
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12906
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12907
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12908
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12909
|
+
* against the wrong camera (D8).
|
|
12910
|
+
*
|
|
12911
|
+
* Construction can dial hardware, and the migrated source is
|
|
12912
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12913
|
+
* rather than the 60 s default.
|
|
12914
|
+
*/
|
|
12915
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12916
|
+
kind: "mutation",
|
|
12917
|
+
timeoutMs: 3 * 6e4
|
|
12918
|
+
}),
|
|
12871
12919
|
supportsDiscovery: method(object({}), boolean()),
|
|
12872
12920
|
/**
|
|
12873
12921
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13195,7 +13243,8 @@ method(object({
|
|
|
13195
13243
|
targetId: number()
|
|
13196
13244
|
}), MigrateDeviceResultSchema, {
|
|
13197
13245
|
kind: "mutation",
|
|
13198
|
-
auth: "admin"
|
|
13246
|
+
auth: "admin",
|
|
13247
|
+
timeoutMs: 12 * 6e4
|
|
13199
13248
|
}), 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({
|
|
13200
13249
|
deviceId: number(),
|
|
13201
13250
|
name: string()
|
|
@@ -32663,6 +32712,147 @@ var BaseDevice = class {
|
|
|
32663
32712
|
}
|
|
32664
32713
|
};
|
|
32665
32714
|
/**
|
|
32715
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
32716
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
32717
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
32718
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
32719
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
32720
|
+
*/
|
|
32721
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
32722
|
+
1e4,
|
|
32723
|
+
3e4,
|
|
32724
|
+
9e4
|
|
32725
|
+
];
|
|
32726
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
32727
|
+
function sleep$1(ms, signal) {
|
|
32728
|
+
return new Promise((resolve) => {
|
|
32729
|
+
if (signal.aborted) {
|
|
32730
|
+
resolve();
|
|
32731
|
+
return;
|
|
32732
|
+
}
|
|
32733
|
+
const onAbort = () => {
|
|
32734
|
+
clearTimeout(timer);
|
|
32735
|
+
resolve();
|
|
32736
|
+
};
|
|
32737
|
+
const timer = setTimeout(() => {
|
|
32738
|
+
signal.removeEventListener("abort", onAbort);
|
|
32739
|
+
resolve();
|
|
32740
|
+
}, ms);
|
|
32741
|
+
timer.unref?.();
|
|
32742
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
32743
|
+
});
|
|
32744
|
+
}
|
|
32745
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
32746
|
+
* not reject (callers wrap their own try/catch). */
|
|
32747
|
+
async function runWithConcurrency(items, width, fn) {
|
|
32748
|
+
const queue = [...items];
|
|
32749
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
32750
|
+
const lane = async () => {
|
|
32751
|
+
for (;;) {
|
|
32752
|
+
const item = queue.shift();
|
|
32753
|
+
if (item === void 0) return;
|
|
32754
|
+
await fn(item);
|
|
32755
|
+
}
|
|
32756
|
+
};
|
|
32757
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
32758
|
+
}
|
|
32759
|
+
var DeviceRestoreRetryScheduler = class {
|
|
32760
|
+
#logger;
|
|
32761
|
+
#attempt;
|
|
32762
|
+
#onPermanentFailure;
|
|
32763
|
+
#delaysMs;
|
|
32764
|
+
#concurrency;
|
|
32765
|
+
#now;
|
|
32766
|
+
#abort = new AbortController();
|
|
32767
|
+
constructor(options) {
|
|
32768
|
+
this.#logger = options.logger;
|
|
32769
|
+
this.#attempt = options.attempt;
|
|
32770
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
32771
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32772
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
32773
|
+
this.#now = options.now ?? Date.now;
|
|
32774
|
+
}
|
|
32775
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
32776
|
+
* permanently failed — the next boot restores them from disk. */
|
|
32777
|
+
cancel() {
|
|
32778
|
+
this.#abort.abort();
|
|
32779
|
+
}
|
|
32780
|
+
/**
|
|
32781
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
32782
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
32783
|
+
* cancelled. Never rejects.
|
|
32784
|
+
*/
|
|
32785
|
+
async run(initialFailures) {
|
|
32786
|
+
let pending = initialFailures.map((failure) => ({
|
|
32787
|
+
saved: failure.saved,
|
|
32788
|
+
lastError: failure.error,
|
|
32789
|
+
attempts: 1
|
|
32790
|
+
}));
|
|
32791
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
32792
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
32793
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
32794
|
+
if (this.#abort.signal.aborted) break;
|
|
32795
|
+
pending = await this.#runRound(pending, round);
|
|
32796
|
+
}
|
|
32797
|
+
if (this.#abort.signal.aborted) return [];
|
|
32798
|
+
const terminal = pending.map((entry) => ({
|
|
32799
|
+
deviceId: entry.saved.id,
|
|
32800
|
+
stableId: entry.saved.stableId,
|
|
32801
|
+
type: String(entry.saved.type),
|
|
32802
|
+
attempts: entry.attempts,
|
|
32803
|
+
lastError: entry.lastError,
|
|
32804
|
+
failedAt: this.#now()
|
|
32805
|
+
}));
|
|
32806
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
32807
|
+
return terminal;
|
|
32808
|
+
}
|
|
32809
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
32810
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
32811
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
32812
|
+
async #runRound(pending, round) {
|
|
32813
|
+
const next = [];
|
|
32814
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
32815
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
32816
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
32817
|
+
if (this.#abort.signal.aborted) {
|
|
32818
|
+
next.push(entry);
|
|
32819
|
+
return;
|
|
32820
|
+
}
|
|
32821
|
+
const attemptNo = entry.attempts + 1;
|
|
32822
|
+
try {
|
|
32823
|
+
await this.#attempt(entry.saved);
|
|
32824
|
+
this.#logger.info("Device restored on retry", {
|
|
32825
|
+
tags: {
|
|
32826
|
+
deviceId: entry.saved.id,
|
|
32827
|
+
stableId: entry.saved.stableId
|
|
32828
|
+
},
|
|
32829
|
+
meta: { attempt: attemptNo }
|
|
32830
|
+
});
|
|
32831
|
+
} catch (err) {
|
|
32832
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
32833
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
32834
|
+
this.#logger.warn("Device restore retry failed", {
|
|
32835
|
+
tags: {
|
|
32836
|
+
deviceId: entry.saved.id,
|
|
32837
|
+
stableId: entry.saved.stableId
|
|
32838
|
+
},
|
|
32839
|
+
meta: {
|
|
32840
|
+
attempt: attemptNo,
|
|
32841
|
+
remainingRetries,
|
|
32842
|
+
error: lastError
|
|
32843
|
+
}
|
|
32844
|
+
});
|
|
32845
|
+
next.push({
|
|
32846
|
+
saved: entry.saved,
|
|
32847
|
+
lastError,
|
|
32848
|
+
attempts: attemptNo
|
|
32849
|
+
});
|
|
32850
|
+
}
|
|
32851
|
+
});
|
|
32852
|
+
return next;
|
|
32853
|
+
}
|
|
32854
|
+
};
|
|
32855
|
+
/**
|
|
32666
32856
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
32667
32857
|
* device-provider cap router. Shared across all providers.
|
|
32668
32858
|
*/
|
|
@@ -32711,6 +32901,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32711
32901
|
}];
|
|
32712
32902
|
}
|
|
32713
32903
|
async onShutdown() {
|
|
32904
|
+
this.cancelRestoreRetries();
|
|
32714
32905
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32715
32906
|
for (const device of devices) try {
|
|
32716
32907
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -32728,9 +32919,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32728
32919
|
async start() {}
|
|
32729
32920
|
async stop() {}
|
|
32730
32921
|
async getStatus() {
|
|
32922
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32923
|
+
const summary = this.restoreFailureSummary();
|
|
32924
|
+
if (summary === null) return {
|
|
32925
|
+
connected: true,
|
|
32926
|
+
deviceCount: all.length
|
|
32927
|
+
};
|
|
32731
32928
|
return {
|
|
32732
32929
|
connected: true,
|
|
32733
|
-
deviceCount:
|
|
32930
|
+
deviceCount: all.length,
|
|
32931
|
+
error: summary
|
|
32734
32932
|
};
|
|
32735
32933
|
}
|
|
32736
32934
|
async getDevices() {
|
|
@@ -32820,8 +33018,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32820
33018
|
};
|
|
32821
33019
|
}
|
|
32822
33020
|
async restoreDevices(savedDevices) {
|
|
32823
|
-
await this.onRestoreDevices(savedDevices);
|
|
32824
|
-
if (savedDevices.length
|
|
33021
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33022
|
+
if (savedDevices.length === 0) return;
|
|
33023
|
+
if (report && report.failedCount > 0) {
|
|
33024
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33025
|
+
return;
|
|
33026
|
+
}
|
|
33027
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33028
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33029
|
+
}
|
|
33030
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33031
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33032
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33033
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33034
|
+
restoreRetryConcurrency = 4;
|
|
33035
|
+
_restoreRetryScheduler = null;
|
|
33036
|
+
_restoreRetryCompletion = null;
|
|
33037
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33038
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33039
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33040
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33041
|
+
* with the devices that restored, and a late success is announced
|
|
33042
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33043
|
+
get restoreRetryCompletion() {
|
|
33044
|
+
return this._restoreRetryCompletion;
|
|
33045
|
+
}
|
|
33046
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33047
|
+
get permanentRestoreFailures() {
|
|
33048
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33049
|
+
}
|
|
33050
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33051
|
+
* `null` when every device restored. */
|
|
33052
|
+
restoreFailureSummary() {
|
|
33053
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33054
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33055
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33056
|
+
}
|
|
33057
|
+
cancelRestoreRetries() {
|
|
33058
|
+
this._restoreRetryScheduler?.cancel();
|
|
33059
|
+
this._restoreRetryScheduler = null;
|
|
33060
|
+
}
|
|
33061
|
+
recordPermanentRestoreFailure(failure) {
|
|
33062
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33063
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33064
|
+
tags: {
|
|
33065
|
+
deviceId: failure.deviceId,
|
|
33066
|
+
stableId: failure.stableId
|
|
33067
|
+
},
|
|
33068
|
+
meta: {
|
|
33069
|
+
type: failure.type,
|
|
33070
|
+
attempts: failure.attempts,
|
|
33071
|
+
error: failure.lastError
|
|
33072
|
+
}
|
|
33073
|
+
});
|
|
33074
|
+
}
|
|
33075
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33076
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33077
|
+
logger: this.ctx.logger,
|
|
33078
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33079
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33080
|
+
attempt,
|
|
33081
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33082
|
+
});
|
|
33083
|
+
this._restoreRetryScheduler = scheduler;
|
|
33084
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33085
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33086
|
+
});
|
|
33087
|
+
}
|
|
33088
|
+
/**
|
|
33089
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33090
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33091
|
+
* and no other device this provider owns is disturbed.
|
|
33092
|
+
*
|
|
33093
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33094
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33095
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33096
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33097
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33098
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33099
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33100
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33101
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33102
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33103
|
+
*
|
|
33104
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33105
|
+
* reload its parent instead.
|
|
33106
|
+
*/
|
|
33107
|
+
async reloadDevice(input) {
|
|
33108
|
+
const { stableId } = input;
|
|
33109
|
+
const devices = this.ctx.kernel.devices;
|
|
33110
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33111
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33112
|
+
if (live) await devices.decommission(live.id);
|
|
33113
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33114
|
+
addonId: this.addonId,
|
|
33115
|
+
stableId
|
|
33116
|
+
});
|
|
33117
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33118
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33119
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33120
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33121
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33122
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33123
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33124
|
+
for (const row of rows) {
|
|
33125
|
+
if (row.parentDeviceId !== id) continue;
|
|
33126
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33127
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33128
|
+
if (!ChildClass) continue;
|
|
33129
|
+
try {
|
|
33130
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33131
|
+
} catch (err) {
|
|
33132
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33133
|
+
tags: {
|
|
33134
|
+
deviceId: row.id,
|
|
33135
|
+
stableId: row.stableId
|
|
33136
|
+
},
|
|
33137
|
+
meta: {
|
|
33138
|
+
parentDeviceId: id,
|
|
33139
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33140
|
+
}
|
|
33141
|
+
});
|
|
33142
|
+
}
|
|
33143
|
+
}
|
|
33144
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33145
|
+
tags: { deviceId: id },
|
|
33146
|
+
meta: {
|
|
33147
|
+
stableId,
|
|
33148
|
+
type: meta.type
|
|
33149
|
+
}
|
|
33150
|
+
});
|
|
33151
|
+
return { deviceId: id };
|
|
32825
33152
|
}
|
|
32826
33153
|
/**
|
|
32827
33154
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -32847,55 +33174,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32847
33174
|
* accessory-spawn flow handles via the parent's
|
|
32848
33175
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
32849
33176
|
* fit.
|
|
33177
|
+
*
|
|
33178
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33179
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33180
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33181
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33182
|
+
* `getStatus().error`.
|
|
32850
33183
|
*/
|
|
33184
|
+
/**
|
|
33185
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33186
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33187
|
+
*
|
|
33188
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33189
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33190
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33191
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33192
|
+
* been emptied failed all four bounded attempts against fields
|
|
33193
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33194
|
+
*
|
|
33195
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33196
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33197
|
+
* the bound, then reported — never swallowed.
|
|
33198
|
+
*/
|
|
33199
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
32851
33200
|
async onRestoreDevices(savedDevices) {
|
|
32852
33201
|
const restored = /* @__PURE__ */ new Set();
|
|
33202
|
+
const failures = [];
|
|
33203
|
+
const attemptRestore = async (saved) => {
|
|
33204
|
+
if (restored.has(saved.id)) return;
|
|
33205
|
+
const Class = this.deviceClasses[saved.type];
|
|
33206
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33207
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33208
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33209
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33210
|
+
restored.add(saved.id);
|
|
33211
|
+
};
|
|
32853
33212
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
32854
33213
|
const restoreOne = async (saved) => {
|
|
32855
|
-
|
|
32856
|
-
if (!Class) {
|
|
33214
|
+
if (!this.deviceClasses[saved.type]) {
|
|
32857
33215
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
32858
|
-
tags: {
|
|
33216
|
+
tags: {
|
|
33217
|
+
deviceId: saved.id,
|
|
33218
|
+
stableId: saved.stableId
|
|
33219
|
+
},
|
|
32859
33220
|
meta: { type: saved.type }
|
|
32860
33221
|
});
|
|
32861
33222
|
return;
|
|
32862
33223
|
}
|
|
32863
33224
|
try {
|
|
32864
|
-
await
|
|
32865
|
-
restored.add(saved.id);
|
|
33225
|
+
await attemptRestore(saved);
|
|
32866
33226
|
} catch (err) {
|
|
32867
|
-
|
|
32868
|
-
|
|
33227
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33228
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33229
|
+
tags: {
|
|
33230
|
+
deviceId: saved.id,
|
|
33231
|
+
stableId: saved.stableId
|
|
33232
|
+
},
|
|
32869
33233
|
meta: {
|
|
32870
33234
|
type: saved.type,
|
|
32871
|
-
|
|
33235
|
+
attempt: 1,
|
|
33236
|
+
error
|
|
32872
33237
|
}
|
|
32873
33238
|
});
|
|
33239
|
+
failures.push({
|
|
33240
|
+
saved,
|
|
33241
|
+
error
|
|
33242
|
+
});
|
|
32874
33243
|
}
|
|
32875
33244
|
};
|
|
32876
33245
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33246
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
32877
33247
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
32878
33248
|
for (const saved of childRows) {
|
|
32879
|
-
|
|
32880
|
-
if (!Class) continue;
|
|
33249
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
32881
33250
|
if (saved.parentDeviceId === null) continue;
|
|
32882
|
-
if (
|
|
32883
|
-
|
|
32884
|
-
|
|
32885
|
-
|
|
32886
|
-
|
|
32887
|
-
|
|
33251
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33252
|
+
try {
|
|
33253
|
+
await attemptRestore(saved);
|
|
33254
|
+
} catch (err) {
|
|
33255
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33256
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33257
|
+
tags: {
|
|
33258
|
+
deviceId: saved.id,
|
|
33259
|
+
stableId: saved.stableId,
|
|
33260
|
+
parentDeviceId: saved.parentDeviceId
|
|
33261
|
+
},
|
|
33262
|
+
meta: {
|
|
33263
|
+
type: saved.type,
|
|
33264
|
+
attempt: 1,
|
|
33265
|
+
error
|
|
33266
|
+
}
|
|
33267
|
+
});
|
|
33268
|
+
failures.push({
|
|
33269
|
+
saved,
|
|
33270
|
+
error
|
|
33271
|
+
});
|
|
33272
|
+
}
|
|
33273
|
+
continue;
|
|
33274
|
+
}
|
|
33275
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33276
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
32888
33277
|
tags: {
|
|
33278
|
+
deviceId: saved.id,
|
|
32889
33279
|
stableId: saved.stableId,
|
|
32890
33280
|
parentDeviceId: saved.parentDeviceId
|
|
32891
33281
|
},
|
|
32892
|
-
meta: {
|
|
32893
|
-
type: saved.type,
|
|
32894
|
-
error: err instanceof Error ? err.message : String(err)
|
|
32895
|
-
}
|
|
33282
|
+
meta: { type: saved.type }
|
|
32896
33283
|
});
|
|
33284
|
+
failures.push({
|
|
33285
|
+
saved,
|
|
33286
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33287
|
+
});
|
|
33288
|
+
continue;
|
|
32897
33289
|
}
|
|
32898
33290
|
}
|
|
33291
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33292
|
+
return {
|
|
33293
|
+
restoredCount: restored.size,
|
|
33294
|
+
failedCount: failures.length
|
|
33295
|
+
};
|
|
32899
33296
|
}
|
|
32900
33297
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
32901
33298
|
toSummary(device) {
|
|
@@ -34724,6 +35121,12 @@ Object.freeze({
|
|
|
34724
35121
|
addonId: null,
|
|
34725
35122
|
access: "view"
|
|
34726
35123
|
},
|
|
35124
|
+
"deviceProvider.reloadDevice": {
|
|
35125
|
+
capName: "device-provider",
|
|
35126
|
+
capScope: "system",
|
|
35127
|
+
addonId: null,
|
|
35128
|
+
access: "create"
|
|
35129
|
+
},
|
|
34727
35130
|
"deviceProvider.start": {
|
|
34728
35131
|
capName: "device-provider",
|
|
34729
35132
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -12788,7 +12788,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
12788
12788
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
12789
12789
|
* flows live through the cap STATUS SLICE after adoption.
|
|
12790
12790
|
*/
|
|
12791
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
12791
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
12792
|
+
/**
|
|
12793
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
12794
|
+
*
|
|
12795
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
12796
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
12797
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
12798
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
12799
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
12800
|
+
* three child cameras offline for four hours.
|
|
12801
|
+
*
|
|
12802
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
12803
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
12804
|
+
* that cannot tell simply never sets it.
|
|
12805
|
+
*/
|
|
12806
|
+
alreadyOnboarded: boolean().optional(),
|
|
12807
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
12808
|
+
onboardedDeviceId: number().optional(),
|
|
12809
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
12810
|
+
onboardedName: string().optional()
|
|
12792
12811
|
});
|
|
12793
12812
|
/**
|
|
12794
12813
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -12844,6 +12863,35 @@ var deviceProviderCapability = {
|
|
|
12844
12863
|
name: string(),
|
|
12845
12864
|
type: string()
|
|
12846
12865
|
}))),
|
|
12866
|
+
/**
|
|
12867
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
12868
|
+
* touching no other device this provider owns.
|
|
12869
|
+
*
|
|
12870
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
12871
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
12872
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
12873
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
12874
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
12875
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
12876
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
12877
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
12878
|
+
*
|
|
12879
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
12880
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
12881
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
12882
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
12883
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
12884
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
12885
|
+
* against the wrong camera (D8).
|
|
12886
|
+
*
|
|
12887
|
+
* Construction can dial hardware, and the migrated source is
|
|
12888
|
+
* characteristically dead — the timeout covers a full activate window
|
|
12889
|
+
* rather than the 60 s default.
|
|
12890
|
+
*/
|
|
12891
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
12892
|
+
kind: "mutation",
|
|
12893
|
+
timeoutMs: 3 * 6e4
|
|
12894
|
+
}),
|
|
12847
12895
|
supportsDiscovery: method(object({}), boolean()),
|
|
12848
12896
|
/**
|
|
12849
12897
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13171,7 +13219,8 @@ method(object({
|
|
|
13171
13219
|
targetId: number()
|
|
13172
13220
|
}), MigrateDeviceResultSchema, {
|
|
13173
13221
|
kind: "mutation",
|
|
13174
|
-
auth: "admin"
|
|
13222
|
+
auth: "admin",
|
|
13223
|
+
timeoutMs: 12 * 6e4
|
|
13175
13224
|
}), 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({
|
|
13176
13225
|
deviceId: number(),
|
|
13177
13226
|
name: string()
|
|
@@ -32639,6 +32688,147 @@ var BaseDevice = class {
|
|
|
32639
32688
|
}
|
|
32640
32689
|
};
|
|
32641
32690
|
/**
|
|
32691
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
32692
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
32693
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
32694
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
32695
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
32696
|
+
*/
|
|
32697
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
32698
|
+
1e4,
|
|
32699
|
+
3e4,
|
|
32700
|
+
9e4
|
|
32701
|
+
];
|
|
32702
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
32703
|
+
function sleep$1(ms, signal) {
|
|
32704
|
+
return new Promise((resolve) => {
|
|
32705
|
+
if (signal.aborted) {
|
|
32706
|
+
resolve();
|
|
32707
|
+
return;
|
|
32708
|
+
}
|
|
32709
|
+
const onAbort = () => {
|
|
32710
|
+
clearTimeout(timer);
|
|
32711
|
+
resolve();
|
|
32712
|
+
};
|
|
32713
|
+
const timer = setTimeout(() => {
|
|
32714
|
+
signal.removeEventListener("abort", onAbort);
|
|
32715
|
+
resolve();
|
|
32716
|
+
}, ms);
|
|
32717
|
+
timer.unref?.();
|
|
32718
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
32719
|
+
});
|
|
32720
|
+
}
|
|
32721
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
32722
|
+
* not reject (callers wrap their own try/catch). */
|
|
32723
|
+
async function runWithConcurrency(items, width, fn) {
|
|
32724
|
+
const queue = [...items];
|
|
32725
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
32726
|
+
const lane = async () => {
|
|
32727
|
+
for (;;) {
|
|
32728
|
+
const item = queue.shift();
|
|
32729
|
+
if (item === void 0) return;
|
|
32730
|
+
await fn(item);
|
|
32731
|
+
}
|
|
32732
|
+
};
|
|
32733
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
32734
|
+
}
|
|
32735
|
+
var DeviceRestoreRetryScheduler = class {
|
|
32736
|
+
#logger;
|
|
32737
|
+
#attempt;
|
|
32738
|
+
#onPermanentFailure;
|
|
32739
|
+
#delaysMs;
|
|
32740
|
+
#concurrency;
|
|
32741
|
+
#now;
|
|
32742
|
+
#abort = new AbortController();
|
|
32743
|
+
constructor(options) {
|
|
32744
|
+
this.#logger = options.logger;
|
|
32745
|
+
this.#attempt = options.attempt;
|
|
32746
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
32747
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
32748
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
32749
|
+
this.#now = options.now ?? Date.now;
|
|
32750
|
+
}
|
|
32751
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
32752
|
+
* permanently failed — the next boot restores them from disk. */
|
|
32753
|
+
cancel() {
|
|
32754
|
+
this.#abort.abort();
|
|
32755
|
+
}
|
|
32756
|
+
/**
|
|
32757
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
32758
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
32759
|
+
* cancelled. Never rejects.
|
|
32760
|
+
*/
|
|
32761
|
+
async run(initialFailures) {
|
|
32762
|
+
let pending = initialFailures.map((failure) => ({
|
|
32763
|
+
saved: failure.saved,
|
|
32764
|
+
lastError: failure.error,
|
|
32765
|
+
attempts: 1
|
|
32766
|
+
}));
|
|
32767
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
32768
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
32769
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
32770
|
+
if (this.#abort.signal.aborted) break;
|
|
32771
|
+
pending = await this.#runRound(pending, round);
|
|
32772
|
+
}
|
|
32773
|
+
if (this.#abort.signal.aborted) return [];
|
|
32774
|
+
const terminal = pending.map((entry) => ({
|
|
32775
|
+
deviceId: entry.saved.id,
|
|
32776
|
+
stableId: entry.saved.stableId,
|
|
32777
|
+
type: String(entry.saved.type),
|
|
32778
|
+
attempts: entry.attempts,
|
|
32779
|
+
lastError: entry.lastError,
|
|
32780
|
+
failedAt: this.#now()
|
|
32781
|
+
}));
|
|
32782
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
32783
|
+
return terminal;
|
|
32784
|
+
}
|
|
32785
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
32786
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
32787
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
32788
|
+
async #runRound(pending, round) {
|
|
32789
|
+
const next = [];
|
|
32790
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
32791
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
32792
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
32793
|
+
if (this.#abort.signal.aborted) {
|
|
32794
|
+
next.push(entry);
|
|
32795
|
+
return;
|
|
32796
|
+
}
|
|
32797
|
+
const attemptNo = entry.attempts + 1;
|
|
32798
|
+
try {
|
|
32799
|
+
await this.#attempt(entry.saved);
|
|
32800
|
+
this.#logger.info("Device restored on retry", {
|
|
32801
|
+
tags: {
|
|
32802
|
+
deviceId: entry.saved.id,
|
|
32803
|
+
stableId: entry.saved.stableId
|
|
32804
|
+
},
|
|
32805
|
+
meta: { attempt: attemptNo }
|
|
32806
|
+
});
|
|
32807
|
+
} catch (err) {
|
|
32808
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
32809
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
32810
|
+
this.#logger.warn("Device restore retry failed", {
|
|
32811
|
+
tags: {
|
|
32812
|
+
deviceId: entry.saved.id,
|
|
32813
|
+
stableId: entry.saved.stableId
|
|
32814
|
+
},
|
|
32815
|
+
meta: {
|
|
32816
|
+
attempt: attemptNo,
|
|
32817
|
+
remainingRetries,
|
|
32818
|
+
error: lastError
|
|
32819
|
+
}
|
|
32820
|
+
});
|
|
32821
|
+
next.push({
|
|
32822
|
+
saved: entry.saved,
|
|
32823
|
+
lastError,
|
|
32824
|
+
attempts: attemptNo
|
|
32825
|
+
});
|
|
32826
|
+
}
|
|
32827
|
+
});
|
|
32828
|
+
return next;
|
|
32829
|
+
}
|
|
32830
|
+
};
|
|
32831
|
+
/**
|
|
32642
32832
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
32643
32833
|
* device-provider cap router. Shared across all providers.
|
|
32644
32834
|
*/
|
|
@@ -32687,6 +32877,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32687
32877
|
}];
|
|
32688
32878
|
}
|
|
32689
32879
|
async onShutdown() {
|
|
32880
|
+
this.cancelRestoreRetries();
|
|
32690
32881
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32691
32882
|
for (const device of devices) try {
|
|
32692
32883
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -32704,9 +32895,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32704
32895
|
async start() {}
|
|
32705
32896
|
async stop() {}
|
|
32706
32897
|
async getStatus() {
|
|
32898
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
32899
|
+
const summary = this.restoreFailureSummary();
|
|
32900
|
+
if (summary === null) return {
|
|
32901
|
+
connected: true,
|
|
32902
|
+
deviceCount: all.length
|
|
32903
|
+
};
|
|
32707
32904
|
return {
|
|
32708
32905
|
connected: true,
|
|
32709
|
-
deviceCount:
|
|
32906
|
+
deviceCount: all.length,
|
|
32907
|
+
error: summary
|
|
32710
32908
|
};
|
|
32711
32909
|
}
|
|
32712
32910
|
async getDevices() {
|
|
@@ -32796,8 +32994,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32796
32994
|
};
|
|
32797
32995
|
}
|
|
32798
32996
|
async restoreDevices(savedDevices) {
|
|
32799
|
-
await this.onRestoreDevices(savedDevices);
|
|
32800
|
-
if (savedDevices.length
|
|
32997
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
32998
|
+
if (savedDevices.length === 0) return;
|
|
32999
|
+
if (report && report.failedCount > 0) {
|
|
33000
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33001
|
+
return;
|
|
33002
|
+
}
|
|
33003
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33004
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33005
|
+
}
|
|
33006
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33007
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33008
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33009
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33010
|
+
restoreRetryConcurrency = 4;
|
|
33011
|
+
_restoreRetryScheduler = null;
|
|
33012
|
+
_restoreRetryCompletion = null;
|
|
33013
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33014
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33015
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33016
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33017
|
+
* with the devices that restored, and a late success is announced
|
|
33018
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33019
|
+
get restoreRetryCompletion() {
|
|
33020
|
+
return this._restoreRetryCompletion;
|
|
33021
|
+
}
|
|
33022
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33023
|
+
get permanentRestoreFailures() {
|
|
33024
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33025
|
+
}
|
|
33026
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33027
|
+
* `null` when every device restored. */
|
|
33028
|
+
restoreFailureSummary() {
|
|
33029
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33030
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33031
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33032
|
+
}
|
|
33033
|
+
cancelRestoreRetries() {
|
|
33034
|
+
this._restoreRetryScheduler?.cancel();
|
|
33035
|
+
this._restoreRetryScheduler = null;
|
|
33036
|
+
}
|
|
33037
|
+
recordPermanentRestoreFailure(failure) {
|
|
33038
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33039
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33040
|
+
tags: {
|
|
33041
|
+
deviceId: failure.deviceId,
|
|
33042
|
+
stableId: failure.stableId
|
|
33043
|
+
},
|
|
33044
|
+
meta: {
|
|
33045
|
+
type: failure.type,
|
|
33046
|
+
attempts: failure.attempts,
|
|
33047
|
+
error: failure.lastError
|
|
33048
|
+
}
|
|
33049
|
+
});
|
|
33050
|
+
}
|
|
33051
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33052
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33053
|
+
logger: this.ctx.logger,
|
|
33054
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33055
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33056
|
+
attempt,
|
|
33057
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33058
|
+
});
|
|
33059
|
+
this._restoreRetryScheduler = scheduler;
|
|
33060
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33061
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33062
|
+
});
|
|
33063
|
+
}
|
|
33064
|
+
/**
|
|
33065
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33066
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33067
|
+
* and no other device this provider owns is disturbed.
|
|
33068
|
+
*
|
|
33069
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33070
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33071
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33072
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33073
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33074
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33075
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33076
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33077
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33078
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33079
|
+
*
|
|
33080
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33081
|
+
* reload its parent instead.
|
|
33082
|
+
*/
|
|
33083
|
+
async reloadDevice(input) {
|
|
33084
|
+
const { stableId } = input;
|
|
33085
|
+
const devices = this.ctx.kernel.devices;
|
|
33086
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33087
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33088
|
+
if (live) await devices.decommission(live.id);
|
|
33089
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33090
|
+
addonId: this.addonId,
|
|
33091
|
+
stableId
|
|
33092
|
+
});
|
|
33093
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33094
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33095
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33096
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33097
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33098
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33099
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33100
|
+
for (const row of rows) {
|
|
33101
|
+
if (row.parentDeviceId !== id) continue;
|
|
33102
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33103
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33104
|
+
if (!ChildClass) continue;
|
|
33105
|
+
try {
|
|
33106
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33107
|
+
} catch (err) {
|
|
33108
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33109
|
+
tags: {
|
|
33110
|
+
deviceId: row.id,
|
|
33111
|
+
stableId: row.stableId
|
|
33112
|
+
},
|
|
33113
|
+
meta: {
|
|
33114
|
+
parentDeviceId: id,
|
|
33115
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33116
|
+
}
|
|
33117
|
+
});
|
|
33118
|
+
}
|
|
33119
|
+
}
|
|
33120
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33121
|
+
tags: { deviceId: id },
|
|
33122
|
+
meta: {
|
|
33123
|
+
stableId,
|
|
33124
|
+
type: meta.type
|
|
33125
|
+
}
|
|
33126
|
+
});
|
|
33127
|
+
return { deviceId: id };
|
|
32801
33128
|
}
|
|
32802
33129
|
/**
|
|
32803
33130
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -32823,55 +33150,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
32823
33150
|
* accessory-spawn flow handles via the parent's
|
|
32824
33151
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
32825
33152
|
* fit.
|
|
33153
|
+
*
|
|
33154
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33155
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33156
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33157
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33158
|
+
* `getStatus().error`.
|
|
32826
33159
|
*/
|
|
33160
|
+
/**
|
|
33161
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33162
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33163
|
+
*
|
|
33164
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33165
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33166
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33167
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33168
|
+
* been emptied failed all four bounded attempts against fields
|
|
33169
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33170
|
+
*
|
|
33171
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33172
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33173
|
+
* the bound, then reported — never swallowed.
|
|
33174
|
+
*/
|
|
33175
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
32827
33176
|
async onRestoreDevices(savedDevices) {
|
|
32828
33177
|
const restored = /* @__PURE__ */ new Set();
|
|
33178
|
+
const failures = [];
|
|
33179
|
+
const attemptRestore = async (saved) => {
|
|
33180
|
+
if (restored.has(saved.id)) return;
|
|
33181
|
+
const Class = this.deviceClasses[saved.type];
|
|
33182
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33183
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33184
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33185
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33186
|
+
restored.add(saved.id);
|
|
33187
|
+
};
|
|
32829
33188
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
32830
33189
|
const restoreOne = async (saved) => {
|
|
32831
|
-
|
|
32832
|
-
if (!Class) {
|
|
33190
|
+
if (!this.deviceClasses[saved.type]) {
|
|
32833
33191
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
32834
|
-
tags: {
|
|
33192
|
+
tags: {
|
|
33193
|
+
deviceId: saved.id,
|
|
33194
|
+
stableId: saved.stableId
|
|
33195
|
+
},
|
|
32835
33196
|
meta: { type: saved.type }
|
|
32836
33197
|
});
|
|
32837
33198
|
return;
|
|
32838
33199
|
}
|
|
32839
33200
|
try {
|
|
32840
|
-
await
|
|
32841
|
-
restored.add(saved.id);
|
|
33201
|
+
await attemptRestore(saved);
|
|
32842
33202
|
} catch (err) {
|
|
32843
|
-
|
|
32844
|
-
|
|
33203
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33204
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33205
|
+
tags: {
|
|
33206
|
+
deviceId: saved.id,
|
|
33207
|
+
stableId: saved.stableId
|
|
33208
|
+
},
|
|
32845
33209
|
meta: {
|
|
32846
33210
|
type: saved.type,
|
|
32847
|
-
|
|
33211
|
+
attempt: 1,
|
|
33212
|
+
error
|
|
32848
33213
|
}
|
|
32849
33214
|
});
|
|
33215
|
+
failures.push({
|
|
33216
|
+
saved,
|
|
33217
|
+
error
|
|
33218
|
+
});
|
|
32850
33219
|
}
|
|
32851
33220
|
};
|
|
32852
33221
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33222
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
32853
33223
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
32854
33224
|
for (const saved of childRows) {
|
|
32855
|
-
|
|
32856
|
-
if (!Class) continue;
|
|
33225
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
32857
33226
|
if (saved.parentDeviceId === null) continue;
|
|
32858
|
-
if (
|
|
32859
|
-
|
|
32860
|
-
|
|
32861
|
-
|
|
32862
|
-
|
|
32863
|
-
|
|
33227
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33228
|
+
try {
|
|
33229
|
+
await attemptRestore(saved);
|
|
33230
|
+
} catch (err) {
|
|
33231
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33232
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33233
|
+
tags: {
|
|
33234
|
+
deviceId: saved.id,
|
|
33235
|
+
stableId: saved.stableId,
|
|
33236
|
+
parentDeviceId: saved.parentDeviceId
|
|
33237
|
+
},
|
|
33238
|
+
meta: {
|
|
33239
|
+
type: saved.type,
|
|
33240
|
+
attempt: 1,
|
|
33241
|
+
error
|
|
33242
|
+
}
|
|
33243
|
+
});
|
|
33244
|
+
failures.push({
|
|
33245
|
+
saved,
|
|
33246
|
+
error
|
|
33247
|
+
});
|
|
33248
|
+
}
|
|
33249
|
+
continue;
|
|
33250
|
+
}
|
|
33251
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33252
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
32864
33253
|
tags: {
|
|
33254
|
+
deviceId: saved.id,
|
|
32865
33255
|
stableId: saved.stableId,
|
|
32866
33256
|
parentDeviceId: saved.parentDeviceId
|
|
32867
33257
|
},
|
|
32868
|
-
meta: {
|
|
32869
|
-
type: saved.type,
|
|
32870
|
-
error: err instanceof Error ? err.message : String(err)
|
|
32871
|
-
}
|
|
33258
|
+
meta: { type: saved.type }
|
|
32872
33259
|
});
|
|
33260
|
+
failures.push({
|
|
33261
|
+
saved,
|
|
33262
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33263
|
+
});
|
|
33264
|
+
continue;
|
|
32873
33265
|
}
|
|
32874
33266
|
}
|
|
33267
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33268
|
+
return {
|
|
33269
|
+
restoredCount: restored.size,
|
|
33270
|
+
failedCount: failures.length
|
|
33271
|
+
};
|
|
32875
33272
|
}
|
|
32876
33273
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
32877
33274
|
toSummary(device) {
|
|
@@ -34700,6 +35097,12 @@ Object.freeze({
|
|
|
34700
35097
|
addonId: null,
|
|
34701
35098
|
access: "view"
|
|
34702
35099
|
},
|
|
35100
|
+
"deviceProvider.reloadDevice": {
|
|
35101
|
+
capName: "device-provider",
|
|
35102
|
+
capScope: "system",
|
|
35103
|
+
addonId: null,
|
|
35104
|
+
access: "create"
|
|
35105
|
+
},
|
|
34703
35106
|
"deviceProvider.start": {
|
|
34704
35107
|
capName: "device-provider",
|
|
34705
35108
|
capScope: "system",
|