@camstack/types 1.2.142 → 1.2.144
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 +1 -1
- package/dist/addon.mjs +1 -1
- package/dist/capabilities/device-manager.cap.d.ts +19 -0
- package/dist/capabilities/device-provider.cap.d.ts +39 -0
- package/dist/device/base-device-provider.d.ts +93 -1
- package/dist/device/device-restore-retry.d.ts +93 -0
- package/dist/device/index.d.ts +3 -1
- package/dist/generated/addon-api.d.ts +7 -0
- package/dist/generated/device-proxy.d.ts +2 -0
- package/dist/generated/method-access-map.d.ts +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +444 -26
- package/dist/index.mjs +442 -27
- package/dist/{sleep-Cwo3fRlv.mjs → sleep-B195W080.mjs} +1 -0
- package/dist/{sleep-BDmIj1HV.js → sleep-jdpPltQH.js} +1 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_event_category = require("./event-category-BaEgqJNv.js");
|
|
3
|
-
const require_sleep = require("./sleep-
|
|
3
|
+
const require_sleep = require("./sleep-jdpPltQH.js");
|
|
4
4
|
const require_canonical_hash = require("./canonical-hash-DNV8S5ET.js");
|
|
5
5
|
const require_enums = require("./enums.js");
|
|
6
6
|
const require_err_msg = require("./err-msg-COpsHMw2.js");
|
|
@@ -10061,7 +10061,26 @@ var DiscoveryCandidateSchema = zod.z.object({
|
|
|
10061
10061
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
10062
10062
|
* flows live through the cap STATUS SLICE after adoption.
|
|
10063
10063
|
*/
|
|
10064
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
10064
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
10065
|
+
/**
|
|
10066
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
10067
|
+
*
|
|
10068
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
10069
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
10070
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
10071
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
10072
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
10073
|
+
* three child cameras offline for four hours.
|
|
10074
|
+
*
|
|
10075
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
10076
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
10077
|
+
* that cannot tell simply never sets it.
|
|
10078
|
+
*/
|
|
10079
|
+
alreadyOnboarded: zod.z.boolean().optional(),
|
|
10080
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
10081
|
+
onboardedDeviceId: zod.z.number().optional(),
|
|
10082
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
10083
|
+
onboardedName: zod.z.string().optional()
|
|
10065
10084
|
});
|
|
10066
10085
|
/**
|
|
10067
10086
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -10117,6 +10136,35 @@ var deviceProviderCapability = {
|
|
|
10117
10136
|
name: zod.z.string(),
|
|
10118
10137
|
type: zod.z.string()
|
|
10119
10138
|
}))),
|
|
10139
|
+
/**
|
|
10140
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
10141
|
+
* touching no other device this provider owns.
|
|
10142
|
+
*
|
|
10143
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
10144
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
10145
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
10146
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
10147
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
10148
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
10149
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
10150
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
10151
|
+
*
|
|
10152
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
10153
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
10154
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
10155
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
10156
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
10157
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
10158
|
+
* against the wrong camera (D8).
|
|
10159
|
+
*
|
|
10160
|
+
* Construction can dial hardware, and the migrated source is
|
|
10161
|
+
* characteristically dead — the timeout covers a full activate window
|
|
10162
|
+
* rather than the 60 s default.
|
|
10163
|
+
*/
|
|
10164
|
+
reloadDevice: require_sleep.method(zod.z.object({ stableId: zod.z.string() }), zod.z.object({ deviceId: zod.z.number() }), {
|
|
10165
|
+
kind: "mutation",
|
|
10166
|
+
timeoutMs: 3 * 6e4
|
|
10167
|
+
}),
|
|
10120
10168
|
supportsDiscovery: require_sleep.method(zod.z.object({}), zod.z.boolean()),
|
|
10121
10169
|
/**
|
|
10122
10170
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -10511,13 +10559,21 @@ var deviceManagerCapability = {
|
|
|
10511
10559
|
* `sourceStillLive` names what stayed live; empty is the only value that
|
|
10512
10560
|
* means the old hardware is quiet. The migration proceeds either way —
|
|
10513
10561
|
* refusing would refuse the case this exists for.
|
|
10562
|
+
*
|
|
10563
|
+
* **Safe to retry.** A duplicate call while a migration involving either
|
|
10564
|
+
* device is in flight is REFUSED, never queued; a repeat of a swap that
|
|
10565
|
+
* already completed (matched against the `addonId`/`stableId` fingerprint
|
|
10566
|
+
* the swap left on the rows, within 15 min) is a no-op returning the
|
|
10567
|
+
* original report. A blind re-run can no longer silently undo the
|
|
10568
|
+
* migration it retried.
|
|
10514
10569
|
*/
|
|
10515
10570
|
migrateDevice: require_sleep.method(zod.z.object({
|
|
10516
10571
|
sourceId: zod.z.number(),
|
|
10517
10572
|
targetId: zod.z.number()
|
|
10518
10573
|
}), MigrateDeviceResultSchema, {
|
|
10519
10574
|
kind: "mutation",
|
|
10520
|
-
auth: "admin"
|
|
10575
|
+
auth: "admin",
|
|
10576
|
+
timeoutMs: 12 * 6e4
|
|
10521
10577
|
}),
|
|
10522
10578
|
/** Register a device in the DB + in-memory registry. Called by DeviceManagerApi.register(). */
|
|
10523
10579
|
registerDevice: require_sleep.method(DeviceRegisterPayloadSchema, zod.z.void(), { kind: "mutation" }),
|
|
@@ -36391,6 +36447,152 @@ var BaseDevice = class {
|
|
|
36391
36447
|
}
|
|
36392
36448
|
};
|
|
36393
36449
|
//#endregion
|
|
36450
|
+
//#region src/device/device-restore-retry.ts
|
|
36451
|
+
/**
|
|
36452
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
36453
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
36454
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
36455
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
36456
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
36457
|
+
*/
|
|
36458
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
36459
|
+
1e4,
|
|
36460
|
+
3e4,
|
|
36461
|
+
9e4
|
|
36462
|
+
];
|
|
36463
|
+
/** Lane width for retry rounds. See module docblock — retries are
|
|
36464
|
+
* evidence of congestion, so they never re-stampede full-width. */
|
|
36465
|
+
var DEVICE_RESTORE_RETRY_CONCURRENCY = 4;
|
|
36466
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
36467
|
+
function sleep$1(ms, signal) {
|
|
36468
|
+
return new Promise((resolve) => {
|
|
36469
|
+
if (signal.aborted) {
|
|
36470
|
+
resolve();
|
|
36471
|
+
return;
|
|
36472
|
+
}
|
|
36473
|
+
const onAbort = () => {
|
|
36474
|
+
clearTimeout(timer);
|
|
36475
|
+
resolve();
|
|
36476
|
+
};
|
|
36477
|
+
const timer = setTimeout(() => {
|
|
36478
|
+
signal.removeEventListener("abort", onAbort);
|
|
36479
|
+
resolve();
|
|
36480
|
+
}, ms);
|
|
36481
|
+
timer.unref?.();
|
|
36482
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
36483
|
+
});
|
|
36484
|
+
}
|
|
36485
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
36486
|
+
* not reject (callers wrap their own try/catch). */
|
|
36487
|
+
async function runWithConcurrency(items, width, fn) {
|
|
36488
|
+
const queue = [...items];
|
|
36489
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
36490
|
+
const lane = async () => {
|
|
36491
|
+
for (;;) {
|
|
36492
|
+
const item = queue.shift();
|
|
36493
|
+
if (item === void 0) return;
|
|
36494
|
+
await fn(item);
|
|
36495
|
+
}
|
|
36496
|
+
};
|
|
36497
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
36498
|
+
}
|
|
36499
|
+
var DeviceRestoreRetryScheduler = class {
|
|
36500
|
+
#logger;
|
|
36501
|
+
#attempt;
|
|
36502
|
+
#onPermanentFailure;
|
|
36503
|
+
#delaysMs;
|
|
36504
|
+
#concurrency;
|
|
36505
|
+
#now;
|
|
36506
|
+
#abort = new AbortController();
|
|
36507
|
+
constructor(options) {
|
|
36508
|
+
this.#logger = options.logger;
|
|
36509
|
+
this.#attempt = options.attempt;
|
|
36510
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
36511
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
36512
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
36513
|
+
this.#now = options.now ?? Date.now;
|
|
36514
|
+
}
|
|
36515
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
36516
|
+
* permanently failed — the next boot restores them from disk. */
|
|
36517
|
+
cancel() {
|
|
36518
|
+
this.#abort.abort();
|
|
36519
|
+
}
|
|
36520
|
+
/**
|
|
36521
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
36522
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
36523
|
+
* cancelled. Never rejects.
|
|
36524
|
+
*/
|
|
36525
|
+
async run(initialFailures) {
|
|
36526
|
+
let pending = initialFailures.map((failure) => ({
|
|
36527
|
+
saved: failure.saved,
|
|
36528
|
+
lastError: failure.error,
|
|
36529
|
+
attempts: 1
|
|
36530
|
+
}));
|
|
36531
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
36532
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
36533
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
36534
|
+
if (this.#abort.signal.aborted) break;
|
|
36535
|
+
pending = await this.#runRound(pending, round);
|
|
36536
|
+
}
|
|
36537
|
+
if (this.#abort.signal.aborted) return [];
|
|
36538
|
+
const terminal = pending.map((entry) => ({
|
|
36539
|
+
deviceId: entry.saved.id,
|
|
36540
|
+
stableId: entry.saved.stableId,
|
|
36541
|
+
type: String(entry.saved.type),
|
|
36542
|
+
attempts: entry.attempts,
|
|
36543
|
+
lastError: entry.lastError,
|
|
36544
|
+
failedAt: this.#now()
|
|
36545
|
+
}));
|
|
36546
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
36547
|
+
return terminal;
|
|
36548
|
+
}
|
|
36549
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
36550
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
36551
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
36552
|
+
async #runRound(pending, round) {
|
|
36553
|
+
const next = [];
|
|
36554
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
36555
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
36556
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
36557
|
+
if (this.#abort.signal.aborted) {
|
|
36558
|
+
next.push(entry);
|
|
36559
|
+
return;
|
|
36560
|
+
}
|
|
36561
|
+
const attemptNo = entry.attempts + 1;
|
|
36562
|
+
try {
|
|
36563
|
+
await this.#attempt(entry.saved);
|
|
36564
|
+
this.#logger.info("Device restored on retry", {
|
|
36565
|
+
tags: {
|
|
36566
|
+
deviceId: entry.saved.id,
|
|
36567
|
+
stableId: entry.saved.stableId
|
|
36568
|
+
},
|
|
36569
|
+
meta: { attempt: attemptNo }
|
|
36570
|
+
});
|
|
36571
|
+
} catch (err) {
|
|
36572
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
36573
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
36574
|
+
this.#logger.warn("Device restore retry failed", {
|
|
36575
|
+
tags: {
|
|
36576
|
+
deviceId: entry.saved.id,
|
|
36577
|
+
stableId: entry.saved.stableId
|
|
36578
|
+
},
|
|
36579
|
+
meta: {
|
|
36580
|
+
attempt: attemptNo,
|
|
36581
|
+
remainingRetries,
|
|
36582
|
+
error: lastError
|
|
36583
|
+
}
|
|
36584
|
+
});
|
|
36585
|
+
next.push({
|
|
36586
|
+
saved: entry.saved,
|
|
36587
|
+
lastError,
|
|
36588
|
+
attempts: attemptNo
|
|
36589
|
+
});
|
|
36590
|
+
}
|
|
36591
|
+
});
|
|
36592
|
+
return next;
|
|
36593
|
+
}
|
|
36594
|
+
};
|
|
36595
|
+
//#endregion
|
|
36394
36596
|
//#region src/device/base-device-provider.ts
|
|
36395
36597
|
/**
|
|
36396
36598
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
@@ -36441,6 +36643,7 @@ var BaseDeviceProvider = class extends require_sleep.BaseAddon {
|
|
|
36441
36643
|
}];
|
|
36442
36644
|
}
|
|
36443
36645
|
async onShutdown() {
|
|
36646
|
+
this.cancelRestoreRetries();
|
|
36444
36647
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
36445
36648
|
for (const device of devices) try {
|
|
36446
36649
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -36458,9 +36661,16 @@ var BaseDeviceProvider = class extends require_sleep.BaseAddon {
|
|
|
36458
36661
|
async start() {}
|
|
36459
36662
|
async stop() {}
|
|
36460
36663
|
async getStatus() {
|
|
36664
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
36665
|
+
const summary = this.restoreFailureSummary();
|
|
36666
|
+
if (summary === null) return {
|
|
36667
|
+
connected: true,
|
|
36668
|
+
deviceCount: all.length
|
|
36669
|
+
};
|
|
36461
36670
|
return {
|
|
36462
36671
|
connected: true,
|
|
36463
|
-
deviceCount:
|
|
36672
|
+
deviceCount: all.length,
|
|
36673
|
+
error: summary
|
|
36464
36674
|
};
|
|
36465
36675
|
}
|
|
36466
36676
|
async getDevices() {
|
|
@@ -36550,8 +36760,137 @@ var BaseDeviceProvider = class extends require_sleep.BaseAddon {
|
|
|
36550
36760
|
};
|
|
36551
36761
|
}
|
|
36552
36762
|
async restoreDevices(savedDevices) {
|
|
36553
|
-
await this.onRestoreDevices(savedDevices);
|
|
36554
|
-
if (savedDevices.length
|
|
36763
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
36764
|
+
if (savedDevices.length === 0) return;
|
|
36765
|
+
if (report && report.failedCount > 0) {
|
|
36766
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
36767
|
+
return;
|
|
36768
|
+
}
|
|
36769
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
36770
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
36771
|
+
}
|
|
36772
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
36773
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
36774
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
36775
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
36776
|
+
restoreRetryConcurrency = 4;
|
|
36777
|
+
_restoreRetryScheduler = null;
|
|
36778
|
+
_restoreRetryCompletion = null;
|
|
36779
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
36780
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
36781
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
36782
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
36783
|
+
* with the devices that restored, and a late success is announced
|
|
36784
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
36785
|
+
get restoreRetryCompletion() {
|
|
36786
|
+
return this._restoreRetryCompletion;
|
|
36787
|
+
}
|
|
36788
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
36789
|
+
get permanentRestoreFailures() {
|
|
36790
|
+
return [...this._permanentRestoreFailures.values()];
|
|
36791
|
+
}
|
|
36792
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
36793
|
+
* `null` when every device restored. */
|
|
36794
|
+
restoreFailureSummary() {
|
|
36795
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
36796
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
36797
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
36798
|
+
}
|
|
36799
|
+
cancelRestoreRetries() {
|
|
36800
|
+
this._restoreRetryScheduler?.cancel();
|
|
36801
|
+
this._restoreRetryScheduler = null;
|
|
36802
|
+
}
|
|
36803
|
+
recordPermanentRestoreFailure(failure) {
|
|
36804
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
36805
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
36806
|
+
tags: {
|
|
36807
|
+
deviceId: failure.deviceId,
|
|
36808
|
+
stableId: failure.stableId
|
|
36809
|
+
},
|
|
36810
|
+
meta: {
|
|
36811
|
+
type: failure.type,
|
|
36812
|
+
attempts: failure.attempts,
|
|
36813
|
+
error: failure.lastError
|
|
36814
|
+
}
|
|
36815
|
+
});
|
|
36816
|
+
}
|
|
36817
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
36818
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
36819
|
+
logger: this.ctx.logger,
|
|
36820
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
36821
|
+
concurrency: this.restoreRetryConcurrency,
|
|
36822
|
+
attempt,
|
|
36823
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
36824
|
+
});
|
|
36825
|
+
this._restoreRetryScheduler = scheduler;
|
|
36826
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
36827
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
36828
|
+
});
|
|
36829
|
+
}
|
|
36830
|
+
/**
|
|
36831
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
36832
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
36833
|
+
* and no other device this provider owns is disturbed.
|
|
36834
|
+
*
|
|
36835
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
36836
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
36837
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
36838
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
36839
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
36840
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
36841
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
36842
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
36843
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
36844
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
36845
|
+
*
|
|
36846
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
36847
|
+
* reload its parent instead.
|
|
36848
|
+
*/
|
|
36849
|
+
async reloadDevice(input) {
|
|
36850
|
+
const { stableId } = input;
|
|
36851
|
+
const devices = this.ctx.kernel.devices;
|
|
36852
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
36853
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
36854
|
+
if (live) await devices.decommission(live.id);
|
|
36855
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
36856
|
+
addonId: this.addonId,
|
|
36857
|
+
stableId
|
|
36858
|
+
});
|
|
36859
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
36860
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
36861
|
+
const deviceType = Object.values(require_sleep.DeviceType).find((t) => t === meta.type);
|
|
36862
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
36863
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
36864
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
36865
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
36866
|
+
for (const row of rows) {
|
|
36867
|
+
if (row.parentDeviceId !== id) continue;
|
|
36868
|
+
const childType = Object.values(require_sleep.DeviceType).find((t) => t === row.type);
|
|
36869
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
36870
|
+
if (!ChildClass) continue;
|
|
36871
|
+
try {
|
|
36872
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
36873
|
+
} catch (err) {
|
|
36874
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
36875
|
+
tags: {
|
|
36876
|
+
deviceId: row.id,
|
|
36877
|
+
stableId: row.stableId
|
|
36878
|
+
},
|
|
36879
|
+
meta: {
|
|
36880
|
+
parentDeviceId: id,
|
|
36881
|
+
error: err instanceof Error ? err.message : String(err)
|
|
36882
|
+
}
|
|
36883
|
+
});
|
|
36884
|
+
}
|
|
36885
|
+
}
|
|
36886
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
36887
|
+
tags: { deviceId: id },
|
|
36888
|
+
meta: {
|
|
36889
|
+
stableId,
|
|
36890
|
+
type: meta.type
|
|
36891
|
+
}
|
|
36892
|
+
});
|
|
36893
|
+
return { deviceId: id };
|
|
36555
36894
|
}
|
|
36556
36895
|
/**
|
|
36557
36896
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -36577,55 +36916,125 @@ var BaseDeviceProvider = class extends require_sleep.BaseAddon {
|
|
|
36577
36916
|
* accessory-spawn flow handles via the parent's
|
|
36578
36917
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
36579
36918
|
* fit.
|
|
36919
|
+
*
|
|
36920
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
36921
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
36922
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
36923
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
36924
|
+
* `getStatus().error`.
|
|
36580
36925
|
*/
|
|
36926
|
+
/**
|
|
36927
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
36928
|
+
* Default: no-op — most providers have nothing to heal.
|
|
36929
|
+
*
|
|
36930
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
36931
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
36932
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
36933
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
36934
|
+
* been emptied failed all four bounded attempts against fields
|
|
36935
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
36936
|
+
*
|
|
36937
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
36938
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
36939
|
+
* the bound, then reported — never swallowed.
|
|
36940
|
+
*/
|
|
36941
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
36581
36942
|
async onRestoreDevices(savedDevices) {
|
|
36582
36943
|
const restored = /* @__PURE__ */ new Set();
|
|
36944
|
+
const failures = [];
|
|
36945
|
+
const attemptRestore = async (saved) => {
|
|
36946
|
+
if (restored.has(saved.id)) return;
|
|
36947
|
+
const Class = this.deviceClasses[saved.type];
|
|
36948
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
36949
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
36950
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
36951
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
36952
|
+
restored.add(saved.id);
|
|
36953
|
+
};
|
|
36583
36954
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
36584
36955
|
const restoreOne = async (saved) => {
|
|
36585
|
-
|
|
36586
|
-
if (!Class) {
|
|
36956
|
+
if (!this.deviceClasses[saved.type]) {
|
|
36587
36957
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
36588
|
-
tags: {
|
|
36958
|
+
tags: {
|
|
36959
|
+
deviceId: saved.id,
|
|
36960
|
+
stableId: saved.stableId
|
|
36961
|
+
},
|
|
36589
36962
|
meta: { type: saved.type }
|
|
36590
36963
|
});
|
|
36591
36964
|
return;
|
|
36592
36965
|
}
|
|
36593
36966
|
try {
|
|
36594
|
-
await
|
|
36595
|
-
restored.add(saved.id);
|
|
36967
|
+
await attemptRestore(saved);
|
|
36596
36968
|
} catch (err) {
|
|
36597
|
-
|
|
36598
|
-
|
|
36969
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
36970
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
36971
|
+
tags: {
|
|
36972
|
+
deviceId: saved.id,
|
|
36973
|
+
stableId: saved.stableId
|
|
36974
|
+
},
|
|
36599
36975
|
meta: {
|
|
36600
36976
|
type: saved.type,
|
|
36601
|
-
|
|
36977
|
+
attempt: 1,
|
|
36978
|
+
error
|
|
36602
36979
|
}
|
|
36603
36980
|
});
|
|
36981
|
+
failures.push({
|
|
36982
|
+
saved,
|
|
36983
|
+
error
|
|
36984
|
+
});
|
|
36604
36985
|
}
|
|
36605
36986
|
};
|
|
36606
36987
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
36988
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
36607
36989
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
36608
36990
|
for (const saved of childRows) {
|
|
36609
|
-
|
|
36610
|
-
if (!Class) continue;
|
|
36991
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
36611
36992
|
if (saved.parentDeviceId === null) continue;
|
|
36612
|
-
if (
|
|
36613
|
-
|
|
36614
|
-
|
|
36615
|
-
|
|
36616
|
-
|
|
36617
|
-
|
|
36993
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
36994
|
+
try {
|
|
36995
|
+
await attemptRestore(saved);
|
|
36996
|
+
} catch (err) {
|
|
36997
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
36998
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
36999
|
+
tags: {
|
|
37000
|
+
deviceId: saved.id,
|
|
37001
|
+
stableId: saved.stableId,
|
|
37002
|
+
parentDeviceId: saved.parentDeviceId
|
|
37003
|
+
},
|
|
37004
|
+
meta: {
|
|
37005
|
+
type: saved.type,
|
|
37006
|
+
attempt: 1,
|
|
37007
|
+
error
|
|
37008
|
+
}
|
|
37009
|
+
});
|
|
37010
|
+
failures.push({
|
|
37011
|
+
saved,
|
|
37012
|
+
error
|
|
37013
|
+
});
|
|
37014
|
+
}
|
|
37015
|
+
continue;
|
|
37016
|
+
}
|
|
37017
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
37018
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
36618
37019
|
tags: {
|
|
37020
|
+
deviceId: saved.id,
|
|
36619
37021
|
stableId: saved.stableId,
|
|
36620
37022
|
parentDeviceId: saved.parentDeviceId
|
|
36621
37023
|
},
|
|
36622
|
-
meta: {
|
|
36623
|
-
|
|
36624
|
-
|
|
36625
|
-
|
|
37024
|
+
meta: { type: saved.type }
|
|
37025
|
+
});
|
|
37026
|
+
failures.push({
|
|
37027
|
+
saved,
|
|
37028
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
36626
37029
|
});
|
|
37030
|
+
continue;
|
|
36627
37031
|
}
|
|
36628
37032
|
}
|
|
37033
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
37034
|
+
return {
|
|
37035
|
+
restoredCount: restored.size,
|
|
37036
|
+
failedCount: failures.length
|
|
37037
|
+
};
|
|
36629
37038
|
}
|
|
36630
37039
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
36631
37040
|
toSummary(device) {
|
|
@@ -41033,6 +41442,12 @@ var METHOD_ACCESS_MAP = Object.freeze({
|
|
|
41033
41442
|
addonId: null,
|
|
41034
41443
|
access: "view"
|
|
41035
41444
|
},
|
|
41445
|
+
"deviceProvider.reloadDevice": {
|
|
41446
|
+
capName: "device-provider",
|
|
41447
|
+
capScope: "system",
|
|
41448
|
+
addonId: null,
|
|
41449
|
+
access: "create"
|
|
41450
|
+
},
|
|
41036
41451
|
"deviceProvider.start": {
|
|
41037
41452
|
capName: "device-provider",
|
|
41038
41453
|
capScope: "system",
|
|
@@ -52779,6 +53194,8 @@ exports.DEVICE_BACKEND_TO_FORMAT = DEVICE_BACKEND_TO_FORMAT;
|
|
|
52779
53194
|
exports.DEVICE_CAP_NAMES = DEVICE_CAP_NAMES;
|
|
52780
53195
|
exports.DEVICE_CHILDREN_BATCH_MAX = DEVICE_CHILDREN_BATCH_MAX;
|
|
52781
53196
|
exports.DEVICE_PROFILES = DEVICE_PROFILES;
|
|
53197
|
+
exports.DEVICE_RESTORE_RETRY_CONCURRENCY = DEVICE_RESTORE_RETRY_CONCURRENCY;
|
|
53198
|
+
exports.DEVICE_RESTORE_RETRY_DELAYS_MS = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
52782
53199
|
exports.DEVICE_SCOPED_CAPS = require_sleep.DEVICE_SCOPED_CAPS;
|
|
52783
53200
|
exports.DEVICE_SETTINGS_CONTRIBUTION_METHODS = require_sleep.DEVICE_SETTINGS_CONTRIBUTION_METHODS;
|
|
52784
53201
|
exports.DEVICE_STATE_READERS = DEVICE_STATE_READERS;
|
|
@@ -52808,6 +53225,7 @@ exports.DeviceExportUnexposeInputSchema = UnexposeInputSchema;
|
|
|
52808
53225
|
exports.DeviceFeature = require_sleep.DeviceFeature;
|
|
52809
53226
|
exports.DeviceInfoSchema = DeviceInfoSchema;
|
|
52810
53227
|
exports.DeviceNetworkStatsSchema = DeviceNetworkStatsSchema;
|
|
53228
|
+
exports.DeviceRestoreRetryScheduler = DeviceRestoreRetryScheduler;
|
|
52811
53229
|
exports.DeviceRole = require_sleep.DeviceRole;
|
|
52812
53230
|
exports.DeviceRuntimeState = DeviceRuntimeState;
|
|
52813
53231
|
exports.DeviceSelectorSchema = DeviceSelectorSchema;
|