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.distributedHardware.deviceManager'; 24import deviceManager2 from '@ohos.distributedDeviceManager'; 25import featureAbility from '@ohos.ability.featureAbility'; 26 27let results; 28let isConnected = false; 29let bundleName = "com.ohos.distributekvdisjs"; 30let abilityName = "com.ohos.distributekvdisjs.ServiceAbility"; 31let bundleNameObject = "com.ohos.distributeobjectdisjs"; 32let abilityNameObject = "com.ohos.distributeobjectdisjs.ServiceAbility"; 33 34let deviceList; 35 36export default class TestService { 37 callback; 38 39 onCreate() { 40 console.info(logTag + 'AceApplication onCreate'); 41 } 42 43 onDestroy() { 44 console.info(logTag + 'AceApplication onDestroy'); 45 } 46 47 constructor() { 48 49 } 50 51 async invoke(gIRemoteObject,message) { 52 var messageParcel = rpc.MessageParcel.create(); 53 console.log(logTag + "create object successfully."); 54 var messageParcelreply = rpc.MessageParcel.create(); 55 var option = new rpc.MessageOption(); 56 var writeResult = messageParcel.writeSequenceable(message); 57 gIRemoteObject.sendRequest(CODE_INVOKE, messageParcel, messageParcelreply, option).then(function (result) { 58 if (result.errCode === 0) { 59 console.log(logTag + "sendRequest got result"); 60 var ret = new ApiMessage(null, null, null, null, null, null,null); 61 var dataReply = result.reply.readSequenceable(ret); 62 console.log(logTag + "run readSequenceable success, result is" + dataReply); 63 results = JSON.parse(ret._apiResult); 64 console.log(logTag + " read success, results is" + results._result); 65 return results._result; 66 } else { 67 console.log(logTag + "sendRequest failed, errCode: " + errCode); 68 } 69 }).catch(function (e) { 70 console.log(logTag + " sendRequest got exception: " + e.message); 71 }); 72 } 73 74 getDeviceList(deviceManager) { 75 deviceList = deviceManager.getTrustedDeviceListSync(); 76 console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList)); 77 } 78 getDeviceList2(deviceManager2) { 79 deviceList = deviceManager2.getAvailableDeviceListSync(); 80 console.info(logTag + "getDeviceList success, deviceList id: " + JSON.stringify(deviceList)); 81 } 82 83 toConnectAbility() { 84 console.info(logTag + " toConnectAbility"); 85 return new Promise(resolve=>{ 86 let self = this; 87 let dmInstance = deviceManager2.createDeviceManager('com.ohos.distributekvdisjs'); 88 self.getDeviceList2(dmInstance); 89 console.info("got deviceManager2: " + dmInstance) 90 let deviceId = deviceList[0].networkId; 91 console.info(logTag + "deviceid : " + deviceId); 92 console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 93 let want = { 94 "bundleName": bundleName, 95 "abilityName": abilityName, 96 "deviceId": deviceId, 97 "flags": 256 98 } 99 let connect = { 100 onConnect: function (elementName, remoteProxy) { 101 console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 102 resolve(remoteProxy); 103 }, 104 onDisconnect: function (elementName) { 105 console.log(logTag + "onDisconnect"); 106 }, 107 onFailed: function () { 108 console.log(logTag + "onFailed"); 109 } 110 } 111 let connectId = featureAbility.connectAbility(want, connect); 112 console.info(logTag + "connect ability got id: " + connectId); 113 114 }) 115 } 116 117 toConnectRdbAbility() { 118 console.info(logTag + " toConnectRdbAbility"); 119 return new Promise(resolve=>{ 120 let self = this; 121 deviceManager.createDeviceManager('distributerdbdisjs', (error, deviceManager) => { 122 self.getDeviceList(deviceManager); 123 console.info(logTag + "got deviceManager: " + deviceManager); 124 let deviceId = deviceList[0].networkId; 125 console.info(logTag + "deviceid : " + deviceId); 126 console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 127 let want = { 128 "bundleName": "com.ohos.distributerdbdisjs", 129 "abilityName": "com.ohos.distributerdbdisjs.ServiceAbility", 130 "deviceId": deviceId, 131 "flags": 256 132 } 133 let connect = { 134 onConnect: function (elementName, remoteProxy) { 135 console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 136 resolve(remoteProxy); 137 }, 138 onDisconnect: function (elementName) { 139 console.log(logTag + "onDisconnect"); 140 }, 141 onFailed: function () { 142 console.log(logTag + "onFailed"); 143 } 144 } 145 let connectId = featureAbility.connectAbility(want, connect); 146 console.info(logTag + "connect ability got id: " + connectId); 147 }) 148 }) 149 } 150 151 toConnectObjectAbility() { 152 console.info(logTag + " toConnectObjectAbility"); 153 return new Promise(resolve=>{ 154 let self = this; 155 deviceManager.createDeviceManager('distributeobjectdisjs', (error, deviceManager) => { 156 self.getDeviceList(deviceManager); 157 console.info(logTag + "got deviceManager: " + deviceManager); 158 let deviceId = deviceList[0].networkId; 159 console.info(logTag + "deviceid : " + deviceId); 160 console.info(logTag + "online deviceList id: " + JSON.stringify(deviceList)); 161 let want = { 162 "bundleName": bundleNameObject, 163 "abilityName": abilityNameObject, 164 "deviceId": deviceId, 165 "flags": 256 166 } 167 let connect = { 168 onConnect: function (elementName, remoteProxy) { 169 console.log(logTag + 'onConnect called, remoteProxy: ' + remoteProxy); 170 resolve(remoteProxy); 171 }, 172 onDisconnect: function (elementName) { 173 console.log(logTag + "onDisconnect"); 174 }, 175 onFailed: function () { 176 console.log(logTag + "onFailed"); 177 } 178 } 179 let connectId = featureAbility.connectAbility(want, connect); 180 console.info(logTag + "connect ability got id: " + connectId); 181 }) 182 }) 183 } 184} 185