|
@@ -24,36 +24,42 @@ interface UsbDeviceInfo {
|
|
|
|
|
|
// USB设备管理类(单例)
|
|
|
export class USBDeviceManager {
|
|
|
- // 单例实例
|
|
|
private static instance: USBDeviceManager | null = null;
|
|
|
- // 存储已连接的USB设备列表
|
|
|
private connectedDevices: Array<usbManager.USBDevice> = [];
|
|
|
- // 定时器ID
|
|
|
private checkIntervalId: number | null = null;
|
|
|
+ private refreshLock: Promise<void> = Promise.resolve();
|
|
|
+ private deviceCheckLock: Promise<void> = Promise.resolve();
|
|
|
|
|
|
- // 私有构造函数
|
|
|
private constructor() {
|
|
|
- this.refreshDeviceList();
|
|
|
+ this.init();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 异步初始化
|
|
|
+ private async init(): Promise<void> {
|
|
|
+ await this.refreshDeviceList();
|
|
|
this.startAutoCheck();
|
|
|
}
|
|
|
|
|
|
- // 获取单例实例
|
|
|
- public static getInstance(): USBDeviceManager {
|
|
|
+ // 获取单例实例(异步)
|
|
|
+ public static async getInstance(): Promise<USBDeviceManager> {
|
|
|
if (!USBDeviceManager.instance) {
|
|
|
USBDeviceManager.instance = new USBDeviceManager();
|
|
|
+ await USBDeviceManager.instance.init();
|
|
|
}
|
|
|
return USBDeviceManager.instance;
|
|
|
}
|
|
|
|
|
|
- // 开始自动检测(每2秒一次)
|
|
|
+ // 开始自动检测(异步)
|
|
|
private startAutoCheck(): void {
|
|
|
- // 先停止已有的定时器
|
|
|
this.stopAutoCheck();
|
|
|
|
|
|
- // 启动新的定时器
|
|
|
- this.checkIntervalId = setInterval(() => {
|
|
|
- this.refreshDeviceList();
|
|
|
- this.checkDevices();
|
|
|
+ this.checkIntervalId = setInterval(async () => {
|
|
|
+ try {
|
|
|
+ await this.refreshDeviceList();
|
|
|
+ await this.checkDevices();
|
|
|
+ } catch (error) {
|
|
|
+ console.error('[AsyncUSB] 自动检测失败:', (error as BusinessError).message);
|
|
|
+ }
|
|
|
}, 2000);
|
|
|
}
|
|
|
|
|
@@ -65,65 +71,86 @@ export class USBDeviceManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 检查设备连接状态
|
|
|
- private checkDevices(): void {
|
|
|
- if (this.hasConnectedDevices()) {
|
|
|
- const isCameraConnected = this.isDeviceConnectedByName('Sony_Camera');
|
|
|
- const isScannerConnected = this.isDeviceConnectedByName('Scanning_Gun');
|
|
|
- AppStorage.setOrCreate<boolean>('CameraStatus', isCameraConnected);
|
|
|
- AppStorage.setOrCreate<boolean>('ScanningGunStatus', isScannerConnected);
|
|
|
- } else {
|
|
|
- console.log('USB状态检测', '没有USB设备连接');
|
|
|
- }
|
|
|
+ // 异步检查设备连接状态
|
|
|
+ private async checkDevices(): Promise<void> {
|
|
|
+ return this.deviceCheckLock = this.deviceCheckLock.then(async () => {
|
|
|
+ const hasDevices = await this.hasConnectedDevices();
|
|
|
+ if (hasDevices) {
|
|
|
+ // 替换解构赋值为单独变量声明
|
|
|
+ const connectedResults = await Promise.all([
|
|
|
+ this.isDeviceConnectedByName('Sony_Camera'),
|
|
|
+ this.isDeviceConnectedByName('Scanning_Gun')
|
|
|
+ ]);
|
|
|
+ const isCameraConnected = connectedResults[0];
|
|
|
+ const isScannerConnected = connectedResults[1];
|
|
|
+ AppStorage.setOrCreate<boolean>('CameraStatus', isCameraConnected);
|
|
|
+ AppStorage.setOrCreate<boolean>('ScanningGunStatus', isScannerConnected);
|
|
|
+ } else {
|
|
|
+ console.log('USB状态检测', '没有USB设备连接');
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- // 刷新设备列表,获取当前系统已连接的所有USB设备
|
|
|
- public refreshDeviceList(): void {
|
|
|
- try {
|
|
|
- this.connectedDevices = usbManager.getDevices();
|
|
|
- console.log('[USBDeviceManager] 当前连接的设备:', this.connectedDevices);
|
|
|
- } catch (error) {
|
|
|
- console.error('[USBDeviceManager] 获取USB设备失败:', (error as BusinessError).message);
|
|
|
- this.connectedDevices = [];
|
|
|
- }
|
|
|
+ // 异步刷新设备列表
|
|
|
+ public async refreshDeviceList(): Promise<void> {
|
|
|
+ return this.refreshLock = this.refreshLock.then(async () => {
|
|
|
+ try {
|
|
|
+ this.connectedDevices = await new Promise((resolve, reject) => {
|
|
|
+ try {
|
|
|
+ const devices = usbManager.getDevices();
|
|
|
+ resolve(devices);
|
|
|
+ } catch (error) {
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ console.log('[AsyncUSB] 当前连接的设备:', this.connectedDevices);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('[AsyncUSB] 获取USB设备失败:', (error as BusinessError).message);
|
|
|
+ this.connectedDevices = [];
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- // 检查是否有USB设备连接
|
|
|
- public hasConnectedDevices(): boolean {
|
|
|
+ // 异步检查是否有USB设备连接
|
|
|
+ public async hasConnectedDevices(): Promise<boolean> {
|
|
|
+ await this.refreshLock;
|
|
|
return this.connectedDevices.length > 0;
|
|
|
}
|
|
|
|
|
|
- // 根据供应商ID和产品ID检查特定USB设备是否连接
|
|
|
- public isDeviceConnectedById(vendorId: number, productId: number): boolean {
|
|
|
+ // 异步检查特定USB设备是否连接
|
|
|
+ public async isDeviceConnectedById(vendorId: number, productId: number): Promise<boolean> {
|
|
|
+ await this.refreshLock;
|
|
|
return this.connectedDevices.some(device =>
|
|
|
device.vendorId === vendorId && device.productId === productId
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- // 根据设备信息检查USB设备是否连接
|
|
|
- public isDeviceConnectedByInfo(usbDevice: UsbDeviceInfo): boolean {
|
|
|
+ // 异步检查USB设备是否连接
|
|
|
+ public async isDeviceConnectedByInfo(usbDevice: UsbDeviceInfo): Promise<boolean> {
|
|
|
return this.isDeviceConnectedById(usbDevice.VendorId, usbDevice.ProductId);
|
|
|
}
|
|
|
|
|
|
- // 根据预定义的设备名称检查USB设备是否连接
|
|
|
- public isDeviceConnectedByName(deviceName: string): boolean {
|
|
|
+ // 异步检查预定义的设备是否连接
|
|
|
+ public async isDeviceConnectedByName(deviceName: string): Promise<boolean> {
|
|
|
+ await this.refreshLock;
|
|
|
const deviceInfo = USBDeviceIds.find(item => item.DeviceName === deviceName);
|
|
|
|
|
|
if (!deviceInfo) {
|
|
|
- console.warn(`[USBDeviceManager] 设备 ${deviceName} 不在预定义列表中`);
|
|
|
+ console.warn(`[AsyncUSB] 设备 ${deviceName} 不在预定义列表中`);
|
|
|
return false;
|
|
|
}
|
|
|
return this.isDeviceConnectedById(deviceInfo.VendorId, deviceInfo.ProductId);
|
|
|
}
|
|
|
|
|
|
- // 获取所有已连接的USB设备
|
|
|
- public getAllDevices(): Array<usbManager.USBDevice> {
|
|
|
- return [...this.connectedDevices]; // 返回副本以避免外部修改
|
|
|
+ // 异步获取所有已连接的USB设备
|
|
|
+ public async getAllDevices(): Promise<Array<usbManager.USBDevice>> {
|
|
|
+ await this.refreshLock;
|
|
|
+ return [...this.connectedDevices];
|
|
|
}
|
|
|
|
|
|
// 销毁实例
|
|
|
public destroy(): void {
|
|
|
this.stopAutoCheck();
|
|
|
- USBDeviceManager.instance = null; // 现在可以正常赋值为null
|
|
|
+ USBDeviceManager.instance = null;
|
|
|
}
|
|
|
}
|