@camstack/addon-provider-hikvision 1.2.67 → 1.2.69
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
|
@@ -13044,7 +13044,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
13044
13044
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
13045
13045
|
* flows live through the cap STATUS SLICE after adoption.
|
|
13046
13046
|
*/
|
|
13047
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
13047
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
13048
|
+
/**
|
|
13049
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
13050
|
+
*
|
|
13051
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
13052
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
13053
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
13054
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
13055
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
13056
|
+
* three child cameras offline for four hours.
|
|
13057
|
+
*
|
|
13058
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
13059
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
13060
|
+
* that cannot tell simply never sets it.
|
|
13061
|
+
*/
|
|
13062
|
+
alreadyOnboarded: boolean().optional(),
|
|
13063
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
13064
|
+
onboardedDeviceId: number().optional(),
|
|
13065
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
13066
|
+
onboardedName: string().optional()
|
|
13048
13067
|
});
|
|
13049
13068
|
/**
|
|
13050
13069
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -13100,6 +13119,35 @@ var deviceProviderCapability = {
|
|
|
13100
13119
|
name: string(),
|
|
13101
13120
|
type: string()
|
|
13102
13121
|
}))),
|
|
13122
|
+
/**
|
|
13123
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13124
|
+
* touching no other device this provider owns.
|
|
13125
|
+
*
|
|
13126
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13127
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13128
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13129
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13130
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13131
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13132
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13133
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13134
|
+
*
|
|
13135
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13136
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13137
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13138
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13139
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13140
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13141
|
+
* against the wrong camera (D8).
|
|
13142
|
+
*
|
|
13143
|
+
* Construction can dial hardware, and the migrated source is
|
|
13144
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13145
|
+
* rather than the 60 s default.
|
|
13146
|
+
*/
|
|
13147
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13148
|
+
kind: "mutation",
|
|
13149
|
+
timeoutMs: 3 * 6e4
|
|
13150
|
+
}),
|
|
13103
13151
|
supportsDiscovery: method(object({}), boolean()),
|
|
13104
13152
|
/**
|
|
13105
13153
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13427,7 +13475,8 @@ method(object({
|
|
|
13427
13475
|
targetId: number()
|
|
13428
13476
|
}), MigrateDeviceResultSchema, {
|
|
13429
13477
|
kind: "mutation",
|
|
13430
|
-
auth: "admin"
|
|
13478
|
+
auth: "admin",
|
|
13479
|
+
timeoutMs: 12 * 6e4
|
|
13431
13480
|
}), 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({
|
|
13432
13481
|
deviceId: number(),
|
|
13433
13482
|
name: string()
|
|
@@ -33252,6 +33301,147 @@ var BaseDevice = class {
|
|
|
33252
33301
|
}
|
|
33253
33302
|
};
|
|
33254
33303
|
/**
|
|
33304
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33305
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33306
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33307
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33308
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33309
|
+
*/
|
|
33310
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33311
|
+
1e4,
|
|
33312
|
+
3e4,
|
|
33313
|
+
9e4
|
|
33314
|
+
];
|
|
33315
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33316
|
+
function sleep$1(ms, signal) {
|
|
33317
|
+
return new Promise((resolve) => {
|
|
33318
|
+
if (signal.aborted) {
|
|
33319
|
+
resolve();
|
|
33320
|
+
return;
|
|
33321
|
+
}
|
|
33322
|
+
const onAbort = () => {
|
|
33323
|
+
clearTimeout(timer);
|
|
33324
|
+
resolve();
|
|
33325
|
+
};
|
|
33326
|
+
const timer = setTimeout(() => {
|
|
33327
|
+
signal.removeEventListener("abort", onAbort);
|
|
33328
|
+
resolve();
|
|
33329
|
+
}, ms);
|
|
33330
|
+
timer.unref?.();
|
|
33331
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33332
|
+
});
|
|
33333
|
+
}
|
|
33334
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33335
|
+
* not reject (callers wrap their own try/catch). */
|
|
33336
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33337
|
+
const queue = [...items];
|
|
33338
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33339
|
+
const lane = async () => {
|
|
33340
|
+
for (;;) {
|
|
33341
|
+
const item = queue.shift();
|
|
33342
|
+
if (item === void 0) return;
|
|
33343
|
+
await fn(item);
|
|
33344
|
+
}
|
|
33345
|
+
};
|
|
33346
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33347
|
+
}
|
|
33348
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33349
|
+
#logger;
|
|
33350
|
+
#attempt;
|
|
33351
|
+
#onPermanentFailure;
|
|
33352
|
+
#delaysMs;
|
|
33353
|
+
#concurrency;
|
|
33354
|
+
#now;
|
|
33355
|
+
#abort = new AbortController();
|
|
33356
|
+
constructor(options) {
|
|
33357
|
+
this.#logger = options.logger;
|
|
33358
|
+
this.#attempt = options.attempt;
|
|
33359
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33360
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33361
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33362
|
+
this.#now = options.now ?? Date.now;
|
|
33363
|
+
}
|
|
33364
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33365
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33366
|
+
cancel() {
|
|
33367
|
+
this.#abort.abort();
|
|
33368
|
+
}
|
|
33369
|
+
/**
|
|
33370
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33371
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33372
|
+
* cancelled. Never rejects.
|
|
33373
|
+
*/
|
|
33374
|
+
async run(initialFailures) {
|
|
33375
|
+
let pending = initialFailures.map((failure) => ({
|
|
33376
|
+
saved: failure.saved,
|
|
33377
|
+
lastError: failure.error,
|
|
33378
|
+
attempts: 1
|
|
33379
|
+
}));
|
|
33380
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33381
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33382
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33383
|
+
if (this.#abort.signal.aborted) break;
|
|
33384
|
+
pending = await this.#runRound(pending, round);
|
|
33385
|
+
}
|
|
33386
|
+
if (this.#abort.signal.aborted) return [];
|
|
33387
|
+
const terminal = pending.map((entry) => ({
|
|
33388
|
+
deviceId: entry.saved.id,
|
|
33389
|
+
stableId: entry.saved.stableId,
|
|
33390
|
+
type: String(entry.saved.type),
|
|
33391
|
+
attempts: entry.attempts,
|
|
33392
|
+
lastError: entry.lastError,
|
|
33393
|
+
failedAt: this.#now()
|
|
33394
|
+
}));
|
|
33395
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33396
|
+
return terminal;
|
|
33397
|
+
}
|
|
33398
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33399
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33400
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33401
|
+
async #runRound(pending, round) {
|
|
33402
|
+
const next = [];
|
|
33403
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33404
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33405
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33406
|
+
if (this.#abort.signal.aborted) {
|
|
33407
|
+
next.push(entry);
|
|
33408
|
+
return;
|
|
33409
|
+
}
|
|
33410
|
+
const attemptNo = entry.attempts + 1;
|
|
33411
|
+
try {
|
|
33412
|
+
await this.#attempt(entry.saved);
|
|
33413
|
+
this.#logger.info("Device restored on retry", {
|
|
33414
|
+
tags: {
|
|
33415
|
+
deviceId: entry.saved.id,
|
|
33416
|
+
stableId: entry.saved.stableId
|
|
33417
|
+
},
|
|
33418
|
+
meta: { attempt: attemptNo }
|
|
33419
|
+
});
|
|
33420
|
+
} catch (err) {
|
|
33421
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33422
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33423
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33424
|
+
tags: {
|
|
33425
|
+
deviceId: entry.saved.id,
|
|
33426
|
+
stableId: entry.saved.stableId
|
|
33427
|
+
},
|
|
33428
|
+
meta: {
|
|
33429
|
+
attempt: attemptNo,
|
|
33430
|
+
remainingRetries,
|
|
33431
|
+
error: lastError
|
|
33432
|
+
}
|
|
33433
|
+
});
|
|
33434
|
+
next.push({
|
|
33435
|
+
saved: entry.saved,
|
|
33436
|
+
lastError,
|
|
33437
|
+
attempts: attemptNo
|
|
33438
|
+
});
|
|
33439
|
+
}
|
|
33440
|
+
});
|
|
33441
|
+
return next;
|
|
33442
|
+
}
|
|
33443
|
+
};
|
|
33444
|
+
/**
|
|
33255
33445
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33256
33446
|
* device-provider cap router. Shared across all providers.
|
|
33257
33447
|
*/
|
|
@@ -33300,6 +33490,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33300
33490
|
}];
|
|
33301
33491
|
}
|
|
33302
33492
|
async onShutdown() {
|
|
33493
|
+
this.cancelRestoreRetries();
|
|
33303
33494
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33304
33495
|
for (const device of devices) try {
|
|
33305
33496
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33317,9 +33508,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33317
33508
|
async start() {}
|
|
33318
33509
|
async stop() {}
|
|
33319
33510
|
async getStatus() {
|
|
33511
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33512
|
+
const summary = this.restoreFailureSummary();
|
|
33513
|
+
if (summary === null) return {
|
|
33514
|
+
connected: true,
|
|
33515
|
+
deviceCount: all.length
|
|
33516
|
+
};
|
|
33320
33517
|
return {
|
|
33321
33518
|
connected: true,
|
|
33322
|
-
deviceCount:
|
|
33519
|
+
deviceCount: all.length,
|
|
33520
|
+
error: summary
|
|
33323
33521
|
};
|
|
33324
33522
|
}
|
|
33325
33523
|
async getDevices() {
|
|
@@ -33409,8 +33607,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33409
33607
|
};
|
|
33410
33608
|
}
|
|
33411
33609
|
async restoreDevices(savedDevices) {
|
|
33412
|
-
await this.onRestoreDevices(savedDevices);
|
|
33413
|
-
if (savedDevices.length
|
|
33610
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33611
|
+
if (savedDevices.length === 0) return;
|
|
33612
|
+
if (report && report.failedCount > 0) {
|
|
33613
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33614
|
+
return;
|
|
33615
|
+
}
|
|
33616
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33617
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33618
|
+
}
|
|
33619
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33620
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33621
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33622
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33623
|
+
restoreRetryConcurrency = 4;
|
|
33624
|
+
_restoreRetryScheduler = null;
|
|
33625
|
+
_restoreRetryCompletion = null;
|
|
33626
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33627
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33628
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33629
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33630
|
+
* with the devices that restored, and a late success is announced
|
|
33631
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33632
|
+
get restoreRetryCompletion() {
|
|
33633
|
+
return this._restoreRetryCompletion;
|
|
33634
|
+
}
|
|
33635
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33636
|
+
get permanentRestoreFailures() {
|
|
33637
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33638
|
+
}
|
|
33639
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33640
|
+
* `null` when every device restored. */
|
|
33641
|
+
restoreFailureSummary() {
|
|
33642
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33643
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33644
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33645
|
+
}
|
|
33646
|
+
cancelRestoreRetries() {
|
|
33647
|
+
this._restoreRetryScheduler?.cancel();
|
|
33648
|
+
this._restoreRetryScheduler = null;
|
|
33649
|
+
}
|
|
33650
|
+
recordPermanentRestoreFailure(failure) {
|
|
33651
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33652
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33653
|
+
tags: {
|
|
33654
|
+
deviceId: failure.deviceId,
|
|
33655
|
+
stableId: failure.stableId
|
|
33656
|
+
},
|
|
33657
|
+
meta: {
|
|
33658
|
+
type: failure.type,
|
|
33659
|
+
attempts: failure.attempts,
|
|
33660
|
+
error: failure.lastError
|
|
33661
|
+
}
|
|
33662
|
+
});
|
|
33663
|
+
}
|
|
33664
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33665
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33666
|
+
logger: this.ctx.logger,
|
|
33667
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33668
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33669
|
+
attempt,
|
|
33670
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33671
|
+
});
|
|
33672
|
+
this._restoreRetryScheduler = scheduler;
|
|
33673
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33674
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33675
|
+
});
|
|
33676
|
+
}
|
|
33677
|
+
/**
|
|
33678
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33679
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33680
|
+
* and no other device this provider owns is disturbed.
|
|
33681
|
+
*
|
|
33682
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33683
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33684
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33685
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33686
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33687
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33688
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33689
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33690
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33691
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33692
|
+
*
|
|
33693
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33694
|
+
* reload its parent instead.
|
|
33695
|
+
*/
|
|
33696
|
+
async reloadDevice(input) {
|
|
33697
|
+
const { stableId } = input;
|
|
33698
|
+
const devices = this.ctx.kernel.devices;
|
|
33699
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33700
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33701
|
+
if (live) await devices.decommission(live.id);
|
|
33702
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33703
|
+
addonId: this.addonId,
|
|
33704
|
+
stableId
|
|
33705
|
+
});
|
|
33706
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33707
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33708
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33709
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33710
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33711
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33712
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33713
|
+
for (const row of rows) {
|
|
33714
|
+
if (row.parentDeviceId !== id) continue;
|
|
33715
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33716
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33717
|
+
if (!ChildClass) continue;
|
|
33718
|
+
try {
|
|
33719
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33720
|
+
} catch (err) {
|
|
33721
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33722
|
+
tags: {
|
|
33723
|
+
deviceId: row.id,
|
|
33724
|
+
stableId: row.stableId
|
|
33725
|
+
},
|
|
33726
|
+
meta: {
|
|
33727
|
+
parentDeviceId: id,
|
|
33728
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33729
|
+
}
|
|
33730
|
+
});
|
|
33731
|
+
}
|
|
33732
|
+
}
|
|
33733
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33734
|
+
tags: { deviceId: id },
|
|
33735
|
+
meta: {
|
|
33736
|
+
stableId,
|
|
33737
|
+
type: meta.type
|
|
33738
|
+
}
|
|
33739
|
+
});
|
|
33740
|
+
return { deviceId: id };
|
|
33414
33741
|
}
|
|
33415
33742
|
/**
|
|
33416
33743
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33436,55 +33763,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33436
33763
|
* accessory-spawn flow handles via the parent's
|
|
33437
33764
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33438
33765
|
* fit.
|
|
33766
|
+
*
|
|
33767
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33768
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33769
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33770
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33771
|
+
* `getStatus().error`.
|
|
33772
|
+
*/
|
|
33773
|
+
/**
|
|
33774
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33775
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33776
|
+
*
|
|
33777
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33778
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33779
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33780
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33781
|
+
* been emptied failed all four bounded attempts against fields
|
|
33782
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33783
|
+
*
|
|
33784
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33785
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33786
|
+
* the bound, then reported — never swallowed.
|
|
33439
33787
|
*/
|
|
33788
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
33440
33789
|
async onRestoreDevices(savedDevices) {
|
|
33441
33790
|
const restored = /* @__PURE__ */ new Set();
|
|
33791
|
+
const failures = [];
|
|
33792
|
+
const attemptRestore = async (saved) => {
|
|
33793
|
+
if (restored.has(saved.id)) return;
|
|
33794
|
+
const Class = this.deviceClasses[saved.type];
|
|
33795
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33796
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33797
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33798
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33799
|
+
restored.add(saved.id);
|
|
33800
|
+
};
|
|
33442
33801
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33443
33802
|
const restoreOne = async (saved) => {
|
|
33444
|
-
|
|
33445
|
-
if (!Class) {
|
|
33803
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33446
33804
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33447
|
-
tags: {
|
|
33805
|
+
tags: {
|
|
33806
|
+
deviceId: saved.id,
|
|
33807
|
+
stableId: saved.stableId
|
|
33808
|
+
},
|
|
33448
33809
|
meta: { type: saved.type }
|
|
33449
33810
|
});
|
|
33450
33811
|
return;
|
|
33451
33812
|
}
|
|
33452
33813
|
try {
|
|
33453
|
-
await
|
|
33454
|
-
restored.add(saved.id);
|
|
33814
|
+
await attemptRestore(saved);
|
|
33455
33815
|
} catch (err) {
|
|
33456
|
-
|
|
33457
|
-
|
|
33816
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33817
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33818
|
+
tags: {
|
|
33819
|
+
deviceId: saved.id,
|
|
33820
|
+
stableId: saved.stableId
|
|
33821
|
+
},
|
|
33458
33822
|
meta: {
|
|
33459
33823
|
type: saved.type,
|
|
33460
|
-
|
|
33824
|
+
attempt: 1,
|
|
33825
|
+
error
|
|
33461
33826
|
}
|
|
33462
33827
|
});
|
|
33828
|
+
failures.push({
|
|
33829
|
+
saved,
|
|
33830
|
+
error
|
|
33831
|
+
});
|
|
33463
33832
|
}
|
|
33464
33833
|
};
|
|
33465
33834
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33835
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33466
33836
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33467
33837
|
for (const saved of childRows) {
|
|
33468
|
-
|
|
33469
|
-
if (!Class) continue;
|
|
33838
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33470
33839
|
if (saved.parentDeviceId === null) continue;
|
|
33471
|
-
if (
|
|
33472
|
-
|
|
33473
|
-
|
|
33474
|
-
|
|
33475
|
-
|
|
33476
|
-
|
|
33840
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33841
|
+
try {
|
|
33842
|
+
await attemptRestore(saved);
|
|
33843
|
+
} catch (err) {
|
|
33844
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33845
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33846
|
+
tags: {
|
|
33847
|
+
deviceId: saved.id,
|
|
33848
|
+
stableId: saved.stableId,
|
|
33849
|
+
parentDeviceId: saved.parentDeviceId
|
|
33850
|
+
},
|
|
33851
|
+
meta: {
|
|
33852
|
+
type: saved.type,
|
|
33853
|
+
attempt: 1,
|
|
33854
|
+
error
|
|
33855
|
+
}
|
|
33856
|
+
});
|
|
33857
|
+
failures.push({
|
|
33858
|
+
saved,
|
|
33859
|
+
error
|
|
33860
|
+
});
|
|
33861
|
+
}
|
|
33862
|
+
continue;
|
|
33863
|
+
}
|
|
33864
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33865
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33477
33866
|
tags: {
|
|
33867
|
+
deviceId: saved.id,
|
|
33478
33868
|
stableId: saved.stableId,
|
|
33479
33869
|
parentDeviceId: saved.parentDeviceId
|
|
33480
33870
|
},
|
|
33481
|
-
meta: {
|
|
33482
|
-
|
|
33483
|
-
|
|
33484
|
-
|
|
33871
|
+
meta: { type: saved.type }
|
|
33872
|
+
});
|
|
33873
|
+
failures.push({
|
|
33874
|
+
saved,
|
|
33875
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33485
33876
|
});
|
|
33877
|
+
continue;
|
|
33486
33878
|
}
|
|
33487
33879
|
}
|
|
33880
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33881
|
+
return {
|
|
33882
|
+
restoredCount: restored.size,
|
|
33883
|
+
failedCount: failures.length
|
|
33884
|
+
};
|
|
33488
33885
|
}
|
|
33489
33886
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33490
33887
|
toSummary(device) {
|
|
@@ -35465,6 +35862,12 @@ Object.freeze({
|
|
|
35465
35862
|
addonId: null,
|
|
35466
35863
|
access: "view"
|
|
35467
35864
|
},
|
|
35865
|
+
"deviceProvider.reloadDevice": {
|
|
35866
|
+
capName: "device-provider",
|
|
35867
|
+
capScope: "system",
|
|
35868
|
+
addonId: null,
|
|
35869
|
+
access: "create"
|
|
35870
|
+
},
|
|
35468
35871
|
"deviceProvider.start": {
|
|
35469
35872
|
capName: "device-provider",
|
|
35470
35873
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -13045,7 +13045,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
13045
13045
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
13046
13046
|
* flows live through the cap STATUS SLICE after adoption.
|
|
13047
13047
|
*/
|
|
13048
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
13048
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
13049
|
+
/**
|
|
13050
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
13051
|
+
*
|
|
13052
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
13053
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
13054
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
13055
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
13056
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
13057
|
+
* three child cameras offline for four hours.
|
|
13058
|
+
*
|
|
13059
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
13060
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
13061
|
+
* that cannot tell simply never sets it.
|
|
13062
|
+
*/
|
|
13063
|
+
alreadyOnboarded: boolean().optional(),
|
|
13064
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
13065
|
+
onboardedDeviceId: number().optional(),
|
|
13066
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
13067
|
+
onboardedName: string().optional()
|
|
13049
13068
|
});
|
|
13050
13069
|
/**
|
|
13051
13070
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -13101,6 +13120,35 @@ var deviceProviderCapability = {
|
|
|
13101
13120
|
name: string(),
|
|
13102
13121
|
type: string()
|
|
13103
13122
|
}))),
|
|
13123
|
+
/**
|
|
13124
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
13125
|
+
* touching no other device this provider owns.
|
|
13126
|
+
*
|
|
13127
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
13128
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
13129
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
13130
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
13131
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
13132
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
13133
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
13134
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
13135
|
+
*
|
|
13136
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
13137
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
13138
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
13139
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
13140
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
13141
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
13142
|
+
* against the wrong camera (D8).
|
|
13143
|
+
*
|
|
13144
|
+
* Construction can dial hardware, and the migrated source is
|
|
13145
|
+
* characteristically dead — the timeout covers a full activate window
|
|
13146
|
+
* rather than the 60 s default.
|
|
13147
|
+
*/
|
|
13148
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
13149
|
+
kind: "mutation",
|
|
13150
|
+
timeoutMs: 3 * 6e4
|
|
13151
|
+
}),
|
|
13104
13152
|
supportsDiscovery: method(object({}), boolean()),
|
|
13105
13153
|
/**
|
|
13106
13154
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -13428,7 +13476,8 @@ method(object({
|
|
|
13428
13476
|
targetId: number()
|
|
13429
13477
|
}), MigrateDeviceResultSchema, {
|
|
13430
13478
|
kind: "mutation",
|
|
13431
|
-
auth: "admin"
|
|
13479
|
+
auth: "admin",
|
|
13480
|
+
timeoutMs: 12 * 6e4
|
|
13432
13481
|
}), 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({
|
|
13433
13482
|
deviceId: number(),
|
|
13434
13483
|
name: string()
|
|
@@ -33253,6 +33302,147 @@ var BaseDevice = class {
|
|
|
33253
33302
|
}
|
|
33254
33303
|
};
|
|
33255
33304
|
/**
|
|
33305
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
33306
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
33307
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
33308
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
33309
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
33310
|
+
*/
|
|
33311
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
33312
|
+
1e4,
|
|
33313
|
+
3e4,
|
|
33314
|
+
9e4
|
|
33315
|
+
];
|
|
33316
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
33317
|
+
function sleep$1(ms, signal) {
|
|
33318
|
+
return new Promise((resolve) => {
|
|
33319
|
+
if (signal.aborted) {
|
|
33320
|
+
resolve();
|
|
33321
|
+
return;
|
|
33322
|
+
}
|
|
33323
|
+
const onAbort = () => {
|
|
33324
|
+
clearTimeout(timer);
|
|
33325
|
+
resolve();
|
|
33326
|
+
};
|
|
33327
|
+
const timer = setTimeout(() => {
|
|
33328
|
+
signal.removeEventListener("abort", onAbort);
|
|
33329
|
+
resolve();
|
|
33330
|
+
}, ms);
|
|
33331
|
+
timer.unref?.();
|
|
33332
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
33333
|
+
});
|
|
33334
|
+
}
|
|
33335
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
33336
|
+
* not reject (callers wrap their own try/catch). */
|
|
33337
|
+
async function runWithConcurrency(items, width, fn) {
|
|
33338
|
+
const queue = [...items];
|
|
33339
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
33340
|
+
const lane = async () => {
|
|
33341
|
+
for (;;) {
|
|
33342
|
+
const item = queue.shift();
|
|
33343
|
+
if (item === void 0) return;
|
|
33344
|
+
await fn(item);
|
|
33345
|
+
}
|
|
33346
|
+
};
|
|
33347
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
33348
|
+
}
|
|
33349
|
+
var DeviceRestoreRetryScheduler = class {
|
|
33350
|
+
#logger;
|
|
33351
|
+
#attempt;
|
|
33352
|
+
#onPermanentFailure;
|
|
33353
|
+
#delaysMs;
|
|
33354
|
+
#concurrency;
|
|
33355
|
+
#now;
|
|
33356
|
+
#abort = new AbortController();
|
|
33357
|
+
constructor(options) {
|
|
33358
|
+
this.#logger = options.logger;
|
|
33359
|
+
this.#attempt = options.attempt;
|
|
33360
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
33361
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33362
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
33363
|
+
this.#now = options.now ?? Date.now;
|
|
33364
|
+
}
|
|
33365
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
33366
|
+
* permanently failed — the next boot restores them from disk. */
|
|
33367
|
+
cancel() {
|
|
33368
|
+
this.#abort.abort();
|
|
33369
|
+
}
|
|
33370
|
+
/**
|
|
33371
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
33372
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
33373
|
+
* cancelled. Never rejects.
|
|
33374
|
+
*/
|
|
33375
|
+
async run(initialFailures) {
|
|
33376
|
+
let pending = initialFailures.map((failure) => ({
|
|
33377
|
+
saved: failure.saved,
|
|
33378
|
+
lastError: failure.error,
|
|
33379
|
+
attempts: 1
|
|
33380
|
+
}));
|
|
33381
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
33382
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
33383
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
33384
|
+
if (this.#abort.signal.aborted) break;
|
|
33385
|
+
pending = await this.#runRound(pending, round);
|
|
33386
|
+
}
|
|
33387
|
+
if (this.#abort.signal.aborted) return [];
|
|
33388
|
+
const terminal = pending.map((entry) => ({
|
|
33389
|
+
deviceId: entry.saved.id,
|
|
33390
|
+
stableId: entry.saved.stableId,
|
|
33391
|
+
type: String(entry.saved.type),
|
|
33392
|
+
attempts: entry.attempts,
|
|
33393
|
+
lastError: entry.lastError,
|
|
33394
|
+
failedAt: this.#now()
|
|
33395
|
+
}));
|
|
33396
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
33397
|
+
return terminal;
|
|
33398
|
+
}
|
|
33399
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
33400
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
33401
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
33402
|
+
async #runRound(pending, round) {
|
|
33403
|
+
const next = [];
|
|
33404
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
33405
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
33406
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
33407
|
+
if (this.#abort.signal.aborted) {
|
|
33408
|
+
next.push(entry);
|
|
33409
|
+
return;
|
|
33410
|
+
}
|
|
33411
|
+
const attemptNo = entry.attempts + 1;
|
|
33412
|
+
try {
|
|
33413
|
+
await this.#attempt(entry.saved);
|
|
33414
|
+
this.#logger.info("Device restored on retry", {
|
|
33415
|
+
tags: {
|
|
33416
|
+
deviceId: entry.saved.id,
|
|
33417
|
+
stableId: entry.saved.stableId
|
|
33418
|
+
},
|
|
33419
|
+
meta: { attempt: attemptNo }
|
|
33420
|
+
});
|
|
33421
|
+
} catch (err) {
|
|
33422
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
33423
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
33424
|
+
this.#logger.warn("Device restore retry failed", {
|
|
33425
|
+
tags: {
|
|
33426
|
+
deviceId: entry.saved.id,
|
|
33427
|
+
stableId: entry.saved.stableId
|
|
33428
|
+
},
|
|
33429
|
+
meta: {
|
|
33430
|
+
attempt: attemptNo,
|
|
33431
|
+
remainingRetries,
|
|
33432
|
+
error: lastError
|
|
33433
|
+
}
|
|
33434
|
+
});
|
|
33435
|
+
next.push({
|
|
33436
|
+
saved: entry.saved,
|
|
33437
|
+
lastError,
|
|
33438
|
+
attempts: attemptNo
|
|
33439
|
+
});
|
|
33440
|
+
}
|
|
33441
|
+
});
|
|
33442
|
+
return next;
|
|
33443
|
+
}
|
|
33444
|
+
};
|
|
33445
|
+
/**
|
|
33256
33446
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
33257
33447
|
* device-provider cap router. Shared across all providers.
|
|
33258
33448
|
*/
|
|
@@ -33301,6 +33491,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33301
33491
|
}];
|
|
33302
33492
|
}
|
|
33303
33493
|
async onShutdown() {
|
|
33494
|
+
this.cancelRestoreRetries();
|
|
33304
33495
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33305
33496
|
for (const device of devices) try {
|
|
33306
33497
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -33318,9 +33509,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33318
33509
|
async start() {}
|
|
33319
33510
|
async stop() {}
|
|
33320
33511
|
async getStatus() {
|
|
33512
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
33513
|
+
const summary = this.restoreFailureSummary();
|
|
33514
|
+
if (summary === null) return {
|
|
33515
|
+
connected: true,
|
|
33516
|
+
deviceCount: all.length
|
|
33517
|
+
};
|
|
33321
33518
|
return {
|
|
33322
33519
|
connected: true,
|
|
33323
|
-
deviceCount:
|
|
33520
|
+
deviceCount: all.length,
|
|
33521
|
+
error: summary
|
|
33324
33522
|
};
|
|
33325
33523
|
}
|
|
33326
33524
|
async getDevices() {
|
|
@@ -33410,8 +33608,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33410
33608
|
};
|
|
33411
33609
|
}
|
|
33412
33610
|
async restoreDevices(savedDevices) {
|
|
33413
|
-
await this.onRestoreDevices(savedDevices);
|
|
33414
|
-
if (savedDevices.length
|
|
33611
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
33612
|
+
if (savedDevices.length === 0) return;
|
|
33613
|
+
if (report && report.failedCount > 0) {
|
|
33614
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
33615
|
+
return;
|
|
33616
|
+
}
|
|
33617
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
33618
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
33619
|
+
}
|
|
33620
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
33621
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
33622
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
33623
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
33624
|
+
restoreRetryConcurrency = 4;
|
|
33625
|
+
_restoreRetryScheduler = null;
|
|
33626
|
+
_restoreRetryCompletion = null;
|
|
33627
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
33628
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
33629
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
33630
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
33631
|
+
* with the devices that restored, and a late success is announced
|
|
33632
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
33633
|
+
get restoreRetryCompletion() {
|
|
33634
|
+
return this._restoreRetryCompletion;
|
|
33635
|
+
}
|
|
33636
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
33637
|
+
get permanentRestoreFailures() {
|
|
33638
|
+
return [...this._permanentRestoreFailures.values()];
|
|
33639
|
+
}
|
|
33640
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
33641
|
+
* `null` when every device restored. */
|
|
33642
|
+
restoreFailureSummary() {
|
|
33643
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
33644
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
33645
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
33646
|
+
}
|
|
33647
|
+
cancelRestoreRetries() {
|
|
33648
|
+
this._restoreRetryScheduler?.cancel();
|
|
33649
|
+
this._restoreRetryScheduler = null;
|
|
33650
|
+
}
|
|
33651
|
+
recordPermanentRestoreFailure(failure) {
|
|
33652
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
33653
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
33654
|
+
tags: {
|
|
33655
|
+
deviceId: failure.deviceId,
|
|
33656
|
+
stableId: failure.stableId
|
|
33657
|
+
},
|
|
33658
|
+
meta: {
|
|
33659
|
+
type: failure.type,
|
|
33660
|
+
attempts: failure.attempts,
|
|
33661
|
+
error: failure.lastError
|
|
33662
|
+
}
|
|
33663
|
+
});
|
|
33664
|
+
}
|
|
33665
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
33666
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
33667
|
+
logger: this.ctx.logger,
|
|
33668
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
33669
|
+
concurrency: this.restoreRetryConcurrency,
|
|
33670
|
+
attempt,
|
|
33671
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
33672
|
+
});
|
|
33673
|
+
this._restoreRetryScheduler = scheduler;
|
|
33674
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
33675
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
33676
|
+
});
|
|
33677
|
+
}
|
|
33678
|
+
/**
|
|
33679
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
33680
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
33681
|
+
* and no other device this provider owns is disturbed.
|
|
33682
|
+
*
|
|
33683
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
33684
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
33685
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
33686
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
33687
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
33688
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
33689
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
33690
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
33691
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
33692
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
33693
|
+
*
|
|
33694
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
33695
|
+
* reload its parent instead.
|
|
33696
|
+
*/
|
|
33697
|
+
async reloadDevice(input) {
|
|
33698
|
+
const { stableId } = input;
|
|
33699
|
+
const devices = this.ctx.kernel.devices;
|
|
33700
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
33701
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
33702
|
+
if (live) await devices.decommission(live.id);
|
|
33703
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
33704
|
+
addonId: this.addonId,
|
|
33705
|
+
stableId
|
|
33706
|
+
});
|
|
33707
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
33708
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
33709
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
33710
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
33711
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
33712
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
33713
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
33714
|
+
for (const row of rows) {
|
|
33715
|
+
if (row.parentDeviceId !== id) continue;
|
|
33716
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
33717
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
33718
|
+
if (!ChildClass) continue;
|
|
33719
|
+
try {
|
|
33720
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
33721
|
+
} catch (err) {
|
|
33722
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
33723
|
+
tags: {
|
|
33724
|
+
deviceId: row.id,
|
|
33725
|
+
stableId: row.stableId
|
|
33726
|
+
},
|
|
33727
|
+
meta: {
|
|
33728
|
+
parentDeviceId: id,
|
|
33729
|
+
error: err instanceof Error ? err.message : String(err)
|
|
33730
|
+
}
|
|
33731
|
+
});
|
|
33732
|
+
}
|
|
33733
|
+
}
|
|
33734
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
33735
|
+
tags: { deviceId: id },
|
|
33736
|
+
meta: {
|
|
33737
|
+
stableId,
|
|
33738
|
+
type: meta.type
|
|
33739
|
+
}
|
|
33740
|
+
});
|
|
33741
|
+
return { deviceId: id };
|
|
33415
33742
|
}
|
|
33416
33743
|
/**
|
|
33417
33744
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -33437,55 +33764,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
33437
33764
|
* accessory-spawn flow handles via the parent's
|
|
33438
33765
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
33439
33766
|
* fit.
|
|
33767
|
+
*
|
|
33768
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
33769
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
33770
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
33771
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
33772
|
+
* `getStatus().error`.
|
|
33773
|
+
*/
|
|
33774
|
+
/**
|
|
33775
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
33776
|
+
* Default: no-op — most providers have nothing to heal.
|
|
33777
|
+
*
|
|
33778
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
33779
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
33780
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
33781
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
33782
|
+
* been emptied failed all four bounded attempts against fields
|
|
33783
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
33784
|
+
*
|
|
33785
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
33786
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
33787
|
+
* the bound, then reported — never swallowed.
|
|
33440
33788
|
*/
|
|
33789
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
33441
33790
|
async onRestoreDevices(savedDevices) {
|
|
33442
33791
|
const restored = /* @__PURE__ */ new Set();
|
|
33792
|
+
const failures = [];
|
|
33793
|
+
const attemptRestore = async (saved) => {
|
|
33794
|
+
if (restored.has(saved.id)) return;
|
|
33795
|
+
const Class = this.deviceClasses[saved.type];
|
|
33796
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
33797
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
33798
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
33799
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
33800
|
+
restored.add(saved.id);
|
|
33801
|
+
};
|
|
33443
33802
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
33444
33803
|
const restoreOne = async (saved) => {
|
|
33445
|
-
|
|
33446
|
-
if (!Class) {
|
|
33804
|
+
if (!this.deviceClasses[saved.type]) {
|
|
33447
33805
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
33448
|
-
tags: {
|
|
33806
|
+
tags: {
|
|
33807
|
+
deviceId: saved.id,
|
|
33808
|
+
stableId: saved.stableId
|
|
33809
|
+
},
|
|
33449
33810
|
meta: { type: saved.type }
|
|
33450
33811
|
});
|
|
33451
33812
|
return;
|
|
33452
33813
|
}
|
|
33453
33814
|
try {
|
|
33454
|
-
await
|
|
33455
|
-
restored.add(saved.id);
|
|
33815
|
+
await attemptRestore(saved);
|
|
33456
33816
|
} catch (err) {
|
|
33457
|
-
|
|
33458
|
-
|
|
33817
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33818
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
33819
|
+
tags: {
|
|
33820
|
+
deviceId: saved.id,
|
|
33821
|
+
stableId: saved.stableId
|
|
33822
|
+
},
|
|
33459
33823
|
meta: {
|
|
33460
33824
|
type: saved.type,
|
|
33461
|
-
|
|
33825
|
+
attempt: 1,
|
|
33826
|
+
error
|
|
33462
33827
|
}
|
|
33463
33828
|
});
|
|
33829
|
+
failures.push({
|
|
33830
|
+
saved,
|
|
33831
|
+
error
|
|
33832
|
+
});
|
|
33464
33833
|
}
|
|
33465
33834
|
};
|
|
33466
33835
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
33836
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
33467
33837
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
33468
33838
|
for (const saved of childRows) {
|
|
33469
|
-
|
|
33470
|
-
if (!Class) continue;
|
|
33839
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
33471
33840
|
if (saved.parentDeviceId === null) continue;
|
|
33472
|
-
if (
|
|
33473
|
-
|
|
33474
|
-
|
|
33475
|
-
|
|
33476
|
-
|
|
33477
|
-
|
|
33841
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
33842
|
+
try {
|
|
33843
|
+
await attemptRestore(saved);
|
|
33844
|
+
} catch (err) {
|
|
33845
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
33846
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
33847
|
+
tags: {
|
|
33848
|
+
deviceId: saved.id,
|
|
33849
|
+
stableId: saved.stableId,
|
|
33850
|
+
parentDeviceId: saved.parentDeviceId
|
|
33851
|
+
},
|
|
33852
|
+
meta: {
|
|
33853
|
+
type: saved.type,
|
|
33854
|
+
attempt: 1,
|
|
33855
|
+
error
|
|
33856
|
+
}
|
|
33857
|
+
});
|
|
33858
|
+
failures.push({
|
|
33859
|
+
saved,
|
|
33860
|
+
error
|
|
33861
|
+
});
|
|
33862
|
+
}
|
|
33863
|
+
continue;
|
|
33864
|
+
}
|
|
33865
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
33866
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
33478
33867
|
tags: {
|
|
33868
|
+
deviceId: saved.id,
|
|
33479
33869
|
stableId: saved.stableId,
|
|
33480
33870
|
parentDeviceId: saved.parentDeviceId
|
|
33481
33871
|
},
|
|
33482
|
-
meta: {
|
|
33483
|
-
|
|
33484
|
-
|
|
33485
|
-
|
|
33872
|
+
meta: { type: saved.type }
|
|
33873
|
+
});
|
|
33874
|
+
failures.push({
|
|
33875
|
+
saved,
|
|
33876
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
33486
33877
|
});
|
|
33878
|
+
continue;
|
|
33487
33879
|
}
|
|
33488
33880
|
}
|
|
33881
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
33882
|
+
return {
|
|
33883
|
+
restoredCount: restored.size,
|
|
33884
|
+
failedCount: failures.length
|
|
33885
|
+
};
|
|
33489
33886
|
}
|
|
33490
33887
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
33491
33888
|
toSummary(device) {
|
|
@@ -35466,6 +35863,12 @@ Object.freeze({
|
|
|
35466
35863
|
addonId: null,
|
|
35467
35864
|
access: "view"
|
|
35468
35865
|
},
|
|
35866
|
+
"deviceProvider.reloadDevice": {
|
|
35867
|
+
capName: "device-provider",
|
|
35868
|
+
capScope: "system",
|
|
35869
|
+
addonId: null,
|
|
35870
|
+
access: "create"
|
|
35871
|
+
},
|
|
35469
35872
|
"deviceProvider.start": {
|
|
35470
35873
|
capName: "device-provider",
|
|
35471
35874
|
capScope: "system",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camstack/addon-provider-hikvision",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.69",
|
|
4
4
|
"description": "Hikvision camera device provider addon for CamStack — ISAPI over HTTP(S) with digest auth (snapshot, alarm stream, RTSP discovery)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"camstack",
|