123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- module(...,package.seeall)
- function table_leng(t)
- local leng=0
- for k, v in pairs(t) do
- leng=leng+1
- end
- return leng;
- end
- local DIR_PATH = {
- Users = "/sdcard0/common",
- Wrench = "/sdcard0/common",
- WorkingParts = "/sdcard0/common",
- WorkingPosition = "/sdcard0/common",
- WorkPlan = "/sdcard0/common",
- File = "/sdcard0/log",
- Wind = "/sdcard0/common"
- }
- local ALLOWCUSTOM_FILE = {
- Fan = 1,
- WorkingParts = 1,
- WorkingPosition = 1
- }
- -- 根据操作写文件
- function operateFs(op, cntType, cntVersion, content )
- -- body
- local opRes, count, desc = "",fileRes
- if op == "add" then
- -- body
- if DIR_PATH[cntType] then
- -- body
- if ALLOWCUSTOM_FILE[cntType] then
- -- body
- delBaseInfo(cntType, 0, DIR_PATH[cntType])
- end
- opRes, desc = saveBaseInfo(cntType, content, DIR_PATH[cntType] )
- else
- if ALLOWCUSTOM_FILE[cntType] then
- -- body
- delBaseInfo(cntType, 0, "/sdcard0/"..content.wnum)
- end
- if cntType == "WorkRecord" then
- -- body
- opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
- else
- opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
- end
- end
- end
- if op == "update" then
- -- body
- if DIR_PATH[cntType] then
- -- body
- opRes,count, desc = updateBaseInfo(cntType, content, DIR_PATH[cntType] )
- if not opRes or desc:sub(1, 29) == "this data not found where id=" then
- -- body
- opRes, desc = saveBaseInfo(cntType, content, DIR_PATH[cntType] )
- end
- else
- if cntType == "WorkRecord" then
- -- body
- opRes,count, desc = updateBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
- else
- opRes,count, desc = updateBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
- end
- if not opRes or desc:sub(1, 29) == "this data not found where id=" then
- -- body
- if cntType == "WorkRecord" then
- -- body
- opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
- else
- opRes, desc = saveBaseInfo(cntType, content, "/sdcard0/"..content.wnum )
- end
- end
- end
- end
- if op == "delete" then
- -- body
- if DIR_PATH[cntType] then
- -- body
- opRes,count, desc = delBaseInfo(cntType, content.id, DIR_PATH[cntType] )
- else
- if cntType == "WorkRecord" then
- -- body
- opRes,count, desc = delBaseInfo(cntType, content.id, "/sdcard0/"..content.wnum .. "/" .. content.fnum)
- else
- opRes,count, desc = delBaseInfo(cntType, content.id, "/sdcard0/"..content.wnum )
- end
- end
- end
- if op == "directive" then
- if cntType == "File" then
- -- body
- if content.file_name == "/sdcard0/log/0" then
- -- body
- opRes = logModuel.readfile(nil)
- else
- opRes = logModuel.readfile(content.file_name)
- end
- end
- if cntType == "SysCMD" then
- -- body
- if content.cmd_content == "Reboot" then
- -- body
- opRes = true
- sys.restart("Sever send reboot CMD! ")
- end
- if content.cmd_content == "ResetUploadCache" then
- -- body
- rtos.remove_dir("/sdcard0/common/temp")
- sys.restart("Server send ResetUploadCache CMD! ")
- end
- if content.cmd_content == "FormatSD" then
- -- body
- local fg = io.format(io.SDCARD)
- log.info("format res", fg)
- if fg == 1 then
- -- body
- nvm.set("localCntVersion", "0")
- sys.restart("Server send Format SDcard CMD! ")
- else
- opRes = false
- desc = "格式化SDcard失败"
- end
-
- end
- end
- if cntType == "Config" then
- -- body
- for k,v in pairs(content) do
- nvm.set(k, v)
- end
- opRes = true
- sys.restart("Sever send config update! ")
- end
-
- end
- local localVersion = nvm.get("localCntVersion")
- if cntVersion > localVersion then
- -- body
- local dateObj = os.date("*t")
- local updateTime = dateObj.year .. dateObj.month .. dateObj.day .. dateObj.hour .. dateObj.min .. dateObj.sec
- nvm.set("localCntVersion", cntVersion)
- nvm.set("localCntUpdateTime", updateTime)
- log.info("nvm set success, value:", cntVersion)
- end
- -- log.info("optype:",op,"cntType:", cntType, "opRes:",opRes, "count:",count, "desc:",desc)
- if not opRes then
- -- body
- if not desc then
- -- body
- desc = ""
- end
- logModuel.debug_log("mqtt_recieve operate fs failed! desc:"..desc..",optype:"..op..",cntVersion:"..cntVersion)
- end
- return opRes,desc
- end
- --- MQTT客户端数据接收处理
- -- @param mqttClient,MQTT客户端对象
- -- @return 处理成功返回true,处理出错返回false
- -- @usage mqttInMsg.proc(mqttClient)
- function proc(mqttClient)
- local result,data,param
- while true do
- result, data, param = mqttClient:receive(30000,"LOCAL_PUB_MSG")
- --接收到数据
- -- opType = "update"时,若不存在,则 add
- if result then
- local resData = json.decode(data.payload)
-
- log.info("origin mqttrec data:",data.payload)
- if resData.msgid and resData.success then
- -- body
- log.info("three topic msgid:",resData.msgid, "type:", type(resData.msgid))
- sys.publish("WAIT_UPDATA_RESP_SUCCESS", resData.msgid )
- break
- end
- local ack = {
- Imei = misc.getImei(),
- CntVersion = "",
- Succ = "",
- Extra = {}
- }
- --{"Extra":{},"Succ":"1","Imei":"863488052331447","CntVersion":"1659751519989"}
-
- local extrTable = {}
- local hasFile = false
- for k,v in pairs(resData) do
- local opRes, desc = operateFs(v.OpType, v.CntType, v.CntVersion, v.Content)
- if not opRes then
- -- body
- ack.Succ = "-1"
- table.insert(extrTable, "error_version:"..v.CntVersion..",OpType:"..v.OpType..",CntType:"..v.CntType..",desc:"..desc)
- end
- if v.OpType =="directive" and v.CntType == "File" then
- -- body
- table.insert(extrTable, "file_name:"..(v.Content.file_name or "unknown_file" )..",content:"..opRes)
- hasFile = true
- end
- end
- if ack.Succ == "" then
- -- body
- ack.Succ = "1"
- end
- ack.CntVersion = nvm.get("localCntVersion")
- ack.Extra = extrTable
- local mqttSendContent = ""
- if hasFile then
- -- body
- mqttSendContent = '{"Imei":' .. misc.getImei() ..',' .. '"CntVersion":'.. ack.CntVersion .. ',' .. '"Succ":'.. ack.Succ .. ','.. '"Extra":'.. table.concat(extrTable) .. '}'
- else
- mqttSendContent = json.encode(ack)
- end
- -- log.info("json temple:", att)
- -- mqttClient:publish("SHEGCL/IntelligenTool/Ack", json.encode(ack))
- mqttClient:publish("SHEGCL/IntelligenTool/Ack", mqttSendContent)
- elseif data == "LOCAL_PUB_MSG" then
- if type(param) == "string" then
- -- body
- param = json.decode(param)
- if param.topic and param.data then
- -- body
- -- if param.topic == "SHEGCL/IntelligenTool/UploadWorkRecordMA" or param.topic == "SHEGCL/IntelligenTool/UploadWorkRecordFA" or param.topic == "SHEGCL/IntelligenTool/Heart" then
- -- -- body
- -- upData.msgid = devTool.guid()
- -- end
- local upData = param.data
- upData.PublishVersion = nvm.get("localCntVersion")
- upData.IMEI = misc.getImei()
- mqttClient:publish(param.topic, json.encode(upData))
- end
- else
- log.error("mqtt_recieve.proc", "param type invalid!! param:",param)
- end
- elseif data == "timeout" then
- log.info("mqtt_recieve.proc", "mqtt wait receive timeout!")
- else
- break
- end
- end
-
- return result or data=="timeout" or data=="LOCAL_PUB_MSG"
- end
- --定时上传因网络问题未上传的数据
- function uploadOfflineData( )
- sys.wait(5000)
- -- body
- while true do
- if socket.isReady() then
- -- body
- local uploadData, cn, des = getBaseInfo_ununique("wait2upload", nil, "/sdcard0/common/temp")
- if not uploadData then
- -- body
- log.info("wait2upload file is empty!", "count:", cn, "des:", des)
- else
- for k,v in pairs(uploadData) do
- sys.publish("LOCAL_PUB_MSG", json.encode(v) )
- local uploadRes,msgid = sys.waitUntil("WAIT_UPDATA_RESP_SUCCESS", 3000)
- -- log.info("msgid:", msgid, "v.data.msgid:", v.data.msgid)
- if uploadRes and msgid then
- local re,be,ce = delBaseInfo("wait2upload",v.data.msgid,"/sdcard0/common/temp")
- -- log.info("re:",re,"be:",be, "ce:",ce)
- end
- sys.wait(500)
- end
- end
- end
- sys.wait(10000)
- end
- end
- sys.taskInit(uploadOfflineData)
|