示例-AX多平台选择key上报
lua示例
aprus.lua 参考
package.cpath="./?.so"
package.path="./?.lua"
cjson = require "cjson"
config = require "config"
function act_control(m, json)
for k,v in pairs(json) do
if k ~= "Act" then
dlt645.write(dlt1, k, v)
end
end
end
function mqttdata_handle(m, topic, data)
local json = cjson.decode(data)
if json.Act == "Control" then
act_control(m, json)
end
end
function mqttsys_handle(m, code)
if code == 0 then
dlt645.stop(dlt1)
elseif code == 1 then
dlt645.run(dlt1)
end
end
local Server2_Pub_Keys = {"key1", "key2"}
local Server2_Data = {}
-- 判断val是否在arr里
function isInArray(arr, key)
for _, v in ipairs(arr) do
if v == key then
return true
end
end
return false
end
function dlt645event_handle(session, code, style_L, val_L, style_E, val_E, z)
if code == 15 then -- 周期上报
local json = cjson.encode(dltbuf[session])
mqtt.publish(m1, dltstr[session], "r", json)
dltbuf[session] = {}
local temp = cjson.encode(Server2_Data)
mqtt.publish(m2, dltstr[session], "r", temp)
Server2_Data = {}
elseif code > 10 then
dltbuf[session][style_L] = val_L
dltbuf[session][style_E] = val_E
if isInArray(Server2_Pub_Keys,style_L) then
Server2_Data[style_L] = val_L
end
else -- 变化上报
local json = cjson.encode({[style_L] = val_L, [style_E] = val_E, ["Z"] = z})
mqtt.publish(m1, dltstr[session], "r", json)
if isInArray(Server2_Pub_Keys,style_L) then
local temp = cjson.encode({[style_L] = val_L})
mqtt.publish(m2, dltstr[session], "r", temp)
end
end
end
function dlt645_load_collectnodes(session, nodes)
for k,v in pairs(nodes)
do
dlt645.add_collectnode(session, v.addr, v.dflags)
end
end
function dlt645_load_varnodes(session, nodes)
for k,v in pairs(nodes)
do
dlt645.add_varnode(session, v.addr, v.dflags, v.author, v.pwd, v.dtype, v.format, v.pMode, v.dStyle, v.dOffset, v.dExt)
end
end
function init()
mqttObj1 = mqtt.new()
mqtt.config(mqttObj1,"","server1.mixiot.top","1883","user","pwd") -- 上报平台1
mqttObj2 = mqtt.new()
mqtt.config(mqttObj2,"","server2.mixiot.top","1883","user","pwd") -- 上报平台2
user.setluaver(config.AprusX.luaver)
user.setdevinfo(config.AprusX.devinfo)
user.ipconfig(config.AprusX.ipmode, config.AprusX.inet_addr, config.AprusX.netmask)
end
function start()
init()
----------DLT645配置----------
DLTCfg = config.DLT645
--初始化变量
dlt1 = nil
dltbuf = { dlt1 }
dltstr = { dlt1 }
--创建配置实例
dlt1 = dlt645.new()
dltbuf[dlt1] = {}
dltstr[dlt1] = "dlt1"
dlt645.config(dlt1, DLTCfg.interface.rate, DLTCfg.interface.databit, DLTCfg.interface.stopbit, DLTCfg.interface.parity)
dlt645_load_collectnodes(dlt1, DLTCfg.node)
dlt645_load_varnodes(dlt1, DLTCfg.node)
----------------------------------
mqtt.run(mqttObj1)
mqtt.run(mqttObj2)
while true do
local r = user.waitmsg()
if r.from == "mqtt-sys" then
mqttsys_handle(r.session, r.code)
elseif r.from == "mqtt-msg" then
mqttdata_handle(r.session, r.topic, r.payload)
elseif r.from == "dlt645" then
dlt645event_handle(r.session, r.code, r.style_L, r.val_L, r.style_E, r.val_E, r.z)
end
end
end
start()
参考上述代码,适配器向 server2.mixiot.top
上报数据只会发 “key1”, “key2” 采集点
文档更新时间: 2024-08-29 10:29 作者:技术支持