This repository has been archived on 2024-09-16. You can view files and clone it, but cannot push or open issues or pull requests.
niimblue-nightly/niimbluelib/utils/gen-printer-models.js
Bot 356e0a9c56
All checks were successful
Test project build / Build (push) Successful in 1m12s
Working tree changes 2024-08-08 01:00
2024-08-08 01:00:01 +03:00

86 lines
2.4 KiB
JavaScript

// https://print.niimbot.com/api/hardware/list
fetch("https://oss-print.niimbot.com/public_resources/static_resources/devices.json")
.then((resp) => resp.json())
.then((items) => {
const dir_d = {
270: "left",
180: "top",
90: "left",
0: "top",
};
const ppmm_d = {
203: 8,
300: 11.81,
};
const labeltypes_d = {
"1": "LT.WithGaps",
"2": "LT.Black",
"3": "LT.Continuous",
"4": "LT.Perforated",
"5": "LT.Transparent",
"6": "LT.PvcTag",
"10": "LT.BlackMarkGap",
"11": "LT.HeatShrinkTube",
}
console.log("/* AUTO-GENERATED FILE. DO NOT EDIT! */");
console.log("/* use 'yarn gen-printer-models' to generate */\n");
console.log('import { PrintDirection, LabelType as LT } from ".";\n');
console.log("export enum PrinterModel {");
console.log(' UNKNOWN = "UNKNOWN",');
for (const item of items) {
const name = item.name.toUpperCase().replaceAll("-", "_");
console.log(` ${name} = "${name}",`);
}
console.log("};");
console.log(`
export interface PrinterModelMeta {
model: PrinterModel;
id: [number, ...number[]];
dpi: number;
printDirection: PrintDirection;
printheadPixels: number;
paperTypes: number[];
}
`);
console.log("export const modelsLibrary: PrinterModelMeta[] = [");
for (const item of items) {
if (item.codes.length === 0) {
continue;
}
const name = item.name.toUpperCase().replaceAll("-", "_");
const dir = dir_d[item.printDirection];
const ppmm = ppmm_d[item.paccuracyName];
const paperTypes = item.paperType.split(',').map(e => labeltypes_d[e]).join(", ");
console.log(" {");
console.log(` model: PrinterModel.${name},`);
console.log(` id: [${item.codes.join(', ')}],`);
console.log(` dpi: ${item.paccuracyName},`);
console.log(` printDirection: "${dir}",`);
console.log(` printheadPixels: ${Math.ceil(item.widthSetEnd * ppmm)},`);
console.log(` paperTypes: [${paperTypes}],`);
console.log(" },");
}
console.log("];");
console.log(`
export const getPrinterMetaById = (id: number): PrinterModelMeta | undefined => {
return modelsLibrary.find((o) => o.id.includes(id));
};
export const getPrinterMetaByModel = (model: PrinterModel): PrinterModelMeta | undefined => {
return modelsLibrary.find((o) => o.model === model);
};`);
});