Baichuan commands as data: an id, the firmware's name, and the parameter elements its bodies carry.
The firmware dispatches each command id to a handler that reads and writes a fixed set of parameter elements. Which of them appear in a request and which in a reply depends on the command (a GET sends nothing and gets the parameters back, a SET sends them and gets an empty reply), so both directions are typed as the same partial record of those parameters.
command(37, "GET_NETPORT_CFG_V20", [serverPort, rtspPort]) body type { ServerPort?: { serverPort: number; enable: number }; RtspPort?: … }
Encode and decode a command body
Encode and decode a command body
import { assertEquals } from "@std/assert"; import { command, decodeCommandBody, encodeCommandBody, } from "@hertzg/reolink-api/protocol/command"; import { int, xmlParam } from "@hertzg/reolink-api/protocol/xml"; const rtspPort = xmlParam("RtspPort", { rtspPort: int(), enable: int() }); const setPorts = command(36, "SET_NETPORT_CFG_V20", [rtspPort]); const xml = encodeCommandBody(setPorts, { RtspPort: { rtspPort: 554, enable: 1 }, }); assertEquals(decodeCommandBody(setPorts, xml), { RtspPort: { rtspPort: 554, enable: 1 }, });
Reads a command body. Elements that are not among the command's parameters are ignored.
Writes a command body as XML. Parameters missing from body are left out;
an empty body gives an empty string, which the firmware treats as "use
the current values" rather than as an empty <body/>.
A Baichuan command: its id, firmware name, and parameter elements.
-
firmware: readonly Firmware[]
The firmware builds known to register the command, empty when unknown. Registration does not promise that a given device answers it.
-
id: number
The command id sent in the message header.
-
name: string
The name the firmware's dispatch table gives the command.
-
params: P
The parameter elements the command's bodies may carry.
The body of a Command, request or reply: each of its parameters by element name, all optional.
A firmware build whose command table this package was derived from.
Usage
import * as mod from "protocol/command.ts";