-- pm.wake("sdcard") local comSize = 4 --每个公共目录下文件大小 单位 kb -- 挂载SD卡,返回值0表示失败,1表示成功 -- local sdcard = io.mount(io.SDCARD) -- log.info('mount sd card', sdcard) local sdCardTotalSize = rtos.get_fs_total_size(1, 1) log.info("sd card total size " .. sdCardTotalSize .. " KB") local delimiter = '\0' if io.opendir("/sdcard0/common") then for i=1,100 do local fType,fName,fSize = io.readdir() log.info("fname",fName) if fType==32 then -- log.info("sd card file",fName,fSize) elseif fType == nil then break end end io.closedir("/sdcard0/commmon") end -- 删除目录 -- local rmfile = rtos.remove_dir('/sdcard0/common') -- log.info('delete', rmfile) -- rtos.make_dir('/sdcard0/log') -- debug_log('写入测试日志') -- local del = os.remove(comDir .. '/user') -- log.info('del user file', del) -- local readval = readfile() -- 参数:完整路径文件名 不传或者传'/sdcard0/log/0'都是获取最新日志文件 -- print(readval) --获取table长度 function table_leng(t) local leng=0 for k, v in pairs(t) do leng=leng+1 end return leng; end -- 分割字符串 ---@param str string 元字符串 ---@param seq string 分割字符 ---@return table function split(str, seq) local nFindStartIndex = 1 local nSplitIndex = 1 local nSplitArray = {} while true do local nFindLastIndex = string.find(str, seq, nFindStartIndex) if not nFindLastIndex then nSplitArray[nSplitIndex] = string.sub(str, nFindStartIndex, string.len(str)) break end nSplitArray[nSplitIndex] = string.sub(str, nFindStartIndex, nFindLastIndex - 1) nFindStartIndex = nFindLastIndex + string.len(seq) nSplitIndex = nSplitIndex + 1 end return nSplitArray end --数组去重 function table.unique(t, bArray, mainKey) local check = {} local n = {} local idx = 1 for k, v in pairs(t) do local judgeKey = v if mainKey then judgeKey = v[mainKey] or v end if not check[judgeKey] then if bArray then n[idx] = v idx = idx + 1 else n[k] = v end check[judgeKey] = true end end return n end --循环创建文件夹 function createAllFolder(path) -- print(path) local path_tb={} local new_path="" -- 分割路径保存到table for i,s in pairs(split(path,'/')) do if s~=nil then table.insert(path_tb,s) end end -- print(json.encode(path_tb)) local cdir = false --创建结果 -- 遍历并拼接路径检测是否存在,不存在则新建 for k,v in ipairs(path_tb) do if v ~= '' then if k==1 then new_path=v else new_path=new_path.."/"..v end -- print(new_path,io.opendir(new_path)) local opflag = io.opendir(new_path) if opflag ~= 1 then cdir = rtos.make_dir(new_path) log.info("path:", new_path, "flag:",cdir) end end end io.closedir() return cdir end -- 打开sd卡公共目录,不存在则创建 获取最新文件, function getComLastFile(dirPath,filename) local lastfile = filename --返回最新文件 -- log.info('func:getComLastFile open dir', io.opendir(dirPath)) if io.opendir(dirPath) ~= 0 then local file = {} -- 该目录所有文件名 for i = 1, 999 do local fType, fName, fSize = io.readdir() -- log.info("file or dir",fType, fName, fSize) if fType == 32 then -- log.info("func getComLastFile common file", fName, fSize) local tname = split(fName,'-') -- log.debug('func:getComLastFile tname',tname,tname[1],tname[2]) if tname[1] == filename then if tname[2] then table.insert(file,tname[2]) end end -- local c = io.readFile(dirPath..'/'..fName) -- log.info('文件内容',c) elseif fType == nil then break end end -- log.error('files',json.encode(file)) --获取文件名后缀最大值 local max = nil -- 最新的文件 即数字名称值最大的文件 for i, v in pairs(file) do v = tonumber(v) --数组中是string类型 比较会有问题 if max == nil then max = v end if max < v then max = v end end -- print(max) if max then -- 判断该最新文件大小 local cnt = io.fileSize(dirPath .. '/' .. filename..'-'..max) if cnt then if cnt >= (comSize * 1024) then -- 文件大于设置的文件大小 新建新文件 max = max + 1 end lastfile = filename..'-'..max end else -- 判断该最新文件大小 local cnt = io.fileSize(dirPath .. '/' .. filename) if cnt then if cnt >= (comSize * 1024) then -- 文件大于设置的文件大小 新建新文件 lastfile = filename..'-1' end end end -- print(lastfile) log.debug('func:getComLastFile lastfile',lastfile) io.closedir() return lastfile else local cdir = createAllFolder(dirPath) if cdir then log.info('创建公共目录成功', dirPath) else log.error('创建公共目录失败',dirPath) end return lastfile end end --根据某个目录下文件名获取该类型所有文件 返回table function getAllFiles( dir, file ) local fs = {} if not file then return false,'fun:getAllFiles,desc:file not found' end log.error('open dir',io.opendir(dir)) if io.opendir(dir) ~= 0 then for i = 1, 999 do local fType, fName, fSize = io.readdir() -- log.info("file or dir",fType, fName, fSize) if fType == 32 then local tname = split(fName,'-') -- log.debug('func:getAllFiles tname',tname,tname[1],tname[2]) if tname[1] == file then table.insert(fs,fName) end elseif fType == nil then break end end else io.closedir() return false,'fun:getAllFiles,desc:dir not found' end io.closedir() return fs,'fun:getAllFiles,desc:get all files is ok' end -- 保存基础信息所有扳手信息 -- 文件 所有扳手信息 查询条件扳手编号 wrench -- 文件 所有风场信息 wind -- 文件 部件列表 unit -- 文件 用户信息 user 查询条件用户名+密码 -- 文件 任务计划 task 查询条件任务编号 -- 文件 工作位置列表 workspace 查询条件风机型号+部件号 -- 文件 待上传信息 -- 文件 配置信息 比如最后一次使用时间 用于存储空间不够时删除 --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 保存数据 table类型 function saveBaseInfo( filename,data,dirPath) -- print(filename) if not filename then log.error('savebaseinfo','filename not fount') return false,'filename not fount' end if not data then log.error('savebaseinfo','data not fount') return false,'data not fount' end if not dirPath then log.error('savebaseinfo','dirPath not fount') return false,'dirPath not fount' end --最新文件名称 local filename = getComLastFile(dirPath,filename) log.debug('func:saveBaseInfo last file',filename) local path = dirPath..'/'..filename log.info('save path',path) data = json.encode(data)..delimiter local c = io.writeFile(path,data, "a+b") if c then log.info('write result', c) return true else log.error('error', '基础信息写入失败') return false,'基础信息写入失败' end end -- 保存基础信息 -- local wrench = {id=1,name='测试设备'} -- local res,desc= saveBaseInfo('wrench',wrench) -- log.debug('save result',res,desc) -- 模拟数据 -- for i=113,150 do -- local user = {id=i,name='小亮'..i,pwd='49BA59ABBE56E057',time=os.time()} --password md5加密 16位大写 123456 -- local res,desc= saveBaseInfo('user',user,'/sdcard0/wind/12345') -- log.debug('save result',res,desc) -- break -- end --查询设备信息 --参数[filename] 文件名称 如wrench、wind、unit。。。[param] 保存数据 table类型 nil查询所有数据 --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述 function getBaseInfo( filename,param,dirPath) local selectRes = {} --查询结果 local count = 0 --查询文件个数 if not filename then log.error('func:getBaseInfo','filename not fount') return false,count,'filename not fount' end if not dirPath then log.error('func:getBaseInfo','dirPath not fount') return false,count,'dirPath not fount' end --获取所有该文件及副本文件 local files,desc = getAllFiles(dirPath,filename) local filevals = nil --文件内容 if not files then return false,count,desc end local succ_cnt = 0; --比较成功个数 for i,file in pairs(files) do local isbreak = false local path = dirPath..'/'..file local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开 if not filehandle then -- 判断文件是否存在 isbreak = true end fileval = filehandle:read("*all") -- 读出文件内容 -- print(fileval) if not fileval then isbreak = true end local filevaltable = split(fileval,delimiter) local table_length = table_leng(filevaltable) if table_length <= 1 then isbreak = true end -- log.info("isbreak:", isbreak, table_length) if not isbreak then -- body for a = 1,table_length-1 do local val = json.decode(filevaltable[a]) if param then --有查询参数 local p_length = table_leng(param) --条件长度 for p,s in pairs(param) do if filename == "Users" then if p == 'pwd' then s = string.sub(string.upper(crypto.md5(s,#s,16)),9,24) --密码加密 加密结果32位 取16位(第9-24)字符 end end -- log.info("val[p] ", val[p] , "s", s, succ_cnt) if val[p] == s then succ_cnt = succ_cnt + 1 else break end end if p_length == succ_cnt then --找到结果 停止继续查找下个文件 selectRes[i] = val isbreak = true break end else --无查询参数 查询所有 selectRes[i] = val end end end count = i -- if isbreak then --找到结果 停止继续查找下个文件 -- break; -- end filehandle:close() -- 关闭文件 end -- print(table_leng(selectRes),json.encode(selectRes)) if table_leng(selectRes) > 0 then --去重 local result = table.unique(selectRes,true,'id') return result,count,'select success' else return false,count,'this data is not found' end end -- 查询文件内容复数 function getBaseInfo_ununique( filename,param,dirPath) local selectRes = {} --查询结果 local count = 0 --查询文件个数 if not filename then log.error('func:getBaseInfo','filename not fount') return false,count,'filename not fount' end if not dirPath then log.error('func:getBaseInfo','dirPath not fount') return false,count,'dirPath not fount' end --获取所有该文件及副本文件 local files,desc = getAllFiles(dirPath,filename) local filevals = nil --文件内容 if not files then return false,count,desc end local succ_cnt = 0; --比较成功个数 for i,file in pairs(files) do local isbreak = false local path = dirPath..'/'..file local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开 if not filehandle then -- 判断文件是否存在 isbreak = true end fileval = filehandle:read("*all") -- 读出文件内容 -- print(fileval) if not fileval then isbreak = true end local filevaltable = split(fileval,delimiter) local table_length = table_leng(filevaltable) if table_length <= 1 then isbreak = true end if not isbreak then -- body for a = 1,table_length-1 do local val = json.decode(filevaltable[a]) succ_cnt = 0 if param then --有查询参数 local p_length = table_leng(param) --条件长度 for p,s in pairs(param) do if val[p] == s then -- log.info("val[p]:", val[p], "s:",s) succ_cnt = succ_cnt + 1 else break end end if p_length == succ_cnt then --找到结果 停止继续查找下个文件 table.insert(selectRes, val) end else --无查询参数 查询所有 table.insert(selectRes, val) end end end count = i -- if isbreak then --找到结果 停止继续查找下个文件 -- break; -- end filehandle:close() -- 关闭文件 end -- print(table_leng(selectRes),json.encode(selectRes)) if table_leng(selectRes) > 0 then --去重 local result = table.unique(selectRes,true,'id') return result,count,'select success' else return false,count,'this data is not found' end end -- 查询文件内容复数_模糊查询 function getBaseInfo_ununique_dim( filename,param,dirPath) local selectRes = {} --查询结果 local count = 0 --查询文件个数 if not filename then log.error('func:getBaseInfo','filename not fount') return false,count,'filename not fount' end if not dirPath then log.error('func:getBaseInfo','dirPath not fount') return false,count,'dirPath not fount' end --获取所有该文件及副本文件 local files,desc = getAllFiles(dirPath,filename) local filevals = nil --文件内容 if not files then return false,count,desc end local succ_cnt = 0; --比较成功个数 for i,file in pairs(files) do local isbreak = false local path = dirPath..'/'..file local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开 if not filehandle then -- 判断文件是否存在 isbreak = true -- isbreak = true end fileval = filehandle:read("*all") -- 读出文件内容 -- print(fileval) if not fileval then isbreak = true -- isbreak = true end local filevaltable = split(fileval,delimiter) local table_length = table_leng(filevaltable) if table_length <= 1 then isbreak = true -- isbreak = true end if not isbreak then -- body for a = 1,table_length-1 do local val = json.decode(filevaltable[a]) if param then --有查询参数 local p_length = table_leng(param) --条件长度 local pattern = "" for p,s in pairs(param) do if succ_cnt > 49 then -- body break end -- log.info("val[p]:", val[p], "s:",s) local wholeStr = val[p] if type(val[p]) ~= "userdata" then -- body pattern = string.gsub(s, "[%[%]&=+%%%c%(%)%-%/%+]", function(c) return "%"..c end) local a,b = string.find(wholeStr,pattern) local transMatchres = string.match(wholeStr,pattern) if a and (b-a+1) == #transMatchres then succ_cnt = succ_cnt + 1 table.insert(selectRes, val) end end end -- if p_length == succ_cnt then --找到结果 停止继续查找下个文件 -- table.insert(selectRes, val) -- end else --无查询参数 查询所有 if succ_cnt > 49 then -- body else if filename == "Wind" then -- body local fieldDisplayRadius = nvm.get("fieldDisplayRadius") or 200 local gpsLng, gpsLat = nvm.get("gpsLng"), nvm.get("gpsLat") local dist = devTool.getDistance( val.lat, val.lon, gpsLat, gpsLng) -- log.info("dist:",dist, json.encode(v)) if dist < fieldDisplayRadius then -- body -- log.info("cicle",v.lat) val.dist = dist table.insert(selectRes, val) succ_cnt = succ_cnt+1 end else table.insert(selectRes, val) succ_cnt = succ_cnt+1 end end end end end count = i -- if isbreak then --找到结果 停止继续查找下个文件 -- break; -- end filehandle:close() -- 关闭文件 end -- print(table_leng(selectRes),json.encode(selectRes)) if table_leng(selectRes) > 0 then --去重 local result = table.unique(selectRes,true,'id') return result,count,'select success' else return false,count,'this data is not found' end end -- local res,cnt,desc = getBaseInfo('user',{pwd="123456",name="小亮113"},'/sdcard0/wind/12345') -- log.info('select result',res,cnt,desc) -- for i,j in ipairs(res) do -- for m,n in pairs(j) do -- print(m,n) -- end -- end --删除基本信息 --参数[filename] 文件名称 如wrench、wind、unit。。。[id] 删除数据id function delBaseInfo(filename,id,dirPath) local res = false --删除结果 local count = 0 --查询文件个数 log.info("del process:",filename,id,dirPath) if not filename then log.error('func:delBaseInfo','filename not fount') return res,count,'filename not fount' end if not id then log.error('func:delBaseInfo','id not fount') return res,count,'id not fount' end if not dirPath then log.error('func:delBaseInfo','dirPath not fount') return res,count,'dirPath not fount' end --获取所有该文件及副本文件 local files,desc = getAllFiles(dirPath,filename) local filevals = nil --文件内容 if not files then return res,count,desc end local isCurFile = false --要删除文件是否找到 for i,file in pairs(files) do local isbreak = false local path = dirPath..'/'..file local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开 if not filehandle then -- 判断文件是否存在 isbreak = true end fileval = filehandle:read("*all") -- 读出文件内容 if not fileval then isbreak = true end local filevaltable = split(fileval,delimiter) local table_lenght = table_leng(filevaltable) if table_lenght <= 1 then isbreak = true end local newfileval = "" --删除后文件内容 if not isbreak then -- body for a = 1,table_lenght-1 do local val = json.decode(filevaltable[a]) if val.id ~= id then data = json.encode(val) if newfileval ~= "" then newfileval = newfileval..data..delimiter else newfileval = data..delimiter end else isCurFile = true end end end filehandle:close() -- 关闭文件 if isCurFile then --删除旧文件 local del = os.remove(path) log.debug('func:delBaseInfo del file', del) --保存删除后的内容 local c = io.writeFile(path,newfileval, "a+b") if c then count = i log.info('func:delBaseInfo save file', c) return true,count,'del success' else log.error('error', '基础信息写入失败') return false,count,'del ok,but save fail' end break else count = i end end if not isCurFile then return false,count,'this data not found where id='..id end end function delOfflineCacheTemp( dirPath ) -- body return rtos.remove_dir(dirPath) end -- local res,cnt,desc = delBaseInfo('user',113,'/sdcard0/wind/12345') -- log.info('del result',res,cnt,desc) --修改基本信息 --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 要修改的数据 table类型 要包含数据id --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述 function updateBaseInfo( filename,data,dirPath) local res = false --修改结果 local count = 0 --查询文件个数 if not filename then log.error('func:updateBaseInfo','filename not fount') return res,count,'filename not fount' end if not data.id then log.error('func:updateBaseInfo','data not fount') return res,count,'the id of data is not fount' end --获取所有该文件及副本文件 local files,desc = getAllFiles(dirPath,filename) local filevals = nil --文件内容 if not files then return res,count,desc end local isCurFile = false --要修改数据文件是否找到 for i,file in pairs(files) do local isbreak = false local path = dirPath..'/'..file local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开 if not filehandle then -- 判断文件是否存在 isbreak = true end fileval = filehandle:read("*all") -- 读出文件内容 if not fileval then isbreak = true end local filevaltable = split(fileval,delimiter) local table_length = table_leng(filevaltable) if table_length <= 1 then isbreak = true end local newfileval = nil --删除后文件内容 if not isbreak then -- body for a = 1,table_length-1 do local val = json.decode(filevaltable[a]) if val.id ~= data.id then --过滤要修改的那条数据 dataStr = json.encode(val) if newfileval then newfileval = newfileval..dataStr..delimiter else newfileval = dataStr..delimiter end else --将新数据放入新的文件内容中 local newData = json.encode(data) if newfileval then newfileval = newfileval..newData..delimiter else newfileval = newData..delimiter end isCurFile = true end end end filehandle:close() -- 关闭文件 if isCurFile then if newfileval then --删除旧文件 local del = os.remove(path) if not del then return false,count,'func:updateBaseInfo del fail,please reoperate' end --保存删除后的内容 local c = io.writeFile(path,newfileval, "a+b") if c then count = i return true,count,'func:updateBaseInfo update success' else return false,count,'func:updateBaseInfo del success,but save fail' end else return false,count,'the file content after del create fail' end break else count = i end end if not isCurFile then return false,count,'this data not found where id='..data.id end end sys.taskInit(function() -- while true do -- log.info("test00") -- led.blinkPwm(blueLed,2000,1000) -- led.blinkPwm(redLed,3000,1000) -- sys.wait(2000) -- end sys.wait(5000) local moutedFlag = io.mount(io.SDCARD) log.info('mount sd card', moutedFlag) local sdCardTotalSize = rtos.get_fs_total_size(1,1) log.info("sd card total size "..sdCardTotalSize.." KB") if moutedFlag == 1 then -- body sys.publish("IO_SDCARD_MOUTED") end end)