fix(devices): Hikvision clock sync owns the time zone (fixed offset, re-set at DST edges)

park-2's cameras burned Beijing time into every frame: factory zone CST-8,
and the sync compared instants only, so zero drift meant the zone was never
touched. The G3H firmware also accepts a POSIX zone with a DST rule, echoes
it back, and ignores the rule.

syncClock now derives the site's current UTC offset as a fixed Hikvision
zone (hikvisionFixedZone) and rewrites it when it differs, sending the clock
with it. The device monitor adds a third trigger beside offline->ready and
the 24h backstop: the moment the site's UTC offset changes, so a DST switch
is handled within one poll. ClockSyncResult reports zoneChanged/timeZone.

Both park-2 cameras were set to CST-2:00:00 by hand on 2026-10-09; this
must be deployed there before 2026-10-25 or the OSD reads 1h ahead from
that day until it is.
This commit is contained in:
2026-10-11 18:17:25 +02:00
parent 9332802e5d
commit 7b29331f52
5 changed files with 163 additions and 31 deletions
+18 -6
View File
@@ -27,8 +27,10 @@ const POLL_MS = Number(process.env.DEVICE_POLL_MS ?? 8000);
// Camera clock sync (Hikvision loses its clock on power cuts — reboots at the 1970
// epoch until a human logs into its web UI). The monitor re-syncs from the HOST
// clock (the site's offline time authority) at the offline→ready edge — exactly the
// power-restored moment — plus a daily backstop; drift under the threshold is left
// alone. See wiki/entities/lpr-camera.md (clock sync).
// power-restored moment — plus a daily backstop, plus the moment the site's UTC offset
// changes (a DST switch: the camera's zone is a FIXED offset the host rewrites, see
// HikvisionCamera.syncClock); drift under the threshold is left alone. See
// wiki/entities/lpr-camera.md (clock sync).
const CLOCK_SYNC_BACKSTOP_MS = 24 * 60 * 60 * 1000;
const CLOCK_MAX_DRIFT_SEC = 60;
@@ -201,18 +203,26 @@ export class DeviceMonitor {
}
// Camera clock re-sync at the power-restored edge (prev offline/unknown →
// ready) + a daily backstop. Stamped BEFORE the async attempt so a failing
// camera is retried at backstop cadence, never every poll.
// ready) + a daily backstop + the site's UTC-offset edge (DST switch). Stamped
// BEFORE the async attempt so a failing camera is retried at backstop cadence,
// never every poll.
if (row.category === "camera" && next.state === "ready" && device && isClockSyncable(device)) {
const prev = this.#latest.get(row.id);
const cameBack = !prev || prev.state === "offline";
const last = this.#clockSyncedAt.get(row.id) ?? 0;
if (cameBack || Date.now() - last > CLOCK_SYNC_BACKSTOP_MS) {
const nowIso = localIsoWithOffset(siteTz(this.#db));
const offset = nowIso.slice(-6);
const offsetChanged = this.#clockSyncedOffset.get(row.id) !== offset;
if (cameBack || offsetChanged || Date.now() - last > CLOCK_SYNC_BACKSTOP_MS) {
this.#clockSyncedAt.set(row.id, Date.now());
this.#clockSyncedOffset.set(row.id, offset);
const cam = device;
void (async () => {
try {
const r = await cam.syncClock(localIsoWithOffset(siteTz(this.#db)), CLOCK_MAX_DRIFT_SEC);
const r = await cam.syncClock(nowIso, CLOCK_MAX_DRIFT_SEC);
if (r.zoneChanged) {
this.#log.warn(`device-monitor: camera ${row.id} time zone set to ${r.timeZone ?? "?"} (site offset ${offset})`);
}
if (r.synced) {
// A large jump is the 1970 power-cut signature — warn (persisted) so
// the reboot stays visible; a small correction is routine info.
@@ -249,6 +259,8 @@ export class DeviceMonitor {
/** Per-camera timestamp of the last clock-sync ATTEMPT (backstop pacing). */
readonly #clockSyncedAt = new Map<string, number>();
/** Per-camera site UTC offset ("+02:00") at the last attempt — a change (DST) re-syncs. */
readonly #clockSyncedOffset = new Map<string, string>();
/** Cache + emit a status, but only when it CHANGED (state or detail). */
#publish(id: string, next: DeviceStatusEvent): void {