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'; 19let logTag = "RpcClient_RemoteHelper: "; 20let CODE_INVOKE =1; 21export default class RemoteHelper{ 22 testservice = undefined; 23 gIRemoteObject = undefined; 24 constructor(testservice,gIRemoteObject){ 25 this.testservice = testservice; 26 this.gIRemoteObject = gIRemoteObject; 27 } 28 29 async getReq(message) { 30 console.log(logTag + "getReq begin"); 31 let messageParcel = rpc.MessageParcel.create(); 32 console.log(logTag + "create object successfully."); 33 let messageParcelreply = rpc.MessageParcel.create(); 34 let option = new rpc.MessageOption(); 35 let writeResult = messageParcel.writeSequenceable(message); 36 await this.gIRemoteObject.sendRequest(CODE_INVOKE, messageParcel, messageParcelreply, option); 37 console.log(logTag + "sendRequest got result"); 38 let ret = new ApiMessage(null, null, null, null, null, null, null); 39 let dataReply = messageParcelreply.readSequenceable(ret); 40 console.log(logTag + "run readSequenceable success, result is" + dataReply); 41 let retApi = JSON.parse(ret._apiResult); 42 let retApiResult = retApi._result; 43 console.log(logTag + " read success, results is" + retApiResult); 44 return retApiResult; 45} 46 47 async startBackgroundRunning(){ 48 console.info(logTag + "_methodName is startBackgroundRunning"); 49 let message = new ApiMessage("openHarmony","testApi","startBackgroundRunning"," ",[],[]," "); 50 let resGetReq = await this.getReq(message); 51 return resGetReq; 52 } 53 54 async stopBackgroundRunning(){ 55 console.info(logTag + "_methodName is stopBackgroundRunning"); 56 let message = new ApiMessage("openHarmony","testApi","stopBackgroundRunning"," ",[],[]," "); 57 let resGetReq = await this.getReq(message); 58 return resGetReq; 59 } 60 async add(a,b) { 61 console.log(logTag+"_methodName is add"); 62 let message = new ApiMessage("openHarmony","testApi","add"," ", 63 ["number","number"],[String(a),String(b)]," "); 64 let resGetReq = await this.getReq(message); 65 return resGetReq; 66 } 67 68 69 async sub(a,b) { 70 console.log(logTag+"_methodName is sub"); 71 let message = new ApiMessage("openHarmony","testApi","sub"," ", 72 ["number","number"],[String(a),String(b)]," "); 73 let resGetReq = await this.getReq(message); 74 return resGetReq; 75 } 76 async createKvManager(){ 77 console.info(logTag + "_methodName is createKvManager"); 78 let message = new ApiMessage("openHarmony","testApi","createKvManager"," ",[],[]," "); 79 let resGetReq = await this.getReq(message); 80 return resGetReq; 81 } 82 83 async getKvStore(storeId,SecurityLevel,encrypt){ 84 // if SecurityLevel in ["S0","S1","S2","S3","S4"],the kvstore corresponding to the security level will be created 85 // if SecurityLevel not in ["S0","S1","S2","S3","S4"], the kvstore with security level is NO_LEVEL will be created 86 // if encrypt is true,it will create encrypt kvStore,otherwise it weill create unencrypt kvStore 87 console.info(logTag + "_methodName is getKvStore"); 88 let message = new ApiMessage("openHarmony","testApi","getKvStore"," ", 89 ["string","string","string"],[String(storeId),String(SecurityLevel),String(encrypt)]," "); 90 let resGetReq = await this.getReq(message); 91 return resGetReq; 92 } 93 async closeKvStore(storeId){ 94 console.info(logTag + "_methodName is closeKvStore"); 95 let message = new ApiMessage("openHarmony","testApi","closeKvStore"," ", 96 ["string"],[String(storeId)]," "); 97 let resGetReq = await this.getReq(message); 98 return resGetReq; 99 } 100 101 async kvPut(key,value,valueType){ 102 // valueType<string>: The type of value. 103 // Available values:["String","Number","Uint8Array","Boolean"] 104 // To avoid unknown errors, it is recommended to pass in only values of string type 105 // If insert data successful,it will return true, otherwise it will return errInfo 106 console.info(logTag + "_methodName is kvPut"); 107 let message = new ApiMessage("openHarmony","testApi","kvPut"," ", 108 ["string",valueType,"string"],[String(key),value.toString(),String(valueType)]," "); 109 console.info(logTag + " test key is: " + key + " value is " + value.toString()) 110 let resGetReq = await this.getReq(message); 111 return resGetReq; 112 } 113 114 async kvGet(key){ 115 console.info(logTag + "_methodName is kvGet"); 116 let message = new ApiMessage("openHarmony","testApi","kvGet"," ", 117 ["string"],[String(key)]," "); 118 let resGetReq = await this.getReq(message); 119 return resGetReq; 120 } 121 122 async kvDelete(key){ 123 console.info(logTag + "_methodName is kvDelete"); 124 let message = new ApiMessage("openHarmony","testApi","kvDelete"," ", 125 ["string"],[String(key)]," "); 126 let resGetReq = await this.getReq(message); 127 return resGetReq; 128 } 129 130 async kvSync(deviceID,mode){ 131 // deviceID<string> 132 // mode<string>,Available values:["PUSH","PUSH_PULL"] 133 console.info(logTag + "_methodName is kvDelete"); 134 let message = new ApiMessage("openHarmony","testApi","kvSync"," ", 135 ["string","string"],[String(deviceID),String(mode)]," "); 136 let resGetReq = await this.getReq(message); 137 return resGetReq; 138 } 139 140 async getRdbStore(rdbStoreName){ 141 // rdbStoreName<string>: Name of relational store.Example: "UpdataTest.db" 142 console.info(logTag + "_methodName is getRdbStore"); 143 let message = new ApiMessage("openHarmony","testApi","getRdbStore"," ", 144 ["string"],[String(rdbStoreName)]," "); 145 let resGetReq = await this.getReq(message); 146 return resGetReq; 147 } 148 149} 150