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