AX多平台上报

mqttObj1 = mqtt.new()
mqtt.config(mqttObj1,"","server1.mixiot.top","1883","user","pwd")  -- 上报平台1

aprus.lua模板代码默认连一个平台 server1.mixiot.top, 如需向 server2.mixiot.top 上报,新增如下配置:

mqttObj2 = mqtt.new()
mqtt.config(mqttObj2,"","server2.mixiot.top","1883","user","pwd")  -- 上报平台2

相当于复制了一个 mqttObj1, 整个aprus.lua中有mqttObj1的语句复制并改为mqttObj2

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

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(mqttObj1, dltstr[session], "r", json)
        mqtt.publish(mqttObj2, dltstr[session], "r", json)
        dltbuf[session] = {}
    elseif code > 10 then
        dltbuf[session][style_L] = val_L
        dltbuf[session][style_E] = val_E
    else 
        local json = cjson.encode({[style_L] = val_L, [style_E] = val_E, ["Z"] = z})
        mqtt.publish(mqttObj1, dltstr[session], "r", json)
        mqtt.publish(mqttObj2, dltstr[session], "r", json)
    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()
文档更新时间: 2024-08-29 10:29   作者:技术支持