* Encodes a payload into a Tuya-protocol-compliant packet. * @param {Object} options Options for encoding * @param {Buffer|String|Object} options.data data to encode * @param {Boolean} options.encrypted whether or not to encrypt the data * @param {Number} options.commandByte * command
(options)
| 312 | * @returns {Buffer} Encoded Buffer |
| 313 | */ |
| 314 | encode(options) { |
| 315 | // Check command byte |
| 316 | if (!Object.values(CommandType).includes(options.commandByte)) { |
| 317 | throw new TypeError('Command byte not defined.'); |
| 318 | } |
| 319 | |
| 320 | // Convert Objects to Strings, Strings to Buffers |
| 321 | if (!(options.data instanceof Buffer)) { |
| 322 | if (typeof options.data !== 'string') { |
| 323 | options.data = JSON.stringify(options.data); |
| 324 | } |
| 325 | |
| 326 | options.data = Buffer.from(options.data); |
| 327 | } |
| 328 | |
| 329 | if (this.version === '3.4') { |
| 330 | return this._encode34(options); |
| 331 | } |
| 332 | |
| 333 | if (this.version === '3.5') { |
| 334 | return this._encode35(options); |
| 335 | } |
| 336 | |
| 337 | return this._encodePre34(options); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Encodes a payload into a Tuya-protocol-compliant packet for protocol version 3.3 and below. |