1/*
2 * Copyright (c) 2023 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 */
15
16import distributedDeviceManager from '@ohos.distributedDeviceManager';
17import { Callback } from '@ohos.base';
18import { GlobalThis } from '../Model/GlobalThis';
19
20const SUBSCRIBE_ID = 100;
21const BundleName = 'com.example.serviceextensiondemo';
22const PrintLog = '[ServiceExtensionDemo]: ';
23
24export default class RemoteDeviceModel {
25    deviceList: distributedDeviceManager.DeviceBasicInfo[] = [];
26    discoverList: distributedDeviceManager.DeviceBasicInfo[] = [];
27    callback: Callback<void> = () => {};
28    authCallback = null;
29    MyDeviceManager: distributedDeviceManager.DeviceManager = distributedDeviceManager.createDeviceManager(BundleName);
30
31    constructor() {
32    }
33
34    registerDeviceListCallback(callback: Callback<void>): void {
35        console.info(PrintLog + 'registerDeviceListCallback in');
36        if (typeof (this.MyDeviceManager) === 'undefined') {
37            console.log(PrintLog + ' deviceManager.createDeviceManager begin');
38            try {
39                this.MyDeviceManager = distributedDeviceManager.createDeviceManager(BundleName);
40                this.registerDeviceListCallback_(callback);
41            } catch (err) {
42                console.error(PrintLog + 'createDeviceManager error: ' + JSON.stringify(err));
43            }
44            console.log(PrintLog + ' deviceManager.createDeviceManager end');
45        } else {
46            this.registerDeviceListCallback_(callback);
47        }
48    }
49
50    registerDeviceListCallback_(callback: Callback<void>): void {
51        console.info(PrintLog + ' registerDeviceListCallback_');
52        this.callback = callback;
53        if (this.MyDeviceManager == undefined) {
54            console.error(PrintLog + ' deviceManager has not initialized');
55            this.callback();
56        } else {
57            console.info(PrintLog + ' getTrustedDeviceListSync begin');
58            const list = this.MyDeviceManager.getAvailableDeviceListSync();
59            console.info(PrintLog + ' getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
60            if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
61                this.deviceList = list;
62            }
63            this.callback();
64        }
65        console.info(PrintLog + ' callback finished');
66    }
67
68    getTrustDeviceList(callback: Callback<void>): void {
69        console.info(PrintLog + ' getTrustDeviceListSync begin');
70        const list = this.MyDeviceManager.getAvailableDeviceListSync();
71        console.info(PrintLog + ' getTrustedDeviceListSync end, deviceList=' + JSON.stringify(list));
72        if (typeof (list) != 'undefined' && typeof (list.length) != 'undefined') {
73            this.deviceList = list;
74        }
75        console.info(PrintLog + ' getTrustDeviceListSync end');
76        callback();
77    }
78}