@camstack/addon-provider-rademacher 0.2.58 → 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 +428 -25
- package/dist/addon.mjs +428 -25
- package/package.json +1 -1
package/dist/addon.js
CHANGED
|
@@ -13741,7 +13741,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
13741
13741
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
13742
13742
|
* flows live through the cap STATUS SLICE after adoption.
|
|
13743
13743
|
*/
|
|
13744
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
13744
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
13745
|
+
/**
|
|
13746
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
13747
|
+
*
|
|
13748
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
13749
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
13750
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
13751
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
13752
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
13753
|
+
* three child cameras offline for four hours.
|
|
13754
|
+
*
|
|
13755
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
13756
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
13757
|
+
* that cannot tell simply never sets it.
|
|
13758
|
+
*/
|
|
13759
|
+
alreadyOnboarded: boolean().optional(),
|
|
13760
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
13761
|
+
onboardedDeviceId: number().optional(),
|
|
13762
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
13763
|
+
onboardedName: string().optional()
|
|
13745
13764
|
});
|
|
13746
13765
|
/**
|
|
13747
13766
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -13797,6 +13816,35 @@ var deviceProviderCapability = {
|
|
|
13797
13816
|
name: string(),
|
|
13798
13817
|
type: string()
|
|
13799
13818
|
}))),
|
|
13819
|
+
/**
|
|
13820
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13821
|
+
* touching no other device this provider owns.
|
|
13822
|
+
*
|
|
13823
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13824
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13825
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13826
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13827
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13828
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13829
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13830
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13831
|
+
*
|
|
13832
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13833
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13834
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13835
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13836
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13837
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13838
|
+
* against the wrong camera (D8).
|
|
13839
|
+
*
|
|
13840
|
+
* Construction can dial hardware, and the migrated source is
|
|
13841
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13842
|
+
* rather than the 60 s default.
|
|
13843
|
+
*/
|
|
13844
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13845
|
+
kind: "mutation",
|
|
13846
|
+
timeoutMs: 3 * 6e4
|
|
13847
|
+
}),
|
|
13800
13848
|
supportsDiscovery: method(object({}), boolean()),
|
|
13801
13849
|
/**
|
|
13802
13850
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -14124,7 +14172,8 @@ method(object({
|
|
|
14124
14172
|
targetId: number()
|
|
14125
14173
|
}), MigrateDeviceResultSchema, {
|
|
14126
14174
|
kind: "mutation",
|
|
14127
|
-
auth: "admin"
|
|
14175
|
+
auth: "admin",
|
|
14176
|
+
timeoutMs: 12 * 6e4
|
|
14128
14177
|
}), 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({
|
|
14129
14178
|
deviceId: number(),
|
|
14130
14179
|
name: string()
|
|
@@ -33480,6 +33529,147 @@ var BaseDevice = class {
|
|
|
33480
33529
|
}
|
|
33481
33530
|
};
|
|
33482
33531
|
/**
|
|
33532
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33533
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33534
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33535
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33536
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33537
|
+
*/
|
|
33538
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33539
|
+
1e4,
|
|
33540
|
+
3e4,
|
|
33541
|
+
9e4
|
|
33542
|
+
];
|
|
33543
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33544
|
+
function sleep$1(ms, signal) {
|
|
33545
|
+
return new Promise((resolve) => {
|
|
33546
|
+
if (signal.aborted) {
|
|
33547
|
+
resolve();
|
|
33548
|
+
return;
|
|
33549
|
+
}
|
|
33550
|
+
const onAbort = () => {
|
|
33551
|
+
clearTimeout(timer);
|
|
33552
|
+
resolve();
|
|
33553
|
+
};
|
|
33554
|
+
const timer = setTimeout(() => {
|
|
33555
|
+
signal.removeEventListener("abort", onAbort);
|
|
33556
|
+
resolve();
|
|
33557
|
+
}, ms);
|
|
33558
|
+
timer.unref?.();
|
|
33559
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33560
|
+
});
|
|
33561
|
+
}
|
|
33562
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33563
|
+
* not reject (callers wrap their own try/catch). */
|
|
33564
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33565
|
+
const queue = [...items];
|
|
33566
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33567
|
+
const lane = async () => {
|
|
33568
|
+
for (;;) {
|
|
33569
|
+
const item = queue.shift();
|
|
33570
|
+
if (item === void 0) return;
|
|
33571
|
+
await fn(item);
|
|
33572
|
+
}
|
|
33573
|
+
};
|
|
33574
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33575
|
+
}
|
|
33576
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33577
|
+
#logger;
|
|
33578
|
+
#attempt;
|
|
33579
|
+
#onPermanentFailure;
|
|
33580
|
+
#delaysMs;
|
|
33581
|
+
#concurrency;
|
|
33582
|
+
#now;
|
|
33583
|
+
#abort = new AbortController();
|
|
33584
|
+
constructor(options) {
|
|
33585
|
+
this.#logger = options.logger;
|
|
33586
|
+
this.#attempt = options.attempt;
|
|
33587
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33588
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33589
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33590
|
+
this.#now = options.now ?? Date.now;
|
|
33591
|
+
}
|
|
33592
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33593
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33594
|
+
cancel() {
|
|
33595
|
+
this.#abort.abort();
|
|
33596
|
+
}
|
|
33597
|
+
/**
|
|
33598
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33599
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33600
|
+
* cancelled. Never rejects.
|
|
33601
|
+
*/
|
|
33602
|
+
async run(initialFailures) {
|
|
33603
|
+
let pending = initialFailures.map((failure) => ({
|
|
33604
|
+
saved: failure.saved,
|
|
33605
|
+
lastError: failure.error,
|
|
33606
|
+
attempts: 1
|
|
33607
|
+
}));
|
|
33608
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33609
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33610
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33611
|
+
if (this.#abort.signal.aborted) break;
|
|
33612
|
+
pending = await this.#runRound(pending, round);
|
|
33613
|
+
}
|
|
33614
|
+
if (this.#abort.signal.aborted) return [];
|
|
33615
|
+
const terminal = pending.map((entry) => ({
|
|
33616
|
+
deviceId: entry.saved.id,
|
|
33617
|
+
stableId: entry.saved.stableId,
|
|
33618
|
+
type: String(entry.saved.type),
|
|
33619
|
+
attempts: entry.attempts,
|
|
33620
|
+
lastError: entry.lastError,
|
|
33621
|
+
failedAt: this.#now()
|
|
33622
|
+
}));
|
|
33623
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33624
|
+
return terminal;
|
|
33625
|
+
}
|
|
33626
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33627
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33628
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33629
|
+
async #runRound(pending, round) {
|
|
33630
|
+
const next = [];
|
|
33631
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33632
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33633
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33634
|
+
if (this.#abort.signal.aborted) {
|
|
33635
|
+
next.push(entry);
|
|
33636
|
+
return;
|
|
33637
|
+
}
|
|
33638
|
+
const attemptNo = entry.attempts + 1;
|
|
33639
|
+
try {
|
|
33640
|
+
await this.#attempt(entry.saved);
|
|
33641
|
+
this.#logger.info("Device restored on retry", {
|
|
33642
|
+
tags: {
|
|
33643
|
+
deviceId: entry.saved.id,
|
|
33644
|
+
stableId: entry.saved.stableId
|
|
33645
|
+
},
|
|
33646
|
+
meta: { attempt: attemptNo }
|
|
33647
|
+
});
|
|
33648
|
+
} catch (err) {
|
|
33649
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33650
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33651
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33652
|
+
tags: {
|
|
33653
|
+
deviceId: entry.saved.id,
|
|
33654
|
+
stableId: entry.saved.stableId
|
|
33655
|
+
},
|
|
33656
|
+
meta: {
|
|
33657
|
+
attempt: attemptNo,
|
|
33658
|
+
remainingRetries,
|
|
33659
|
+
error: lastError
|
|
33660
|
+
}
|
|
33661
|
+
});
|
|
33662
|
+
next.push({
|
|
33663
|
+
saved: entry.saved,
|
|
33664
|
+
lastError,
|
|
33665
|
+
attempts: attemptNo
|
|
33666
|
+
});
|
|
33667
|
+
}
|
|
33668
|
+
});
|
|
33669
|
+
return next;
|
|
33670
|
+
}
|
|
33671
|
+
};
|
|
33672
|
+
/**
|
|
33483
33673
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33484
33674
|
* device-provider cap router. Shared across all providers.
|
|
33485
33675
|
*/
|
|
@@ -33528,6 +33718,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33528
33718
|
}];
|
|
33529
33719
|
}
|
|
33530
33720
|
async onShutdown() {
|
|
33721
|
+
this.cancelRestoreRetries();
|
|
33531
33722
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33532
33723
|
for (const device of devices) try {
|
|
33533
33724
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33545,9 +33736,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33545
33736
|
async start() {}
|
|
33546
33737
|
async stop() {}
|
|
33547
33738
|
async getStatus() {
|
|
33739
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33740
|
+
const summary = this.restoreFailureSummary();
|
|
33741
|
+
if (summary === null) return {
|
|
33742
|
+
connected: true,
|
|
33743
|
+
deviceCount: all.length
|
|
33744
|
+
};
|
|
33548
33745
|
return {
|
|
33549
33746
|
connected: true,
|
|
33550
|
-
deviceCount:
|
|
33747
|
+
deviceCount: all.length,
|
|
33748
|
+
error: summary
|
|
33551
33749
|
};
|
|
33552
33750
|
}
|
|
33553
33751
|
async getDevices() {
|
|
@@ -33637,8 +33835,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33637
33835
|
};
|
|
33638
33836
|
}
|
|
33639
33837
|
async restoreDevices(savedDevices) {
|
|
33640
|
-
await this.onRestoreDevices(savedDevices);
|
|
33641
|
-
if (savedDevices.length
|
|
33838
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33839
|
+
if (savedDevices.length === 0) return;
|
|
33840
|
+
if (report && report.failedCount > 0) {
|
|
33841
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33842
|
+
return;
|
|
33843
|
+
}
|
|
33844
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33845
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33846
|
+
}
|
|
33847
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33848
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33849
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33850
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33851
|
+
restoreRetryConcurrency = 4;
|
|
33852
|
+
_restoreRetryScheduler = null;
|
|
33853
|
+
_restoreRetryCompletion = null;
|
|
33854
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33855
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33856
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33857
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33858
|
+
* with the devices that restored, and a late success is announced
|
|
33859
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33860
|
+
get restoreRetryCompletion() {
|
|
33861
|
+
return this._restoreRetryCompletion;
|
|
33862
|
+
}
|
|
33863
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33864
|
+
get permanentRestoreFailures() {
|
|
33865
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33866
|
+
}
|
|
33867
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33868
|
+
* `null` when every device restored. */
|
|
33869
|
+
restoreFailureSummary() {
|
|
33870
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33871
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33872
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33873
|
+
}
|
|
33874
|
+
cancelRestoreRetries() {
|
|
33875
|
+
this._restoreRetryScheduler?.cancel();
|
|
33876
|
+
this._restoreRetryScheduler = null;
|
|
33877
|
+
}
|
|
33878
|
+
recordPermanentRestoreFailure(failure) {
|
|
33879
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33880
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33881
|
+
tags: {
|
|
33882
|
+
deviceId: failure.deviceId,
|
|
33883
|
+
stableId: failure.stableId
|
|
33884
|
+
},
|
|
33885
|
+
meta: {
|
|
33886
|
+
type: failure.type,
|
|
33887
|
+
attempts: failure.attempts,
|
|
33888
|
+
error: failure.lastError
|
|
33889
|
+
}
|
|
33890
|
+
});
|
|
33891
|
+
}
|
|
33892
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33893
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33894
|
+
logger: this.ctx.logger,
|
|
33895
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33896
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33897
|
+
attempt,
|
|
33898
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33899
|
+
});
|
|
33900
|
+
this._restoreRetryScheduler = scheduler;
|
|
33901
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33902
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33903
|
+
});
|
|
33904
|
+
}
|
|
33905
|
+
/**
|
|
33906
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33907
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33908
|
+
* and no other device this provider owns is disturbed.
|
|
33909
|
+
*
|
|
33910
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33911
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33912
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33913
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33914
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33915
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33916
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33917
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33918
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33919
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33920
|
+
*
|
|
33921
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33922
|
+
* reload its parent instead.
|
|
33923
|
+
*/
|
|
33924
|
+
async reloadDevice(input) {
|
|
33925
|
+
const { stableId } = input;
|
|
33926
|
+
const devices = this.ctx.kernel.devices;
|
|
33927
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33928
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33929
|
+
if (live) await devices.decommission(live.id);
|
|
33930
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33931
|
+
addonId: this.addonId,
|
|
33932
|
+
stableId
|
|
33933
|
+
});
|
|
33934
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33935
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33936
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33937
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33938
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33939
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33940
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33941
|
+
for (const row of rows) {
|
|
33942
|
+
if (row.parentDeviceId !== id) continue;
|
|
33943
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33944
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33945
|
+
if (!ChildClass) continue;
|
|
33946
|
+
try {
|
|
33947
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33948
|
+
} catch (err) {
|
|
33949
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33950
|
+
tags: {
|
|
33951
|
+
deviceId: row.id,
|
|
33952
|
+
stableId: row.stableId
|
|
33953
|
+
},
|
|
33954
|
+
meta: {
|
|
33955
|
+
parentDeviceId: id,
|
|
33956
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33957
|
+
}
|
|
33958
|
+
});
|
|
33959
|
+
}
|
|
33960
|
+
}
|
|
33961
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33962
|
+
tags: { deviceId: id },
|
|
33963
|
+
meta: {
|
|
33964
|
+
stableId,
|
|
33965
|
+
type: meta.type
|
|
33966
|
+
}
|
|
33967
|
+
});
|
|
33968
|
+
return { deviceId: id };
|
|
33642
33969
|
}
|
|
33643
33970
|
/**
|
|
33644
33971
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33664,55 +33991,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33664
33991
|
* accessory-spawn flow handles via the parent's
|
|
33665
33992
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33666
33993
|
* fit.
|
|
33994
|
+
*
|
|
33995
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33996
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33997
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33998
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33999
|
+
* `getStatus().error`.
|
|
33667
34000
|
*/
|
|
34001
|
+
/**
|
|
34002
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
34003
|
+
* Default: no-op — most providers have nothing to heal.
|
|
34004
|
+
*
|
|
34005
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
34006
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
34007
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
34008
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
34009
|
+
* been emptied failed all four bounded attempts against fields
|
|
34010
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
34011
|
+
*
|
|
34012
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
34013
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
34014
|
+
* the bound, then reported — never swallowed.
|
|
34015
|
+
*/
|
|
34016
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
33668
34017
|
async onRestoreDevices(savedDevices) {
|
|
33669
34018
|
const restored = /* @__PURE__ */ new Set();
|
|
34019
|
+
const failures = [];
|
|
34020
|
+
const attemptRestore = async (saved) => {
|
|
34021
|
+
if (restored.has(saved.id)) return;
|
|
34022
|
+
const Class = this.deviceClasses[saved.type];
|
|
34023
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
34024
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
34025
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
34026
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
34027
|
+
restored.add(saved.id);
|
|
34028
|
+
};
|
|
33670
34029
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33671
34030
|
const restoreOne = async (saved) => {
|
|
33672
|
-
|
|
33673
|
-
if (!Class) {
|
|
34031
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33674
34032
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33675
|
-
tags: {
|
|
34033
|
+
tags: {
|
|
34034
|
+
deviceId: saved.id,
|
|
34035
|
+
stableId: saved.stableId
|
|
34036
|
+
},
|
|
33676
34037
|
meta: { type: saved.type }
|
|
33677
34038
|
});
|
|
33678
34039
|
return;
|
|
33679
34040
|
}
|
|
33680
34041
|
try {
|
|
33681
|
-
await
|
|
33682
|
-
restored.add(saved.id);
|
|
34042
|
+
await attemptRestore(saved);
|
|
33683
34043
|
} catch (err) {
|
|
33684
|
-
|
|
33685
|
-
|
|
34044
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34045
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
34046
|
+
tags: {
|
|
34047
|
+
deviceId: saved.id,
|
|
34048
|
+
stableId: saved.stableId
|
|
34049
|
+
},
|
|
33686
34050
|
meta: {
|
|
33687
34051
|
type: saved.type,
|
|
33688
|
-
|
|
34052
|
+
attempt: 1,
|
|
34053
|
+
error
|
|
33689
34054
|
}
|
|
33690
34055
|
});
|
|
34056
|
+
failures.push({
|
|
34057
|
+
saved,
|
|
34058
|
+
error
|
|
34059
|
+
});
|
|
33691
34060
|
}
|
|
33692
34061
|
};
|
|
33693
34062
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
34063
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33694
34064
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33695
34065
|
for (const saved of childRows) {
|
|
33696
|
-
|
|
33697
|
-
if (!Class) continue;
|
|
34066
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33698
34067
|
if (saved.parentDeviceId === null) continue;
|
|
33699
|
-
if (
|
|
33700
|
-
|
|
33701
|
-
|
|
33702
|
-
|
|
33703
|
-
|
|
33704
|
-
|
|
34068
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
34069
|
+
try {
|
|
34070
|
+
await attemptRestore(saved);
|
|
34071
|
+
} catch (err) {
|
|
34072
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34073
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
34074
|
+
tags: {
|
|
34075
|
+
deviceId: saved.id,
|
|
34076
|
+
stableId: saved.stableId,
|
|
34077
|
+
parentDeviceId: saved.parentDeviceId
|
|
34078
|
+
},
|
|
34079
|
+
meta: {
|
|
34080
|
+
type: saved.type,
|
|
34081
|
+
attempt: 1,
|
|
34082
|
+
error
|
|
34083
|
+
}
|
|
34084
|
+
});
|
|
34085
|
+
failures.push({
|
|
34086
|
+
saved,
|
|
34087
|
+
error
|
|
34088
|
+
});
|
|
34089
|
+
}
|
|
34090
|
+
continue;
|
|
34091
|
+
}
|
|
34092
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
34093
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33705
34094
|
tags: {
|
|
34095
|
+
deviceId: saved.id,
|
|
33706
34096
|
stableId: saved.stableId,
|
|
33707
34097
|
parentDeviceId: saved.parentDeviceId
|
|
33708
34098
|
},
|
|
33709
|
-
meta: {
|
|
33710
|
-
|
|
33711
|
-
|
|
33712
|
-
|
|
34099
|
+
meta: { type: saved.type }
|
|
34100
|
+
});
|
|
34101
|
+
failures.push({
|
|
34102
|
+
saved,
|
|
34103
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33713
34104
|
});
|
|
34105
|
+
continue;
|
|
33714
34106
|
}
|
|
33715
34107
|
}
|
|
34108
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
34109
|
+
return {
|
|
34110
|
+
restoredCount: restored.size,
|
|
34111
|
+
failedCount: failures.length
|
|
34112
|
+
};
|
|
33716
34113
|
}
|
|
33717
34114
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33718
34115
|
toSummary(device) {
|
|
@@ -35475,6 +35872,12 @@ Object.freeze({
|
|
|
35475
35872
|
addonId: null,
|
|
35476
35873
|
access: "view"
|
|
35477
35874
|
},
|
|
35875
|
+
"deviceProvider.reloadDevice": {
|
|
35876
|
+
capName: "device-provider",
|
|
35877
|
+
capScope: "system",
|
|
35878
|
+
addonId: null,
|
|
35879
|
+
access: "create"
|
|
35880
|
+
},
|
|
35478
35881
|
"deviceProvider.start": {
|
|
35479
35882
|
capName: "device-provider",
|
|
35480
35883
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -13740,7 +13740,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
13740
13740
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
13741
13741
|
* flows live through the cap STATUS SLICE after adoption.
|
|
13742
13742
|
*/
|
|
13743
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
13743
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
13744
|
+
/**
|
|
13745
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
13746
|
+
*
|
|
13747
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
13748
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
13749
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
13750
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
13751
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
13752
|
+
* three child cameras offline for four hours.
|
|
13753
|
+
*
|
|
13754
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
13755
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
13756
|
+
* that cannot tell simply never sets it.
|
|
13757
|
+
*/
|
|
13758
|
+
alreadyOnboarded: boolean().optional(),
|
|
13759
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
13760
|
+
onboardedDeviceId: number().optional(),
|
|
13761
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
13762
|
+
onboardedName: string().optional()
|
|
13744
13763
|
});
|
|
13745
13764
|
/**
|
|
13746
13765
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -13796,6 +13815,35 @@ var deviceProviderCapability = {
|
|
|
13796
13815
|
name: string(),
|
|
13797
13816
|
type: string()
|
|
13798
13817
|
}))),
|
|
13818
|
+
/**
|
|
13819
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13820
|
+
* touching no other device this provider owns.
|
|
13821
|
+
*
|
|
13822
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13823
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13824
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13825
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13826
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13827
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13828
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13829
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13830
|
+
*
|
|
13831
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13832
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13833
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13834
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13835
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13836
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13837
|
+
* against the wrong camera (D8).
|
|
13838
|
+
*
|
|
13839
|
+
* Construction can dial hardware, and the migrated source is
|
|
13840
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13841
|
+
* rather than the 60 s default.
|
|
13842
|
+
*/
|
|
13843
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13844
|
+
kind: "mutation",
|
|
13845
|
+
timeoutMs: 3 * 6e4
|
|
13846
|
+
}),
|
|
13799
13847
|
supportsDiscovery: method(object({}), boolean()),
|
|
13800
13848
|
/**
|
|
13801
13849
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -14123,7 +14171,8 @@ method(object({
|
|
|
14123
14171
|
targetId: number()
|
|
14124
14172
|
}), MigrateDeviceResultSchema, {
|
|
14125
14173
|
kind: "mutation",
|
|
14126
|
-
auth: "admin"
|
|
14174
|
+
auth: "admin",
|
|
14175
|
+
timeoutMs: 12 * 6e4
|
|
14127
14176
|
}), 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({
|
|
14128
14177
|
deviceId: number(),
|
|
14129
14178
|
name: string()
|
|
@@ -33479,6 +33528,147 @@ var BaseDevice = class {
|
|
|
33479
33528
|
}
|
|
33480
33529
|
};
|
|
33481
33530
|
/**
|
|
33531
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33532
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33533
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33534
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33535
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33536
|
+
*/
|
|
33537
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33538
|
+
1e4,
|
|
33539
|
+
3e4,
|
|
33540
|
+
9e4
|
|
33541
|
+
];
|
|
33542
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33543
|
+
function sleep$1(ms, signal) {
|
|
33544
|
+
return new Promise((resolve) => {
|
|
33545
|
+
if (signal.aborted) {
|
|
33546
|
+
resolve();
|
|
33547
|
+
return;
|
|
33548
|
+
}
|
|
33549
|
+
const onAbort = () => {
|
|
33550
|
+
clearTimeout(timer);
|
|
33551
|
+
resolve();
|
|
33552
|
+
};
|
|
33553
|
+
const timer = setTimeout(() => {
|
|
33554
|
+
signal.removeEventListener("abort", onAbort);
|
|
33555
|
+
resolve();
|
|
33556
|
+
}, ms);
|
|
33557
|
+
timer.unref?.();
|
|
33558
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33559
|
+
});
|
|
33560
|
+
}
|
|
33561
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33562
|
+
* not reject (callers wrap their own try/catch). */
|
|
33563
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33564
|
+
const queue = [...items];
|
|
33565
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33566
|
+
const lane = async () => {
|
|
33567
|
+
for (;;) {
|
|
33568
|
+
const item = queue.shift();
|
|
33569
|
+
if (item === void 0) return;
|
|
33570
|
+
await fn(item);
|
|
33571
|
+
}
|
|
33572
|
+
};
|
|
33573
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33574
|
+
}
|
|
33575
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33576
|
+
#logger;
|
|
33577
|
+
#attempt;
|
|
33578
|
+
#onPermanentFailure;
|
|
33579
|
+
#delaysMs;
|
|
33580
|
+
#concurrency;
|
|
33581
|
+
#now;
|
|
33582
|
+
#abort = new AbortController();
|
|
33583
|
+
constructor(options) {
|
|
33584
|
+
this.#logger = options.logger;
|
|
33585
|
+
this.#attempt = options.attempt;
|
|
33586
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33587
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33588
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33589
|
+
this.#now = options.now ?? Date.now;
|
|
33590
|
+
}
|
|
33591
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33592
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33593
|
+
cancel() {
|
|
33594
|
+
this.#abort.abort();
|
|
33595
|
+
}
|
|
33596
|
+
/**
|
|
33597
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33598
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33599
|
+
* cancelled. Never rejects.
|
|
33600
|
+
*/
|
|
33601
|
+
async run(initialFailures) {
|
|
33602
|
+
let pending = initialFailures.map((failure) => ({
|
|
33603
|
+
saved: failure.saved,
|
|
33604
|
+
lastError: failure.error,
|
|
33605
|
+
attempts: 1
|
|
33606
|
+
}));
|
|
33607
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33608
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33609
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33610
|
+
if (this.#abort.signal.aborted) break;
|
|
33611
|
+
pending = await this.#runRound(pending, round);
|
|
33612
|
+
}
|
|
33613
|
+
if (this.#abort.signal.aborted) return [];
|
|
33614
|
+
const terminal = pending.map((entry) => ({
|
|
33615
|
+
deviceId: entry.saved.id,
|
|
33616
|
+
stableId: entry.saved.stableId,
|
|
33617
|
+
type: String(entry.saved.type),
|
|
33618
|
+
attempts: entry.attempts,
|
|
33619
|
+
lastError: entry.lastError,
|
|
33620
|
+
failedAt: this.#now()
|
|
33621
|
+
}));
|
|
33622
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33623
|
+
return terminal;
|
|
33624
|
+
}
|
|
33625
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33626
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33627
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33628
|
+
async #runRound(pending, round) {
|
|
33629
|
+
const next = [];
|
|
33630
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33631
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33632
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33633
|
+
if (this.#abort.signal.aborted) {
|
|
33634
|
+
next.push(entry);
|
|
33635
|
+
return;
|
|
33636
|
+
}
|
|
33637
|
+
const attemptNo = entry.attempts + 1;
|
|
33638
|
+
try {
|
|
33639
|
+
await this.#attempt(entry.saved);
|
|
33640
|
+
this.#logger.info("Device restored on retry", {
|
|
33641
|
+
tags: {
|
|
33642
|
+
deviceId: entry.saved.id,
|
|
33643
|
+
stableId: entry.saved.stableId
|
|
33644
|
+
},
|
|
33645
|
+
meta: { attempt: attemptNo }
|
|
33646
|
+
});
|
|
33647
|
+
} catch (err) {
|
|
33648
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33649
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33650
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33651
|
+
tags: {
|
|
33652
|
+
deviceId: entry.saved.id,
|
|
33653
|
+
stableId: entry.saved.stableId
|
|
33654
|
+
},
|
|
33655
|
+
meta: {
|
|
33656
|
+
attempt: attemptNo,
|
|
33657
|
+
remainingRetries,
|
|
33658
|
+
error: lastError
|
|
33659
|
+
}
|
|
33660
|
+
});
|
|
33661
|
+
next.push({
|
|
33662
|
+
saved: entry.saved,
|
|
33663
|
+
lastError,
|
|
33664
|
+
attempts: attemptNo
|
|
33665
|
+
});
|
|
33666
|
+
}
|
|
33667
|
+
});
|
|
33668
|
+
return next;
|
|
33669
|
+
}
|
|
33670
|
+
};
|
|
33671
|
+
/**
|
|
33482
33672
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33483
33673
|
* device-provider cap router. Shared across all providers.
|
|
33484
33674
|
*/
|
|
@@ -33527,6 +33717,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33527
33717
|
}];
|
|
33528
33718
|
}
|
|
33529
33719
|
async onShutdown() {
|
|
33720
|
+
this.cancelRestoreRetries();
|
|
33530
33721
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33531
33722
|
for (const device of devices) try {
|
|
33532
33723
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33544,9 +33735,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33544
33735
|
async start() {}
|
|
33545
33736
|
async stop() {}
|
|
33546
33737
|
async getStatus() {
|
|
33738
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33739
|
+
const summary = this.restoreFailureSummary();
|
|
33740
|
+
if (summary === null) return {
|
|
33741
|
+
connected: true,
|
|
33742
|
+
deviceCount: all.length
|
|
33743
|
+
};
|
|
33547
33744
|
return {
|
|
33548
33745
|
connected: true,
|
|
33549
|
-
deviceCount:
|
|
33746
|
+
deviceCount: all.length,
|
|
33747
|
+
error: summary
|
|
33550
33748
|
};
|
|
33551
33749
|
}
|
|
33552
33750
|
async getDevices() {
|
|
@@ -33636,8 +33834,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33636
33834
|
};
|
|
33637
33835
|
}
|
|
33638
33836
|
async restoreDevices(savedDevices) {
|
|
33639
|
-
await this.onRestoreDevices(savedDevices);
|
|
33640
|
-
if (savedDevices.length
|
|
33837
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33838
|
+
if (savedDevices.length === 0) return;
|
|
33839
|
+
if (report && report.failedCount > 0) {
|
|
33840
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33841
|
+
return;
|
|
33842
|
+
}
|
|
33843
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33844
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33845
|
+
}
|
|
33846
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33847
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33848
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33849
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33850
|
+
restoreRetryConcurrency = 4;
|
|
33851
|
+
_restoreRetryScheduler = null;
|
|
33852
|
+
_restoreRetryCompletion = null;
|
|
33853
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33854
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33855
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33856
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33857
|
+
* with the devices that restored, and a late success is announced
|
|
33858
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33859
|
+
get restoreRetryCompletion() {
|
|
33860
|
+
return this._restoreRetryCompletion;
|
|
33861
|
+
}
|
|
33862
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33863
|
+
get permanentRestoreFailures() {
|
|
33864
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33865
|
+
}
|
|
33866
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33867
|
+
* `null` when every device restored. */
|
|
33868
|
+
restoreFailureSummary() {
|
|
33869
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33870
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33871
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33872
|
+
}
|
|
33873
|
+
cancelRestoreRetries() {
|
|
33874
|
+
this._restoreRetryScheduler?.cancel();
|
|
33875
|
+
this._restoreRetryScheduler = null;
|
|
33876
|
+
}
|
|
33877
|
+
recordPermanentRestoreFailure(failure) {
|
|
33878
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33879
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33880
|
+
tags: {
|
|
33881
|
+
deviceId: failure.deviceId,
|
|
33882
|
+
stableId: failure.stableId
|
|
33883
|
+
},
|
|
33884
|
+
meta: {
|
|
33885
|
+
type: failure.type,
|
|
33886
|
+
attempts: failure.attempts,
|
|
33887
|
+
error: failure.lastError
|
|
33888
|
+
}
|
|
33889
|
+
});
|
|
33890
|
+
}
|
|
33891
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33892
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33893
|
+
logger: this.ctx.logger,
|
|
33894
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33895
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33896
|
+
attempt,
|
|
33897
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33898
|
+
});
|
|
33899
|
+
this._restoreRetryScheduler = scheduler;
|
|
33900
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33901
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33902
|
+
});
|
|
33903
|
+
}
|
|
33904
|
+
/**
|
|
33905
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33906
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33907
|
+
* and no other device this provider owns is disturbed.
|
|
33908
|
+
*
|
|
33909
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33910
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33911
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33912
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33913
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33914
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33915
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33916
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33917
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33918
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33919
|
+
*
|
|
33920
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33921
|
+
* reload its parent instead.
|
|
33922
|
+
*/
|
|
33923
|
+
async reloadDevice(input) {
|
|
33924
|
+
const { stableId } = input;
|
|
33925
|
+
const devices = this.ctx.kernel.devices;
|
|
33926
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33927
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33928
|
+
if (live) await devices.decommission(live.id);
|
|
33929
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33930
|
+
addonId: this.addonId,
|
|
33931
|
+
stableId
|
|
33932
|
+
});
|
|
33933
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33934
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33935
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33936
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33937
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33938
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33939
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33940
|
+
for (const row of rows) {
|
|
33941
|
+
if (row.parentDeviceId !== id) continue;
|
|
33942
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33943
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33944
|
+
if (!ChildClass) continue;
|
|
33945
|
+
try {
|
|
33946
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33947
|
+
} catch (err) {
|
|
33948
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33949
|
+
tags: {
|
|
33950
|
+
deviceId: row.id,
|
|
33951
|
+
stableId: row.stableId
|
|
33952
|
+
},
|
|
33953
|
+
meta: {
|
|
33954
|
+
parentDeviceId: id,
|
|
33955
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33956
|
+
}
|
|
33957
|
+
});
|
|
33958
|
+
}
|
|
33959
|
+
}
|
|
33960
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33961
|
+
tags: { deviceId: id },
|
|
33962
|
+
meta: {
|
|
33963
|
+
stableId,
|
|
33964
|
+
type: meta.type
|
|
33965
|
+
}
|
|
33966
|
+
});
|
|
33967
|
+
return { deviceId: id };
|
|
33641
33968
|
}
|
|
33642
33969
|
/**
|
|
33643
33970
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33663,55 +33990,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33663
33990
|
* accessory-spawn flow handles via the parent's
|
|
33664
33991
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33665
33992
|
* fit.
|
|
33993
|
+
*
|
|
33994
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33995
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33996
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33997
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33998
|
+
* `getStatus().error`.
|
|
33666
33999
|
*/
|
|
34000
|
+
/**
|
|
34001
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
34002
|
+
* Default: no-op — most providers have nothing to heal.
|
|
34003
|
+
*
|
|
34004
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
34005
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
34006
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
34007
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
34008
|
+
* been emptied failed all four bounded attempts against fields
|
|
34009
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
34010
|
+
*
|
|
34011
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
34012
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
34013
|
+
* the bound, then reported — never swallowed.
|
|
34014
|
+
*/
|
|
34015
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
33667
34016
|
async onRestoreDevices(savedDevices) {
|
|
33668
34017
|
const restored = /* @__PURE__ */ new Set();
|
|
34018
|
+
const failures = [];
|
|
34019
|
+
const attemptRestore = async (saved) => {
|
|
34020
|
+
if (restored.has(saved.id)) return;
|
|
34021
|
+
const Class = this.deviceClasses[saved.type];
|
|
34022
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
34023
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
34024
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
34025
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
34026
|
+
restored.add(saved.id);
|
|
34027
|
+
};
|
|
33669
34028
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33670
34029
|
const restoreOne = async (saved) => {
|
|
33671
|
-
|
|
33672
|
-
if (!Class) {
|
|
34030
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33673
34031
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33674
|
-
tags: {
|
|
34032
|
+
tags: {
|
|
34033
|
+
deviceId: saved.id,
|
|
34034
|
+
stableId: saved.stableId
|
|
34035
|
+
},
|
|
33675
34036
|
meta: { type: saved.type }
|
|
33676
34037
|
});
|
|
33677
34038
|
return;
|
|
33678
34039
|
}
|
|
33679
34040
|
try {
|
|
33680
|
-
await
|
|
33681
|
-
restored.add(saved.id);
|
|
34041
|
+
await attemptRestore(saved);
|
|
33682
34042
|
} catch (err) {
|
|
33683
|
-
|
|
33684
|
-
|
|
34043
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34044
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
34045
|
+
tags: {
|
|
34046
|
+
deviceId: saved.id,
|
|
34047
|
+
stableId: saved.stableId
|
|
34048
|
+
},
|
|
33685
34049
|
meta: {
|
|
33686
34050
|
type: saved.type,
|
|
33687
|
-
|
|
34051
|
+
attempt: 1,
|
|
34052
|
+
error
|
|
33688
34053
|
}
|
|
33689
34054
|
});
|
|
34055
|
+
failures.push({
|
|
34056
|
+
saved,
|
|
34057
|
+
error
|
|
34058
|
+
});
|
|
33690
34059
|
}
|
|
33691
34060
|
};
|
|
33692
34061
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
34062
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33693
34063
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33694
34064
|
for (const saved of childRows) {
|
|
33695
|
-
|
|
33696
|
-
if (!Class) continue;
|
|
34065
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33697
34066
|
if (saved.parentDeviceId === null) continue;
|
|
33698
|
-
if (
|
|
33699
|
-
|
|
33700
|
-
|
|
33701
|
-
|
|
33702
|
-
|
|
33703
|
-
|
|
34067
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
34068
|
+
try {
|
|
34069
|
+
await attemptRestore(saved);
|
|
34070
|
+
} catch (err) {
|
|
34071
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
34072
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
34073
|
+
tags: {
|
|
34074
|
+
deviceId: saved.id,
|
|
34075
|
+
stableId: saved.stableId,
|
|
34076
|
+
parentDeviceId: saved.parentDeviceId
|
|
34077
|
+
},
|
|
34078
|
+
meta: {
|
|
34079
|
+
type: saved.type,
|
|
34080
|
+
attempt: 1,
|
|
34081
|
+
error
|
|
34082
|
+
}
|
|
34083
|
+
});
|
|
34084
|
+
failures.push({
|
|
34085
|
+
saved,
|
|
34086
|
+
error
|
|
34087
|
+
});
|
|
34088
|
+
}
|
|
34089
|
+
continue;
|
|
34090
|
+
}
|
|
34091
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
34092
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33704
34093
|
tags: {
|
|
34094
|
+
deviceId: saved.id,
|
|
33705
34095
|
stableId: saved.stableId,
|
|
33706
34096
|
parentDeviceId: saved.parentDeviceId
|
|
33707
34097
|
},
|
|
33708
|
-
meta: {
|
|
33709
|
-
|
|
33710
|
-
|
|
33711
|
-
|
|
34098
|
+
meta: { type: saved.type }
|
|
34099
|
+
});
|
|
34100
|
+
failures.push({
|
|
34101
|
+
saved,
|
|
34102
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33712
34103
|
});
|
|
34104
|
+
continue;
|
|
33713
34105
|
}
|
|
33714
34106
|
}
|
|
34107
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
34108
|
+
return {
|
|
34109
|
+
restoredCount: restored.size,
|
|
34110
|
+
failedCount: failures.length
|
|
34111
|
+
};
|
|
33715
34112
|
}
|
|
33716
34113
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33717
34114
|
toSummary(device) {
|
|
@@ -35474,6 +35871,12 @@ Object.freeze({
|
|
|
35474
35871
|
addonId: null,
|
|
35475
35872
|
access: "view"
|
|
35476
35873
|
},
|
|
35874
|
+
"deviceProvider.reloadDevice": {
|
|
35875
|
+
capName: "device-provider",
|
|
35876
|
+
capScope: "system",
|
|
35877
|
+
addonId: null,
|
|
35878
|
+
access: "create"
|
|
35879
|
+
},
|
|
35477
35880
|
"deviceProvider.start": {
|
|
35478
35881
|
capName: "device-provider",
|
|
35479
35882
|
capScope: "system",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camstack/addon-provider-rademacher",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.60",
|
|
4
4
|
"description": "Rademacher HomePilot device-provider addon for CamStack — wraps the @apocaliss92/noderademacher local-hub client (roller shutters over the cover cap)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"camstack",
|