UNPKG

@mmote/niimblue-node

Version:

Headless clients for niimbluelib. Command line interface, simple REST server are also included.

96 lines (95 loc) 4.06 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cliFlashFirmware = exports.cliPrinterInfo = exports.cliScan = exports.cliConnectAndPrintImageFile = void 0; const fs_1 = __importDefault(require("fs")); const sharp_1 = __importDefault(require("sharp")); const __1 = require(".."); const utils_1 = require("../utils"); const extra_typings_1 = require("@commander-js/extra-typings"); const cliConnectAndPrintImageFile = async (path, options) => { const client = (0, utils_1.initClient)(options.transport, options.address, options.debug); if (options.debug) { console.log("Connecting to", options.transport, options.address); } await client.connect(); let image = await (0, utils_1.loadImageFromFile)(path); image = image.flatten({ background: "#fff" }).threshold(options.threshold); if (options.labelWidth !== undefined && options.labelHeight !== undefined) { image = image.resize(options.labelWidth, options.labelHeight, { kernel: sharp_1.default.kernel.nearest, fit: options.imageFit ?? "contain", position: options.imagePosition ?? "centre", background: "#fff", }); } else if (options.imageFit !== undefined || options.imagePosition !== undefined) { throw new extra_typings_1.InvalidArgumentError("label-width and label-height must be set"); } const printDirection = options.printDirection ?? client.getModelMetadata()?.printDirection; const printTask = options.printTask ?? client.getPrintTaskType(); const encoded = await __1.ImageEncoder.encodeImage(image, printDirection); if (printTask === undefined) { throw new Error("Unable to detect print task, please set it manually"); } if (options.debug) { console.log("Print task:", printTask); } try { await (0, utils_1.printImage)(client, printTask, encoded, { quantity: options.quantity, labelType: options.labelType, density: options.density, }); } finally { await client.disconnect(); } process.exit(0); }; exports.cliConnectAndPrintImageFile = cliConnectAndPrintImageFile; const cliScan = async (options) => { if (options.transport === "ble") { const devices = await __1.NiimbotHeadlessBleClient.scan(options.timeout); devices.forEach((dev) => console.log(`${dev.address}: ${dev.name}`)); } else if (options.transport === "serial") { const devices = await __1.NiimbotHeadlessSerialClient.scan(); devices.forEach((dev) => console.log(`${dev.address}: ${dev.name}`)); } process.exit(0); }; exports.cliScan = cliScan; const cliPrinterInfo = async (options) => { const client = (0, utils_1.initClient)(options.transport, options.address, options.debug); await client.connect(); console.log("Printer info:", client.getPrinterInfo()); console.log("Model metadata:", client.getModelMetadata()); console.log("Detected print task:", client.getPrintTaskType()); await client.disconnect(); process.exit(0); }; exports.cliPrinterInfo = cliPrinterInfo; const cliFlashFirmware = async (options) => { const data = fs_1.default.readFileSync(options.file); const client = (0, utils_1.initClient)(options.transport, options.address, options.debug); await client.connect(); client.stopHeartbeat(); const listener = (e) => { console.log(`Sending ${e.currentChunk}/${e.totalChunks}`); }; client.on("firmwareprogress", listener); try { console.log("Uploading firmware..."); await client.abstraction.firmwareUpgrade(data, options.newVersion); console.log("Done, printer will shut down"); } finally { client.off("firmwareprogress", listener); await client.disconnect(); } process.exit(0); }; exports.cliFlashFirmware = cliFlashFirmware;