Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Better IP detection, manually set ip too
Browse files Browse the repository at this point in the history
If the IP can't be gathered from MQTT, prompt the user to provide it.
Added bambu-cli set-ip <machine> --ip xx.xx.xx.xx
  • Loading branch information
davglass committed Jan 5, 2024
1 parent 33ac70a commit 559f47b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
2 changes: 2 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const args = require('yargs')
.describe('force', 'Skip the cache or force an operation')
.boolean('force')
.describe('id', 'Pass a device id to limit to one')
.describe('ip', 'The IP Address to set for a machine')
.describe('json', 'Print JSON output')
.boolean('json')
.describe('keys', 'Alone shows all keys in message, pass a comma-sep list of keys to print')
Expand All @@ -40,6 +41,7 @@ const args = require('yargs')
.command('mqtt', 'Show mqtt messages [--keys] [--json] [--path] (--json --keys ams,vt_tray) [--path $..ams.ams[0].tray[0]]')
.command('parse', 'Parse details from a .3mf file [--file] [--force]')
//.command('print', 'Pass file name on printer to print [--file]')
.command('set-ip', 'Manually set a machine ip [--ip xx.xx.xx.xx]')
.command('status', 'Check machine connectivity [--id to get detailed info] [--slim] [--json]')
.command('timelapse', 'Show video files on machine [--id] [--filter] [--download] [--delete] [--yes]')
.command('upload', 'Upload a .gcode or .gcode.3mf file [--id] [--upload]')
Expand Down
2 changes: 2 additions & 0 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ module.exports.machines = require('./machines.js');
module.exports.print = require('./print.js');
module.exports.mqtt = require('./mqtt.js');

module.exports['set-ip'] = require('./mip.js');

module.exports.ls = module.exports.machines; //Alias for machines
4 changes: 2 additions & 2 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ const MQTT = cfg.get('mqtt_user');

const MACHINES = cfg.get('machines');

const DEBUG = `🐞`;
const DEBUG = `🐞 `;
const ERROR = `⛔`;
const WARN = `⚠️ `;
const INFO = `ℹ️`;
const INFO = `ℹ️ `;

let DEBUGGING = false;

Expand Down
39 changes: 30 additions & 9 deletions lib/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,39 @@ const getIPs = (tokens, machines, cb) => {
const machine = getDevice(device);
const json = JSON.parse(message.toString());
logger.debug(`MQTT Message:`, JSON.stringify(json, null, 4));
if (json.print && json.print.ipcam && json.print.ipcam.rtsp_url) {
const ip = parse(json.print.ipcam.rtsp_url).hostname;
logger.log(`Found IP for ${machine.name}: ${ip}`);
machines.forEach((m) => {
if (m.id === device) {
m.ip = ip;
if (json.print && json.print.ipcam) {
if (json.print.ipcam.rtsp_url) {
const ip = parse(json.print.ipcam.rtsp_url).hostname;
logger.log(`Found IP for ${machine.name}: ${ip}`);
machines.forEach((m) => {
if (m.id === device) {
m.ip = ip;
}
});
config.set('machines', machines);
validate();
} else {
client.end();
if (!DONE) {
setIPs(machines);
}
});
config.set('machines', machines);
validate();
DONE = true;
}
}
});
let DONE;
const setIPs = () => {
logger.warn(`Doh! Machine IP doesn't appear in the messages.`);
logger.warn(`Let's do this by hand, gather the IP addresses of your printer(s)..`);
for (const m of machines) {
const ip = rl.question(`What is the IP address of ${m.name}? `);
if (ip) {
m.ip = ip;
}
}
config.set('machines', machines);
logger.log(`Finished setting up machines. All Good!`);
};

const validate = () => {
const machines = config.get('machines');
Expand Down
25 changes: 25 additions & 0 deletions lib/mip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

const cfg = require('./config.js');
const logger = require('./logger.js');

const RE = /^(\d{1,3}\.){3}\d{1,3}$/;

module.exports = (args) => {
if (!args.id || !args.ip || args.ip === true) {
logger.error(`Please pass a machine and an --ip`);
process.exit(1);
}
if (!RE.test(args.ip)) {
logger.error(`Please provide a valid IP Address.`);
process.exit(1);
}
logger.info(`Setting ip address of ${args.id} to ${args.ip}`);
const machines = cfg.get('machines');
machines.forEach(m => {
if (m.id === args.id) {
m.ip = args.ip;
}
});
cfg.set('machines', machines);
logger.log(`IP address saved.`);
};

0 comments on commit 559f47b

Please sign in to comment.