//规则名称为rule1,规则备注,执行优先级
rule "rule1" "rule-describtion" salience 1
begin
//过滤主题为aaa的消息才进行处理
if Message.Topic == "a/b/c" {
//反序列化消息中的载体
payload = Core.FromJson(Message.Payload)
//判断消息载体是否为空
if isNil(payload) {
println("parse error")
return
}
//实例化一个map用于转换消息体格式
res = Core.MakeMap()
//把原消息的msg内容付值到新消息体的m键中
res["m"] = payload["msg"]
//序列化新的消息体为json
reMsg = Core.ToJson(res)
//传送序列化是否失败
if isNil(reMsg) {
println("format error")
return
}
//打印序列化结果于服务端
println(Core.String(reMsg))
//实例化mqtt消息体
newMsg = Core.MakeMessage()
//设置新的接收主题
newMsg.Topic = "bbb/#"
//消息质量控制
newMsg.QOS = 0
newMsg.Retain = false
//把新的载体付值到新消息载体中
newMsg.Payload = reMsg
//通过内核发布新消息
err = Core.Publish(newMsg)
if err != "" {
println(err)
}
}
end
接口身份证验证方式需调用/api/login后取到token后放到http的header中,如下是消息格式转义后转发到另一个主题的示例:
curl --location --request POST 'http://localhost:8081/v1/rules' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJDUDciLCJleHAiOjE2MjE1NzcxMjEsImp0aSI6ImFkbWluIiwiaWF0IjoxNjIxNTY5OTIxLCJpc3MiOiJjb29scHkubmV0IiwibmJmIjoxNjIxNTY5OTIxfQ.cE1YusJXYQbrU2Pa9gl6gqqsf93rkHB4ePNiXuDSdeg' \
--header 'Content-Type: text/plain' \
--data-raw '//规则名称为rule2,规则备注,执行优先级
rule "rule2" "rule-describtion" salience 1
begin
//过滤主题为aaa的消息才进行处理
if Message.Topic == "aaa" {
//反序列化消息中的载体
payload = Core.FromJson(Message.Payload)
//判断消息载体是否为空
if isNil(payload) {
println("parse error")
return
}
//实例化一个map用于转换消息体格式
res = Core.MakeMap()
//把原消息的msg内容付值到新消息体的m键中
res["m"] = payload["msg"]
//序列化新的消息体为json
reMsg = Core.ToJson(res)
//传送序列化是否失败
if isNil(reMsg) {
println("format error")
return
}
//打印序列化结果于服务端
println(Core.String(reMsg))
//实例化mqtt消息体
newMsg = Core.MakeMessage()
//设置新的接收主题
newMsg.Topic = "bbb/#"
//消息质量控制
newMsg.QOS = 0
newMsg.Retain = false
//把新的载体付值到新消息载体中
newMsg.Payload = reMsg
//通过内核发布新消息
err = Core.Publish(newMsg)
if err != "" {
println(err)
}
}
end'
如下是消息转义后以提交到第三方http服务接口 curl示例
curl --location --request POST 'http://localhost:8081/v1/rules' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJDUDciLCJleHAiOjE2MjE1NzcxMjEsImp0aSI6ImFkbWluIiwiaWF0IjoxNjIxNTY5OTIxLCJpc3MiOiJjb29scHkubmV0IiwibmJmIjoxNjIxNTY5OTIxfQ.cE1YusJXYQbrU2Pa9gl6gqqsf93rkHB4ePNiXuDSdeg' \
--header 'Content-Type: text/plain' \
--data-raw 'rule "rule1" "rule-describtion" salience 10
begin
//过滤主题为aaa的消息才进行处理
if Message.Topic == "aaa" {
//反序列化消息中的载体
payload = Core.FromJson(Message.Payload)
//判断消息载体是否为空
if isNil(payload) {
println("parse error")
return
}
//实例化一个map用于转换消息体格式
res = Core.MakeMap()
//把原消息的msg内容付值到新消息体的m键中
res["m"] = payload["msg"]
//序列化新的消息体为json
reMsg = Core.ToJson(res)
//传送序列化是否失败
if isNil(reMsg) {
println("format error")
return
}
//打印序列化结果于服务端
println(Core.String(reMsg))
//通过http转发消息
//实例化一个map用于转换消息体格式
header = Core.MakeMap()
header["Authorization"] = "Bearer " + "token"
err = Core.Http("http://localhost:8081/v1/test","POST",header,reMsg,2)
if err != "" {
println("format error")
}
}
end'
{
"success": true,
"data": {
"rule_desc": "rule-describtion",
"rule_name": "rule2",
"salience": 1
}
}
curl --location --request DELETE 'http://localhost:8081/v1/rules/rule1' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJDUDciLCJleHAiOjE2MjIwMzA5OTUsImp0aSI6ImFkbWluIiwiaWF0IjoxNjIyMDIzNzk1LCJpc3MiOiJjb29scHkubmV0IiwibmJmIjoxNjIyMDIzNzk1fQ.XeNnwyWWFR-Rz2hByigNQVvTvTuXR7nzFcl7O6fqleQ'
{
"success": true,
"data": "rule1"
}
curl --location --request GET 'http://localhost:8081/v1/rules?limit=100' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJDUDciLCJleHAiOjE2MjIwMzA5OTUsImp0aSI6ImFkbWluIiwiaWF0IjoxNjIyMDIzNzk1LCJpc3MiOiJjb29scHkubmV0IiwibmJmIjoxNjIyMDIzNzk1fQ.XeNnwyWWFR-Rz2hByigNQVvTvTuXR7nzFcl7O6fqleQ'
{
"success": true,
"data": {
"current": 0,
"list": [
"rule2"
],
"pageSize": 100,
"total": 1
}
}