feat: 添加DeviceName显示

- 根据usb-device.json映射vendorId与productId
- 为页面添加toast
This commit is contained in:
2025-12-24 16:10:11 +08:00
parent d705058e1d
commit 30e6b98cff
5 changed files with 44871 additions and 4 deletions

32
src/lib/utils/device.ts Normal file
View 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;
}