1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import deviceManager from '@ohos.distributedDeviceManager'; 16import { BUNDLE } from '../model/Const' 17import Logger from '../model/Logger' 18import { BusinessError } from '@ohos.base'; 19 20let SUBSCRIBE_ID: number = 100 21const TAG: string = 'RemoteDeviceModel' 22 23class RemoteDeviceModel { 24 public devices: Array<deviceManager.DeviceBasicInfo> = []; 25 public discoverDevices: Array<deviceManager.DeviceBasicInfo> = []; 26 private stateChangeCallback?: () => void 27 private authCallback?: (device: deviceManager.DeviceBasicInfo) => void; 28 private deviceManager?: deviceManager.DeviceManager 29 30 registerDeviceListCallback(stateChangeCallback: () => void) { 31 if (typeof (this.deviceManager) !== 'undefined') { 32 this.registerDeviceListCallbackImplement(stateChangeCallback) 33 return 34 } 35 Logger.info(TAG, 'deviceManager.createDeviceManager begin') 36 try { 37 let dmInstance = deviceManager.createDeviceManager(BUNDLE); 38 Logger.info(TAG, `dmInstance= ${JSON.stringify(dmInstance)}`); 39 this.deviceManager = dmInstance; 40 this.registerDeviceListCallbackImplement(stateChangeCallback); 41 Logger.info(TAG, `createDeviceManager callback returned, value= ${JSON.stringify(this.deviceManager)}`); 42 } catch (error) { 43 Logger.error(TAG, `createDeviceManager throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 44 } 45 Logger.info(TAG, 'deviceManager.createDeviceManager end'); 46 } 47 48 onDeviceStateChangeActionOnline(device) { 49 this.devices[this.devices.length] = device 50 Logger.info(TAG, `online, device list=${JSON.stringify(this.devices)}`) 51 if (this.authCallback !== null) { 52 this.authCallback(device) 53 this.authCallback = null 54 } 55 } 56 57 onDeviceStateChangeActionReady(device) { 58 if (this.devices.length <= 0) { 59 this.stateChangeCallback() 60 return 61 } 62 63 let list = this.devices.filter((value) => { 64 return value !== device.deviceId 65 }) 66 67 this.devices = list 68 Logger.info(TAG, `ready, device list=${JSON.stringify(this.devices)}`) 69 this.stateChangeCallback() 70 } 71 72 getLocalDevice() { 73 try { 74 Logger.info(TAG, 'getLocalDevice'); 75 let deviceId: string = this.deviceManager.getLocalDeviceId(); 76 Logger.info(TAG, `local deviceInfo=${JSON.stringify(deviceId)}`); 77 return deviceId; 78 } catch (error) { 79 Logger.error(TAG, `getLocalDeviceInfoSync throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 80 } 81 } 82 83 registerDeviceListCallbackImplement(stateChangeCallback: () => void) { 84 Logger.info(TAG, 'registerDeviceListCallback') 85 this.stateChangeCallback = stateChangeCallback 86 if (this.deviceManager === undefined) { 87 Logger.error(TAG, 'deviceManager has not initialized') 88 this.stateChangeCallback() 89 return 90 } 91 Logger.info(TAG, 'getAvailableDeviceListSync begin'); 92 try { 93 let list = this.deviceManager.getAvailableDeviceListSync(); 94 Logger.info(TAG, `getAvailableDeviceListSync end, devices=${JSON.stringify(list)}`); 95 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 96 this.devices = list 97 } 98 } catch (error) { 99 Logger.error(TAG, `getAvailableDeviceListSync throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 100 101 } 102 this.stateChangeCallback() 103 Logger.info(TAG, 'callback finished') 104 try { 105 this.deviceManager.on('deviceStateChange', (data) => { 106 if (data === null) { 107 return 108 } 109 Logger.info(TAG, `deviceStateChange data = ${JSON.stringify(data)}`) 110 switch (data.action) { 111 case deviceManager.DeviceStateChange.AVAILABLE: 112 this.discoverDevices = [] 113 this.devices.push(data.device) 114 this.stateChangeCallback() 115 try { 116 let list = this.deviceManager.getAvailableDeviceListSync(); 117 if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') { 118 this.devices = list 119 } 120 } catch (error) { 121 Logger.error(TAG, `getTrustedDeviceListSync throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 122 } 123 this.stateChangeCallback() 124 break 125 default: 126 break 127 } 128 }) 129 this.deviceManager.on('discoverSuccess', (data) => { 130 if (data === null) { 131 return 132 } 133 Logger.info(TAG, `discoverSuccess data=${JSON.stringify(data)}`); 134 this.onDeviceFound(data) 135 }) 136 this.deviceManager.on('discoverFailure', (data) => { 137 Logger.info(TAG, `discoverFailure data=${JSON.stringify(data)}`); 138 }) 139 this.deviceManager.on('serviceDie', () => { 140 Logger.info(TAG, 'serviceDie') 141 }) 142 } catch (error) { 143 Logger.error(TAG, `on throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 144 } 145 this.startDeviceDiscovery() 146 } 147 148 onDeviceFound(data) { 149 for (let i = 0;i < this.discoverDevices.length; i++) { 150 if (this.discoverDevices[i].deviceId === data.device.deviceId) { 151 Logger.info(TAG, 'device founded ignored') 152 return 153 } 154 } 155 this.discoverDevices[this.discoverDevices.length] = data.device 156 Logger.info(TAG, `deviceFound self.discoverDevices=${this.discoverDevices}`) 157 this.stateChangeCallback() 158 } 159 160 startDeviceDiscovery() { 161 let discoverParam: Record<string, number> = { 162 'discoverTargetType': 1 163 }; 164 let filterOptions: Record<string, number> = { 165 'availableStatus': 0 166 }; 167 Logger.info(TAG, `startDiscovering${SUBSCRIBE_ID}`); 168 try { 169 if (this.deviceManager !== null) { 170 this.deviceManager.startDiscovering(discoverParam, filterOptions); 171 } 172 } catch (error) { 173 Logger.error(TAG, `startDiscovering throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 174 } 175 } 176 177 unregisterDeviceListCallback() { 178 try { 179 Logger.info(TAG, `stopDeviceDiscovery${SUBSCRIBE_ID}`); 180 this.deviceManager.stopDiscovering(); 181 this.deviceManager.off('deviceStateChange') 182 this.deviceManager.off('discoverSuccess'); 183 this.deviceManager.off('discoverFailure'); 184 this.deviceManager.off('serviceDie') 185 this.devices = [] 186 this.discoverDevices = [] 187 } catch (error) { 188 Logger.error(TAG, `off throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 189 } 190 191 } 192 193 authenticateDevice(device, callBack) { 194 Logger.info(TAG, `authenticateDevice ${JSON.stringify(device)}`) 195 for (let i = 0; i < this.discoverDevices.length; i++) { 196 if (this.discoverDevices[i].deviceId !== device.deviceId) { 197 continue 198 } 199 if (this.deviceManager === undefined) { 200 return; 201 } 202 try { 203 if (this.deviceManager !== null) { 204 this.deviceManager.bindTarget(device.deviceId, { 205 bindLevel: 3, 206 bindType: 1, 207 targetPkgName: BUNDLE, 208 appName: 'Distributed rdb', 209 }, (err, data) => { 210 if (err) { 211 Logger.error(TAG, `authenticateDevice throw error, code: ${(err as BusinessError).code} message: ${(err as BusinessError).message}`); 212 this.authCallback = () => {}; 213 return; 214 } 215 Logger.debug(TAG, `authenticateDevice succeed: ${JSON.stringify(data)}`); 216 this.authCallback = callBack; 217 }) 218 } 219 } catch (error) { 220 Logger.error(TAG, `authenticateDevice throw error, code: ${(error as BusinessError).code} message: ${(error as BusinessError).message}`); 221 } 222 } 223 } 224} 225 226export default new RemoteDeviceModel()