lua 固件更新部分 利用lua 虚拟机加载

module(...,package.seeall)

require"http"
require"misc"
require"mqtt"
require"mqttOutMsg"
require"mqttInMsg"


local torigin =
{
    SERVERIP = "182.92.223.22",
    PORT = "1883",
    DEVICEID = "7735d490-4231-11ec-8470-1bf471b2fd10",
    TOKEN = "pWIUd6QEfKl0YUqIiDFj",
}

local function writevalw(filename,value)
    local filehandle = io.open(filename,"w")
    if filehandle then
        filehandle:write(value)
        filehandle:close()
    else
        print("文件不存在或文件输入格式不正确")
    end
end
local function deletefile(filename)
    os.remove(filename)
end

newfenv = {                    
    print = function( ... )
        print(arg[1].."test")
    end
}               

--读取
local function run_file()
    local line =io.readFile("/user_main.lua")
    xpcall(function()
        local f = loadstring(line)
        setfenv(f, newfenv)  
        f()
    end,
    function() 
    print(debug.traceback())
    end
    )
end

local function cbFnc(result,prompt,head,body)
    log.info("testHttp.cbFnc",result,prompt)
    if result and head then
        for k,v in pairs(head) do
            log.info("testHttp.cbFnc",k..": "..v)
        end
    end
    if result and body then
        log.info("testHttp.cbFnc","bodyLen="..body:len())
        log.info("testHttp.cbFnc",body)
        writevalw("/user_main.lua",body)
        sys.wait(1000)
        sys.restart("下载成功软件重启")
    end
end


sys.taskInit(function()
    local retryConnectCnt = 0
    while not socket.isReady() do
            retryConnectCnt=retryConnectCnt+1
            sys.wait(1000)
            log.info("重连次数",retryConnectCnt)
            if retryConnectCnt>20 then sys.restart("程序超时软件重启") end
    end
    log.info("连接成功")
    local jsondata = json.encode(torigin)
    writevalw("/user_config.txt",jsondata)
    log.info("更新配置")
    setmetatable(newfenv,{__index = _G}) 
    run_file()
end)



-- 测试MQTT的任务代码
sys.taskInit(function()
        while not socket.isReady() do sys.wait(1000) end
        log.info("连接上网络")
        sys.wait(2000)
        local mqttc = mqtt.client(misc.getImei(),300,torigin["TOKEN"])
        log.info("连接上服务器ing")
        while not mqttc:connect(torigin['SERVERIP'], 1883) do sys.wait(2000) end
        log.info("连接上服务器")
        if mqttc:subscribe("v1/devices/me/rpc/request/+") then log.info("订阅成功") end
        local msg='{"firmware_type":"jd-iot","firmware_version": "68"}'
        log.info("sendMsg",msg)
        mqttc:publish("v1/devices/me/attributes",msg)
        while true do
            result,data = mqttc:receive(5000,"APP_SOCKET_SEND_DATA")
            if result then
                log.info("mqttInMsg.topic",data.topic)
                log.info("mqttInMsg.payload",data.payload)
                local cfgdata,result,errinfo = json.decode(data.payload)
                url=string.gsub(cfgdata['params']['pathWay'],"https://","")
                log.info("mqttInMsg.url",url)
                http.request("GET",url,nil,nil,nil,nil,cbFnc)
            end
        end
        mqttc:disconnect()
end)

标签: none

添加新评论