UNPKG

hottohts

Version:

TypeScript library for HottoH pellet stoves

100 lines 3.75 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Request = exports.CommandMode = exports.Command = void 0; /** * hottohts - Protocol request/packet builder for HottoH stoves * * Handles CRC, packet formatting, and protocol details for all commands. * Ported 1:1 from Hottohpy for protocol compatibility. * * @module request */ const crc = __importStar(require("crc")); var Command; (function (Command) { Command["DAT"] = "DAT"; })(Command || (exports.Command = Command = {})); var CommandMode; (function (CommandMode) { CommandMode["READ"] = "R"; CommandMode["WRITE"] = "W"; CommandMode["EXECUTE"] = "E"; })(CommandMode || (exports.CommandMode = CommandMode = {})); /** * Build and encode a protocol request for the HottoH stove. */ class Request { constructor(command = Command.DAT, mode = CommandMode.READ, parameters = ["0"], id = 0) { this.request = ""; this.command = command; this.mode = mode; this.parameters = parameters; this.id = id; this._buildRawPacket(); } _buildRawPacket() { // Python: strSocketId = str(0).zfill(5) // TypeScript: always use 0 for socket id for parity const strSocketId = '00000'; const strRawData = this._getRawData(); const strData = `${strSocketId}C---${strRawData}`; const strCrc = this._getCrc(strData); this.request = `#${strData}${strCrc}\n`; } _getRawData() { // Python: lenParameters = f"{len(self._getParameters()):0>4X}" // TypeScript: getParameters().length, pad to 4 hex digits const paramString = this._getParameters(); const lenParameters = paramString.length.toString(16).toUpperCase().padStart(4, '0'); return `${lenParameters}${this.command}${this.mode}${paramString}`; } _getParameters() { return this.parameters.join(';') + ";"; } _getCrc(message) { // Python: crc-ccitt-false, which is crc16ccitt in npm 'crc' const crcValue = crc.crc16ccitt(Buffer.from(message, 'utf-8')); return crcValue.toString(16).toUpperCase().padStart(4, '0'); } /** * Get the encoded request as a Buffer for TCP transmission. */ getRequest() { return Buffer.from(this.request, 'utf-8'); } } exports.Request = Request; //# sourceMappingURL=request.js.map