Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
dohooo committed Mar 27, 2023
2 parents d68acb4 + 202aaa9 commit 9d19e57
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ But my full-time job is very busy. So the progress is slow, And any sponsorship
## Example

````javascript
import { Printer } from "@node-escpos/core";
import { Printer, Image } from "@node-escpos/core";
// install escpos-usb adapter module manually
import USB from "@node-escpos/usb-adapter";
// Select the adapter based on your printer type
import { join } from "path";

const device = new USB();

device.open(async function(err){
Expand Down
6 changes: 6 additions & 0 deletions packages/serialport-adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @node-escpos/serialport-adapter

## 1.0.0

### Major Changes

- 5c6f405: Changed Serial.list() to be static as it is not bind to the object

## 0.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/serialport-adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@node-escpos/serialport-adapter",
"type": "module",
"version": "0.0.3",
"version": "1.0.0",
"packageManager": "pnpm@7.2.1",
"description": "",
"author": "Caspian <zhaodonghao586@outlook.com> (https://github.com/dohooo)",
Expand Down
4 changes: 2 additions & 2 deletions packages/serialport-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export default class Serial extends Adapter<[timeout?: number]> {
}

/**
* List Printers
* List Serial Devices
* @returns {[Array]}
*/
async list() {
static async list() {
const ports: PortInfo[] = await SerialPort.list();
return ports;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/usb-adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @node-escpos/usb-adapter

## 0.1.0

### Minor Changes

- 8a1d9bd: Added getDeviceBySerial and outsourced isPrinter function for reusability

## 0.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/usb-adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@node-escpos/usb-adapter",
"type": "module",
"version": "0.0.3",
"version": "0.1.0",
"packageManager": "pnpm@7.2.1",
"description": "",
"author": "Caspian <zhaodonghao586@outlook.com> (https://github.com/dohooo)",
Expand Down
41 changes: 29 additions & 12 deletions packages/usb-adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import os from "os";

import { Adapter } from "@node-escpos/adapter";
import type { Interface, InEndpoint, OutEndpoint, LibUSBException } from "usb";
import { findBySerialNumber } from "usb";
import { usb, getDeviceList, findByIds } from "usb";

/**
Expand Down Expand Up @@ -61,26 +62,42 @@ export default class USBAdapter extends Adapter<[]> {
usb.on(event, listener);
}

static isPrinter(device: usb.Device): boolean {
try {
const length = device.configDescriptor?.interfaces.filter((iface) => {
return iface.filter((conf) => {
return conf.bInterfaceClass === IFACE_CLASS.PRINTER;
}).length;
}).length;
return (length !== undefined && length > 0);
}
catch (e) {
// console.warn(e)
return false;
}
}

static findPrinter() {
return getDeviceList().filter((device) => {
return getDeviceList().filter((device) => USBAdapter.isPrinter(device));
}

static getDevice(vid: number, pid: number) {
return new Promise((resolve, reject) => {
try {
return device.configDescriptor?.interfaces.filter((iface) => {
return iface.filter((conf) => {
return conf.bInterfaceClass === IFACE_CLASS.PRINTER;
}).length;
}).length;
const device = findByIds(vid, pid);
device?.open();
resolve(device);
}
catch (e) {
// console.warn(e)
return false;
catch (err) {
reject(err);
}
});
}

static getDevice(vid: number, pid: number) {
return new Promise((resolve, reject) => {
static getDeviceBySerial(serialNumber: string) {
return new Promise(async (resolve, reject) => {
try {
const device = findByIds(vid, pid);
const device = await findBySerialNumber(serialNumber);
device?.open();
resolve(device);
}
Expand Down

0 comments on commit 9d19e57

Please sign in to comment.