From 4e6c8ce35e17247780ada37e27a5247a4840f103 Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Tue, 28 Jan 2025 09:54:00 -0500 Subject: [PATCH] wire up mqtt connection on device selection --- mods/bambu/bambu.js | 22 +++++++-- mods/bambu/init.js | 113 ++++++++++++++++++++++++++------------------ src/kiri/main.js | 5 +- 3 files changed, 91 insertions(+), 49 deletions(-) diff --git a/mods/bambu/bambu.js b/mods/bambu/bambu.js index 65f3876a..3a23f605 100644 --- a/mods/bambu/bambu.js +++ b/mods/bambu/bambu.js @@ -71,6 +71,15 @@ self.kiri.load(api => { render_list(); } + function printer_update() { + Object.assign(selected.rec, { + host: in_host.value, + code: in_code.value, + serial: in_serial.value, + modified: true + }); + } + function printer_select(name) { btn_del.disabled = false; let rec = printers[name] || {}; @@ -78,9 +87,9 @@ self.kiri.load(api => { in_host.value = rec.host || ''; in_code.value = rec.code || ''; in_serial.value = rec.serial || ''; - in_host.onkeypress = in_host.onblur = () => rec.host = in_host.value; - in_code.onkeypress = in_code.onblur = () => rec.code = in_code.value; - in_serial.onkeypress = in_serial.onblur = () => rec.serial = in_serial.value; + in_host.onkeypress = in_host.onblur = printer_update; + in_code.onkeypress = in_code.onblur = printer_update; + in_serial.onkeypress = in_serial.onblur = printer_update; monitor_start(rec); } @@ -167,6 +176,13 @@ self.kiri.load(api => { render_list(); }); + api.event.on("modal.hide", which => { + if (selected?.rec.modified) { + api.conf.save(); + selected = undefined; + } + }); + api.event.on("device.selected", devsel => { if (!bound) { return; diff --git a/mods/bambu/init.js b/mods/bambu/init.js index 0dacf418..d0991091 100644 --- a/mods/bambu/init.js +++ b/mods/bambu/init.js @@ -5,70 +5,91 @@ module.exports = async (server) => { const { api, env, util } = server; const confdir = util.confdir(); - - let client, topic_report, topic_request, timer; - const mqtt = require("mqtt"); - const mqtt_options = { - protocol: 'mqtts', - port: 8883, - username: 'bblp', - // ca: fs.readFileSync('ca.crt'), - // key: fs.readFileSync('client.key'), - // cert: fs.readFileSync('client.crt'), - rejectUnauthorized: false - }; + const mcache = {}; + + class MQTT { + #timer; + #client; + #serial; + #topic_report; + #topic_request; + #options = { + protocol: 'mqtts', + port: 8883, + username: 'bblp', + // ca: fs.readFileSync('ca.crt'), + // key: fs.readFileSync('client.key'), + // cert: fs.readFileSync('client.crt'), + rejectUnauthorized: false + } + + constructor(host, code, serial, onready) { + this.#options.host = host; + this.#options.password = code; + this.#serial = serial; + let client = this.#client = mqtt.connect(this.#options); + + client.on("connect", () => { + let report = this.#topic_report = `device/${serial}/report`; + let request = this.#topic_request = `device/${serial}/request`; + util.log({ report, request }); + client.subscribe(report, (err) => { + util.log('mqtt_subd', err); + onready(this); + this.#uptime(); + }); + }); + + client.on("message", (topic, message) => { + util.log('mqtt_recv', JSON.parse(message.toString())); + }); + } + + #uptime() { + clearTimeout(this.#timer); + this.#timer = setTimeout(() => { this.end() }, 30000); + } - const mqtt_fn = { async send(msg) { - if (client) { + if (this.#client) { util.log('mqtt_send', msg); - client.publish(topic_request, JSON.stringify(msg)); - clearTimeout(timer); - timer = setTimeout(() => { mqtt_fn.end() }, 30000); + this.#client.publish(this.#topic_request, JSON.stringify(msg)); + this.#uptime(); return true; } else { return false; } - }, + } end() { - if (client) { + if (this.#client) { util.log('mqtt end'); - client.end(); - client = undefined; + this.#client.end(); + this.#client = undefined; } - topic_report = undefined; - topic_request = undefined; + this.#topic_report = undefined; + this.#topic_request = undefined; + delete mcache[this.#serial]; } - }; + } - function get_mqtt(serial) { + function get_mqtt(host, code, serial) { const fns = {}; const promise = new Promise((resolve, reject) => { Object.assign(fns, { resolve, reject }); }); - if (client) { - return mqtt_fn; + let mqtt = mcache[serial]; + if (mqtt) { + return fns.resolve(mqtt); + } else { + mqtt = new MQTT(host, code, serial, obj => { + mcache[serial] = obj; + fns.resolve(obj); + }) } - client = mqtt.connect(mqtt_options); - - client.on("connect", () => { - topic_report = `device/${serial}/report`; - topic_request = `device/${serial}/request`; - util.log({ topic_report, topic_request }); - client.subscribe(topic_report, (err) => { - util.log('mqtt_subscribed', err); - fns.resolve(mqtt_fn); - }); - }); - - client.on("message", (topic, message) => { - util.log('mqtt_recv', JSON.parse(message.toString())); - }); - return promise; } @@ -137,11 +158,10 @@ module.exports = async (server) => { const data = req.app.post; const { host, password, filename, serial, ams } = query; const ams_mapping = ams ? ams.split(',').map(v => parseInt(v)) : undefined; - const mqtt_conn = serial ? get_mqtt(serial) : undefined; + const mqtt_conn = serial ? get_mqtt(host, password, serial) : undefined; ftp_send({ host, password, filename, data }) .then(() => { if (serial) { - Object.assign(mqtt_options, { host, password }); const cmd = { print: { command: "project_file", @@ -181,6 +201,9 @@ module.exports = async (server) => { console.log(msg); switch (msg.cmd) { case "monitor": + get_mqtt(msg.host, msg.code, msg.serial).then(mqtt => { + console.log({ monitor: mqtt }); + }); break; } }); diff --git a/src/kiri/main.js b/src/kiri/main.js index fb2963b4..feddece1 100644 --- a/src/kiri/main.js +++ b/src/kiri/main.js @@ -674,7 +674,10 @@ gapp.register("kiri.main", (root, exports) => { easing(TWEEN.Easing.Quadratic.InOut). to({pct:0}, 100). onUpdate(() => { style.height = `${info.pct}%` }). - onComplete(() => { style.display = '' }). + onComplete(() => { + style.display = ''; + api.event.emit('modal.hide'); + }). start(); }