improve connect button visible state. add debug flag for less verbose mqtt

This commit is contained in:
Stewart Allen 2025-02-03 09:48:19 -05:00
commit 62ed1e053a
2 changed files with 20 additions and 13 deletions

View file

@ -14,6 +14,7 @@ self.kiri.load(api => {
let init = false; let init = false;
let status = {}; let status = {};
let monitors = [];
let bound, device, printers, select, selected, conn_alert; let bound, device, printers, select, selected, conn_alert;
let btn_del, in_host, in_code, in_serial, filelist; let btn_del, in_host, in_code, in_serial, filelist;
let host, password, serial, amsmap, socket = { let host, password, serial, amsmap, socket = {
@ -34,7 +35,7 @@ self.kiri.load(api => {
}; };
ws.onmessage = msg => { ws.onmessage = msg => {
let data = JSON.parse(msg.data); let data = JSON.parse(msg.data);
let { serial, message, files, found, deleted, error } = data; let { serial, message, monitoring, files, found, deleted, error } = data;
if (error) { if (error) {
console.log({ serial, error }); console.log({ serial, error });
api.alerts.show(`Bambu Error: ${error}`, 3); api.alerts.show(`Bambu Error: ${error}`, 3);
@ -42,6 +43,9 @@ self.kiri.load(api => {
} else if (deleted) { } else if (deleted) {
console.log('file deleted', deleted); console.log('file deleted', deleted);
file_list(); file_list();
} else if (monitoring) {
// console.log({ monitoring });
monitors = monitoring;
} else if (found) { } else if (found) {
for (let bblp of Object.entries(found)) { for (let bblp of Object.entries(found)) {
let [ name, rec ] = bblp; let [ name, rec ] = bblp;
@ -281,9 +285,9 @@ self.kiri.load(api => {
} }
function monitoring() { function monitoring() {
let active = selected?.rec?.serial ? true : false; let mon = selected?.rec?.serial ?? '';
ui.setVisible($('bbl_connect'), !active && select.value !== ''); ui.setVisible($('bbl_connect'), select.value !== '' && monitors.indexOf(mon) < 0);
return active; return mon ? true : false;
} }
function cmd_if(cmd, obj = {}) { function cmd_if(cmd, obj = {}) {

View file

@ -9,6 +9,7 @@ module.exports = async (server) => {
const mcache = {}; const mcache = {};
const wsopen = []; const wsopen = [];
const found = {}; const found = {};
const debug = false;
class MQTT { class MQTT {
#timer; #timer;
@ -38,7 +39,7 @@ module.exports = async (server) => {
let request = this.#topic_request = `device/${serial}/request`; let request = this.#topic_request = `device/${serial}/request`;
// util.log({ report, request }); // util.log({ report, request });
client.subscribe(report, (err) => { client.subscribe(report, (err) => {
util.log('mqtt sub', this.#serial, err || "ok"); debug && util.log('mqtt sub', this.#serial, err || "ok");
onready(this); onready(this);
this.keepalive(); this.keepalive();
// this.keepconn(); // this.keepconn();
@ -50,7 +51,7 @@ module.exports = async (server) => {
if (onmessage) { if (onmessage) {
onmessage(message); onmessage(message);
} else { } else {
util.log('mqtt_recv', this.#serial, message); debug && util.log('mqtt_recv', this.#serial, message);
} }
}); });
@ -76,7 +77,7 @@ module.exports = async (server) => {
async send(msg) { async send(msg) {
if (this.#client) { if (this.#client) {
util.log('mqtt send', this.#serial, msg); debug && util.log('mqtt send', this.#serial, msg);
this.#client.publish(this.#topic_request, JSON.stringify(msg)); this.#client.publish(this.#topic_request, JSON.stringify(msg));
this.keepalive(); this.keepalive();
return true; return true;
@ -87,7 +88,7 @@ module.exports = async (server) => {
end() { end() {
if (this.#client) { if (this.#client) {
util.log('mqtt end', this.#serial); debug && util.log('mqtt end', this.#serial);
this.#client.end(); this.#client.end();
this.#client = undefined; this.#client = undefined;
} }
@ -190,7 +191,7 @@ module.exports = async (server) => {
function file_print(opts = {}) { function file_print(opts = {}) {
const { host, code, serial, filename, amsmap } = opts; const { host, code, serial, filename, amsmap } = opts;
util.log({ file_print: opts }); debug && util.log({ file_print: opts });
const cmd = { const cmd = {
print: { print: {
command: "project_file", command: "project_file",
@ -209,7 +210,7 @@ module.exports = async (server) => {
cmd.print.ams_mapping = amsmap; cmd.print.ams_mapping = amsmap;
} }
get_mqtt(host, code, serial, message => { get_mqtt(host, code, serial, message => {
util.log('mqtt_recv', message); debug && util.log('mqtt_recv', message);
wsend({ serial, message }); wsend({ serial, message });
}) })
.then(mqtt => mqtt.send(cmd)) .then(mqtt => mqtt.send(cmd))
@ -313,7 +314,7 @@ module.exports = async (server) => {
server.ws.register("/bambu", function(ws, req) { server.ws.register("/bambu", function(ws, req) {
wsopen.push(ws); wsopen.push(ws);
util.log('ws open', req.url, wsopen.length); debug && util.log('ws open', req.url, wsopen.length);
wsend({ found }); wsend({ found });
ws.on('message', msg => { ws.on('message', msg => {
msg = JSON.parse(msg); msg = JSON.parse(msg);
@ -340,6 +341,8 @@ module.exports = async (server) => {
command: "get_version" command: "get_version"
} }
}); });
// announce all current monitor hosts
wsend({ monitoring: Object.keys(mcache) });
}).catch(error => { }).catch(error => {
util.log({ mqtt_err: error }); util.log({ mqtt_err: error });
wsend({ serial, error: error.message || error.toString() }); wsend({ serial, error: error.message || error.toString() });
@ -347,7 +350,7 @@ module.exports = async (server) => {
break; break;
case "files": case "files":
ftp_list({ host, code }).then(files => { ftp_list({ host, code }).then(files => {
util.log({ ftp_files: files.length }); debug && util.log({ ftp_files: files.length });
// console.log(JSON.stringify(files,undefined,4)); // console.log(JSON.stringify(files,undefined,4));
files = files files = files
.filter(file => file.name.toLowerCase().endsWith(".3mf")) .filter(file => file.name.toLowerCase().endsWith(".3mf"))
@ -396,7 +399,7 @@ module.exports = async (server) => {
ws.on('close', () => { ws.on('close', () => {
let io = wsopen.indexOf(ws); let io = wsopen.indexOf(ws);
if (io >= 0) wsopen.splice(io, 1); if (io >= 0) wsopen.splice(io, 1);
util.log('ws close', wsopen.length); debug && util.log('ws close', wsopen.length);
}); });
}); });
}; };