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