10704ebd2Sopenharmony_ci/* 20704ebd2Sopenharmony_ci * Copyright (C) 2022-2023 Huawei Device Co., Ltd. 30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License. 50704ebd2Sopenharmony_ci * You may obtain a copy of the License at 60704ebd2Sopenharmony_ci * 70704ebd2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 80704ebd2Sopenharmony_ci * 90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and 130704ebd2Sopenharmony_ci * limitations under the License. 140704ebd2Sopenharmony_ci */ 150704ebd2Sopenharmony_ci 160704ebd2Sopenharmony_ciimport rpc from '@ohos.rpc'; 170704ebd2Sopenharmony_ciimport process from '@ohos.process'; 180704ebd2Sopenharmony_ci 190704ebd2Sopenharmony_cilet logTag = "RpcClient_TestService: "; 200704ebd2Sopenharmony_cilet CODE_INVOKE = 1; 210704ebd2Sopenharmony_ciimport ApiMessage from '../common/apiMessage.js'; 220704ebd2Sopenharmony_ciimport ApiResult from '../common/apiResult.js'; 230704ebd2Sopenharmony_ciimport deviceManager from '@ohos.distributedDeviceManager'; 240704ebd2Sopenharmony_ciimport featureAbility from '@ohos.ability.featureAbility'; 250704ebd2Sopenharmony_ci 260704ebd2Sopenharmony_cilet results; 270704ebd2Sopenharmony_cilet isConnected = false; 280704ebd2Sopenharmony_cilet bundleName = "com.acts.distributekvdisjs"; 290704ebd2Sopenharmony_cilet abilityName = "com.acts.distributekvdisjs.ServiceAbility"; 300704ebd2Sopenharmony_cilet bundleNameObject = "com.acts.distributeobjectdisjs"; 310704ebd2Sopenharmony_cilet abilityNameObject = "com.acts.distributeobjectdisjs.ServiceAbility"; 320704ebd2Sopenharmony_ci 330704ebd2Sopenharmony_cilet deviceList; 340704ebd2Sopenharmony_ci 350704ebd2Sopenharmony_ciexport default class TestService { 360704ebd2Sopenharmony_ci callback; 370704ebd2Sopenharmony_ci 380704ebd2Sopenharmony_ci onCreate() { 390704ebd2Sopenharmony_ci console.info(logTag + 'AceApplication onCreate'); 400704ebd2Sopenharmony_ci } 410704ebd2Sopenharmony_ci 420704ebd2Sopenharmony_ci onDestroy() { 430704ebd2Sopenharmony_ci console.info(logTag + 'AceApplication onDestroy'); 440704ebd2Sopenharmony_ci } 450704ebd2Sopenharmony_ci 460704ebd2Sopenharmony_ci constructor() { 470704ebd2Sopenharmony_ci 480704ebd2Sopenharmony_ci } 490704ebd2Sopenharmony_ci 500704ebd2Sopenharmony_ci getDeviceList(deviceManager) { 510704ebd2Sopenharmony_ci deviceList = deviceManager.getAvailableDeviceListSync(); 520704ebd2Sopenharmony_ci console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList)); 530704ebd2Sopenharmony_ci } 540704ebd2Sopenharmony_ci 550704ebd2Sopenharmony_ci toConnectAbility() { 560704ebd2Sopenharmony_ci console.info(logTag + " toConnectAbility"); 570704ebd2Sopenharmony_ci return new Promise(resolve=>{ 580704ebd2Sopenharmony_ci let self = this; 590704ebd2Sopenharmony_ci let dmInstance = deviceManager.createDeviceManager('com.acts.distributekvdisjs'); 600704ebd2Sopenharmony_ci self.getDeviceList(dmInstance); 610704ebd2Sopenharmony_ci console.info("got deviceManager: " + dmInstance) 620704ebd2Sopenharmony_ci let deviceId = deviceList[0].networkId; 630704ebd2Sopenharmony_ci console.info(logTag + "deviceid : " + deviceId); 640704ebd2Sopenharmony_ci console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 650704ebd2Sopenharmony_ci let want = { 660704ebd2Sopenharmony_ci "bundleName": bundleName, 670704ebd2Sopenharmony_ci "abilityName": abilityName, 680704ebd2Sopenharmony_ci "deviceId": deviceId, 690704ebd2Sopenharmony_ci "flags": 256 700704ebd2Sopenharmony_ci } 710704ebd2Sopenharmony_ci let connect = { 720704ebd2Sopenharmony_ci onConnect: function (elementName, remoteProxy) { 730704ebd2Sopenharmony_ci console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 740704ebd2Sopenharmony_ci resolve(remoteProxy); 750704ebd2Sopenharmony_ci }, 760704ebd2Sopenharmony_ci onDisconnect: function (elementName) { 770704ebd2Sopenharmony_ci console.log(logTag + "onDisconnect"); 780704ebd2Sopenharmony_ci }, 790704ebd2Sopenharmony_ci onFailed: function () { 800704ebd2Sopenharmony_ci console.log(logTag + "onFailed"); 810704ebd2Sopenharmony_ci } 820704ebd2Sopenharmony_ci } 830704ebd2Sopenharmony_ci let connectId = featureAbility.connectAbility(want, connect); 840704ebd2Sopenharmony_ci console.info(logTag + "connect ability got id: " + connectId); 850704ebd2Sopenharmony_ci 860704ebd2Sopenharmony_ci }) 870704ebd2Sopenharmony_ci } 880704ebd2Sopenharmony_ci 890704ebd2Sopenharmony_ci toConnectRdbAbility() { 900704ebd2Sopenharmony_ci console.info(logTag + " toConnectRdbAbility"); 910704ebd2Sopenharmony_ci return new Promise(resolve=>{ 920704ebd2Sopenharmony_ci let self = this; 930704ebd2Sopenharmony_ci let dmInstance = deviceManager.createDeviceManager('com.acts.distributerdbdisjs'); 940704ebd2Sopenharmony_ci self.getDeviceList(dmInstance); 950704ebd2Sopenharmony_ci console.info(logTag + "got deviceManager: " + dmInstance); 960704ebd2Sopenharmony_ci let deviceId = deviceList[0].networkId; 970704ebd2Sopenharmony_ci console.info(logTag + "deviceid : " + deviceId); 980704ebd2Sopenharmony_ci console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 990704ebd2Sopenharmony_ci let want = { 1000704ebd2Sopenharmony_ci "bundleName": "com.acts.distributerdbdisjs", 1010704ebd2Sopenharmony_ci "abilityName": "com.acts.distributerdbdisjs.ServiceAbility", 1020704ebd2Sopenharmony_ci "deviceId": deviceId, 1030704ebd2Sopenharmony_ci "flags": 256 1040704ebd2Sopenharmony_ci } 1050704ebd2Sopenharmony_ci let connect = { 1060704ebd2Sopenharmony_ci onConnect: function (elementName, remoteProxy) { 1070704ebd2Sopenharmony_ci console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 1080704ebd2Sopenharmony_ci resolve(remoteProxy); 1090704ebd2Sopenharmony_ci }, 1100704ebd2Sopenharmony_ci onDisconnect: function (elementName) { 1110704ebd2Sopenharmony_ci console.log(logTag + "onDisconnect"); 1120704ebd2Sopenharmony_ci }, 1130704ebd2Sopenharmony_ci onFailed: function () { 1140704ebd2Sopenharmony_ci console.log(logTag + "onFailed"); 1150704ebd2Sopenharmony_ci } 1160704ebd2Sopenharmony_ci } 1170704ebd2Sopenharmony_ci let connectId = featureAbility.connectAbility(want, connect); 1180704ebd2Sopenharmony_ci console.info(logTag + "connect ability got id: " + connectId); 1190704ebd2Sopenharmony_ci }) 1200704ebd2Sopenharmony_ci } 1210704ebd2Sopenharmony_ci 1220704ebd2Sopenharmony_ci toConnectObjectAbility() { 1230704ebd2Sopenharmony_ci console.info(logTag + " toConnectObjectAbility"); 1240704ebd2Sopenharmony_ci return new Promise(resolve=>{ 1250704ebd2Sopenharmony_ci let self = this; 1260704ebd2Sopenharmony_ci let dmInstance = deviceManager.createDeviceManager('com.acts.distributeobjectdisjs'); 1270704ebd2Sopenharmony_ci self.getDeviceList(dmInstance); 1280704ebd2Sopenharmony_ci console.info(logTag + "got deviceManager: " + dmInstance); 1290704ebd2Sopenharmony_ci let deviceId = deviceList[0].networkId; 1300704ebd2Sopenharmony_ci console.info(logTag + "deviceid : " + deviceId); 1310704ebd2Sopenharmony_ci console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 1320704ebd2Sopenharmony_ci let want = { 1330704ebd2Sopenharmony_ci "bundleName": bundleNameObject, 1340704ebd2Sopenharmony_ci "abilityName": abilityNameObject, 1350704ebd2Sopenharmony_ci "deviceId": deviceId, 1360704ebd2Sopenharmony_ci "flags": 256 1370704ebd2Sopenharmony_ci } 1380704ebd2Sopenharmony_ci let connect = { 1390704ebd2Sopenharmony_ci onConnect: function (elementName, remoteProxy) { 1400704ebd2Sopenharmony_ci console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 1410704ebd2Sopenharmony_ci resolve(remoteProxy); 1420704ebd2Sopenharmony_ci }, 1430704ebd2Sopenharmony_ci onDisconnect: function (elementName) { 1440704ebd2Sopenharmony_ci console.log(logTag + "onDisconnect"); 1450704ebd2Sopenharmony_ci }, 1460704ebd2Sopenharmony_ci onFailed: function () { 1470704ebd2Sopenharmony_ci console.log(logTag + "onFailed"); 1480704ebd2Sopenharmony_ci } 1490704ebd2Sopenharmony_ci } 1500704ebd2Sopenharmony_ci let connectId = featureAbility.connectAbility(want, connect); 1510704ebd2Sopenharmony_ci console.info(logTag + "connect ability got id: " + connectId); 1520704ebd2Sopenharmony_ci }) 1530704ebd2Sopenharmony_ci } 1540704ebd2Sopenharmony_ci} 155