1/* 2 * Copyright (C) 2024 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 ApiMessage from '../common/apiMessage.ets'; 17import ApiResult from '../common/apiResult.ets'; 18import rpc from '@ohos.rpc'; 19 20let logTag = "[RpcClient_RemoteHelper: ]"; 21let CODE_INVOKE =1; 22let CODE_INVOKE_TESTCASE = 99; 23 24export default class RemoteHelper{ 25 testservice = undefined; 26 gIRemoteObject = undefined; 27 constructor(testservice,gIRemoteObject){ 28 this.testservice = testservice; 29 this.gIRemoteObject = gIRemoteObject; 30 } 31 32 async getReq(message) { 33 try { 34 console.log(logTag + "getReq begin"); 35 console.log(logTag + "getReq. message= "+ JSON.stringify(message)); 36 let data= rpc.MessageSequence.create(); 37 let reply = rpc.MessageSequence.create(); 38 let option = new rpc.MessageOption(); 39 data.writeParcelable(message); 40 await this.gIRemoteObject.sendMessageRequest(CODE_INVOKE, data, reply, option); 41 console.log(logTag + "sendMessageRequest got result"); 42 let ret = new ApiMessage(null, null, null, null, null, null, null); 43 reply.readParcelable(ret); 44 let retApi = JSON.parse(ret._apiResult); 45 let retApiResult = retApi._result; 46 console.log(logTag + "*********** read success, results is " + retApiResult + "**************"); 47 return retApiResult; 48 } catch(err) { 49 console.error('***********catch getReq err:' + `, error code is ${err.code}, message is ${err.message}`); 50 return 411; 51 } 52 } 53 54 async getReqTestCase(message) { 55 try { 56 console.log(logTag + "getReqTestCase begin"); 57 console.log(logTag + "getReqTestCase. message= "+ JSON.stringify(message)); 58 let data= rpc.MessageSequence.create(); 59 let reply = rpc.MessageSequence.create(); 60 let option = new rpc.MessageOption(); 61 data.writeParcelable(message); 62 await this.gIRemoteObject.sendMessageRequest(CODE_INVOKE_TESTCASE, data, reply, option); 63 console.log(logTag + "sendMessageRequest got result"); 64 let ret = new ApiMessage(null, null, null, null, null, null, null); 65 reply.readParcelable(ret); 66 let retApi = JSON.parse(ret._apiResult); 67 let retApiResult = retApi._result; 68 console.log(logTag + "*********** read success, results is " + retApiResult + "**************"); 69 return retApiResult; 70 } catch(err) { 71 console.error('***********catch getReq err:' + `, error code is ${err.code}, message is ${err.message}`); 72 return 411; 73 } 74 } 75 76 async setTestCaseName(name){ 77 console.info(logTag + " **************** _methodName is setTestCaseName **************** "); 78 let message = new ApiMessage("openHarmony","testApi","setTestCaseName"," ", 79 ["string"],[String(name)]," "); 80 let resGetReq = await this.getReqTestCase(message); 81 return resGetReq; 82 } 83 84 async startBackgroundRunning(){ 85 console.info(logTag + "_methodName is startBackgroundRunning"); 86 let message = new ApiMessage("openHarmony","testApi","startBackgroundRunning"," ",[],[]," "); 87 let resGetReq = await this.getReq(message); 88 return resGetReq; 89 } 90 91 async stopBackgroundRunning(){ 92 console.info(logTag + "_methodName is stopBackgroundRunning"); 93 let message = new ApiMessage("openHarmony","testApi","stopBackgroundRunning"," ",[],[]," "); 94 let resGetReq = await this.getReq(message); 95 return resGetReq; 96 } 97 async add(a,b) { 98 console.log(logTag+"_methodName is add"); 99 let message = new ApiMessage("openHarmony","testApi","add"," ", 100 ["number","number"],[String(a),String(b)]," "); 101 let resGetReq = await this.getReq(message); 102 return resGetReq; 103 } 104 105 106 async sub(a,b) { 107 console.log(logTag+"_methodName is sub"); 108 let message = new ApiMessage("openHarmony","testApi","sub"," ", 109 ["number","number"],[String(a),String(b)]," "); 110 let resGetReq = await this.getReq(message); 111 return resGetReq; 112 } 113 async createKvManager(){ 114 console.info(logTag + "_methodName is createKvManager"); 115 let message = new ApiMessage("openHarmony","testApi","createKvManager"," ",[],[]," "); 116 let resGetReq = await this.getReq(message); 117 return resGetReq; 118 } 119 120 async getKvStore(storeId,SecurityLevel,encrypt){ 121 // if SecurityLevel in ["S0","S1","S2","S3","S4"],the kvstore corresponding to the security level will be created 122 // if SecurityLevel not in ["S0","S1","S2","S3","S4"], the kvstore with security level is NO_LEVEL will be created 123 // if encrypt is true,it will create encrypt kvStore,otherwise it weill create unencrypt kvStore 124 console.info(logTag + "_methodName is getKvStore"); 125 let message = new ApiMessage("openHarmony","testApi","getKvStore"," ", 126 ["string","string","string"],[String(storeId),String(SecurityLevel),String(encrypt)]," "); 127 let resGetReq = await this.getReq(message); 128 return resGetReq; 129 } 130 async closeKvStore(storeId){ 131 console.info(logTag + "_methodName is closeKvStore"); 132 let message = new ApiMessage("openHarmony","testApi","closeKvStore"," ", 133 ["string"],[String(storeId)]," "); 134 let resGetReq = await this.getReq(message); 135 return resGetReq; 136 } 137 138 async kvPut(key,value,valueType){ 139 // valueType<string>: The type of value. 140 // Available values:["String","Number","Uint8Array","Boolean"] 141 // To avoid unknown errors, it is recommended to pass in only values of string type 142 // If insert data successful,it will return true, otherwise it will return errInfo 143 console.info(logTag + "_methodName is kvPut"); 144 console.info(logTag + "_methodName is String(value)" + String(value)); 145 let message = new ApiMessage("openHarmony","testApi","kvPut"," ", 146 ["string",valueType,"string"],[String(key),String(value),String(valueType)]," "); 147 let resGetReq = await this.getReq(message); 148 return resGetReq; 149 } 150 151 async kvGet(key){ 152 console.info(logTag + "_methodName is kvGet"); 153 let message = new ApiMessage("openHarmony","testApi","kvGet"," ", 154 ["string"],[String(key)]," "); 155 let resGetReq = await this.getReq(message); 156 return resGetReq; 157 } 158 159 async kvDelete(key){ 160 console.info(logTag + "_methodName is kvDelete"); 161 let message = new ApiMessage("openHarmony","testApi","kvDelete"," ", 162 ["string"],[String(key)]," "); 163 let resGetReq = await this.getReq(message); 164 return resGetReq; 165 } 166 167 async kvSync(deviceID,mode){ 168 // deviceID<string> 169 // mode<string>,Available values:["PUSH","PUSH_PULL"] 170 console.info(logTag + "_methodName is kvDelete"); 171 let message = new ApiMessage("openHarmony","testApi","kvSync"," ", 172 ["string","string"],[String(deviceID),String(mode)]," "); 173 let resGetReq = await this.getReq(message); 174 return resGetReq; 175 } 176 177 async getRdbStore(rdbStoreName){ 178 // rdbStoreName<string>: Name of relational store.Example: "UpdataTest.db" 179 console.info(logTag + "_methodName is getRdbStore"); 180 let message = new ApiMessage("openHarmony","testApi","getRdbStore"," ", 181 ["string"],[String(rdbStoreName)]," "); 182 let resGetReq = await this.getReq(message); 183 return resGetReq; 184 } 185 186 async createObject(objectname, age, isVis){ 187 console.info(logTag + " **************** _methodName is createObject **************** "); 188 let message = new ApiMessage("openHarmony","testApi","createObject"," ", 189 ["string","string","string"],[String(objectname),String(age),String(isVis)]," "); 190 let resGetReq = await this.getReq(message); 191 return resGetReq; 192 } 193 194 async setSessionId(sessionId){ 195 console.info(logTag + " **************** _methodName is setSessionId **************** "); 196 let message = new ApiMessage("openHarmony","testApi","setSessionId"," ", 197 ["string"],[String(sessionId)]," "); 198 let resGetReq = await this.getReq(message); 199 return resGetReq; 200 } 201 202 async objectPut(key,value){ 203 console.info(logTag + "**************** _methodName is objectPut ****************"); 204 let message = new ApiMessage("openHarmony","testApi","objectPut"," ", 205 ["string","string"],[String(key),String(value)]," "); 206 console.info(logTag + "key=" + key + " value=" + value) 207 let resGetReq = await this.getReq(message); 208 return resGetReq; 209 } 210 211} 212