• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.distributedHardware.deviceManager'
16import Logger from '../model/Logger'
17import { BUNDLE } from '../model/RdbConst'
18
19let SUBSCRIBE_ID = 100
20const TAG: string = 'RemoteDeviceModel'
21
22class RemoteDeviceModel {
23    public deviceList: Array<deviceManager.DeviceInfo> = []
24    public discoverList: Array<deviceManager.DeviceInfo> = []
25    private callback: () => void = null
26    private authCallback: (device: deviceManager.DeviceInfo) => void = null
27    private deviceManager: deviceManager.DeviceManager = undefined
28
29    registerDeviceListCallback(callback) {
30        if (typeof (this.deviceManager) != 'undefined') {
31            this.registerDeviceListCallbackImplement(callback)
32            return
33        }
34        Logger.info(TAG, 'deviceManager.createDeviceManager begin')
35        deviceManager.createDeviceManager(BUNDLE, (error, value) => {
36            if (error) {
37                Logger.error(TAG, 'createDeviceManager failed.')
38                return;
39            }
40            this.deviceManager = value
41            this.registerDeviceListCallbackImplement(callback)
42            Logger.info(TAG, `createDeviceManager callback returned,value=${value}`)
43        })
44        Logger.info(TAG, 'deviceManager.createDeviceManager end')
45    }
46
47    deviceStateChangeActionOnline(device) {
48        this.deviceList[this.deviceList.length] = device
49        Logger.info(TAG, `online, device list=${JSON.stringify(this.deviceList)}`)
50        if (this.authCallback != null) {
51            this.authCallback(device)
52            this.authCallback = null
53        }
54    }
55
56    deviceStateChangeActionReady(device) {
57        if (this.deviceList.length <= 0) {
58            this.callback()
59            return
60        }
61        let list = new Array()
62        for (let i = 0; i < this.deviceList.length; i++) {
63            if (this.deviceList[i].deviceId != device.deviceId) {
64                list[i] = device
65            }
66        }
67        this.deviceList = list
68        Logger.info(TAG, `ready, device list=${JSON.stringify(this.deviceList)}`)
69        this.callback()
70    }
71
72    deviceStateChangeActionOffline(device) {
73        if (this.deviceList.length <= 0) {
74            this.callback()
75            return
76        }
77        for (let j = 0; j < this.deviceList.length; j++) {
78            if (this.deviceList[j ].deviceId == device.deviceId) {
79                this.deviceList[j] = device
80                break
81            }
82        }
83        Logger.info(TAG, `offline, device list=${JSON.stringify(this.deviceList)}`)
84    }
85
86    getLocalDevice() {
87        Logger.info(TAG, `getLocalDevice`)
88        let deviceInfo: deviceManager.DeviceInfo = this.deviceManager.getLocalDeviceInfoSync()
89        Logger.info(TAG, `local deviceInfo=${JSON.stringify(deviceInfo)}`)
90        return deviceInfo.deviceId
91    }
92
93    registerDeviceListCallbackImplement(callback) {
94        Logger.info(TAG, 'registerDeviceListCallback')
95        this.callback = callback
96        if (this.deviceManager === undefined) {
97            Logger.error(TAG, 'deviceManager has not initialized')
98            this.callback()
99            return
100        }
101        Logger.info(TAG, 'getTrustedDeviceListSync begin')
102        let list = this.deviceManager.getTrustedDeviceListSync()
103        Logger.info(TAG, `getTrustedDeviceListSync end, deviceList=${JSON.stringify(list)}`)
104        if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
105            this.deviceList = list
106        }
107        this.callback()
108        Logger.info(TAG, 'callback finished')
109        this.deviceManager.on('deviceStateChange', (data) => {
110            if (data == null) {
111                return
112            }
113            console.info('[RemoteDeviceModel] deviceStateChange data=' + JSON.stringify(data))
114            switch (data.action) {
115                case deviceManager.DeviceStateChangeAction.ONLINE:
116                    this.deviceStateChangeActionOnline(data.device)
117                    break;
118                case deviceManager.DeviceStateChangeAction.READY:
119                    this.deviceStateChangeActionReady(data.device)
120                    break;
121                case deviceManager.DeviceStateChangeAction.OFFLINE:
122                case deviceManager.DeviceStateChangeAction.CHANGE:
123                    this.deviceStateChangeActionOffline(data.device)
124                    break
125                default:
126                    break
127            }
128        });
129        this.deviceManager.on('deviceFound', (data) => {
130            if (data == null) {
131                return
132            }
133            Logger.info(TAG, `deviceFound data=${JSON.stringify(data)}`)
134            this.deviceFound(data)
135        });
136        this.deviceManager.on('discoverFail', (data) => {
137            Logger.info(TAG, `discoverFail data=${JSON.stringify(data)}`)
138        });
139        this.deviceManager.on('serviceDie', () => {
140            Logger.info(TAG, 'serviceDie')
141        });
142        this.startDeviceDiscovery()
143    }
144
145    deviceFound(data) {
146        for (let i = 0;i < this.discoverList.length; i++) {
147            if (this.discoverList[i].deviceId == data.device.deviceId) {
148                Logger.info(TAG, 'device founded ignored')
149                return
150            }
151        }
152        this.discoverList[this.discoverList.length] = data.device
153        Logger.info(TAG, `deviceFound self.discoverList=${this.discoverList}`)
154        this.callback();
155    }
156
157    startDeviceDiscovery() {
158        SUBSCRIBE_ID = Math.floor(65536 * Math.random())
159        var info = {
160            subscribeId: SUBSCRIBE_ID,
161            mode: 0xAA,
162            medium: 2,
163            freq: 2,
164            isSameAccount: false,
165            isWakeRemote: true,
166            capability: 0
167        }
168        Logger.info(TAG, `startDeviceDiscovery${SUBSCRIBE_ID}`)
169        this.deviceManager.startDeviceDiscovery(info)
170    }
171
172    unregisterDeviceListCallback() {
173        Logger.info(TAG, `stopDeviceDiscovery${SUBSCRIBE_ID}`)
174        this.deviceManager.stopDeviceDiscovery(SUBSCRIBE_ID);
175        this.deviceManager.off('deviceStateChange')
176        this.deviceManager.off('deviceFound')
177        this.deviceManager.off('discoverFail')
178        this.deviceManager.off('serviceDie')
179        this.deviceList = []
180        this.discoverList = []
181    }
182
183    authenticateDevice(device, callBack) {
184        Logger.info(TAG, `authenticateDevice${JSON.stringify(device)}`)
185        for (let i = 0; i < this.discoverList.length; i++) {
186            if (this.discoverList[i].deviceId != device.deviceId) {
187                continue
188            }
189            let extraInfo = {
190                'targetPkgName': BUNDLE,
191                'appName': 'Distributed rdb',
192                'appDescription': 'Distributed rdb',
193                'business': '0'
194            }
195            let authParam = {
196                'authType': 1,
197                'appIcon': '',
198                'appThumbnail': '',
199                'extraInfo': extraInfo
200            }
201            this.deviceManager.authenticateDevice(device, authParam, (err, data) => {
202                if (err) {
203                    console.info('[RemoteDeviceModel] authenticateDevice error:' + JSON.stringify(err))
204                }
205                console.info('[RemoteDeviceModel] authenticateDevice succeed:' + JSON.stringify(data))
206                this.authCallback = callBack
207            })
208        }
209    }
210}
211
212export default new RemoteDeviceModel()