@camstack/addon-provider-dreame 0.2.59 → 0.2.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addon.js +428 -25
- package/dist/addon.mjs +428 -25
- package/package.json +1 -1
package/dist/addon.js
CHANGED
|
@@ -63278,7 +63278,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
63278
63278
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
63279
63279
|
* flows live through the cap STATUS SLICE after adoption.
|
|
63280
63280
|
*/
|
|
63281
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
63281
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
63282
|
+
/**
|
|
63283
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
63284
|
+
*
|
|
63285
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
63286
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
63287
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
63288
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
63289
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
63290
|
+
* three child cameras offline for four hours.
|
|
63291
|
+
*
|
|
63292
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
63293
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
63294
|
+
* that cannot tell simply never sets it.
|
|
63295
|
+
*/
|
|
63296
|
+
alreadyOnboarded: boolean().optional(),
|
|
63297
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
63298
|
+
onboardedDeviceId: number().optional(),
|
|
63299
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
63300
|
+
onboardedName: string().optional()
|
|
63282
63301
|
});
|
|
63283
63302
|
/**
|
|
63284
63303
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -63334,6 +63353,35 @@ var deviceProviderCapability = {
|
|
|
63334
63353
|
name: string(),
|
|
63335
63354
|
type: string()
|
|
63336
63355
|
}))),
|
|
63356
|
+
/**
|
|
63357
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
63358
|
+
* touching no other device this provider owns.
|
|
63359
|
+
*
|
|
63360
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
63361
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
63362
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
63363
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
63364
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
63365
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
63366
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
63367
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
63368
|
+
*
|
|
63369
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
63370
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
63371
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
63372
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
63373
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
63374
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
63375
|
+
* against the wrong camera (D8).
|
|
63376
|
+
*
|
|
63377
|
+
* Construction can dial hardware, and the migrated source is
|
|
63378
|
+
* characteristically dead — the timeout covers a full activate window
|
|
63379
|
+
* rather than the 60 s default.
|
|
63380
|
+
*/
|
|
63381
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
63382
|
+
kind: "mutation",
|
|
63383
|
+
timeoutMs: 3 * 6e4
|
|
63384
|
+
}),
|
|
63337
63385
|
supportsDiscovery: method(object({}), boolean()),
|
|
63338
63386
|
/**
|
|
63339
63387
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -63661,7 +63709,8 @@ method(object({
|
|
|
63661
63709
|
targetId: number()
|
|
63662
63710
|
}), MigrateDeviceResultSchema, {
|
|
63663
63711
|
kind: "mutation",
|
|
63664
|
-
auth: "admin"
|
|
63712
|
+
auth: "admin",
|
|
63713
|
+
timeoutMs: 12 * 6e4
|
|
63665
63714
|
}), 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({
|
|
63666
63715
|
deviceId: number(),
|
|
63667
63716
|
name: string()
|
|
@@ -83042,6 +83091,147 @@ var BaseDevice = class {
|
|
|
83042
83091
|
}
|
|
83043
83092
|
};
|
|
83044
83093
|
/**
|
|
83094
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
83095
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
83096
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
83097
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
83098
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
83099
|
+
*/
|
|
83100
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
83101
|
+
1e4,
|
|
83102
|
+
3e4,
|
|
83103
|
+
9e4
|
|
83104
|
+
];
|
|
83105
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
83106
|
+
function sleep$1(ms, signal) {
|
|
83107
|
+
return new Promise((resolve) => {
|
|
83108
|
+
if (signal.aborted) {
|
|
83109
|
+
resolve();
|
|
83110
|
+
return;
|
|
83111
|
+
}
|
|
83112
|
+
const onAbort = () => {
|
|
83113
|
+
clearTimeout(timer);
|
|
83114
|
+
resolve();
|
|
83115
|
+
};
|
|
83116
|
+
const timer = setTimeout(() => {
|
|
83117
|
+
signal.removeEventListener("abort", onAbort);
|
|
83118
|
+
resolve();
|
|
83119
|
+
}, ms);
|
|
83120
|
+
timer.unref?.();
|
|
83121
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
83122
|
+
});
|
|
83123
|
+
}
|
|
83124
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
83125
|
+
* not reject (callers wrap their own try/catch). */
|
|
83126
|
+
async function runWithConcurrency(items, width, fn) {
|
|
83127
|
+
const queue = [...items];
|
|
83128
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
83129
|
+
const lane = async () => {
|
|
83130
|
+
for (;;) {
|
|
83131
|
+
const item = queue.shift();
|
|
83132
|
+
if (item === void 0) return;
|
|
83133
|
+
await fn(item);
|
|
83134
|
+
}
|
|
83135
|
+
};
|
|
83136
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
83137
|
+
}
|
|
83138
|
+
var DeviceRestoreRetryScheduler = class {
|
|
83139
|
+
#logger;
|
|
83140
|
+
#attempt;
|
|
83141
|
+
#onPermanentFailure;
|
|
83142
|
+
#delaysMs;
|
|
83143
|
+
#concurrency;
|
|
83144
|
+
#now;
|
|
83145
|
+
#abort = new AbortController();
|
|
83146
|
+
constructor(options) {
|
|
83147
|
+
this.#logger = options.logger;
|
|
83148
|
+
this.#attempt = options.attempt;
|
|
83149
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
83150
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
83151
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
83152
|
+
this.#now = options.now ?? Date.now;
|
|
83153
|
+
}
|
|
83154
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
83155
|
+
* permanently failed — the next boot restores them from disk. */
|
|
83156
|
+
cancel() {
|
|
83157
|
+
this.#abort.abort();
|
|
83158
|
+
}
|
|
83159
|
+
/**
|
|
83160
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
83161
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
83162
|
+
* cancelled. Never rejects.
|
|
83163
|
+
*/
|
|
83164
|
+
async run(initialFailures) {
|
|
83165
|
+
let pending = initialFailures.map((failure) => ({
|
|
83166
|
+
saved: failure.saved,
|
|
83167
|
+
lastError: failure.error,
|
|
83168
|
+
attempts: 1
|
|
83169
|
+
}));
|
|
83170
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
83171
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
83172
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
83173
|
+
if (this.#abort.signal.aborted) break;
|
|
83174
|
+
pending = await this.#runRound(pending, round);
|
|
83175
|
+
}
|
|
83176
|
+
if (this.#abort.signal.aborted) return [];
|
|
83177
|
+
const terminal = pending.map((entry) => ({
|
|
83178
|
+
deviceId: entry.saved.id,
|
|
83179
|
+
stableId: entry.saved.stableId,
|
|
83180
|
+
type: String(entry.saved.type),
|
|
83181
|
+
attempts: entry.attempts,
|
|
83182
|
+
lastError: entry.lastError,
|
|
83183
|
+
failedAt: this.#now()
|
|
83184
|
+
}));
|
|
83185
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
83186
|
+
return terminal;
|
|
83187
|
+
}
|
|
83188
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
83189
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
83190
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
83191
|
+
async #runRound(pending, round) {
|
|
83192
|
+
const next = [];
|
|
83193
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
83194
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
83195
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
83196
|
+
if (this.#abort.signal.aborted) {
|
|
83197
|
+
next.push(entry);
|
|
83198
|
+
return;
|
|
83199
|
+
}
|
|
83200
|
+
const attemptNo = entry.attempts + 1;
|
|
83201
|
+
try {
|
|
83202
|
+
await this.#attempt(entry.saved);
|
|
83203
|
+
this.#logger.info("Device restored on retry", {
|
|
83204
|
+
tags: {
|
|
83205
|
+
deviceId: entry.saved.id,
|
|
83206
|
+
stableId: entry.saved.stableId
|
|
83207
|
+
},
|
|
83208
|
+
meta: { attempt: attemptNo }
|
|
83209
|
+
});
|
|
83210
|
+
} catch (err) {
|
|
83211
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
83212
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
83213
|
+
this.#logger.warn("Device restore retry failed", {
|
|
83214
|
+
tags: {
|
|
83215
|
+
deviceId: entry.saved.id,
|
|
83216
|
+
stableId: entry.saved.stableId
|
|
83217
|
+
},
|
|
83218
|
+
meta: {
|
|
83219
|
+
attempt: attemptNo,
|
|
83220
|
+
remainingRetries,
|
|
83221
|
+
error: lastError
|
|
83222
|
+
}
|
|
83223
|
+
});
|
|
83224
|
+
next.push({
|
|
83225
|
+
saved: entry.saved,
|
|
83226
|
+
lastError,
|
|
83227
|
+
attempts: attemptNo
|
|
83228
|
+
});
|
|
83229
|
+
}
|
|
83230
|
+
});
|
|
83231
|
+
return next;
|
|
83232
|
+
}
|
|
83233
|
+
};
|
|
83234
|
+
/**
|
|
83045
83235
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
83046
83236
|
* device-provider cap router. Shared across all providers.
|
|
83047
83237
|
*/
|
|
@@ -83090,6 +83280,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83090
83280
|
}];
|
|
83091
83281
|
}
|
|
83092
83282
|
async onShutdown() {
|
|
83283
|
+
this.cancelRestoreRetries();
|
|
83093
83284
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
83094
83285
|
for (const device of devices) try {
|
|
83095
83286
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -83107,9 +83298,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83107
83298
|
async start() {}
|
|
83108
83299
|
async stop() {}
|
|
83109
83300
|
async getStatus() {
|
|
83301
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
83302
|
+
const summary = this.restoreFailureSummary();
|
|
83303
|
+
if (summary === null) return {
|
|
83304
|
+
connected: true,
|
|
83305
|
+
deviceCount: all.length
|
|
83306
|
+
};
|
|
83110
83307
|
return {
|
|
83111
83308
|
connected: true,
|
|
83112
|
-
deviceCount:
|
|
83309
|
+
deviceCount: all.length,
|
|
83310
|
+
error: summary
|
|
83113
83311
|
};
|
|
83114
83312
|
}
|
|
83115
83313
|
async getDevices() {
|
|
@@ -83199,8 +83397,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83199
83397
|
};
|
|
83200
83398
|
}
|
|
83201
83399
|
async restoreDevices(savedDevices) {
|
|
83202
|
-
await this.onRestoreDevices(savedDevices);
|
|
83203
|
-
if (savedDevices.length
|
|
83400
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
83401
|
+
if (savedDevices.length === 0) return;
|
|
83402
|
+
if (report && report.failedCount > 0) {
|
|
83403
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
83404
|
+
return;
|
|
83405
|
+
}
|
|
83406
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
83407
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
83408
|
+
}
|
|
83409
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
83410
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
83411
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
83412
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
83413
|
+
restoreRetryConcurrency = 4;
|
|
83414
|
+
_restoreRetryScheduler = null;
|
|
83415
|
+
_restoreRetryCompletion = null;
|
|
83416
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
83417
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
83418
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
83419
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
83420
|
+
* with the devices that restored, and a late success is announced
|
|
83421
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
83422
|
+
get restoreRetryCompletion() {
|
|
83423
|
+
return this._restoreRetryCompletion;
|
|
83424
|
+
}
|
|
83425
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
83426
|
+
get permanentRestoreFailures() {
|
|
83427
|
+
return [...this._permanentRestoreFailures.values()];
|
|
83428
|
+
}
|
|
83429
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
83430
|
+
* `null` when every device restored. */
|
|
83431
|
+
restoreFailureSummary() {
|
|
83432
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
83433
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
83434
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
83435
|
+
}
|
|
83436
|
+
cancelRestoreRetries() {
|
|
83437
|
+
this._restoreRetryScheduler?.cancel();
|
|
83438
|
+
this._restoreRetryScheduler = null;
|
|
83439
|
+
}
|
|
83440
|
+
recordPermanentRestoreFailure(failure) {
|
|
83441
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
83442
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
83443
|
+
tags: {
|
|
83444
|
+
deviceId: failure.deviceId,
|
|
83445
|
+
stableId: failure.stableId
|
|
83446
|
+
},
|
|
83447
|
+
meta: {
|
|
83448
|
+
type: failure.type,
|
|
83449
|
+
attempts: failure.attempts,
|
|
83450
|
+
error: failure.lastError
|
|
83451
|
+
}
|
|
83452
|
+
});
|
|
83453
|
+
}
|
|
83454
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
83455
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
83456
|
+
logger: this.ctx.logger,
|
|
83457
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
83458
|
+
concurrency: this.restoreRetryConcurrency,
|
|
83459
|
+
attempt,
|
|
83460
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
83461
|
+
});
|
|
83462
|
+
this._restoreRetryScheduler = scheduler;
|
|
83463
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
83464
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
83465
|
+
});
|
|
83466
|
+
}
|
|
83467
|
+
/**
|
|
83468
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
83469
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
83470
|
+
* and no other device this provider owns is disturbed.
|
|
83471
|
+
*
|
|
83472
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
83473
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
83474
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
83475
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
83476
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
83477
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
83478
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
83479
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
83480
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
83481
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
83482
|
+
*
|
|
83483
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
83484
|
+
* reload its parent instead.
|
|
83485
|
+
*/
|
|
83486
|
+
async reloadDevice(input) {
|
|
83487
|
+
const { stableId } = input;
|
|
83488
|
+
const devices = this.ctx.kernel.devices;
|
|
83489
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
83490
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
83491
|
+
if (live) await devices.decommission(live.id);
|
|
83492
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
83493
|
+
addonId: this.addonId,
|
|
83494
|
+
stableId
|
|
83495
|
+
});
|
|
83496
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
83497
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
83498
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
83499
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
83500
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
83501
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
83502
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
83503
|
+
for (const row of rows) {
|
|
83504
|
+
if (row.parentDeviceId !== id) continue;
|
|
83505
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
83506
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
83507
|
+
if (!ChildClass) continue;
|
|
83508
|
+
try {
|
|
83509
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
83510
|
+
} catch (err) {
|
|
83511
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
83512
|
+
tags: {
|
|
83513
|
+
deviceId: row.id,
|
|
83514
|
+
stableId: row.stableId
|
|
83515
|
+
},
|
|
83516
|
+
meta: {
|
|
83517
|
+
parentDeviceId: id,
|
|
83518
|
+
error: err instanceof Error ? err.message : String(err)
|
|
83519
|
+
}
|
|
83520
|
+
});
|
|
83521
|
+
}
|
|
83522
|
+
}
|
|
83523
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
83524
|
+
tags: { deviceId: id },
|
|
83525
|
+
meta: {
|
|
83526
|
+
stableId,
|
|
83527
|
+
type: meta.type
|
|
83528
|
+
}
|
|
83529
|
+
});
|
|
83530
|
+
return { deviceId: id };
|
|
83204
83531
|
}
|
|
83205
83532
|
/**
|
|
83206
83533
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -83226,55 +83553,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83226
83553
|
* accessory-spawn flow handles via the parent's
|
|
83227
83554
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
83228
83555
|
* fit.
|
|
83556
|
+
*
|
|
83557
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
83558
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
83559
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
83560
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
83561
|
+
* `getStatus().error`.
|
|
83229
83562
|
*/
|
|
83563
|
+
/**
|
|
83564
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
83565
|
+
* Default: no-op — most providers have nothing to heal.
|
|
83566
|
+
*
|
|
83567
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
83568
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
83569
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
83570
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
83571
|
+
* been emptied failed all four bounded attempts against fields
|
|
83572
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
83573
|
+
*
|
|
83574
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
83575
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
83576
|
+
* the bound, then reported — never swallowed.
|
|
83577
|
+
*/
|
|
83578
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
83230
83579
|
async onRestoreDevices(savedDevices) {
|
|
83231
83580
|
const restored = /* @__PURE__ */ new Set();
|
|
83581
|
+
const failures = [];
|
|
83582
|
+
const attemptRestore = async (saved) => {
|
|
83583
|
+
if (restored.has(saved.id)) return;
|
|
83584
|
+
const Class = this.deviceClasses[saved.type];
|
|
83585
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
83586
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
83587
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
83588
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
83589
|
+
restored.add(saved.id);
|
|
83590
|
+
};
|
|
83232
83591
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
83233
83592
|
const restoreOne = async (saved) => {
|
|
83234
|
-
|
|
83235
|
-
if (!Class) {
|
|
83593
|
+
if (!this.deviceClasses[saved.type]) {
|
|
83236
83594
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
83237
|
-
tags: {
|
|
83595
|
+
tags: {
|
|
83596
|
+
deviceId: saved.id,
|
|
83597
|
+
stableId: saved.stableId
|
|
83598
|
+
},
|
|
83238
83599
|
meta: { type: saved.type }
|
|
83239
83600
|
});
|
|
83240
83601
|
return;
|
|
83241
83602
|
}
|
|
83242
83603
|
try {
|
|
83243
|
-
await
|
|
83244
|
-
restored.add(saved.id);
|
|
83604
|
+
await attemptRestore(saved);
|
|
83245
83605
|
} catch (err) {
|
|
83246
|
-
|
|
83247
|
-
|
|
83606
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
83607
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
83608
|
+
tags: {
|
|
83609
|
+
deviceId: saved.id,
|
|
83610
|
+
stableId: saved.stableId
|
|
83611
|
+
},
|
|
83248
83612
|
meta: {
|
|
83249
83613
|
type: saved.type,
|
|
83250
|
-
|
|
83614
|
+
attempt: 1,
|
|
83615
|
+
error
|
|
83251
83616
|
}
|
|
83252
83617
|
});
|
|
83618
|
+
failures.push({
|
|
83619
|
+
saved,
|
|
83620
|
+
error
|
|
83621
|
+
});
|
|
83253
83622
|
}
|
|
83254
83623
|
};
|
|
83255
83624
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
83625
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
83256
83626
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
83257
83627
|
for (const saved of childRows) {
|
|
83258
|
-
|
|
83259
|
-
if (!Class) continue;
|
|
83628
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
83260
83629
|
if (saved.parentDeviceId === null) continue;
|
|
83261
|
-
if (
|
|
83262
|
-
|
|
83263
|
-
|
|
83264
|
-
|
|
83265
|
-
|
|
83266
|
-
|
|
83630
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
83631
|
+
try {
|
|
83632
|
+
await attemptRestore(saved);
|
|
83633
|
+
} catch (err) {
|
|
83634
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
83635
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
83636
|
+
tags: {
|
|
83637
|
+
deviceId: saved.id,
|
|
83638
|
+
stableId: saved.stableId,
|
|
83639
|
+
parentDeviceId: saved.parentDeviceId
|
|
83640
|
+
},
|
|
83641
|
+
meta: {
|
|
83642
|
+
type: saved.type,
|
|
83643
|
+
attempt: 1,
|
|
83644
|
+
error
|
|
83645
|
+
}
|
|
83646
|
+
});
|
|
83647
|
+
failures.push({
|
|
83648
|
+
saved,
|
|
83649
|
+
error
|
|
83650
|
+
});
|
|
83651
|
+
}
|
|
83652
|
+
continue;
|
|
83653
|
+
}
|
|
83654
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
83655
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
83267
83656
|
tags: {
|
|
83657
|
+
deviceId: saved.id,
|
|
83268
83658
|
stableId: saved.stableId,
|
|
83269
83659
|
parentDeviceId: saved.parentDeviceId
|
|
83270
83660
|
},
|
|
83271
|
-
meta: {
|
|
83272
|
-
type: saved.type,
|
|
83273
|
-
error: err instanceof Error ? err.message : String(err)
|
|
83274
|
-
}
|
|
83661
|
+
meta: { type: saved.type }
|
|
83275
83662
|
});
|
|
83663
|
+
failures.push({
|
|
83664
|
+
saved,
|
|
83665
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
83666
|
+
});
|
|
83667
|
+
continue;
|
|
83276
83668
|
}
|
|
83277
83669
|
}
|
|
83670
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
83671
|
+
return {
|
|
83672
|
+
restoredCount: restored.size,
|
|
83673
|
+
failedCount: failures.length
|
|
83674
|
+
};
|
|
83278
83675
|
}
|
|
83279
83676
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
83280
83677
|
toSummary(device) {
|
|
@@ -85037,6 +85434,12 @@ Object.freeze({
|
|
|
85037
85434
|
addonId: null,
|
|
85038
85435
|
access: "view"
|
|
85039
85436
|
},
|
|
85437
|
+
"deviceProvider.reloadDevice": {
|
|
85438
|
+
capName: "device-provider",
|
|
85439
|
+
capScope: "system",
|
|
85440
|
+
addonId: null,
|
|
85441
|
+
access: "create"
|
|
85442
|
+
},
|
|
85040
85443
|
"deviceProvider.start": {
|
|
85041
85444
|
capName: "device-provider",
|
|
85042
85445
|
capScope: "system",
|
package/dist/addon.mjs
CHANGED
|
@@ -63278,7 +63278,26 @@ var DiscoveryCandidateSchema = object({
|
|
|
63278
63278
|
* identity ahead of adoption. Rendering metadata (unit, precision)
|
|
63279
63279
|
* flows live through the cap STATUS SLICE after adoption.
|
|
63280
63280
|
*/
|
|
63281
|
-
sourceInfo: SourceInfoSchema.optional()
|
|
63281
|
+
sourceInfo: SourceInfoSchema.optional(),
|
|
63282
|
+
/**
|
|
63283
|
+
* Set when this candidate is a device the provider ALREADY owns.
|
|
63284
|
+
*
|
|
63285
|
+
* A scan cannot generally produce the identity a device was onboarded under
|
|
63286
|
+
* (Reolink keys on `mac-<mac>`, learned at adopt time), so a stableId
|
|
63287
|
+
* comparison never matches and an owned device looks addable. Re-adopting one
|
|
63288
|
+
* overwrites its config with scan-derived values — that is how a Home Hub's
|
|
63289
|
+
* Baichuan port was overwritten with its ONVIF port, taking the hub and its
|
|
63290
|
+
* three child cameras offline for four hours.
|
|
63291
|
+
*
|
|
63292
|
+
* A provider that can recognise its own devices says so here. Absent means
|
|
63293
|
+
* "not recognised", which is not the same as "known to be new" — a provider
|
|
63294
|
+
* that cannot tell simply never sets it.
|
|
63295
|
+
*/
|
|
63296
|
+
alreadyOnboarded: boolean().optional(),
|
|
63297
|
+
/** Numeric id of the device this candidate was matched to. Set with `alreadyOnboarded`. */
|
|
63298
|
+
onboardedDeviceId: number().optional(),
|
|
63299
|
+
/** Operator-facing name of the matched device, so the UI can say WHICH one it is. */
|
|
63300
|
+
onboardedName: string().optional()
|
|
63282
63301
|
});
|
|
63283
63302
|
/**
|
|
63284
63303
|
* Flat device summary returned by `createDevice` / `adoptDiscoveredDevice`.
|
|
@@ -63334,6 +63353,35 @@ var deviceProviderCapability = {
|
|
|
63334
63353
|
name: string(),
|
|
63335
63354
|
type: string()
|
|
63336
63355
|
}))),
|
|
63356
|
+
/**
|
|
63357
|
+
* Tear down and reconstruct ONE device in place from its persisted rows —
|
|
63358
|
+
* touching no other device this provider owns.
|
|
63359
|
+
*
|
|
63360
|
+
* The primitive `deviceManager.migrateDevice` uses to flush the two
|
|
63361
|
+
* migrated numbers: after `swapIds` the runner's live instance still
|
|
63362
|
+
* carries the PRE-swap numeric id (baked into the object, its native-cap
|
|
63363
|
+
* registrations and its log tags), and a live object cannot be renumbered.
|
|
63364
|
+
* Before this method the only flush was restarting the whole owning addon
|
|
63365
|
+
* — which took every camera the provider owns down with it (28 devices
|
|
63366
|
+
* for one migrated camera, measured 2026-09-04, and the morning of the
|
|
63367
|
+
* same day ~27 devices' native caps did not come back on their own).
|
|
63368
|
+
*
|
|
63369
|
+
* Keyed by `stableId`, deliberately: the numeric id is exactly the thing
|
|
63370
|
+
* that changes. The reply carries the id the device answers on NOW.
|
|
63371
|
+
* Implemented once in `BaseDeviceProvider` — decommission the live
|
|
63372
|
+
* instance (if any), then re-create from the persisted row: the same
|
|
63373
|
+
* teardown/rehydrate pair every graceful shutdown + boot already uses.
|
|
63374
|
+
* An RPC, never an event: a dropped event would leave the runner writing
|
|
63375
|
+
* against the wrong camera (D8).
|
|
63376
|
+
*
|
|
63377
|
+
* Construction can dial hardware, and the migrated source is
|
|
63378
|
+
* characteristically dead — the timeout covers a full activate window
|
|
63379
|
+
* rather than the 60 s default.
|
|
63380
|
+
*/
|
|
63381
|
+
reloadDevice: method(object({ stableId: string() }), object({ deviceId: number() }), {
|
|
63382
|
+
kind: "mutation",
|
|
63383
|
+
timeoutMs: 3 * 6e4
|
|
63384
|
+
}),
|
|
63337
63385
|
supportsDiscovery: method(object({}), boolean()),
|
|
63338
63386
|
/**
|
|
63339
63387
|
* Run a network scan. `params` carries optional provider-specific scan
|
|
@@ -63661,7 +63709,8 @@ method(object({
|
|
|
63661
63709
|
targetId: number()
|
|
63662
63710
|
}), MigrateDeviceResultSchema, {
|
|
63663
63711
|
kind: "mutation",
|
|
63664
|
-
auth: "admin"
|
|
63712
|
+
auth: "admin",
|
|
63713
|
+
timeoutMs: 12 * 6e4
|
|
63665
63714
|
}), 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({
|
|
63666
63715
|
deviceId: number(),
|
|
63667
63716
|
name: string()
|
|
@@ -83042,6 +83091,147 @@ var BaseDevice = class {
|
|
|
83042
83091
|
}
|
|
83043
83092
|
};
|
|
83044
83093
|
/**
|
|
83094
|
+
* Delays before retry rounds 1..N — the round count IS the bound.
|
|
83095
|
+
* 10 s catches "the hub was busy for a moment"; the full schedule
|
|
83096
|
+
* (10 + 30 + 90 s of waiting, plus up to one 60 s transport timeout
|
|
83097
|
+
* per attempt) covers a device-manager lock held for minutes — the
|
|
83098
|
+
* 2026-09-04 outage's migration hold was ~3.5 min.
|
|
83099
|
+
*/
|
|
83100
|
+
var DEVICE_RESTORE_RETRY_DELAYS_MS = [
|
|
83101
|
+
1e4,
|
|
83102
|
+
3e4,
|
|
83103
|
+
9e4
|
|
83104
|
+
];
|
|
83105
|
+
/** Abortable sleep — resolves early (never rejects) on abort. */
|
|
83106
|
+
function sleep$1(ms, signal) {
|
|
83107
|
+
return new Promise((resolve) => {
|
|
83108
|
+
if (signal.aborted) {
|
|
83109
|
+
resolve();
|
|
83110
|
+
return;
|
|
83111
|
+
}
|
|
83112
|
+
const onAbort = () => {
|
|
83113
|
+
clearTimeout(timer);
|
|
83114
|
+
resolve();
|
|
83115
|
+
};
|
|
83116
|
+
const timer = setTimeout(() => {
|
|
83117
|
+
signal.removeEventListener("abort", onAbort);
|
|
83118
|
+
resolve();
|
|
83119
|
+
}, ms);
|
|
83120
|
+
timer.unref?.();
|
|
83121
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
83122
|
+
});
|
|
83123
|
+
}
|
|
83124
|
+
/** Drain `items` through at most `width` concurrent lanes. `fn` must
|
|
83125
|
+
* not reject (callers wrap their own try/catch). */
|
|
83126
|
+
async function runWithConcurrency(items, width, fn) {
|
|
83127
|
+
const queue = [...items];
|
|
83128
|
+
const laneCount = Math.max(1, Math.min(width, queue.length));
|
|
83129
|
+
const lane = async () => {
|
|
83130
|
+
for (;;) {
|
|
83131
|
+
const item = queue.shift();
|
|
83132
|
+
if (item === void 0) return;
|
|
83133
|
+
await fn(item);
|
|
83134
|
+
}
|
|
83135
|
+
};
|
|
83136
|
+
await Promise.all(Array.from({ length: laneCount }, lane));
|
|
83137
|
+
}
|
|
83138
|
+
var DeviceRestoreRetryScheduler = class {
|
|
83139
|
+
#logger;
|
|
83140
|
+
#attempt;
|
|
83141
|
+
#onPermanentFailure;
|
|
83142
|
+
#delaysMs;
|
|
83143
|
+
#concurrency;
|
|
83144
|
+
#now;
|
|
83145
|
+
#abort = new AbortController();
|
|
83146
|
+
constructor(options) {
|
|
83147
|
+
this.#logger = options.logger;
|
|
83148
|
+
this.#attempt = options.attempt;
|
|
83149
|
+
this.#onPermanentFailure = options.onPermanentFailure;
|
|
83150
|
+
this.#delaysMs = options.delaysMs ?? DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
83151
|
+
this.#concurrency = options.concurrency ?? 4;
|
|
83152
|
+
this.#now = options.now ?? Date.now;
|
|
83153
|
+
}
|
|
83154
|
+
/** Stop retrying (shutdown). Pending entries are NOT marked
|
|
83155
|
+
* permanently failed — the next boot restores them from disk. */
|
|
83156
|
+
cancel() {
|
|
83157
|
+
this.#abort.abort();
|
|
83158
|
+
}
|
|
83159
|
+
/**
|
|
83160
|
+
* Run the bounded retry rounds. Resolves when every entry has either
|
|
83161
|
+
* restored, been marked permanently failed, or the scheduler was
|
|
83162
|
+
* cancelled. Never rejects.
|
|
83163
|
+
*/
|
|
83164
|
+
async run(initialFailures) {
|
|
83165
|
+
let pending = initialFailures.map((failure) => ({
|
|
83166
|
+
saved: failure.saved,
|
|
83167
|
+
lastError: failure.error,
|
|
83168
|
+
attempts: 1
|
|
83169
|
+
}));
|
|
83170
|
+
for (let round = 0; round < this.#delaysMs.length; round += 1) {
|
|
83171
|
+
if (pending.length === 0 || this.#abort.signal.aborted) break;
|
|
83172
|
+
await sleep$1(this.#delaysMs[round] ?? 0, this.#abort.signal);
|
|
83173
|
+
if (this.#abort.signal.aborted) break;
|
|
83174
|
+
pending = await this.#runRound(pending, round);
|
|
83175
|
+
}
|
|
83176
|
+
if (this.#abort.signal.aborted) return [];
|
|
83177
|
+
const terminal = pending.map((entry) => ({
|
|
83178
|
+
deviceId: entry.saved.id,
|
|
83179
|
+
stableId: entry.saved.stableId,
|
|
83180
|
+
type: String(entry.saved.type),
|
|
83181
|
+
attempts: entry.attempts,
|
|
83182
|
+
lastError: entry.lastError,
|
|
83183
|
+
failedAt: this.#now()
|
|
83184
|
+
}));
|
|
83185
|
+
for (const failure of terminal) this.#onPermanentFailure(failure);
|
|
83186
|
+
return terminal;
|
|
83187
|
+
}
|
|
83188
|
+
/** One retry round: parents first (phase 0), then hub-adopted
|
|
83189
|
+
* children (phase 1) — a child's attempt depends on its parent
|
|
83190
|
+
* having landed, exactly like the initial two-pass restore. */
|
|
83191
|
+
async #runRound(pending, round) {
|
|
83192
|
+
const next = [];
|
|
83193
|
+
const parents = pending.filter((entry) => entry.saved.parentDeviceId === null);
|
|
83194
|
+
const children = pending.filter((entry) => entry.saved.parentDeviceId !== null);
|
|
83195
|
+
for (const phase of [parents, children]) await runWithConcurrency(phase, this.#concurrency, async (entry) => {
|
|
83196
|
+
if (this.#abort.signal.aborted) {
|
|
83197
|
+
next.push(entry);
|
|
83198
|
+
return;
|
|
83199
|
+
}
|
|
83200
|
+
const attemptNo = entry.attempts + 1;
|
|
83201
|
+
try {
|
|
83202
|
+
await this.#attempt(entry.saved);
|
|
83203
|
+
this.#logger.info("Device restored on retry", {
|
|
83204
|
+
tags: {
|
|
83205
|
+
deviceId: entry.saved.id,
|
|
83206
|
+
stableId: entry.saved.stableId
|
|
83207
|
+
},
|
|
83208
|
+
meta: { attempt: attemptNo }
|
|
83209
|
+
});
|
|
83210
|
+
} catch (err) {
|
|
83211
|
+
const lastError = err instanceof Error ? err.message : String(err);
|
|
83212
|
+
const remainingRetries = this.#delaysMs.length - (round + 1);
|
|
83213
|
+
this.#logger.warn("Device restore retry failed", {
|
|
83214
|
+
tags: {
|
|
83215
|
+
deviceId: entry.saved.id,
|
|
83216
|
+
stableId: entry.saved.stableId
|
|
83217
|
+
},
|
|
83218
|
+
meta: {
|
|
83219
|
+
attempt: attemptNo,
|
|
83220
|
+
remainingRetries,
|
|
83221
|
+
error: lastError
|
|
83222
|
+
}
|
|
83223
|
+
});
|
|
83224
|
+
next.push({
|
|
83225
|
+
saved: entry.saved,
|
|
83226
|
+
lastError,
|
|
83227
|
+
attempts: attemptNo
|
|
83228
|
+
});
|
|
83229
|
+
}
|
|
83230
|
+
});
|
|
83231
|
+
return next;
|
|
83232
|
+
}
|
|
83233
|
+
};
|
|
83234
|
+
/**
|
|
83045
83235
|
* Convert an IDevice to the flat DeviceSummary shape expected by the
|
|
83046
83236
|
* device-provider cap router. Shared across all providers.
|
|
83047
83237
|
*/
|
|
@@ -83090,6 +83280,7 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83090
83280
|
}];
|
|
83091
83281
|
}
|
|
83092
83282
|
async onShutdown() {
|
|
83283
|
+
this.cancelRestoreRetries();
|
|
83093
83284
|
const devices = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
83094
83285
|
for (const device of devices) try {
|
|
83095
83286
|
await this.ctx.kernel.devices?.decommission(device.id);
|
|
@@ -83107,9 +83298,16 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83107
83298
|
async start() {}
|
|
83108
83299
|
async stop() {}
|
|
83109
83300
|
async getStatus() {
|
|
83301
|
+
const all = await this.ctx.kernel.devices?.getAll() ?? [];
|
|
83302
|
+
const summary = this.restoreFailureSummary();
|
|
83303
|
+
if (summary === null) return {
|
|
83304
|
+
connected: true,
|
|
83305
|
+
deviceCount: all.length
|
|
83306
|
+
};
|
|
83110
83307
|
return {
|
|
83111
83308
|
connected: true,
|
|
83112
|
-
deviceCount:
|
|
83309
|
+
deviceCount: all.length,
|
|
83310
|
+
error: summary
|
|
83113
83311
|
};
|
|
83114
83312
|
}
|
|
83115
83313
|
async getDevices() {
|
|
@@ -83199,8 +83397,137 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83199
83397
|
};
|
|
83200
83398
|
}
|
|
83201
83399
|
async restoreDevices(savedDevices) {
|
|
83202
|
-
await this.onRestoreDevices(savedDevices);
|
|
83203
|
-
if (savedDevices.length
|
|
83400
|
+
const report = await this.onRestoreDevices(savedDevices);
|
|
83401
|
+
if (savedDevices.length === 0) return;
|
|
83402
|
+
if (report && report.failedCount > 0) {
|
|
83403
|
+
this.ctx.logger.warn(`Restored ${report.restoredCount}/${savedDevices.length} ${this.providerName} device(s) — ${report.failedCount} failed, bounded retry scheduled`);
|
|
83404
|
+
return;
|
|
83405
|
+
}
|
|
83406
|
+
const restoredCount = report ? report.restoredCount : savedDevices.length;
|
|
83407
|
+
this.ctx.logger.info(`Restored ${restoredCount} ${this.providerName} device(s)`);
|
|
83408
|
+
}
|
|
83409
|
+
/** Retry schedule. Overridable (tests use millisecond delays). */
|
|
83410
|
+
restoreRetryDelaysMs = DEVICE_RESTORE_RETRY_DELAYS_MS;
|
|
83411
|
+
/** Retry lane width. See `device-restore-retry.ts` for why retries
|
|
83412
|
+
* never re-stampede full-width while the initial pass does (D167). */
|
|
83413
|
+
restoreRetryConcurrency = 4;
|
|
83414
|
+
_restoreRetryScheduler = null;
|
|
83415
|
+
_restoreRetryCompletion = null;
|
|
83416
|
+
_permanentRestoreFailures = /* @__PURE__ */ new Map();
|
|
83417
|
+
/** Settles when the background retry rounds finish (or `null` when
|
|
83418
|
+
* nothing failed). Exposed for tests and subclass diagnostics —
|
|
83419
|
+
* boot NEVER awaits this: the runner's post-init handshake goes out
|
|
83420
|
+
* with the devices that restored, and a late success is announced
|
|
83421
|
+
* through the `native-cap-change` → `updateCaps` path. */
|
|
83422
|
+
get restoreRetryCompletion() {
|
|
83423
|
+
return this._restoreRetryCompletion;
|
|
83424
|
+
}
|
|
83425
|
+
/** Devices that exhausted the retry bound this process lifetime. */
|
|
83426
|
+
get permanentRestoreFailures() {
|
|
83427
|
+
return [...this._permanentRestoreFailures.values()];
|
|
83428
|
+
}
|
|
83429
|
+
/** One-line operator-facing summary for `getStatus().error`, or
|
|
83430
|
+
* `null` when every device restored. */
|
|
83431
|
+
restoreFailureSummary() {
|
|
83432
|
+
if (this._permanentRestoreFailures.size === 0) return null;
|
|
83433
|
+
const ids = [...this._permanentRestoreFailures.keys()].join(", ");
|
|
83434
|
+
return `${this._permanentRestoreFailures.size} device(s) permanently failed restore (deviceIds: ${ids}) — restart the ${this.providerName} provider to retry`;
|
|
83435
|
+
}
|
|
83436
|
+
cancelRestoreRetries() {
|
|
83437
|
+
this._restoreRetryScheduler?.cancel();
|
|
83438
|
+
this._restoreRetryScheduler = null;
|
|
83439
|
+
}
|
|
83440
|
+
recordPermanentRestoreFailure(failure) {
|
|
83441
|
+
this._permanentRestoreFailures.set(failure.deviceId, failure);
|
|
83442
|
+
this.ctx.logger.error("Device restore permanently failed — its capabilities will not register until the provider restarts", {
|
|
83443
|
+
tags: {
|
|
83444
|
+
deviceId: failure.deviceId,
|
|
83445
|
+
stableId: failure.stableId
|
|
83446
|
+
},
|
|
83447
|
+
meta: {
|
|
83448
|
+
type: failure.type,
|
|
83449
|
+
attempts: failure.attempts,
|
|
83450
|
+
error: failure.lastError
|
|
83451
|
+
}
|
|
83452
|
+
});
|
|
83453
|
+
}
|
|
83454
|
+
scheduleRestoreRetries(failures, attempt) {
|
|
83455
|
+
const scheduler = new DeviceRestoreRetryScheduler({
|
|
83456
|
+
logger: this.ctx.logger,
|
|
83457
|
+
delaysMs: this.restoreRetryDelaysMs,
|
|
83458
|
+
concurrency: this.restoreRetryConcurrency,
|
|
83459
|
+
attempt,
|
|
83460
|
+
onPermanentFailure: (failure) => this.recordPermanentRestoreFailure(failure)
|
|
83461
|
+
});
|
|
83462
|
+
this._restoreRetryScheduler = scheduler;
|
|
83463
|
+
this._restoreRetryCompletion = scheduler.run(failures).then(() => void 0).catch((err) => {
|
|
83464
|
+
this.ctx.logger.error("Restore retry scheduler crashed", { meta: { error: err instanceof Error ? err.message : String(err) } });
|
|
83465
|
+
});
|
|
83466
|
+
}
|
|
83467
|
+
/**
|
|
83468
|
+
* Tear down and reconstruct ONE device from its persisted rows — the
|
|
83469
|
+
* `deviceProvider.reloadDevice` cap method. Persistence is never touched,
|
|
83470
|
+
* and no other device this provider owns is disturbed.
|
|
83471
|
+
*
|
|
83472
|
+
* Keyed by `stableId` because the caller's whole reason to be here is that
|
|
83473
|
+
* the NUMERIC id changed (`deviceManager.migrateDevice` swapped it): the
|
|
83474
|
+
* fresh instance resolves its id through `allocateDeviceId`, which returns
|
|
83475
|
+
* whatever number the row carries NOW. The teardown is `decommission` —
|
|
83476
|
+
* exactly what a graceful shutdown runs per device (fires `removeDevice()`,
|
|
83477
|
+
* unregisters native caps, drops the registry entry) — and the rebuild is
|
|
83478
|
+
* the boot restore's own `create()` path, including its pass 2: first-class
|
|
83479
|
+
* children (hub-adopted cameras under an NVR) are decommissioned with the
|
|
83480
|
+
* parent by the cascade and must be re-created explicitly, because only
|
|
83481
|
+
* accessory children come back through `getAccessoryChildren()`.
|
|
83482
|
+
*
|
|
83483
|
+
* Reloading an accessory child directly is refused (no device class) —
|
|
83484
|
+
* reload its parent instead.
|
|
83485
|
+
*/
|
|
83486
|
+
async reloadDevice(input) {
|
|
83487
|
+
const { stableId } = input;
|
|
83488
|
+
const devices = this.ctx.kernel.devices;
|
|
83489
|
+
if (!devices) throw new Error(`${this.providerName}: kernel.devices unavailable — cannot reload`);
|
|
83490
|
+
const live = (await devices.getAll()).find((d) => d.stableId === stableId);
|
|
83491
|
+
if (live) await devices.decommission(live.id);
|
|
83492
|
+
const { id } = await this.ctx.api.deviceManager.allocateDeviceId.mutate({
|
|
83493
|
+
addonId: this.addonId,
|
|
83494
|
+
stableId
|
|
83495
|
+
});
|
|
83496
|
+
const meta = await this.ctx.api.deviceManager.loadMeta.query({ deviceId: id });
|
|
83497
|
+
if (meta === null) throw new Error(`${this.providerName}: no persisted meta for "${stableId}" (id ${id}) — cannot reload`);
|
|
83498
|
+
const deviceType = Object.values(DeviceType).find((t) => t === meta.type);
|
|
83499
|
+
const Class = deviceType !== void 0 ? this.deviceClasses[deviceType] : void 0;
|
|
83500
|
+
if (!Class) throw new Error(`${this.providerName}: no device class for type "${meta.type}" — "${stableId}" is an accessory child; reload its parent instead`);
|
|
83501
|
+
await devices.create(stableId, Class, {}, meta.parentDeviceId ?? null);
|
|
83502
|
+
const rows = await this.ctx.api.deviceManager.listPersistedByAddon.query({ addonId: this.addonId });
|
|
83503
|
+
for (const row of rows) {
|
|
83504
|
+
if (row.parentDeviceId !== id) continue;
|
|
83505
|
+
const childType = Object.values(DeviceType).find((t) => t === row.type);
|
|
83506
|
+
const ChildClass = childType !== void 0 ? this.deviceClasses[childType] : void 0;
|
|
83507
|
+
if (!ChildClass) continue;
|
|
83508
|
+
try {
|
|
83509
|
+
await devices.create(row.stableId, ChildClass, {}, id);
|
|
83510
|
+
} catch (err) {
|
|
83511
|
+
this.ctx.logger.warn("reloadDevice: failed to re-create first-class child", {
|
|
83512
|
+
tags: {
|
|
83513
|
+
deviceId: row.id,
|
|
83514
|
+
stableId: row.stableId
|
|
83515
|
+
},
|
|
83516
|
+
meta: {
|
|
83517
|
+
parentDeviceId: id,
|
|
83518
|
+
error: err instanceof Error ? err.message : String(err)
|
|
83519
|
+
}
|
|
83520
|
+
});
|
|
83521
|
+
}
|
|
83522
|
+
}
|
|
83523
|
+
this.ctx.logger.info("device reloaded in place from persisted rows", {
|
|
83524
|
+
tags: { deviceId: id },
|
|
83525
|
+
meta: {
|
|
83526
|
+
stableId,
|
|
83527
|
+
type: meta.type
|
|
83528
|
+
}
|
|
83529
|
+
});
|
|
83530
|
+
return { deviceId: id };
|
|
83204
83531
|
}
|
|
83205
83532
|
/**
|
|
83206
83533
|
* Restore devices from persisted state. Two-pass:
|
|
@@ -83226,55 +83553,125 @@ var BaseDeviceProvider = class extends BaseAddon {
|
|
|
83226
83553
|
* accessory-spawn flow handles via the parent's
|
|
83227
83554
|
* `getAccessoryChildren()`. Override only when the default doesn't
|
|
83228
83555
|
* fit.
|
|
83556
|
+
*
|
|
83557
|
+
* A row that fails either pass is NOT terminal (D347): it is handed
|
|
83558
|
+
* to a bounded background retry (`DeviceRestoreRetryScheduler`).
|
|
83559
|
+
* Only after the bound is exhausted is the device marked permanently
|
|
83560
|
+
* failed — logged at ERROR with `tags.deviceId` and surfaced via
|
|
83561
|
+
* `getStatus().error`.
|
|
83229
83562
|
*/
|
|
83563
|
+
/**
|
|
83564
|
+
* Repair a row's PERSISTED config blob immediately before it is restored.
|
|
83565
|
+
* Default: no-op — most providers have nothing to heal.
|
|
83566
|
+
*
|
|
83567
|
+
* This exists because a restored device self-hydrates from the DB: `create()`
|
|
83568
|
+
* passes `{}` and `BaseDevice` parses the stored blob against the device
|
|
83569
|
+
* schema. A blob that lost a REQUIRED field therefore fails restore forever,
|
|
83570
|
+
* and no later pass revisits it — a hub-adopted Reolink camera whose blob had
|
|
83571
|
+
* been emptied failed all four bounded attempts against fields
|
|
83572
|
+
* (`host`, `password`) it inherits from its parent and never dials itself.
|
|
83573
|
+
*
|
|
83574
|
+
* Implementations get every saved row, so a child can read its parent's blob.
|
|
83575
|
+
* A heal that throws is treated like any other restore failure: retried under
|
|
83576
|
+
* the bound, then reported — never swallowed.
|
|
83577
|
+
*/
|
|
83578
|
+
async healSavedConfig(_saved, _allSaved) {}
|
|
83230
83579
|
async onRestoreDevices(savedDevices) {
|
|
83231
83580
|
const restored = /* @__PURE__ */ new Set();
|
|
83581
|
+
const failures = [];
|
|
83582
|
+
const attemptRestore = async (saved) => {
|
|
83583
|
+
if (restored.has(saved.id)) return;
|
|
83584
|
+
const Class = this.deviceClasses[saved.type];
|
|
83585
|
+
if (!Class) throw new Error(`no device class registered for type "${saved.type}"`);
|
|
83586
|
+
if (saved.parentDeviceId !== null && !restored.has(saved.parentDeviceId)) throw new Error(`parent device ${saved.parentDeviceId} not restored`);
|
|
83587
|
+
await this.healSavedConfig(saved, savedDevices);
|
|
83588
|
+
await this.ctx.kernel.devices.create(saved.stableId, Class, {}, saved.parentDeviceId);
|
|
83589
|
+
restored.add(saved.id);
|
|
83590
|
+
};
|
|
83232
83591
|
const topLevel = savedDevices.filter((saved) => saved.parentDeviceId === null);
|
|
83233
83592
|
const restoreOne = async (saved) => {
|
|
83234
|
-
|
|
83235
|
-
if (!Class) {
|
|
83593
|
+
if (!this.deviceClasses[saved.type]) {
|
|
83236
83594
|
this.ctx.logger.warn("No device class registered for restored type — skipping", {
|
|
83237
|
-
tags: {
|
|
83595
|
+
tags: {
|
|
83596
|
+
deviceId: saved.id,
|
|
83597
|
+
stableId: saved.stableId
|
|
83598
|
+
},
|
|
83238
83599
|
meta: { type: saved.type }
|
|
83239
83600
|
});
|
|
83240
83601
|
return;
|
|
83241
83602
|
}
|
|
83242
83603
|
try {
|
|
83243
|
-
await
|
|
83244
|
-
restored.add(saved.id);
|
|
83604
|
+
await attemptRestore(saved);
|
|
83245
83605
|
} catch (err) {
|
|
83246
|
-
|
|
83247
|
-
|
|
83606
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
83607
|
+
this.ctx.logger.warn("Failed to restore device — bounded retry scheduled", {
|
|
83608
|
+
tags: {
|
|
83609
|
+
deviceId: saved.id,
|
|
83610
|
+
stableId: saved.stableId
|
|
83611
|
+
},
|
|
83248
83612
|
meta: {
|
|
83249
83613
|
type: saved.type,
|
|
83250
|
-
|
|
83614
|
+
attempt: 1,
|
|
83615
|
+
error
|
|
83251
83616
|
}
|
|
83252
83617
|
});
|
|
83618
|
+
failures.push({
|
|
83619
|
+
saved,
|
|
83620
|
+
error
|
|
83621
|
+
});
|
|
83253
83622
|
}
|
|
83254
83623
|
};
|
|
83255
83624
|
await Promise.all(topLevel.map((saved) => restoreOne(saved)));
|
|
83625
|
+
const failedTopLevelIds = new Set(failures.map((failure) => failure.saved.id));
|
|
83256
83626
|
const childRows = savedDevices.filter((s) => s.parentDeviceId !== null);
|
|
83257
83627
|
for (const saved of childRows) {
|
|
83258
|
-
|
|
83259
|
-
if (!Class) continue;
|
|
83628
|
+
if (!this.deviceClasses[saved.type]) continue;
|
|
83260
83629
|
if (saved.parentDeviceId === null) continue;
|
|
83261
|
-
if (
|
|
83262
|
-
|
|
83263
|
-
|
|
83264
|
-
|
|
83265
|
-
|
|
83266
|
-
|
|
83630
|
+
if (restored.has(saved.parentDeviceId)) {
|
|
83631
|
+
try {
|
|
83632
|
+
await attemptRestore(saved);
|
|
83633
|
+
} catch (err) {
|
|
83634
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
83635
|
+
this.ctx.logger.warn("Failed to restore hub-adopted child — bounded retry scheduled", {
|
|
83636
|
+
tags: {
|
|
83637
|
+
deviceId: saved.id,
|
|
83638
|
+
stableId: saved.stableId,
|
|
83639
|
+
parentDeviceId: saved.parentDeviceId
|
|
83640
|
+
},
|
|
83641
|
+
meta: {
|
|
83642
|
+
type: saved.type,
|
|
83643
|
+
attempt: 1,
|
|
83644
|
+
error
|
|
83645
|
+
}
|
|
83646
|
+
});
|
|
83647
|
+
failures.push({
|
|
83648
|
+
saved,
|
|
83649
|
+
error
|
|
83650
|
+
});
|
|
83651
|
+
}
|
|
83652
|
+
continue;
|
|
83653
|
+
}
|
|
83654
|
+
if (failedTopLevelIds.has(saved.parentDeviceId)) {
|
|
83655
|
+
this.ctx.logger.warn("Hub-adopted child deferred — parent failed initial restore", {
|
|
83267
83656
|
tags: {
|
|
83657
|
+
deviceId: saved.id,
|
|
83268
83658
|
stableId: saved.stableId,
|
|
83269
83659
|
parentDeviceId: saved.parentDeviceId
|
|
83270
83660
|
},
|
|
83271
|
-
meta: {
|
|
83272
|
-
type: saved.type,
|
|
83273
|
-
error: err instanceof Error ? err.message : String(err)
|
|
83274
|
-
}
|
|
83661
|
+
meta: { type: saved.type }
|
|
83275
83662
|
});
|
|
83663
|
+
failures.push({
|
|
83664
|
+
saved,
|
|
83665
|
+
error: `parent device ${saved.parentDeviceId} not restored`
|
|
83666
|
+
});
|
|
83667
|
+
continue;
|
|
83276
83668
|
}
|
|
83277
83669
|
}
|
|
83670
|
+
if (failures.length > 0) this.scheduleRestoreRetries(failures, attemptRestore);
|
|
83671
|
+
return {
|
|
83672
|
+
restoredCount: restored.size,
|
|
83673
|
+
failedCount: failures.length
|
|
83674
|
+
};
|
|
83278
83675
|
}
|
|
83279
83676
|
/** Convert an IDevice to the flat DeviceSummary for the cap router. */
|
|
83280
83677
|
toSummary(device) {
|
|
@@ -85037,6 +85434,12 @@ Object.freeze({
|
|
|
85037
85434
|
addonId: null,
|
|
85038
85435
|
access: "view"
|
|
85039
85436
|
},
|
|
85437
|
+
"deviceProvider.reloadDevice": {
|
|
85438
|
+
capName: "device-provider",
|
|
85439
|
+
capScope: "system",
|
|
85440
|
+
addonId: null,
|
|
85441
|
+
access: "create"
|
|
85442
|
+
},
|
|
85040
85443
|
"deviceProvider.start": {
|
|
85041
85444
|
capName: "device-provider",
|
|
85042
85445
|
capScope: "system",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@camstack/addon-provider-dreame",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.61",
|
|
4
4
|
"description": "Dreame robot-vacuum / lawn-mower device-provider addon for CamStack — wraps the @apocaliss92/nodedreame Dreamehome cloud client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"camstack",
|