• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022-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 rpc from '@ohos.rpc';
17import process from '@ohos.process';
18
19let logTag = "RpcClient_TestService:  ";
20let CODE_INVOKE = 1;
21import ApiMessage from '../common/apiMessage.js';
22import ApiResult from '../common/apiResult.js';
23import deviceManager from '@ohos.distributedDeviceManager';
24import featureAbility from '@ohos.ability.featureAbility';
25
26let results;
27let isConnected = false;
28let bundleName = "com.acts.distributekvdisjs";
29let abilityName = "com.acts.distributekvdisjs.ServiceAbility";
30let bundleNameObject = "com.acts.distributeobjectdisjs";
31let abilityNameObject = "com.acts.distributeobjectdisjs.ServiceAbility";
32
33let deviceList;
34
35export default class TestService {
36    callback;
37
38    onCreate() {
39        console.info(logTag + 'AceApplication onCreate');
40    }
41
42    onDestroy() {
43        console.info(logTag + 'AceApplication onDestroy');
44    }
45
46    constructor() {
47
48    }
49
50    getDeviceList(deviceManager) {
51        deviceList = deviceManager.getAvailableDeviceListSync();
52        console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList));
53    }
54
55    toConnectAbility() {
56        console.info(logTag + " toConnectAbility");
57        return new Promise(resolve=>{
58            let self = this;
59            let dmInstance = deviceManager.createDeviceManager('com.acts.distributekvdisjs');
60            self.getDeviceList(dmInstance);
61            console.info("got deviceManager: " + dmInstance)
62            let deviceId = deviceList[0].networkId;
63            console.info(logTag + "deviceid : " + deviceId);
64            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
65            let want = {
66                "bundleName": bundleName,
67                "abilityName": abilityName,
68                "deviceId": deviceId,
69                "flags": 256
70            }
71            let connect = {
72                onConnect: function (elementName, remoteProxy) {
73                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
74                    resolve(remoteProxy);
75                },
76                onDisconnect: function (elementName) {
77                    console.log(logTag + "onDisconnect");
78                },
79                onFailed: function () {
80                    console.log(logTag + "onFailed");
81                }
82            }
83            let connectId = featureAbility.connectAbility(want, connect);
84            console.info(logTag + "connect ability got id: " + connectId);
85
86        })
87    }
88
89    toConnectRdbAbility() {
90        console.info(logTag + " toConnectRdbAbility");
91        return new Promise(resolve=>{
92            let self = this;
93            let dmInstance = deviceManager.createDeviceManager('com.acts.distributerdbdisjs');
94            self.getDeviceList(dmInstance);
95            console.info(logTag + "got deviceManager: " + dmInstance);
96            let deviceId = deviceList[0].networkId;
97            console.info(logTag + "deviceid : " + deviceId);
98            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
99            let want = {
100                "bundleName": "com.acts.distributerdbdisjs",
101                "abilityName": "com.acts.distributerdbdisjs.ServiceAbility",
102                "deviceId": deviceId,
103                "flags": 256
104            }
105            let connect = {
106                onConnect: function (elementName, remoteProxy) {
107                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
108                    resolve(remoteProxy);
109                },
110                onDisconnect: function (elementName) {
111                    console.log(logTag + "onDisconnect");
112                },
113                onFailed: function () {
114                    console.log(logTag + "onFailed");
115                }
116            }
117            let connectId = featureAbility.connectAbility(want, connect);
118            console.info(logTag + "connect ability got id: " + connectId);
119        })
120    }
121
122    toConnectObjectAbility() {
123        console.info(logTag + " toConnectObjectAbility");
124        return new Promise(resolve=>{
125            let self = this;
126            let dmInstance = deviceManager.createDeviceManager('com.acts.distributeobjectdisjs');
127            self.getDeviceList(dmInstance);
128            console.info(logTag + "got deviceManager: " + dmInstance);
129            let deviceId = deviceList[0].networkId;
130            console.info(logTag + "deviceid : " + deviceId);
131            console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList));
132            let want = {
133                "bundleName": bundleNameObject,
134                "abilityName": abilityNameObject,
135                "deviceId": deviceId,
136                "flags": 256
137            }
138            let connect = {
139                onConnect: function (elementName, remoteProxy) {
140                    console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy);
141                    resolve(remoteProxy);
142                },
143                onDisconnect: function (elementName) {
144                    console.log(logTag + "onDisconnect");
145                },
146                onFailed: function () {
147                    console.log(logTag + "onFailed");
148                }
149            }
150            let connectId = featureAbility.connectAbility(want, connect);
151            console.info(logTag + "connect ability got id: " + connectId);
152        })
153    }
154}
155