feat: 添加DeviceName显示
- 根据usb-device.json映射vendorId与productId - 为页面添加toast
This commit is contained in:
32
src/lib/utils/device.ts
Normal file
32
src/lib/utils/device.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user