wire up mqtt connection on device selection

This commit is contained in:
Stewart Allen 2025-01-28 09:54:00 -05:00
commit 4e6c8ce35e
3 changed files with 90 additions and 48 deletions

View file

@ -71,6 +71,15 @@ self.kiri.load(api => {
render_list(); 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) { function printer_select(name) {
btn_del.disabled = false; btn_del.disabled = false;
let rec = printers[name] || {}; let rec = printers[name] || {};
@ -78,9 +87,9 @@ self.kiri.load(api => {
in_host.value = rec.host || ''; in_host.value = rec.host || '';
in_code.value = rec.code || ''; in_code.value = rec.code || '';
in_serial.value = rec.serial || ''; in_serial.value = rec.serial || '';
in_host.onkeypress = in_host.onblur = () => rec.host = in_host.value; in_host.onkeypress = in_host.onblur = printer_update;
in_code.onkeypress = in_code.onblur = () => rec.code = in_code.value; in_code.onkeypress = in_code.onblur = printer_update;
in_serial.onkeypress = in_serial.onblur = () => rec.serial = in_serial.value; in_serial.onkeypress = in_serial.onblur = printer_update;
monitor_start(rec); monitor_start(rec);
} }
@ -167,6 +176,13 @@ self.kiri.load(api => {
render_list(); render_list();
}); });
api.event.on("modal.hide", which => {
if (selected?.rec.modified) {
api.conf.save();
selected = undefined;
}
});
api.event.on("device.selected", devsel => { api.event.on("device.selected", devsel => {
if (!bound) { if (!bound) {
return; return;

View file

@ -5,70 +5,91 @@ module.exports = async (server) => {
const { api, env, util } = server; const { api, env, util } = server;
const confdir = util.confdir(); const confdir = util.confdir();
let client, topic_report, topic_request, timer;
const mqtt = require("mqtt"); const mqtt = require("mqtt");
const mqtt_options = { const mcache = {};
protocol: 'mqtts',
port: 8883, class MQTT {
username: 'bblp', #timer;
// ca: fs.readFileSync('ca.crt'), #client;
// key: fs.readFileSync('client.key'), #serial;
// cert: fs.readFileSync('client.crt'), #topic_report;
rejectUnauthorized: false #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) { async send(msg) {
if (client) { if (this.#client) {
util.log('mqtt_send', msg); util.log('mqtt_send', msg);
client.publish(topic_request, JSON.stringify(msg)); this.#client.publish(this.#topic_request, JSON.stringify(msg));
clearTimeout(timer); this.#uptime();
timer = setTimeout(() => { mqtt_fn.end() }, 30000);
return true; return true;
} else { } else {
return false; return false;
} }
}, }
end() { end() {
if (client) { if (this.#client) {
util.log('mqtt end'); util.log('mqtt end');
client.end(); this.#client.end();
client = undefined; this.#client = undefined;
} }
topic_report = undefined; this.#topic_report = undefined;
topic_request = undefined; this.#topic_request = undefined;
delete mcache[this.#serial];
} }
}; }
function get_mqtt(serial) { function get_mqtt(host, code, serial) {
const fns = {}; const fns = {};
const promise = new Promise((resolve, reject) => { const promise = new Promise((resolve, reject) => {
Object.assign(fns, { resolve, reject }); Object.assign(fns, { resolve, reject });
}); });
if (client) { let mqtt = mcache[serial];
return mqtt_fn; 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; return promise;
} }
@ -137,11 +158,10 @@ module.exports = async (server) => {
const data = req.app.post; const data = req.app.post;
const { host, password, filename, serial, ams } = query; const { host, password, filename, serial, ams } = query;
const ams_mapping = ams ? ams.split(',').map(v => parseInt(v)) : undefined; 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 }) ftp_send({ host, password, filename, data })
.then(() => { .then(() => {
if (serial) { if (serial) {
Object.assign(mqtt_options, { host, password });
const cmd = { const cmd = {
print: { print: {
command: "project_file", command: "project_file",
@ -181,6 +201,9 @@ module.exports = async (server) => {
console.log(msg); console.log(msg);
switch (msg.cmd) { switch (msg.cmd) {
case "monitor": case "monitor":
get_mqtt(msg.host, msg.code, msg.serial).then(mqtt => {
console.log({ monitor: mqtt });
});
break; break;
} }
}); });

View file

@ -674,7 +674,10 @@ gapp.register("kiri.main", (root, exports) => {
easing(TWEEN.Easing.Quadratic.InOut). easing(TWEEN.Easing.Quadratic.InOut).
to({pct:0}, 100). to({pct:0}, 100).
onUpdate(() => { style.height = `${info.pct}%` }). onUpdate(() => { style.height = `${info.pct}%` }).
onComplete(() => { style.display = '' }). onComplete(() => {
style.display = '';
api.event.emit('modal.hide');
}).
start(); start();
} }