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
+41 -11
View File
@@ -189,15 +189,40 @@ class HttpCamera implements CameraDevice {
// ANPR pushes. So the host (the site's time authority — offline-first, no NTP
// dependency) re-syncs the camera over the same Digest-auth ISAPI used for
// snapshots. The device monitor calls this at the offline→ready edge (the
// power-restored moment) + a daily backstop. See wiki/entities/lpr-camera.md.
// power-restored moment), a daily backstop, and whenever the site's UTC offset
// changes (DST). See wiki/entities/lpr-camera.md.
//
// TIME ZONE: the OSD shows the camera's LOCAL time = instant + its `timeZone` setting.
// park-2 (2026-10-09) ran both cameras on the factory CST-8 (Beijing): the instant was
// right, the burned-in time six hours off, and a correct-instant sync never touched it.
// This firmware (DS-2CD1047G3H) also ignores the DST rule in a POSIX-style zone string
// ("CET-1:00:00DST01:00:00,M3.5.0/02:00:00,M10.5.0/03:00:00" was accepted, echoed back,
// and NOT applied — OSD stayed on +01:00 in October). So the host owns DST too: the zone
// is written as a FIXED offset equal to the site's CURRENT offset ("CST-2:00:00" in summer,
// "CST-1:00:00" in winter — Hikvision's inverted POSIX sign) and rewritten at the next
// sync after the site's offset changes. The abbreviation is cosmetic (the camera renames
// whatever we send to "CST").
/** The Hikvision `timeZone` string for a FIXED offset equal to `localIso`'s UTC offset:
* "+02:00" → "CST-2:00:00", "-05:30" → "CST+5:30:00", "+00:00" → "CST0:00:00". */
export function hikvisionFixedZone(localIso: string): string {
const m = localIso.match(/([+-])(\d{2}):(\d{2})$/);
if (!m) throw new Error(`syncClock: host time has no utc offset: ${localIso}`);
const hours = Number(m[2]);
const minutes = m[3];
if (hours === 0 && minutes === "00") return "CST0:00:00";
// POSIX (and Hikvision) sign is inverted: east of UTC is negative.
const sign = m[1] === "+" ? "-" : "+";
return `CST${sign}${hours}:${minutes}:00`;
}
class HikvisionCamera extends HttpCamera {
/**
* Read the camera clock; when it drifts more than `maxDriftSec` from `localIso`
* (the site's wall-clock now, WITH utc offset), set it via
* PUT /ISAPI/System/time. The camera's own `timeZone` string is echoed back
* verbatim — we correct the CLOCK, never fight the tz/DST config; `localIso`'s
* explicit offset makes the instant unambiguous regardless of that config.
* Read the camera clock + zone. Rewrite the zone when it is not the site's current
* fixed offset (see above); otherwise set the clock only when it drifts more than
* `maxDriftSec` from `localIso` (the site's wall-clock now, WITH utc offset). The PUT
* always carries both: `localIso`'s explicit offset makes the instant unambiguous
* regardless of the zone the camera applies to it.
*/
async syncClock(localIso: string, maxDriftSec: number): Promise<ClockSyncResult> {
const read = await this.deviceRequest("GET", "/ISAPI/System/time");
@@ -207,6 +232,8 @@ class HikvisionCamera extends HttpCamera {
const xml = read.body.toString("utf8");
const cameraTime = xml.match(/<localTime>([^<]+)<\/localTime>/)?.[1]?.trim() ?? null;
const timeZone = xml.match(/<timeZone>([^<]+)<\/timeZone>/)?.[1]?.trim() ?? "";
const wantZone = hikvisionFixedZone(localIso);
const zoneChanged = timeZone !== wantZone;
// Drift: parse both sides as instants. A camera reply without a UTC offset (or
// otherwise unparseable) can't be trusted → treat as infinite drift and sync.
@@ -215,21 +242,24 @@ class HikvisionCamera extends HttpCamera {
const driftSeconds = Number.isFinite(cameraMs)
? Math.round(Math.abs(hostMs - cameraMs) / 1000)
: null;
if (driftSeconds != null && driftSeconds <= maxDriftSec) {
return { driftSeconds, synced: false };
if (!zoneChanged && driftSeconds != null && driftSeconds <= maxDriftSec) {
return { driftSeconds, synced: false, zoneChanged: false, timeZone };
}
const body =
`<?xml version="1.0" encoding="UTF-8"?>` +
`<Time><timeMode>manual</timeMode><localTime>${localIso}</localTime>` +
(timeZone ? `<timeZone>${timeZone}</timeZone>` : "") +
`<timeZone>${wantZone}</timeZone>` +
`</Time>`;
const put = await this.deviceRequest("PUT", "/ISAPI/System/time", body);
if (put.status !== 200) {
throw new Error(`clock set failed: HTTP ${put.status}`);
}
stubLog(this.driverId, `clock synced (was ${driftSeconds ?? "unparseable"}s off)`);
return { driftSeconds, synced: true };
stubLog(
this.driverId,
`clock synced (was ${driftSeconds ?? "unparseable"}s off${zoneChanged ? `; zone ${timeZone || "unset"} → ${wantZone}` : ""})`,
);
return { driftSeconds, synced: true, zoneChanged, timeZone: wantZone };
}
}