|
@@ -17,140 +17,113 @@ export const USBDeviceIds: Array<UsbDeviceInfo> = [
|
|
|
|
|
|
// USB设备信息接口
|
|
|
interface UsbDeviceInfo {
|
|
|
- DeviceName: string
|
|
|
- VendorId: number
|
|
|
- ProductId: number
|
|
|
+ DeviceName: string;
|
|
|
+ VendorId: number;
|
|
|
+ ProductId: number;
|
|
|
}
|
|
|
|
|
|
-// USB设备管理类(单例)
|
|
|
-export class USBDeviceManager {
|
|
|
- private static instance: USBDeviceManager | null = null;
|
|
|
+export
|
|
|
+class USBDeviceManager {
|
|
|
private connectedDevices: Array<usbManager.USBDevice> = [];
|
|
|
- private checkIntervalId: number | null = null;
|
|
|
- private refreshLock: Promise<void> = Promise.resolve();
|
|
|
- private deviceCheckLock: Promise<void> = Promise.resolve();
|
|
|
-
|
|
|
- private constructor() {
|
|
|
- this.init();
|
|
|
- }
|
|
|
-
|
|
|
- // 异步初始化
|
|
|
- private async init(): Promise<void> {
|
|
|
- await this.refreshDeviceList();
|
|
|
- this.startAutoCheck();
|
|
|
- }
|
|
|
-
|
|
|
- // 获取单例实例(异步)
|
|
|
- public static async getInstance(): Promise<USBDeviceManager> {
|
|
|
- if (!USBDeviceManager.instance) {
|
|
|
- USBDeviceManager.instance = new USBDeviceManager();
|
|
|
- await USBDeviceManager.instance.init();
|
|
|
- }
|
|
|
- return USBDeviceManager.instance;
|
|
|
- }
|
|
|
-
|
|
|
- // 开始自动检测(异步)
|
|
|
- private startAutoCheck(): void {
|
|
|
- this.stopAutoCheck();
|
|
|
-
|
|
|
- this.checkIntervalId = setInterval(async () => {
|
|
|
+ private pollTimer: number | null = null;
|
|
|
+ private isPollingPaused: boolean = false;
|
|
|
+
|
|
|
+ // 启动轮询(带暂停控制)
|
|
|
+ public startPolling(interval: number, callback: (devices: usbManager.USBDevice[]) => void): void {
|
|
|
+ this.stopPolling(); // 先停止已有轮询
|
|
|
+ this.pollTimer = setInterval(async () => {
|
|
|
+ if (this.isPollingPaused) return; // 暂停时跳过检测
|
|
|
try {
|
|
|
- await this.refreshDeviceList();
|
|
|
- await this.checkDevices();
|
|
|
+ this.connectedDevices = await this.refreshDeviceList();
|
|
|
+ callback(this.connectedDevices);
|
|
|
} catch (error) {
|
|
|
- console.error('[AsyncUSB] 自动检测失败:', (error as BusinessError).message);
|
|
|
+ console.error('轮询失败:', (error as BusinessError).message);
|
|
|
}
|
|
|
- }, 2000);
|
|
|
+ }, interval) as number; // 鸿蒙中setInterval返回类型需转换
|
|
|
}
|
|
|
|
|
|
- // 停止自动检测
|
|
|
- public stopAutoCheck(): void {
|
|
|
- if (this.checkIntervalId !== null) {
|
|
|
- clearInterval(this.checkIntervalId);
|
|
|
- this.checkIntervalId = null;
|
|
|
- }
|
|
|
+ // 暂停轮询
|
|
|
+ public pausePolling(): void {
|
|
|
+ this.isPollingPaused = true;
|
|
|
}
|
|
|
|
|
|
- // 异步检查设备连接状态
|
|
|
- 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设备连接');
|
|
|
- }
|
|
|
- });
|
|
|
+ // 恢复轮询
|
|
|
+ public resumePolling(): void {
|
|
|
+ this.isPollingPaused = false;
|
|
|
}
|
|
|
|
|
|
- // 异步刷新设备列表
|
|
|
- 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 = [];
|
|
|
- }
|
|
|
- });
|
|
|
+ // 停止轮询(释放资源)
|
|
|
+ public stopPolling(): void {
|
|
|
+ if (this.pollTimer !== null) {
|
|
|
+ clearInterval(this.pollTimer);
|
|
|
+ this.pollTimer = null;
|
|
|
+ }
|
|
|
+ this.isPollingPaused = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* 手动刷新设备列表(需主动调用)
|
|
|
+ * @returns 返回刷新后的设备列表
|
|
|
+ */
|
|
|
+ public async refreshDeviceList(): Promise<Array<usbManager.USBDevice>> {
|
|
|
+ try {
|
|
|
+ this.connectedDevices = usbManager.getDevices();
|
|
|
+ console.log('[USB] 当前连接的设备:', this.connectedDevices);
|
|
|
+ return this.connectedDevices;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('[USB] 获取USB设备失败:', (error as BusinessError).message);
|
|
|
+ this.connectedDevices = [];
|
|
|
+ return [];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // 异步检查是否有USB设备连接
|
|
|
- public async hasConnectedDevices(): Promise<boolean> {
|
|
|
- await this.refreshLock;
|
|
|
+ /* 检查是否有USB设备连接
|
|
|
+ * @param forceRefresh 是否强制刷新设备列表
|
|
|
+ */
|
|
|
+ public async hasConnectedDevices(forceRefresh: boolean = false): Promise<boolean> {
|
|
|
+ if (forceRefresh || this.connectedDevices.length === 0) {
|
|
|
+ await this.refreshDeviceList();
|
|
|
+ }
|
|
|
return this.connectedDevices.length > 0;
|
|
|
}
|
|
|
|
|
|
- // 异步检查特定USB设备是否连接
|
|
|
- public async isDeviceConnectedById(vendorId: number, productId: number): Promise<boolean> {
|
|
|
- await this.refreshLock;
|
|
|
+ /* 检查特定设备是否连接
|
|
|
+ * @param vendorId 厂商ID
|
|
|
+ * @param productId 产品ID
|
|
|
+ * @param forceRefresh 是否强制刷新
|
|
|
+ */
|
|
|
+ public async isDeviceConnectedById(
|
|
|
+ vendorId: number,
|
|
|
+ productId: number,
|
|
|
+ forceRefresh: boolean = false
|
|
|
+ ): Promise<boolean> {
|
|
|
+ if (forceRefresh) {
|
|
|
+ await this.refreshDeviceList();
|
|
|
+ }
|
|
|
return this.connectedDevices.some(device =>
|
|
|
device.vendorId === vendorId && device.productId === productId
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- // 异步检查USB设备是否连接
|
|
|
- public async isDeviceConnectedByInfo(usbDevice: UsbDeviceInfo): Promise<boolean> {
|
|
|
- return this.isDeviceConnectedById(usbDevice.VendorId, usbDevice.ProductId);
|
|
|
- }
|
|
|
-
|
|
|
- // 异步检查预定义的设备是否连接
|
|
|
- public async isDeviceConnectedByName(deviceName: string): Promise<boolean> {
|
|
|
- await this.refreshLock;
|
|
|
+ /* 检查预定义的设备是否连接
|
|
|
+ * @param deviceName 设备名称(需在USBDeviceIds中定义)
|
|
|
+ * @param forceRefresh 是否强制刷新
|
|
|
+ */
|
|
|
+ public async isDeviceConnectedByName(
|
|
|
+ deviceName: string,
|
|
|
+ forceRefresh: boolean = false
|
|
|
+ ): Promise<boolean> {
|
|
|
const deviceInfo = USBDeviceIds.find(item => item.DeviceName === deviceName);
|
|
|
-
|
|
|
if (!deviceInfo) {
|
|
|
- console.warn(`[AsyncUSB] 设备 ${deviceName} 不在预定义列表中`);
|
|
|
+ console.warn(`[USB] 设备 ${deviceName} 不在预定义列表中`);
|
|
|
return false;
|
|
|
}
|
|
|
- return this.isDeviceConnectedById(deviceInfo.VendorId, deviceInfo.ProductId);
|
|
|
+ return this.isDeviceConnectedById(deviceInfo.VendorId, deviceInfo.ProductId, forceRefresh);
|
|
|
}
|
|
|
|
|
|
- // 异步获取所有已连接的USB设备
|
|
|
- public async getAllDevices(): Promise<Array<usbManager.USBDevice>> {
|
|
|
- await this.refreshLock;
|
|
|
+ /*
|
|
|
+ * 获取当前缓存的设备列表(不主动刷新)
|
|
|
+ */
|
|
|
+ public getCachedDevices(): Array<usbManager.USBDevice> {
|
|
|
return [...this.connectedDevices];
|
|
|
}
|
|
|
-
|
|
|
- // 销毁实例
|
|
|
- public destroy(): void {
|
|
|
- this.stopAutoCheck();
|
|
|
- USBDeviceManager.instance = null;
|
|
|
- }
|
|
|
}
|