10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
30704ebd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40704ebd2Sopenharmony_ci * you may not use this file except in compliance with the License.
50704ebd2Sopenharmony_ci * You may obtain a copy of the License at
60704ebd2Sopenharmony_ci *
70704ebd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80704ebd2Sopenharmony_ci *
90704ebd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100704ebd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110704ebd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120704ebd2Sopenharmony_ci * See the License for the specific language governing permissions and
130704ebd2Sopenharmony_ci * limitations under the License.
140704ebd2Sopenharmony_ci */
150704ebd2Sopenharmony_ci
160704ebd2Sopenharmony_ciimport ApiMessage from '../common/apiMessage.ets';
170704ebd2Sopenharmony_ciimport ApiResult from '../common/apiResult.ets';
180704ebd2Sopenharmony_ciimport rpc from '@ohos.rpc';
190704ebd2Sopenharmony_ci
200704ebd2Sopenharmony_cilet logTag = "[RpcClient_RemoteHelper:  ]";
210704ebd2Sopenharmony_cilet CODE_INVOKE =1;
220704ebd2Sopenharmony_cilet CODE_INVOKE_TESTCASE = 99;
230704ebd2Sopenharmony_ci
240704ebd2Sopenharmony_ciexport default class RemoteHelper{
250704ebd2Sopenharmony_ci    testservice = undefined;
260704ebd2Sopenharmony_ci    gIRemoteObject = undefined;
270704ebd2Sopenharmony_ci    constructor(testservice,gIRemoteObject){
280704ebd2Sopenharmony_ci        this.testservice = testservice;
290704ebd2Sopenharmony_ci        this.gIRemoteObject = gIRemoteObject;
300704ebd2Sopenharmony_ci    }
310704ebd2Sopenharmony_ci
320704ebd2Sopenharmony_ci    async getReq(message) {
330704ebd2Sopenharmony_ci        try {
340704ebd2Sopenharmony_ci            console.log(logTag + "getReq begin");
350704ebd2Sopenharmony_ci            console.log(logTag + "getReq. message= "+ JSON.stringify(message));
360704ebd2Sopenharmony_ci            let data= rpc.MessageSequence.create();
370704ebd2Sopenharmony_ci            let reply = rpc.MessageSequence.create();
380704ebd2Sopenharmony_ci            let option = new rpc.MessageOption();
390704ebd2Sopenharmony_ci            data.writeParcelable(message);
400704ebd2Sopenharmony_ci            await this.gIRemoteObject.sendMessageRequest(CODE_INVOKE, data, reply, option);
410704ebd2Sopenharmony_ci            console.log(logTag + "sendMessageRequest got result");
420704ebd2Sopenharmony_ci            let ret = new ApiMessage(null, null, null, null, null, null, null);
430704ebd2Sopenharmony_ci            reply.readParcelable(ret);
440704ebd2Sopenharmony_ci            let retApi = JSON.parse(ret._apiResult);
450704ebd2Sopenharmony_ci            let retApiResult = retApi._result;
460704ebd2Sopenharmony_ci            console.log(logTag + "***********   read success, results is " + retApiResult + "**************");
470704ebd2Sopenharmony_ci            return retApiResult;
480704ebd2Sopenharmony_ci        } catch(err) {
490704ebd2Sopenharmony_ci            console.error('***********catch getReq err:' + `, error code is ${err.code}, message is ${err.message}`);
500704ebd2Sopenharmony_ci            return 411;
510704ebd2Sopenharmony_ci        }
520704ebd2Sopenharmony_ci    }
530704ebd2Sopenharmony_ci
540704ebd2Sopenharmony_ci    async getReqTestCase(message) {
550704ebd2Sopenharmony_ci        try {
560704ebd2Sopenharmony_ci            console.log(logTag + "getReqTestCase begin");
570704ebd2Sopenharmony_ci            console.log(logTag + "getReqTestCase. message= "+ JSON.stringify(message));
580704ebd2Sopenharmony_ci            let data= rpc.MessageSequence.create();
590704ebd2Sopenharmony_ci            let reply = rpc.MessageSequence.create();
600704ebd2Sopenharmony_ci            let option = new rpc.MessageOption();
610704ebd2Sopenharmony_ci            data.writeParcelable(message);
620704ebd2Sopenharmony_ci            await this.gIRemoteObject.sendMessageRequest(CODE_INVOKE_TESTCASE, data, reply, option);
630704ebd2Sopenharmony_ci            console.log(logTag + "sendMessageRequest got result");
640704ebd2Sopenharmony_ci            let ret = new ApiMessage(null, null, null, null, null, null, null);
650704ebd2Sopenharmony_ci            reply.readParcelable(ret);
660704ebd2Sopenharmony_ci            let retApi = JSON.parse(ret._apiResult);
670704ebd2Sopenharmony_ci            let retApiResult = retApi._result;
680704ebd2Sopenharmony_ci            console.log(logTag + "***********   read success, results is " + retApiResult + "**************");
690704ebd2Sopenharmony_ci            return retApiResult;
700704ebd2Sopenharmony_ci        } catch(err) {
710704ebd2Sopenharmony_ci            console.error('***********catch getReq err:' + `, error code is ${err.code}, message is ${err.message}`);
720704ebd2Sopenharmony_ci            return 411;
730704ebd2Sopenharmony_ci        }
740704ebd2Sopenharmony_ci    }
750704ebd2Sopenharmony_ci
760704ebd2Sopenharmony_ci    async setTestCaseName(name){
770704ebd2Sopenharmony_ci        console.info(logTag + " **************** _methodName is setTestCaseName **************** ");
780704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","setTestCaseName"," ",
790704ebd2Sopenharmony_ci        ["string"],[String(name)]," ");
800704ebd2Sopenharmony_ci        let resGetReq = await this.getReqTestCase(message);
810704ebd2Sopenharmony_ci        return resGetReq;
820704ebd2Sopenharmony_ci    }
830704ebd2Sopenharmony_ci
840704ebd2Sopenharmony_ci    async startBackgroundRunning(){
850704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is startBackgroundRunning");
860704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","startBackgroundRunning"," ",[],[]," ");
870704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
880704ebd2Sopenharmony_ci        return resGetReq;
890704ebd2Sopenharmony_ci    }
900704ebd2Sopenharmony_ci
910704ebd2Sopenharmony_ci    async stopBackgroundRunning(){
920704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is stopBackgroundRunning");
930704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","stopBackgroundRunning"," ",[],[]," ");
940704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
950704ebd2Sopenharmony_ci        return resGetReq;
960704ebd2Sopenharmony_ci    }
970704ebd2Sopenharmony_ci   async add(a,b) {
980704ebd2Sopenharmony_ci        console.log(logTag+"_methodName is add");
990704ebd2Sopenharmony_ci	    let message = new ApiMessage("openHarmony","testApi","add"," ",
1000704ebd2Sopenharmony_ci		    ["number","number"],[String(a),String(b)]," ");
1010704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1020704ebd2Sopenharmony_ci        return resGetReq;
1030704ebd2Sopenharmony_ci    }
1040704ebd2Sopenharmony_ci
1050704ebd2Sopenharmony_ci
1060704ebd2Sopenharmony_ci    async sub(a,b) {
1070704ebd2Sopenharmony_ci        console.log(logTag+"_methodName is sub");
1080704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","sub"," ",
1090704ebd2Sopenharmony_ci		    ["number","number"],[String(a),String(b)]," ");
1100704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1110704ebd2Sopenharmony_ci        return resGetReq;
1120704ebd2Sopenharmony_ci    }
1130704ebd2Sopenharmony_ci    async createKvManager(){
1140704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is createKvManager");
1150704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","createKvManager"," ",[],[]," ");
1160704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1170704ebd2Sopenharmony_ci        return resGetReq;
1180704ebd2Sopenharmony_ci    }
1190704ebd2Sopenharmony_ci
1200704ebd2Sopenharmony_ci    async getKvStore(storeId,SecurityLevel,encrypt){
1210704ebd2Sopenharmony_ci        // if SecurityLevel in ["S0","S1","S2","S3","S4"],the kvstore corresponding to the security level will be created
1220704ebd2Sopenharmony_ci        // if SecurityLevel not in ["S0","S1","S2","S3","S4"],  the kvstore with security level is NO_LEVEL will be created
1230704ebd2Sopenharmony_ci        // if encrypt is true,it will create encrypt kvStore,otherwise it weill create unencrypt kvStore
1240704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is getKvStore");
1250704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","getKvStore"," ",
1260704ebd2Sopenharmony_ci        ["string","string","string"],[String(storeId),String(SecurityLevel),String(encrypt)]," ");
1270704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1280704ebd2Sopenharmony_ci        return resGetReq;
1290704ebd2Sopenharmony_ci    }
1300704ebd2Sopenharmony_ci    async closeKvStore(storeId){
1310704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is closeKvStore");
1320704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","closeKvStore"," ",
1330704ebd2Sopenharmony_ci        ["string"],[String(storeId)]," ");
1340704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1350704ebd2Sopenharmony_ci        return resGetReq;
1360704ebd2Sopenharmony_ci    }
1370704ebd2Sopenharmony_ci
1380704ebd2Sopenharmony_ci    async kvPut(key,value,valueType){
1390704ebd2Sopenharmony_ci        // valueType<string>: The type of value.
1400704ebd2Sopenharmony_ci        // Available values:["String","Number","Uint8Array","Boolean"]
1410704ebd2Sopenharmony_ci        // To avoid unknown errors, it is recommended to pass in only values of string type
1420704ebd2Sopenharmony_ci        // If insert data successful,it will return true, otherwise it will return errInfo
1430704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is kvPut");
1440704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is String(value)" + String(value));
1450704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","kvPut"," ",
1460704ebd2Sopenharmony_ci		    ["string",valueType,"string"],[String(key),String(value),String(valueType)]," ");
1470704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1480704ebd2Sopenharmony_ci        return resGetReq;
1490704ebd2Sopenharmony_ci    }
1500704ebd2Sopenharmony_ci
1510704ebd2Sopenharmony_ci    async kvGet(key){
1520704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is kvGet");
1530704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","kvGet"," ",
1540704ebd2Sopenharmony_ci		    ["string"],[String(key)]," ");
1550704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1560704ebd2Sopenharmony_ci        return resGetReq;
1570704ebd2Sopenharmony_ci    }
1580704ebd2Sopenharmony_ci
1590704ebd2Sopenharmony_ci    async kvDelete(key){
1600704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is kvDelete");
1610704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","kvDelete"," ",
1620704ebd2Sopenharmony_ci		    ["string"],[String(key)]," ");
1630704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1640704ebd2Sopenharmony_ci        return resGetReq;
1650704ebd2Sopenharmony_ci    }
1660704ebd2Sopenharmony_ci
1670704ebd2Sopenharmony_ci    async kvSync(deviceID,mode){
1680704ebd2Sopenharmony_ci        // deviceID<string>
1690704ebd2Sopenharmony_ci        // mode<string>,Available values:["PUSH","PUSH_PULL"]
1700704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is kvDelete");
1710704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","kvSync"," ",
1720704ebd2Sopenharmony_ci		    ["string","string"],[String(deviceID),String(mode)]," ");
1730704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1740704ebd2Sopenharmony_ci        return resGetReq;
1750704ebd2Sopenharmony_ci    }
1760704ebd2Sopenharmony_ci
1770704ebd2Sopenharmony_ci    async getRdbStore(rdbStoreName){
1780704ebd2Sopenharmony_ci        // rdbStoreName<string>: Name of relational store.Example: "UpdataTest.db"
1790704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is getRdbStore");
1800704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","getRdbStore"," ",
1810704ebd2Sopenharmony_ci		    ["string"],[String(rdbStoreName)]," ");
1820704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1830704ebd2Sopenharmony_ci        return resGetReq;
1840704ebd2Sopenharmony_ci    }
1850704ebd2Sopenharmony_ci
1860704ebd2Sopenharmony_ci    async createObject(objectname, age, isVis){
1870704ebd2Sopenharmony_ci        console.info(logTag + " **************** _methodName is createObject **************** ");
1880704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","createObject"," ",
1890704ebd2Sopenharmony_ci        ["string","string","string"],[String(objectname),String(age),String(isVis)]," ");
1900704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1910704ebd2Sopenharmony_ci        return resGetReq;
1920704ebd2Sopenharmony_ci    }
1930704ebd2Sopenharmony_ci
1940704ebd2Sopenharmony_ci    async setSessionId(sessionId){
1950704ebd2Sopenharmony_ci        console.info(logTag + " **************** _methodName is setSessionId **************** ");
1960704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","setSessionId"," ",
1970704ebd2Sopenharmony_ci        ["string"],[String(sessionId)]," ");
1980704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
1990704ebd2Sopenharmony_ci        return resGetReq;
2000704ebd2Sopenharmony_ci    }
2010704ebd2Sopenharmony_ci
2020704ebd2Sopenharmony_ci    async objectPut(key,value){
2030704ebd2Sopenharmony_ci        console.info(logTag + "**************** _methodName is objectPut ****************");
2040704ebd2Sopenharmony_ci        let message = new ApiMessage("openHarmony","testApi","objectPut"," ",
2050704ebd2Sopenharmony_ci		    ["string","string"],[String(key),String(value)]," ");
2060704ebd2Sopenharmony_ci        console.info(logTag + "key=" + key + " value=" + value)
2070704ebd2Sopenharmony_ci        let resGetReq = await this.getReq(message);
2080704ebd2Sopenharmony_ci        return resGetReq;
2090704ebd2Sopenharmony_ci    }
2100704ebd2Sopenharmony_ci
2110704ebd2Sopenharmony_ci}
212