33 lines
738 B
TypeScript
33 lines
738 B
TypeScript
import USBJson from '$lib/assets/usb-device.json';
|
|
|
|
type UsbIds = {
|
|
[vendorId: string]: {
|
|
name: string;
|
|
devices: {
|
|
[productId: string]: {
|
|
devname: string;
|
|
};
|
|
};
|
|
};
|
|
};
|
|
|
|
void (USBJson satisfies UsbIds);
|
|
|
|
const USB_IDS: UsbIds = USBJson;
|
|
|
|
export function getDeviceName(info: SerialPortInfo): string | undefined {
|
|
if (!info) {
|
|
return undefined;
|
|
}
|
|
const { usbProductId, usbVendorId } = info;
|
|
if (!usbVendorId || !usbProductId) {
|
|
return undefined;
|
|
}
|
|
const vendor = USB_IDS[usbVendorId.toString(16).padStart(4, '0')];
|
|
if (!vendor) {
|
|
return undefined;
|
|
}
|
|
const product = vendor.devices[usbProductId.toString(16).padStart(4, '0')];
|
|
return product ? product.devname : undefined;
|
|
}
|