add auto-discovery of bblp from ssdp broadcasts
This commit is contained in:
parent
3e68e92ddf
commit
7f65c48299
2 changed files with 52 additions and 5 deletions
|
|
@ -34,7 +34,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, deleted, error } = data;
|
let { serial, message, 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 +42,16 @@ 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 (found) {
|
||||||
|
for (let bblp of Object.entries(found)) {
|
||||||
|
let [ name, rec ] = bblp;
|
||||||
|
let { host, srno } = rec;
|
||||||
|
if (printers && name && !printers[name]) {
|
||||||
|
console.log({ discovered: name, host });
|
||||||
|
printers[name] = { host, serial: srno };
|
||||||
|
render_list();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (serial) {
|
} else if (serial) {
|
||||||
let rec = status[serial] = deepMerge(status[serial] || {}, message);
|
let rec = status[serial] = deepMerge(status[serial] || {}, message);
|
||||||
if (files) {
|
if (files) {
|
||||||
|
|
@ -263,10 +273,6 @@ self.kiri.load(api => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function monitor_keepalive() {
|
function monitor_keepalive() {
|
||||||
// console.log({ keepalive: selected });
|
|
||||||
// if (monitoring()) {
|
|
||||||
// socket.send({ cmd: "keepalive", serial: selected.rec.serial });
|
|
||||||
// }
|
|
||||||
cmd_if("keepalive");
|
cmd_if("keepalive");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -668,6 +674,7 @@ self.kiri.load(api => {
|
||||||
if (which !== 'bambu' || !device) {
|
if (which !== 'bambu' || !device) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
socket.start();
|
||||||
render_list();
|
render_list();
|
||||||
get_ams_map(api.conf.get());
|
get_ams_map(api.conf.get());
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ module.exports = async (server) => {
|
||||||
const mqtt = require("mqtt");
|
const mqtt = require("mqtt");
|
||||||
const mcache = {};
|
const mcache = {};
|
||||||
const wsopen = [];
|
const wsopen = [];
|
||||||
|
const found = {};
|
||||||
|
|
||||||
class MQTT {
|
class MQTT {
|
||||||
#timer;
|
#timer;
|
||||||
|
|
@ -247,6 +248,44 @@ module.exports = async (server) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// start SSDP listener for local Bambu printer broadcasts
|
||||||
|
{
|
||||||
|
const dgram = require("dgram");
|
||||||
|
const SSDP_ADDRESS = "239.255.255.250";
|
||||||
|
const SSDP_PORT = 1990;
|
||||||
|
const socket = dgram.createSocket("udp4");
|
||||||
|
|
||||||
|
socket.on("message", msg => {
|
||||||
|
msg = msg.toString();
|
||||||
|
if (msg.indexOf('.bambu.com') > 0) {
|
||||||
|
let rec = {};
|
||||||
|
map = msg.split('\n')
|
||||||
|
.filter(l => l.indexOf(': ') > 0)
|
||||||
|
.map(l => l.trim().replace('.bambu.com','').split(': '));
|
||||||
|
map.forEach(line => rec[line[0]] = line[1]);
|
||||||
|
// console.log({ ssdp: rec, map })
|
||||||
|
if (rec.DevName && rec.Location) {
|
||||||
|
let nurec = {
|
||||||
|
host: rec.Location,
|
||||||
|
name: rec.DevName,
|
||||||
|
type: rec.DevModel,
|
||||||
|
firm: rec.DevVersion,
|
||||||
|
srno: rec.USN
|
||||||
|
}
|
||||||
|
if (!found[rec.DevName]) {
|
||||||
|
found[rec.DevName] = nurec;
|
||||||
|
util.log(`found Bambu ${nurec.name} ${nurec.srno} @ ${nurec.host}`);
|
||||||
|
}
|
||||||
|
wsend({ found });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.bind(SSDP_PORT, () => {
|
||||||
|
socket.addMembership(SSDP_ADDRESS);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
api.bambu_send = (req, res, next) => {
|
api.bambu_send = (req, res, next) => {
|
||||||
const { app, url, headers } = req;
|
const { app, url, headers } = req;
|
||||||
const { host } = headers;
|
const { host } = headers;
|
||||||
|
|
@ -275,6 +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);
|
util.log('ws open', req.url, wsopen.length);
|
||||||
|
wsend({ found });
|
||||||
ws.on('message', msg => {
|
ws.on('message', msg => {
|
||||||
msg = JSON.parse(msg);
|
msg = JSON.parse(msg);
|
||||||
let { cmd, host, code, serial, path, amsmap, direct } = msg;
|
let { cmd, host, code, serial, path, amsmap, direct } = msg;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue