///
import { Interface } from "./interface";
export class WebUSBInterface implements Interface {
private device: USBDevice;
constructor(device: USBDevice) {
this.device = device;
}
getPrinterName(): string {
return [this.device.manufacturerName, this.device.productName]
.filter(Boolean)
.join(" ");
}
async isPrinterConnected(): Promise {
const device = this.device;
return new Promise((resolve, reject) => {
device
.open()
.then(() =>
device.selectConfiguration(
device.configurations[0].configurationValue
)
)
.then(() => {
return device.claimInterface(
device.configuration?.interfaces?.[0].interfaceNumber ?? 0
);
})
.then(() => {
resolve(true);
})
.catch((err) => {
console.error(err);
resolve(false);
});
});
}
async execute(buffer: Uint8Array): Promise {
const device = this.device;
return new Promise((resolve, reject) => {
return device
.transferOut(1, buffer)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
}