'use strict'; const tinyTypedEmitter = require('tiny-typed-emitter'); const mqtt = require('mqtt'); const tsDeepmerge = require('ts-deepmerge'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const mqtt__default = /*#__PURE__*/_interopDefaultCompat(mqtt); var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; const defaultOptions = { port: 8883, username: "bblp", autoconnect: true }; class Bambu extends tinyTypedEmitter.TypedEmitter { constructor(options) { super(); __publicField(this, "options"); __publicField(this, "client"); __publicField(this, "commandTopic"); __publicField(this, "reportTopic"); __publicField(this, "address"); __publicField(this, "_state"); __publicField(this, "handleMessage", (topic, message) => { const parsed = JSON.parse(message.toString()); if (!this._state) this._state = parsed; this._state = tsDeepmerge.merge.withOptions( { mergeArrays: false, uniqueArrayItems: false }, this._state, parsed ); this.emit("state", this._state); }); __publicField(this, "sendCommandRaw", (command) => { this.client.publish(this.commandTopic, command); }); __publicField(this, "sendCommand", (command) => { this.sendCommandRaw(JSON.stringify(command)); }); __publicField(this, "commandFactory", (command) => { return () => this.sendCommand(command); }); // -------------------------------------------------------------------------------- __publicField(this, "refreshState", this.commandFactory({ pushing: { command: "pushall" } })); __publicField(this, "pausePrint", this.commandFactory({ print: { command: "pause" } })); __publicField(this, "stopPrint", this.commandFactory({ print: { command: "stop" } })); __publicField(this, "resumePrint", this.commandFactory({ print: { command: "resume" } })); __publicField(this, "setLight", (state) => { this.sendCommand({ system: { led_mode: state ? "on" : "off" } }); }); this.options = { ...defaultOptions, ...options }; this.commandTopic = `device/${this.options.serial}/request`; this.reportTopic = `device/${this.options.serial}/report`; this.address = `mqtts://${this.options.host}:${this.options.port}`; this.client = mqtt__default.connect(this.address, { manualConnect: !this.options.autoconnect, username: this.options.username, password: this.options.password, reconnectPeriod: 30, rejectUnauthorized: false }); this.client.on("connect", () => { this.emit("connect"); this.client.subscribe(this.reportTopic); this.refreshState(); }); this.client.on("disconnect", () => { this.emit("disconnect"); }); this.client.on("message", this.handleMessage); } } exports.Bambu = Bambu;