add sample code

Signed-off-by: gou-jingjing <goujingjing@kaihong.com>
This commit is contained in:
gou-jingjing 2023-11-22 16:07:14 +08:00
parent 4120c6a657
commit cefd4212da
6 changed files with 503 additions and 0 deletions

179
examples/Index.ets Normal file
View File

@ -0,0 +1,179 @@
import napitest from '@ohos.napitest';
let ns = new napitest.NodeISayHello();
class NodeISayHelloListenerImpl {
listener = null;
constructor(listener) {
this.listener = listener;
}
onSayHelloStart(info) {
AppStorage.SetOrCreate("textInfoStart", JSON.stringify(info))
console.log('napiTestDemo ----onSayHelloStart', JSON.stringify(info));
}
onSayHelloEnd(info) {
AppStorage.SetOrCreate("textInfoEnd", JSON.stringify(info))
console.log('napiTestDemo ----onSayHelloEnd.', JSON.stringify(info));
}
}
// @ts-ignore
let listener = new NodeISayHelloListenerImpl()
// register注册的回调
function onCallbackfunnm(wid) {
AppStorage.SetOrCreate("callBackNum", JSON.stringify(wid))
console.info("napiTestDemo ----onCallbackfunnm wid = " + wid)
return "ocCallbackfuncnm";
}
@Entry
@Component
struct Index {
@State textErrMsg: string = ''
@State textResult: string = ''
@State textResponse: string = ''
@State returnVal: string = ''
@StorageLink("textInfoStart")textInfoStart: string = ""
@StorageLink("textInfoEnd")textInfoEnd: string = ""
@StorageLink("callBackNum")callBackNum: string = ""
build() {
Row() {
Column() {
Button() {
Text('注册object回调后SayHello调用回调')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick(() => {
AppStorage.SetOrCreate("textInfoStart", "")
AppStorage.SetOrCreate("textInfoEnd", "")
// 注册回调
ns.addSayHelloListener(listener);
// 调用回调
ns.sayHello("js1", "native1", napitest.SayType.kInitiative);
})
Button() {
Text('注销object回调后SayHello调用回调')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick(() => {
AppStorage.SetOrCreate("textInfoStart", "")
AppStorage.SetOrCreate("textInfoEnd", "")
// 注销回调 removeSayHelloListener
ns.removeSayHelloListener(listener);
// 调用回调
ns.sayHello("js2", "native2", napitest.SayType.kInitiative);
})
// promise回调
Button() {
Text('Promise 回调')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick(async () => {
await ns.sayHelloWithResponse("response from", "response to", napitest.SayType.kResponse).then(ret => {
this.textErrMsg = ret.errMsg
this.textResult = JSON.stringify(ret.result)
this.textResponse = ret.response
console.info("napiTestDemo ----sayHelloWithResponse ret.errMsg = " + ret.errMsg)
console.info("napiTestDemo ----sayHelloWithResponse ret.result = " + ret.result)
console.info("napiTestDemo ----sayHelloWithResponse ret.response = " + ret.response)
});
})
Button() {
Text('register回调后sayHi调用回调')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick( () => {
AppStorage.SetOrCreate("callBackNum", "")
// 注册回调
ns.registerCallbackfunc(onCallbackfunnm);
// 调用回调
ns.sayHi("js3", "native3", napitest.SayType.kResponse);
})
Button() {
Text('unRegister回调后sayHi调用回调')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick( () => {
AppStorage.SetOrCreate("callBackNum", "")
// 注销回调
ns.unRegisterCallbackfunc(onCallbackfunnm);
// 调用回调
ns.sayHi("js4", "native4", napitest.SayType.kResponse);
})
// 调用普通函数
Button() {
Text('调用funcTest方法')
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.type(ButtonType.Capsule)
.margin({
top: 10
})
.backgroundColor('#0D9FFB')
.width('90%')
.height('5%')
.onClick( () => {
this.returnVal = napitest.funcTest(false);
console.info("napiTestDemo ----funcTest returnVal = " + this.returnVal)
})
Text('promise回调: errMsg = ' + this.textErrMsg + ', result = ' + this.textResult + ', response = ' + this.textResponse)
.margin({
top: 10
})
Text('sayHelloStart回调: info = ' + this.textInfoStart).margin({ top: 10 })
Text('sayHelloEnd回调: info = ' + this.textInfoEnd).margin({ top: 10 })
Text('register注册的回调 wid = ' + this.callBackNum).margin({ top: 10 })
Text('普通方法funcTest返回值 returnVal = ' + this.returnVal).margin({ top: 10 })
}
.width('100%')
}
.height('100%')
}
}

Binary file not shown.

26
examples/cfg.json Normal file
View File

@ -0,0 +1,26 @@
[
{
"includeName": "../serviceCode/say_hello.h",
"cppName": "../serviceCode/say_hello.cpp",
"interfaceName": "NodeISayHello::sayHello",
"serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHello(from, to, sayType);\n delete p;"
},
{
"includeName": "../serviceCode/say_hello.h",
"cppName": "../serviceCode/say_hello.cpp",
"interfaceName": "NodeISayHello::sayHi",
"serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHi(from, to, sayType);\n delete p;"
},
{
"includeName": "../serviceCode/say_hello.h",
"cppName": "../serviceCode/say_hello.cpp",
"interfaceName": "funcTest",
"serviceCode": "out = napitest::funcTest(v);"
},
{
"includeName": "../serviceCode/say_hello.h",
"cppName": "../serviceCode/say_hello.cpp",
"interfaceName": "NodeISayHello::sayHelloWithResponse",
"serviceCode": "napitest::NodeISayHello *p = new napitest::NodeISayHello();\n p->sayHelloWithResponse(from, to, sayType);\n delete p;"
}
]

158
examples/napitest.cpp Normal file
View File

@ -0,0 +1,158 @@
/*
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "napitest.h"
#include "napitest_middle.h"
#include "hilog/log.h"
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"};
#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \
"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
namespace napitest {
namespace napitest_interface {
NodeISayHelloListener NodeISayHello::listener_ = {};
bool NodeISayHello::addSayHelloListener(NodeISayHelloListener& listener)
{
NodeISayHello::listener_ = listener;
return true;
}
bool NodeISayHello::removeSayHelloListener(NodeISayHelloListener& listener)
{
return true;
}
bool NodeISayHello::registerCallbackfunc()
{
return true;
}
// 供业务调用的回调接口
void NodeISayHello::CallbackfuncCallback(NUMBER_TYPE_2& wid)
{
std::string eventName = "Callbackfunc";
NodeISayHello_middle *ptr = new NodeISayHello_middle();
ptr->CallbackfuncCallbackMiddle(eventName, wid);
delete ptr;
}
bool NodeISayHello::unRegisterCallbackfunc()
{
return true;
}
bool NodeISayHello::sayHello(std::string& from, std::string& to, NUMBER_TYPE_9& sayType)
{
NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType);
SayInfo info1;
info1.from = "js1";
uint32_t a = 992;
info1.fromId.emplace(a);
uint32_t b = 1014;
info1.toId.emplace(b);
info1.to = "native1";
info1.content = "hello1";
info1.saidTime = "123456789";
info1.isEnd = false;
SayInfo info2;
info2.from = "native";
uint32_t c = 101;
info2.fromId.emplace(c);
uint32_t d = 99;
info2.toId.emplace(d);
info2.to = "js";
info2.content = "hello";
info2.saidTime = "987654321";
info2.isEnd = true;
// 业务代码调用 onSayHelloStart callback
listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
// 业务代码调用 onSayHelloEnd callback
listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
return true;
}
bool NodeISayHello::sayHi(std::string& from, std::string& to, NUMBER_TYPE_10& sayType)
{
NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType);
NodeISayHello *ptr = new NodeISayHello();
uint32_t callbackNum = 50;
ptr->CallbackfuncCallback(callbackNum);
delete ptr;
return true;
}
bool NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, NUMBER_TYPE_11& sayType,
uint32_t& outErrCode, AUTO_INTERFACE_5& out)
{
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType);
out.errMsg = "";
out.response = "rec hello.";
out.result = 0;
return true;
}
AUTO_INTERFACE_5 NodeISayHello::auto_interface_5OutRes = {};
void NodeISayHello::auto_interface_5SetCbValue(NUMBER_TYPE_6 result, std::string errMsg, std::string response)
{
NodeISayHello::auto_interface_5OutRes.result = result;
NodeISayHello::auto_interface_5OutRes.errMsg = errMsg;
NodeISayHello::auto_interface_5OutRes.response = response;
return;
}
bool NodeISayHelloListener::onSayHelloStart()
{
return true;
}
// 供业务调用的回调接口
void NodeISayHelloListener::NodeISayHelloListener_onSayHelloStartCallback(SayInfo& info)
{
std::string eventName = "NodeISayHelloListener_onSayHelloStart";
NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
ptr->NodeISayHelloListener_onSayHelloStartCallbackMiddle(eventName, info);
delete ptr;
}
bool NodeISayHelloListener::onSayHelloEnd()
{
return true;
}
// 供业务调用的回调接口
void NodeISayHelloListener::NodeISayHelloListener_onSayHelloEndCallback(SayInfo& info)
{
std::string eventName = "NodeISayHelloListener_onSayHelloEnd";
NodeISayHelloListener_middle *ptr = new NodeISayHelloListener_middle();
ptr->NodeISayHelloListener_onSayHelloEndCallbackMiddle(eventName, info);
delete ptr;
}
bool funcTest(bool& v, std::string& out)
{
if (v) {
out = "ret is true";
} else {
out = "ret is false";
}
return true;
}
}
}

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "say_hello.h"
#include "../generatorCode/napitest.h"
#include "hilog/log.h"
static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0XD002E00, "NAPITESTNAPILayer"};
#define NAPITEST_LOGI(fmt, ...) OHOS::HiviewDFX::HiLog::Info(LABEL, \
"%{public}s:%{public}d " fmt, __func__, __LINE__, ##__VA_ARGS__)
namespace napitest {
// 1. 打印from, to, enum sayType的值
// 2. 调用注册的NodeISayHelloListenerSayHelloStart(info: SayInfo)方法
// 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloStartCallback(SayInfo& info)
// 3. 调用注册的NodeISayHelloListenerSayHelloEnd(info: SayInfo)方法
// 工具提供的业务接口(回调) void NodeISayHello::SayHelloListenerSayHelloEndCallback(SayInfo& info)
void NodeISayHello::sayHello(std::string& from, std::string& to, uint32_t& sayType)
{
// 1.打印
NAPITEST_LOGI("NAPITEST_LOGI sayHello from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHello to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHello sayType = %d\r\n", sayType);
// 2.调用回调
napitest::napitest_interface::SayInfo info1;
info1.from = "js1";
uint32_t a = 992;
info1.fromId.emplace(a);
uint32_t b = 1014;
info1.toId.emplace(b);
info1.to = "native1";
info1.content = "hello1";
info1.saidTime = "123456789";
info1.isEnd = false;
napitest::napitest_interface::SayInfo info2;
info2.from = "native";
uint32_t c = 101;
info2.fromId.emplace(c);
uint32_t d = 99;
info2.toId.emplace(d);
info2.to = "js";
info2.content = "hello";
info2.saidTime = "987654321";
info2.isEnd = true;
// 业务代码调用 onSayHelloStart callback
NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback begin\r\n");
napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloStartCallback(info1);
NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloStartCallback end\r\n");
// 业务代码调用 onSayHelloEnd callback
NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback begin\r\n");
napitest::napitest_interface::NodeISayHello::listener_.NodeISayHelloListener_onSayHelloEndCallback(info2);
NAPITEST_LOGI("NAPITEST_LOGI NodeISayHelloListener_onSayHelloEndCallback end\r\n");
return;
}
// 调用register注册的回调
void NodeISayHello::sayHi(std::string& from, std::string& to, uint32_t& sayType)
{
// 1.打印
NAPITEST_LOGI("NAPITEST_LOGI sayHi from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHi to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHi sayType = %d\r\n", sayType);
// 2.调用回调
napitest::napitest_interface::NodeISayHello *ptr = new napitest::napitest_interface::NodeISayHello();
uint32_t callbackNum = 50;
ptr->CallbackfuncCallback(callbackNum);
delete ptr;
return;
}
// 普通函数调用返回str
std::string funcTest(bool& v)
{
if (v) {
return "ret is true";
} else {
return "ret is false";
}
}
// 1.打印值from, to 以及枚举enum SayType的值
// 2. 将回调值0 "", "recv hello."的值传回Js层
void NodeISayHello::sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType)
{
// 1.打印
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse from = %s\r\n", from.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse to = %s\r\n", to.c_str());
NAPITEST_LOGI("NAPITEST_LOGI sayHelloWithResponse sayType = %d\r\n", sayType);
// 2.调用promise回调 (0, "", "recv hello.")
napitest::napitest_interface::NodeISayHello *p = new napitest::napitest_interface::NodeISayHello();
// 调用工具接口将回调传回工具
p->auto_interface_5SetCbValue(0, "", "recv hello.");
delete p;
return;
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef IMPL_SAYHELLO_H
#define IMPL_SAYHELLO_H
#include <string>
#include <memory>
namespace napitest {
class NodeISayHello;
class NodeISayHello {
public:
void sayHello(std::string& from, std::string& to, uint32_t& sayType);
void sayHi(std::string& from, std::string& to, uint32_t& sayType);
void sayHelloWithResponse(std::string& from, std::string& to, uint32_t& sayType);
};
std::string funcTest(bool& v);
}
#endif // IMPL_SAYHELLO_H