zwave-js
Version:
Z-Wave driver written entirely in JavaScript/TypeScript
66 lines • 2.54 kB
JavaScript
import { ZWaveError, ZWaveErrorCodes } from "@zwave-js/core";
import { Bytes } from "@zwave-js/shared";
function parseCommandList(output) {
const commands = [];
for (const line of output.trim().split("\n")) {
const match = line.match(/^\s*([A-Za-z][A-Za-z0-9_]*)\s{2,}(.*?)\s*$/);
if (match) {
commands.push([match[1], match[2]]);
}
else if (commands.length > 0) {
// Continuation line — append to the previous command's description
let trimmed = line.trim();
if (trimmed) {
// "[" indicates an argument description
if (trimmed.startsWith("["))
trimmed = " " + trimmed;
commands.at(-1)[1] += trimmed;
}
}
}
return commands;
}
/** Encapsulates information about the currently active bootloader */
export class EndDeviceCLI {
constructor(writeSerial, expectMessage) {
this.writeSerial = writeSerial;
this.expectMessage = expectMessage;
this._commands = new Map();
}
writeSerial;
expectMessage;
_commands;
get commands() {
return this._commands;
}
async executeCommand(command) {
const normalizedCommand = command.trim();
const commandName = normalizedCommand.split(/\s+/, 1)[0];
if (!this.commands.has(commandName)) {
throw new ZWaveError(`Unknown CLI command ${normalizedCommand}`, ZWaveErrorCodes.Driver_NotSupported);
}
const response = this.expectMessage();
await this.writeSerial(Bytes.from(normalizedCommand + "\r\n", "ascii"));
let ret = await response;
if (!ret)
return;
// Successful commands echo the command itself, followed by a line break
if (ret.startsWith(normalizedCommand + "\r\n")) {
ret = ret.slice(normalizedCommand.length + 2);
}
ret = ret.trim();
// Most commands prefix their response with the log level, usually "[I] "
ret = ret.replace(/^\[[A-Z]\] /, "");
return ret;
}
async detectCommands() {
const response = this.expectMessage();
await this.writeSerial(Bytes.from("help\r\n", "ascii"));
const commandList = await response;
if (!commandList) {
throw new ZWaveError("Failed to detect CLI commands", ZWaveErrorCodes.Driver_NotSupported);
}
this._commands = new Map(parseCommandList(commandList));
}
}
//# sourceMappingURL=EndDeviceCLI.js.map