10704ebd2Sopenharmony_ci/*
20704ebd2Sopenharmony_ci * Copyright (C) 2022 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_ciimport disData from '@ohos.data.distributedKVStore';
160704ebd2Sopenharmony_ciimport backgroundTaskManager from '@ohos.backgroundTaskManager';
170704ebd2Sopenharmony_ciimport dataRdb from '@ohos.data.rdb';
180704ebd2Sopenharmony_ciimport ApiResult from '../common/apiResult';
190704ebd2Sopenharmony_ciimport distributedObject from '@ohos.data.distributedDataObject';
200704ebd2Sopenharmony_ciimport deviceinfo from '@ohos.deviceInfo'
210704ebd2Sopenharmony_ciimport UIAbility from '@ohos.app.ability.UIAbility';
220704ebd2Sopenharmony_ci
230704ebd2Sopenharmony_ciimport common from "@ohos.app.ability.common"
240704ebd2Sopenharmony_ci
250704ebd2Sopenharmony_ciconst TEST_BUNDLE_NAME = 'com.acts.distributekvdisjs';
260704ebd2Sopenharmony_cilet logTag = "[[RpcServer_TestApi:  ]]";
270704ebd2Sopenharmony_cilet kvManager = null;
280704ebd2Sopenharmony_cilet kvStore = null;
290704ebd2Sopenharmony_ci
300704ebd2Sopenharmony_cilet rdbStore = undefined;
310704ebd2Sopenharmony_ci
320704ebd2Sopenharmony_cilet g_object = undefined;
330704ebd2Sopenharmony_ci
340704ebd2Sopenharmony_cilet context;
350704ebd2Sopenharmony_ci
360704ebd2Sopenharmony_ciexport default class TestApi{
370704ebd2Sopenharmony_ci    constructor(){}
380704ebd2Sopenharmony_ci    add(a,b) {
390704ebd2Sopenharmony_ci        console.log(logTag+"_methodName is add");
400704ebd2Sopenharmony_ci        return Number(a)+Number(b);
410704ebd2Sopenharmony_ci    }
420704ebd2Sopenharmony_ci    sub(a,b) {
430704ebd2Sopenharmony_ci        console.log(logTag+"_methodName is sub");
440704ebd2Sopenharmony_ci         return Number(a)-Number(b);
450704ebd2Sopenharmony_ci    }
460704ebd2Sopenharmony_ci
470704ebd2Sopenharmony_ci    async createKvManager(){
480704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is createKvManager");
490704ebd2Sopenharmony_ci        try {
500704ebd2Sopenharmony_ci             let context = globalThis.extensionContext;
510704ebd2Sopenharmony_ci             console.info(logTag + "context=" + context);
520704ebd2Sopenharmony_ci             const config = {
530704ebd2Sopenharmony_ci                 context: context,
540704ebd2Sopenharmony_ci                 bundleName: TEST_BUNDLE_NAME,
550704ebd2Sopenharmony_ci             }
560704ebd2Sopenharmony_ci             kvManager = disData.createKVManager(config);
570704ebd2Sopenharmony_ci             console.log(logTag + "createKvManager22 success, kvManager=" + kvManager);
580704ebd2Sopenharmony_ci          } catch (error) {
590704ebd2Sopenharmony_ci            console.error(logTag + "createKvManager22 error.code=" + error.code + "error.message=" + error.message);
600704ebd2Sopenharmony_ci          }
610704ebd2Sopenharmony_ci         console.info(logTag + "createKvManager22 end");
620704ebd2Sopenharmony_ci    }
630704ebd2Sopenharmony_ci
640704ebd2Sopenharmony_ci    async getKvStore(storeId,SecurityLevel,encrypt){
650704ebd2Sopenharmony_ci        console.info(logTag + "_methodName is getKvStore, storeId=" + storeId + " SecurityLevel=" + SecurityLevel + " encrypt="+ encrypt);
660704ebd2Sopenharmony_ci        console.info(logTag + "kvManager=" + kvManager);
670704ebd2Sopenharmony_ci        if(!kvManager) {
680704ebd2Sopenharmony_ci            console.info(logTag + "createKvManager22 begin");
690704ebd2Sopenharmony_ci           console.info(logTag + "_methodName is createKvManager22");
700704ebd2Sopenharmony_ci           try {
710704ebd2Sopenharmony_ci                let context = globalThis.extensionContext;
720704ebd2Sopenharmony_ci                console.info(logTag + "context=" + context);
730704ebd2Sopenharmony_ci                const config = {
740704ebd2Sopenharmony_ci                    context: context,
750704ebd2Sopenharmony_ci                    bundleName: TEST_BUNDLE_NAME,
760704ebd2Sopenharmony_ci                }
770704ebd2Sopenharmony_ci                kvManager = disData.createKVManager(config);
780704ebd2Sopenharmony_ci                console.log(logTag + "createKvManager22 success, kvManager=" + kvManager);
790704ebd2Sopenharmony_ci             } catch (error) {
800704ebd2Sopenharmony_ci               console.error(logTag + "createKvManager22 error.code=" + error.code + "error.message=" + error.message);
810704ebd2Sopenharmony_ci             }
820704ebd2Sopenharmony_ci            console.info(logTag + "createKvManager22 end");
830704ebd2Sopenharmony_ci        }
840704ebd2Sopenharmony_ci
850704ebd2Sopenharmony_ci        let flag_41 = 1;
860704ebd2Sopenharmony_ci        let localOSVersion = "";
870704ebd2Sopenharmony_ci        let OSVersion41 = "OpenHarmony-4.1";
880704ebd2Sopenharmony_ci        let osReleaseTypeInfo = deviceinfo.osReleaseType;
890704ebd2Sopenharmony_ci        console.info(logTag + 'the value of the deviceinfo osReleaseType is :' + osReleaseTypeInfo);
900704ebd2Sopenharmony_ci        let osFullNameInfo = deviceinfo.osFullName;
910704ebd2Sopenharmony_ci        console.info(logTag + 'the value of the deviceinfo osFullName is :' + osFullNameInfo);
920704ebd2Sopenharmony_ci
930704ebd2Sopenharmony_ci        localOSVersion = osFullNameInfo.substring(0, 15);
940704ebd2Sopenharmony_ci        console.info(logTag + "localOSVersion is: " + localOSVersion);
950704ebd2Sopenharmony_ci
960704ebd2Sopenharmony_ci        var optionsInfo = {
970704ebd2Sopenharmony_ci            createIfMissing : true,
980704ebd2Sopenharmony_ci            encrypt : false,
990704ebd2Sopenharmony_ci            backup : false,
1000704ebd2Sopenharmony_ci            autoSync: false,
1010704ebd2Sopenharmony_ci            kvStoreType : disData.KVStoreType.SINGLE_VERSION,
1020704ebd2Sopenharmony_ci            schema : '',
1030704ebd2Sopenharmony_ci            securityLevel : disData.SecurityLevel.S1,
1040704ebd2Sopenharmony_ci        }
1050704ebd2Sopenharmony_ci        if(encrypt == "true"){
1060704ebd2Sopenharmony_ci            optionsInfo.encrypt = true;
1070704ebd2Sopenharmony_ci        }else{
1080704ebd2Sopenharmony_ci            optionsInfo.encrypt = false;
1090704ebd2Sopenharmony_ci        }
1100704ebd2Sopenharmony_ci
1110704ebd2Sopenharmony_ci        if(SecurityLevel == "NO_LEVEL"){
1120704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.NO_LEVEL;
1130704ebd2Sopenharmony_ci        }else if(SecurityLevel == "S0"){
1140704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S0;
1150704ebd2Sopenharmony_ci        }else if(SecurityLevel == "S1"){
1160704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S1;
1170704ebd2Sopenharmony_ci        }else if(SecurityLevel == "S2"){
1180704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S2;
1190704ebd2Sopenharmony_ci        }else if(SecurityLevel == "S3"){
1200704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S3;
1210704ebd2Sopenharmony_ci        }else if(SecurityLevel == "S4"){
1220704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S4;
1230704ebd2Sopenharmony_ci        }else {
1240704ebd2Sopenharmony_ci            optionsInfo.securityLevel=disData.SecurityLevel.S1;
1250704ebd2Sopenharmony_ci        }
1260704ebd2Sopenharmony_ci
1270704ebd2Sopenharmony_ci        let name = new disData.FieldNode('name');
1280704ebd2Sopenharmony_ci        name.type = disData.ValueType.INTEGER;
1290704ebd2Sopenharmony_ci        name.nullable = false;
1300704ebd2Sopenharmony_ci        name.default = '0';
1310704ebd2Sopenharmony_ci
1320704ebd2Sopenharmony_ci        let schema = new disData.Schema();
1330704ebd2Sopenharmony_ci        schema.root.appendChild(name);
1340704ebd2Sopenharmony_ci        schema.indexes = ['$.name'];
1350704ebd2Sopenharmony_ci        schema.mode = 1;
1360704ebd2Sopenharmony_ci
1370704ebd2Sopenharmony_ci
1380704ebd2Sopenharmony_ci        let TEST_SCHEMA_STORE_ID_PREF = "SchemaStoreId_";
1390704ebd2Sopenharmony_ci        let schemaIdPref = storeId.substring(0, 14);
1400704ebd2Sopenharmony_ci        console.info(logTag + "schemaIdPref=" + schemaIdPref);
1410704ebd2Sopenharmony_ci        if (schemaIdPref == TEST_SCHEMA_STORE_ID_PREF) {
1420704ebd2Sopenharmony_ci            optionsInfo.schema = schema;
1430704ebd2Sopenharmony_ci        }
1440704ebd2Sopenharmony_ci        console.info(logTag + "optionsInfo.schema=" + optionsInfo.schema);
1450704ebd2Sopenharmony_ci
1460704ebd2Sopenharmony_ci        await kvManager.getKVStore(storeId,optionsInfo).then((store) =>{
1470704ebd2Sopenharmony_ci            kvStore = store;
1480704ebd2Sopenharmony_ci            console.info(logTag + " getKVStore success, securityLevel is: " + optionsInfo.securityLevel);
1490704ebd2Sopenharmony_ci            return String(true);
1500704ebd2Sopenharmony_ci        }).catch((error) => {
1510704ebd2Sopenharmony_ci            console.error(logTag + "getKVStore fail, error.code=" + error.code + "error.message=" + error.message);
1520704ebd2Sopenharmony_ci            return String(error);
1530704ebd2Sopenharmony_ci        });
1540704ebd2Sopenharmony_ci    }
1550704ebd2Sopenharmony_ci    async closeKvStore(storeId){
1560704ebd2Sopenharmony_ci        try{
1570704ebd2Sopenharmony_ci        await kvManager.closeKVStore(TEST_BUNDLE_NAME, storeId).then(async () => {
1580704ebd2Sopenharmony_ci            await kvManager.deleteKVStore(TEST_BUNDLE_NAME, storeId).then(() => {
1590704ebd2Sopenharmony_ci                console.info(logTag + " Server deleteKVStore success, storeId=" + storeId);
1600704ebd2Sopenharmony_ci                return String(true);
1610704ebd2Sopenharmony_ci            })
1620704ebd2Sopenharmony_ci        }).catch((error) => {
1630704ebd2Sopenharmony_ci            console.error(logTag + "Server deleteKVStore fail, error.code=" + error.code + "error.message=" + error.message);
1640704ebd2Sopenharmony_ci            return String(error);
1650704ebd2Sopenharmony_ci        });
1660704ebd2Sopenharmony_ci        } catch (err) {
1670704ebd2Sopenharmony_ci            console.error('catch closeKvStore err:' + `, error code is ${err.code}, message is ${err.message}`);
1680704ebd2Sopenharmony_ci            return String(err);
1690704ebd2Sopenharmony_ci        }
1700704ebd2Sopenharmony_ci    }
1710704ebd2Sopenharmony_ci
1720704ebd2Sopenharmony_ci    async kvPut(key,value,valueType){
1730704ebd2Sopenharmony_ci        let putValue = undefined;
1740704ebd2Sopenharmony_ci        console.info(logTag + " putValue : " + putValue + "  value is: " + value + "  valueType is: " + valueType);
1750704ebd2Sopenharmony_ci        if(valueType == "String"){
1760704ebd2Sopenharmony_ci            putValue = value;
1770704ebd2Sopenharmony_ci        }else if(valueType == "Number"){
1780704ebd2Sopenharmony_ci            putValue = Number(value);
1790704ebd2Sopenharmony_ci        }else if(valueType == "Number_Min"){
1800704ebd2Sopenharmony_ci            putValue = Number.MIN_VALUE;
1810704ebd2Sopenharmony_ci        }else if(valueType == "Uint8Array"){
1820704ebd2Sopenharmony_ci            putValue = value.split(',');
1830704ebd2Sopenharmony_ci        }else if(valueType == "Boolean"){
1840704ebd2Sopenharmony_ci            if(value == "false"){
1850704ebd2Sopenharmony_ci                putValue = false;
1860704ebd2Sopenharmony_ci            }else{
1870704ebd2Sopenharmony_ci                putValue = Boolean(value);
1880704ebd2Sopenharmony_ci            }
1890704ebd2Sopenharmony_ci        }
1900704ebd2Sopenharmony_ci        console.info(logTag + " putValue : " + putValue + "  value is: " + value);
1910704ebd2Sopenharmony_ci        await kvStore.put(key,putValue).then(() =>  {
1920704ebd2Sopenharmony_ci            console.info(logTag + " Server  put data success ,key is : " + key + " value is: " + putValue);
1930704ebd2Sopenharmony_ci            return String(true);
1940704ebd2Sopenharmony_ci        }).catch((error) => {
1950704ebd2Sopenharmony_ci            console.error(logTag + " Server  put fail, error.code=" + error.code + "error.message=" + error.message);
1960704ebd2Sopenharmony_ci            return String(error);
1970704ebd2Sopenharmony_ci        });
1980704ebd2Sopenharmony_ci    }
1990704ebd2Sopenharmony_ci     async kvGet(key,callback){
2000704ebd2Sopenharmony_ci        console.info(logTag + " kvGet start");
2010704ebd2Sopenharmony_ci        let errInfo = undefined;
2020704ebd2Sopenharmony_ci        let result = undefined;
2030704ebd2Sopenharmony_ci        kvStore.get(key, function (err, data){
2040704ebd2Sopenharmony_ci            errInfo = err;
2050704ebd2Sopenharmony_ci            result = data;
2060704ebd2Sopenharmony_ci        })
2070704ebd2Sopenharmony_ci        if(err != null){
2080704ebd2Sopenharmony_ci            console.info(logTag + " kvGet get data error: " + err);
2090704ebd2Sopenharmony_ci            callback(err);
2100704ebd2Sopenharmony_ci        }else{
2110704ebd2Sopenharmony_ci            console.info(logTag + " kvGet get data success");
2120704ebd2Sopenharmony_ci            callback(data);
2130704ebd2Sopenharmony_ci        }
2140704ebd2Sopenharmony_ci    }
2150704ebd2Sopenharmony_ci    async kvDelete(key){
2160704ebd2Sopenharmony_ci        await kvStore.delete(key).then(() => {
2170704ebd2Sopenharmony_ci            console.info(logTag + "Delete data success");
2180704ebd2Sopenharmony_ci            return String(true);
2190704ebd2Sopenharmony_ci        }).catch((err) => {
2200704ebd2Sopenharmony_ci            console.info(logTag + "Delete data error,errorInfo: " + err);
2210704ebd2Sopenharmony_ci            return String(err);
2220704ebd2Sopenharmony_ci
2230704ebd2Sopenharmony_ci        });
2240704ebd2Sopenharmony_ci    }
2250704ebd2Sopenharmony_ci
2260704ebd2Sopenharmony_ci    kvSync(deviceid,mode){
2270704ebd2Sopenharmony_ci        let syncMode = undefined;
2280704ebd2Sopenharmony_ci        if(mode == "PUSH"){
2290704ebd2Sopenharmony_ci            syncMode = disData.SyncMode.PUSH_ONLY;
2300704ebd2Sopenharmony_ci        }else if(mode == "PUSH_PULL"){
2310704ebd2Sopenharmony_ci            syncMode = disData.SyncMode.PUSH_PULL;
2320704ebd2Sopenharmony_ci        }
2330704ebd2Sopenharmony_ci        kvStore.Sync([deviceid],syncMode);
2340704ebd2Sopenharmony_ci        return JSON.stringify(syncMode);
2350704ebd2Sopenharmony_ci    }
2360704ebd2Sopenharmony_ci
2370704ebd2Sopenharmony_ci    async getRdbStore(rdbStoreName){
2380704ebd2Sopenharmony_ci        var storeConfig = {
2390704ebd2Sopenharmony_ci            name: rdbStoreName,
2400704ebd2Sopenharmony_ci        }
2410704ebd2Sopenharmony_ci        await dataRdb.getRdbStore(storeConfig,1).then((store) => {
2420704ebd2Sopenharmony_ci            console.info(logTag + " get rdbStore success");
2430704ebd2Sopenharmony_ci            rdbStore = store;
2440704ebd2Sopenharmony_ci            return String(true);
2450704ebd2Sopenharmony_ci        }).catch((err) => {
2460704ebd2Sopenharmony_ci            console.info(logTag + " get rdbStore error, err: " + err.code + err.message);
2470704ebd2Sopenharmony_ci            return String(err);
2480704ebd2Sopenharmony_ci        });
2490704ebd2Sopenharmony_ci    }
2500704ebd2Sopenharmony_ci
2510704ebd2Sopenharmony_ci    async createObject(objectname, age, isVis){
2520704ebd2Sopenharmony_ci        try{
2530704ebd2Sopenharmony_ci            console.info(logTag + "************* createObject begin ************* ");
2540704ebd2Sopenharmony_ci
2550704ebd2Sopenharmony_ci            let g_context = globalThis.extensionContext;
2560704ebd2Sopenharmony_ci            console.info(logTag + "context 2= " + g_context);
2570704ebd2Sopenharmony_ci            if (g_context == undefined || g_context == null) {
2580704ebd2Sopenharmony_ci                console.info(logTag + "error: g_context = undefined");
2590704ebd2Sopenharmony_ci                return false;
2600704ebd2Sopenharmony_ci            }
2610704ebd2Sopenharmony_ci
2620704ebd2Sopenharmony_ci            g_object = undefined;
2630704ebd2Sopenharmony_ci            let rename = objectname;
2640704ebd2Sopenharmony_ci            let reage = Number(age);
2650704ebd2Sopenharmony_ci            let reisVis = Boolean(isVis);
2660704ebd2Sopenharmony_ci
2670704ebd2Sopenharmony_ci            console.info(logTag + "objectname=" + objectname + "  age=" + age + "  isVis=" + isVis);
2680704ebd2Sopenharmony_ci            console.info(logTag + "rename=" + rename + "  reage=" + reage + "  reisVis=" + reisVis);
2690704ebd2Sopenharmony_ci
2700704ebd2Sopenharmony_ci            g_object = distributedObject.create(g_context, {name: rename, age: reage, isVis: reisVis});
2710704ebd2Sopenharmony_ci            if (g_object == undefined || g_object == null) {
2720704ebd2Sopenharmony_ci                console.info(logTag + "error:  g_object = undefined");
2730704ebd2Sopenharmony_ci                return false;
2740704ebd2Sopenharmony_ci            }
2750704ebd2Sopenharmony_ci            console.info(logTag + "createObject success");
2760704ebd2Sopenharmony_ci            console.info(logTag + g_object.name + "  " + g_object.age + "  " + g_object.isVis);
2770704ebd2Sopenharmony_ci
2780704ebd2Sopenharmony_ci        }catch (err) {
2790704ebd2Sopenharmony_ci            console.info(logTag + "createObject error, err: "+ err.code + err.message);
2800704ebd2Sopenharmony_ci        }
2810704ebd2Sopenharmony_ci    }
2820704ebd2Sopenharmony_ci
2830704ebd2Sopenharmony_ci    async setSessionId(sessionId){
2840704ebd2Sopenharmony_ci        try{
2850704ebd2Sopenharmony_ci            console.info(logTag + "************* setSessionId begin ************* ");
2860704ebd2Sopenharmony_ci            console.info(logTag +"g_object=" + g_object);
2870704ebd2Sopenharmony_ci            if (g_object == undefined || g_object == null) {
2880704ebd2Sopenharmony_ci                console.info(logTag + "error:  g_object = undefined");
2890704ebd2Sopenharmony_ci                return false;
2900704ebd2Sopenharmony_ci            }
2910704ebd2Sopenharmony_ci            g_object.setSessionId(sessionId).then(() => {
2920704ebd2Sopenharmony_ci                console.info(logTag + "remote join session success. sessionId=" + sessionId);
2930704ebd2Sopenharmony_ci            }).catch((error) => {
2940704ebd2Sopenharmony_ci                console.info(logTag + "remote join session error: " + error.code + error.message);
2950704ebd2Sopenharmony_ci                return false;
2960704ebd2Sopenharmony_ci            });
2970704ebd2Sopenharmony_ci        }catch (err) {
2980704ebd2Sopenharmony_ci            console.info(logTag + "setSessionId error, err: "+ err.code + err.message);
2990704ebd2Sopenharmony_ci        }
3000704ebd2Sopenharmony_ci    }
3010704ebd2Sopenharmony_ci
3020704ebd2Sopenharmony_ci    async objectPut(key,value){
3030704ebd2Sopenharmony_ci        console.info(logTag + "objectPut key=" + key + "  value=" + value );
3040704ebd2Sopenharmony_ci        if(key == "name"){
3050704ebd2Sopenharmony_ci            g_object.name = value;
3060704ebd2Sopenharmony_ci            console.info(logTag + "objectPut success. name=" + g_object.name);
3070704ebd2Sopenharmony_ci        }else if(key == "age"){
3080704ebd2Sopenharmony_ci            g_object.age = Number(value);
3090704ebd2Sopenharmony_ci            console.info(logTag + "objectPut success. age=" + g_object.age);
3100704ebd2Sopenharmony_ci        }else if(key == "isVis"){
3110704ebd2Sopenharmony_ci            if(value == "false"){
3120704ebd2Sopenharmony_ci                g_object.isVis = false;
3130704ebd2Sopenharmony_ci            }else{
3140704ebd2Sopenharmony_ci                g_object.isVis = Boolean(value);
3150704ebd2Sopenharmony_ci            }
3160704ebd2Sopenharmony_ci            console.info(logTag + "objectPut success. isVis=" + g_object.isVis);
3170704ebd2Sopenharmony_ci        }
3180704ebd2Sopenharmony_ci    }
3190704ebd2Sopenharmony_ci
3200704ebd2Sopenharmony_ci}
321