|
@@ -0,0 +1,644 @@
|
|
|
+-- 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
|
|
|
+ -- log.info("common file", fName, fSize)
|
|
|
+ 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
|
|
|
+ for i,file in pairs(files) do
|
|
|
+ local path = dirPath..'/'..file
|
|
|
+ local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
|
|
|
+ if not filehandle then -- 判断文件是否存在
|
|
|
+ break --不存在 跳过 查询下个文件
|
|
|
+ end
|
|
|
+ fileval = filehandle:read("*all") -- 读出文件内容
|
|
|
+ -- print(fileval)
|
|
|
+ if not fileval then
|
|
|
+ break --空文件 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local filevaltable = split(fileval,delimiter)
|
|
|
+ local table_length = table_leng(filevaltable)
|
|
|
+ if table_length <= 1 then
|
|
|
+ break --无'-'标识符 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local isBreak = false
|
|
|
+
|
|
|
+ for a = 1,table_length-1 do
|
|
|
+ local val = json.decode(filevaltable[a])
|
|
|
+
|
|
|
+ if param then --有查询参数
|
|
|
+ local p_length = table_leng(param) --条件长度
|
|
|
+ local succ_cnt = 0; --比较成功个数
|
|
|
+ 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
|
|
|
+ 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
|
|
|
+ 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
|
|
|
+ for i,file in pairs(files) do
|
|
|
+ local path = dirPath..'/'..file
|
|
|
+ local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
|
|
|
+ if not filehandle then -- 判断文件是否存在
|
|
|
+ break --不存在 跳过 查询下个文件
|
|
|
+ end
|
|
|
+ fileval = filehandle:read("*all") -- 读出文件内容
|
|
|
+ -- print(fileval)
|
|
|
+ if not fileval then
|
|
|
+ break --空文件 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local filevaltable = split(fileval,delimiter)
|
|
|
+ local table_length = table_leng(filevaltable)
|
|
|
+ if table_length <= 1 then
|
|
|
+ break --无'-'标识符 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local isBreak = false
|
|
|
+
|
|
|
+ for a = 1,table_length-1 do
|
|
|
+ local val = json.decode(filevaltable[a])
|
|
|
+
|
|
|
+ if param then --有查询参数
|
|
|
+ local p_length = table_leng(param) --条件长度
|
|
|
+ local succ_cnt = 0; --比较成功个数
|
|
|
+ for p,s in pairs(param) do
|
|
|
+
|
|
|
+ if val[p] == s then
|
|
|
+ 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
|
|
|
+ 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 path = dirPath..'/'..file
|
|
|
+ local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
|
|
|
+ if not filehandle then -- 判断文件是否存在
|
|
|
+ break --不存在 跳过 查询下个文件
|
|
|
+ end
|
|
|
+ fileval = filehandle:read("*all") -- 读出文件内容
|
|
|
+ if not fileval then
|
|
|
+ break --空文件 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local filevaltable = split(fileval,delimiter)
|
|
|
+ local table_lenght = table_leng(filevaltable)
|
|
|
+ if table_lenght <= 1 then
|
|
|
+ break --无'-'标识符 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local newfileval = "" --删除后文件内容
|
|
|
+ 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
|
|
|
+ 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
|
|
|
+-- 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 path = dirPath..'/'..file
|
|
|
+ local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
|
|
|
+ if not filehandle then -- 判断文件是否存在
|
|
|
+ break --不存在 跳过 查询下个文件
|
|
|
+ end
|
|
|
+ fileval = filehandle:read("*all") -- 读出文件内容
|
|
|
+ if not fileval then
|
|
|
+ break --空文件 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local filevaltable = split(fileval,delimiter)
|
|
|
+ local table_length = table_leng(filevaltable)
|
|
|
+ if table_length <= 1 then
|
|
|
+ break --无'-'标识符 跳过读取下一个文件
|
|
|
+ end
|
|
|
+ local newfileval = nil --删除后文件内容
|
|
|
+ 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
|
|
|
+ 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)
|
|
|
+
|
|
|
+
|
|
|
+-- saveBaseInfo("test", {id=1, name="test", time=os.time()}, "/sdcard0/test")
|
|
|
+ -- local user = {id=112,name='小亮xxx',pwd='49BA59ABBE56E057',time=os.time()} --password md5加密 16位大写 123456
|
|
|
+ -- local res,desc= updateBaseInfo('user',user,'/sdcard0/wind/12345')
|
|
|
+ -- log.debug('update result',res,desc)
|
|
|
+-- local del = os.remove(comDir .. '/user-1')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-2')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-3')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-4')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-5')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-6')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user-8')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- local del = os.remove(comDir .. '/user')
|
|
|
+-- log.info('del user file', del)
|
|
|
+-- sys.init(0, 0)
|
|
|
+-- sys.run()
|