UNPKG

zwave-js

Version:

Z-Wave driver written entirely in JavaScript/TypeScript

93 lines (92 loc) 3.27 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var EndDeviceCLI_exports = {}; __export(EndDeviceCLI_exports, { EndDeviceCLI: () => EndDeviceCLI }); module.exports = __toCommonJS(EndDeviceCLI_exports); var import_core = require("@zwave-js/core"); var import_shared = require("@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) { let trimmed = line.trim(); if (trimmed) { if (trimmed.startsWith("[")) trimmed = " " + trimmed; commands.at(-1)[1] += trimmed; } } } return commands; } __name(parseCommandList, "parseCommandList"); class EndDeviceCLI { static { __name(this, "EndDeviceCLI"); } constructor(writeSerial, expectMessage) { this.writeSerial = writeSerial; this.expectMessage = expectMessage; this._commands = /* @__PURE__ */ 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 import_core.ZWaveError(`Unknown CLI command ${normalizedCommand}`, import_core.ZWaveErrorCodes.Driver_NotSupported); } const response = this.expectMessage(); await this.writeSerial(import_shared.Bytes.from(normalizedCommand + "\r\n", "ascii")); let ret = await response; if (!ret) return; if (ret.startsWith(normalizedCommand + "\r\n")) { ret = ret.slice(normalizedCommand.length + 2); } ret = ret.trim(); ret = ret.replace(/^\[[A-Z]\] /, ""); return ret; } async detectCommands() { const response = this.expectMessage(); await this.writeSerial(import_shared.Bytes.from("help\r\n", "ascii")); const commandList = await response; if (!commandList) { throw new import_core.ZWaveError("Failed to detect CLI commands", import_core.ZWaveErrorCodes.Driver_NotSupported); } this._commands = new Map(parseCommandList(commandList)); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EndDeviceCLI }); //# sourceMappingURL=EndDeviceCLI.js.map