sdModuel.lua 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. -- pm.wake("sdcard")
  2. local comSize = 4 --每个公共目录下文件大小 单位 kb
  3. -- 挂载SD卡,返回值0表示失败,1表示成功
  4. -- local sdcard = io.mount(io.SDCARD)
  5. -- log.info('mount sd card', sdcard)
  6. local sdCardTotalSize = rtos.get_fs_total_size(1, 1)
  7. log.info("sd card total size " .. sdCardTotalSize .. " KB")
  8. local delimiter = '\0'
  9. if io.opendir("/sdcard0/common") then
  10. for i=1,100 do
  11. local fType,fName,fSize = io.readdir()
  12. log.info("fname",fName)
  13. if fType==32 then
  14. -- log.info("sd card file",fName,fSize)
  15. elseif fType == nil then
  16. break
  17. end
  18. end
  19. io.closedir("/sdcard0/commmon")
  20. end
  21. -- 删除目录
  22. -- local rmfile = rtos.remove_dir('/sdcard0/common')
  23. -- log.info('delete', rmfile)
  24. -- rtos.make_dir('/sdcard0/log')
  25. -- debug_log('写入测试日志')
  26. -- local del = os.remove(comDir .. '/user')
  27. -- log.info('del user file', del)
  28. -- local readval = readfile() -- 参数:完整路径文件名 不传或者传'/sdcard0/log/0'都是获取最新日志文件
  29. -- print(readval)
  30. --获取table长度
  31. function table_leng(t)
  32. local leng=0
  33. for k, v in pairs(t) do
  34. leng=leng+1
  35. end
  36. return leng;
  37. end
  38. -- 分割字符串
  39. ---@param str string 元字符串
  40. ---@param seq string 分割字符
  41. ---@return table
  42. function split(str, seq)
  43. local nFindStartIndex = 1
  44. local nSplitIndex = 1
  45. local nSplitArray = {}
  46. while true do
  47. local nFindLastIndex = string.find(str, seq, nFindStartIndex)
  48. if not nFindLastIndex then
  49. nSplitArray[nSplitIndex] = string.sub(str, nFindStartIndex, string.len(str))
  50. break
  51. end
  52. nSplitArray[nSplitIndex] = string.sub(str, nFindStartIndex, nFindLastIndex - 1)
  53. nFindStartIndex = nFindLastIndex + string.len(seq)
  54. nSplitIndex = nSplitIndex + 1
  55. end
  56. return nSplitArray
  57. end
  58. --数组去重
  59. function table.unique(t, bArray, mainKey)
  60. local check = {}
  61. local n = {}
  62. local idx = 1
  63. for k, v in pairs(t) do
  64. local judgeKey = v
  65. if mainKey then
  66. judgeKey = v[mainKey] or v
  67. end
  68. if not check[judgeKey] then
  69. if bArray then
  70. n[idx] = v
  71. idx = idx + 1
  72. else
  73. n[k] = v
  74. end
  75. check[judgeKey] = true
  76. end
  77. end
  78. return n
  79. end
  80. --循环创建文件夹
  81. function createAllFolder(path)
  82. -- print(path)
  83. local path_tb={}
  84. local new_path=""
  85. -- 分割路径保存到table
  86. for i,s in pairs(split(path,'/')) do
  87. if s~=nil then
  88. table.insert(path_tb,s)
  89. end
  90. end
  91. -- print(json.encode(path_tb))
  92. local cdir = false --创建结果
  93. -- 遍历并拼接路径检测是否存在,不存在则新建
  94. for k,v in ipairs(path_tb) do
  95. if v ~= '' then
  96. if k==1 then
  97. new_path=v
  98. else
  99. new_path=new_path.."/"..v
  100. end
  101. -- print(new_path,io.opendir(new_path))
  102. local opflag = io.opendir(new_path)
  103. if opflag ~= 1 then
  104. cdir = rtos.make_dir(new_path)
  105. log.info("path:", new_path, "flag:",cdir)
  106. end
  107. end
  108. end
  109. io.closedir()
  110. return cdir
  111. end
  112. -- 打开sd卡公共目录,不存在则创建 获取最新文件,
  113. function getComLastFile(dirPath,filename)
  114. local lastfile = filename --返回最新文件
  115. -- log.info('func:getComLastFile open dir', io.opendir(dirPath))
  116. if io.opendir(dirPath) ~= 0 then
  117. local file = {} -- 该目录所有文件名
  118. for i = 1, 999 do
  119. local fType, fName, fSize = io.readdir()
  120. -- log.info("file or dir",fType, fName, fSize)
  121. if fType == 32 then
  122. -- log.info("func getComLastFile common file", fName, fSize)
  123. local tname = split(fName,'-')
  124. -- log.debug('func:getComLastFile tname',tname,tname[1],tname[2])
  125. if tname[1] == filename then
  126. if tname[2] then
  127. table.insert(file,tname[2])
  128. end
  129. end
  130. -- local c = io.readFile(dirPath..'/'..fName)
  131. -- log.info('文件内容',c)
  132. elseif fType == nil then
  133. break
  134. end
  135. end
  136. -- log.error('files',json.encode(file))
  137. --获取文件名后缀最大值
  138. local max = nil -- 最新的文件 即数字名称值最大的文件
  139. for i, v in pairs(file) do
  140. v = tonumber(v) --数组中是string类型 比较会有问题
  141. if max == nil then max = v end
  142. if max < v then max = v end
  143. end
  144. -- print(max)
  145. if max then
  146. -- 判断该最新文件大小
  147. local cnt = io.fileSize(dirPath .. '/' .. filename..'-'..max)
  148. if cnt then
  149. if cnt >= (comSize * 1024) then -- 文件大于设置的文件大小 新建新文件
  150. max = max + 1
  151. end
  152. lastfile = filename..'-'..max
  153. end
  154. else
  155. -- 判断该最新文件大小
  156. local cnt = io.fileSize(dirPath .. '/' .. filename)
  157. if cnt then
  158. if cnt >= (comSize * 1024) then -- 文件大于设置的文件大小 新建新文件
  159. lastfile = filename..'-1'
  160. end
  161. end
  162. end
  163. -- print(lastfile)
  164. log.debug('func:getComLastFile lastfile',lastfile)
  165. io.closedir()
  166. return lastfile
  167. else
  168. local cdir = createAllFolder(dirPath)
  169. if cdir then
  170. log.info('创建公共目录成功', dirPath)
  171. else
  172. log.error('创建公共目录失败',dirPath)
  173. end
  174. return lastfile
  175. end
  176. end
  177. --根据某个目录下文件名获取该类型所有文件 返回table
  178. function getAllFiles( dir, file )
  179. local fs = {}
  180. if not file then
  181. return false,'fun:getAllFiles,desc:file not found'
  182. end
  183. log.error('open dir',io.opendir(dir))
  184. if io.opendir(dir) ~= 0 then
  185. for i = 1, 999 do
  186. local fType, fName, fSize = io.readdir()
  187. -- log.info("file or dir",fType, fName, fSize)
  188. if fType == 32 then
  189. -- log.info("common file", fName, fSize)
  190. local tname = split(fName,'-')
  191. -- log.debug('func:getAllFiles tname',tname,tname[1],tname[2])
  192. if tname[1] == file then
  193. table.insert(fs,fName)
  194. end
  195. elseif fType == nil then
  196. break
  197. end
  198. end
  199. else
  200. io.closedir()
  201. return false,'fun:getAllFiles,desc:dir not found'
  202. end
  203. io.closedir()
  204. return fs,'fun:getAllFiles,desc:get all files is ok'
  205. end
  206. -- 保存基础信息所有扳手信息
  207. -- 文件 所有扳手信息 查询条件扳手编号 wrench
  208. -- 文件 所有风场信息 wind
  209. -- 文件 部件列表 unit
  210. -- 文件 用户信息 user 查询条件用户名+密码
  211. -- 文件 任务计划 task 查询条件任务编号
  212. -- 文件 工作位置列表 workspace 查询条件风机型号+部件号
  213. -- 文件 待上传信息
  214. -- 文件 配置信息 比如最后一次使用时间 用于存储空间不够时删除
  215. --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 保存数据 table类型
  216. function saveBaseInfo( filename,data,dirPath)
  217. -- print(filename)
  218. if not filename then
  219. log.error('savebaseinfo','filename not fount')
  220. return false,'filename not fount'
  221. end
  222. if not data then
  223. log.error('savebaseinfo','data not fount')
  224. return false,'data not fount'
  225. end
  226. if not dirPath then
  227. log.error('savebaseinfo','dirPath not fount')
  228. return false,'dirPath not fount'
  229. end
  230. --最新文件名称
  231. local filename = getComLastFile(dirPath,filename)
  232. log.debug('func:saveBaseInfo last file',filename)
  233. local path = dirPath..'/'..filename
  234. log.info('save path',path)
  235. data = json.encode(data)..delimiter
  236. local c = io.writeFile(path,data, "a+b")
  237. if c then
  238. log.info('write result', c)
  239. return true
  240. else
  241. log.error('error', '基础信息写入失败')
  242. return false,'基础信息写入失败'
  243. end
  244. end
  245. -- 保存基础信息
  246. -- local wrench = {id=1,name='测试设备'}
  247. -- local res,desc= saveBaseInfo('wrench',wrench)
  248. -- log.debug('save result',res,desc)
  249. -- 模拟数据
  250. -- for i=113,150 do
  251. -- local user = {id=i,name='小亮'..i,pwd='49BA59ABBE56E057',time=os.time()} --password md5加密 16位大写 123456
  252. -- local res,desc= saveBaseInfo('user',user,'/sdcard0/wind/12345')
  253. -- log.debug('save result',res,desc)
  254. -- break
  255. -- end
  256. --查询设备信息
  257. --参数[filename] 文件名称 如wrench、wind、unit。。。[param] 保存数据 table类型 nil查询所有数据
  258. --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述
  259. function getBaseInfo( filename,param,dirPath)
  260. local selectRes = {} --查询结果
  261. local count = 0 --查询文件个数
  262. if not filename then
  263. log.error('func:getBaseInfo','filename not fount')
  264. return false,count,'filename not fount'
  265. end
  266. if not dirPath then
  267. log.error('func:getBaseInfo','dirPath not fount')
  268. return false,count,'dirPath not fount'
  269. end
  270. --获取所有该文件及副本文件
  271. local files,desc = getAllFiles(dirPath,filename)
  272. local filevals = nil --文件内容
  273. if not files then
  274. return false,count,desc
  275. end
  276. for i,file in pairs(files) do
  277. local path = dirPath..'/'..file
  278. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  279. if not filehandle then -- 判断文件是否存在
  280. break --不存在 跳过 查询下个文件
  281. end
  282. fileval = filehandle:read("*all") -- 读出文件内容
  283. -- print(fileval)
  284. if not fileval then
  285. break --空文件 跳过读取下一个文件
  286. end
  287. local filevaltable = split(fileval,delimiter)
  288. local table_length = table_leng(filevaltable)
  289. if table_length <= 1 then
  290. break --无'-'标识符 跳过读取下一个文件
  291. end
  292. local isBreak = false
  293. for a = 1,table_length-1 do
  294. local val = json.decode(filevaltable[a])
  295. if param then --有查询参数
  296. local p_length = table_leng(param) --条件长度
  297. local succ_cnt = 0; --比较成功个数
  298. for p,s in pairs(param) do
  299. if filename == "Users" then
  300. if p == 'pwd' then
  301. s = string.sub(string.upper(crypto.md5(s,#s,16)),9,24) --密码加密 加密结果32位 取16位(第9-24)字符
  302. end
  303. end
  304. if val[p] == s then
  305. succ_cnt = succ_cnt + 1
  306. else
  307. break
  308. end
  309. end
  310. if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  311. selectRes[i] = val
  312. isBreak = true
  313. break
  314. end
  315. else --无查询参数 查询所有
  316. selectRes[i] = val
  317. end
  318. end
  319. count = i
  320. if isBreak then --找到结果 停止继续查找下个文件
  321. break;
  322. end
  323. filehandle:close() -- 关闭文件
  324. end
  325. -- print(table_leng(selectRes),json.encode(selectRes))
  326. if table_leng(selectRes) > 0 then
  327. --去重
  328. local result = table.unique(selectRes,true,'id')
  329. return result,count,'select success'
  330. else
  331. return false,count,'this data is not found'
  332. end
  333. end
  334. -- 查询文件内容复数
  335. function getBaseInfo_ununique( filename,param,dirPath)
  336. local selectRes = {} --查询结果
  337. local count = 0 --查询文件个数
  338. if not filename then
  339. log.error('func:getBaseInfo','filename not fount')
  340. return false,count,'filename not fount'
  341. end
  342. if not dirPath then
  343. log.error('func:getBaseInfo','dirPath not fount')
  344. return false,count,'dirPath not fount'
  345. end
  346. --获取所有该文件及副本文件
  347. local files,desc = getAllFiles(dirPath,filename)
  348. local filevals = nil --文件内容
  349. if not files then
  350. return false,count,desc
  351. end
  352. for i,file in pairs(files) do
  353. local path = dirPath..'/'..file
  354. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  355. if not filehandle then -- 判断文件是否存在
  356. break --不存在 跳过 查询下个文件
  357. end
  358. fileval = filehandle:read("*all") -- 读出文件内容
  359. -- print(fileval)
  360. if not fileval then
  361. break --空文件 跳过读取下一个文件
  362. end
  363. local filevaltable = split(fileval,delimiter)
  364. local table_length = table_leng(filevaltable)
  365. if table_length <= 1 then
  366. break --无'-'标识符 跳过读取下一个文件
  367. end
  368. local isBreak = false
  369. for a = 1,table_length-1 do
  370. local val = json.decode(filevaltable[a])
  371. if param then --有查询参数
  372. local p_length = table_leng(param) --条件长度
  373. local succ_cnt = 0; --比较成功个数
  374. for p,s in pairs(param) do
  375. if val[p] == s then
  376. succ_cnt = succ_cnt + 1
  377. else
  378. break
  379. end
  380. end
  381. if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  382. table.insert(selectRes, val)
  383. end
  384. else --无查询参数 查询所有
  385. table.insert(selectRes, val)
  386. end
  387. end
  388. count = i
  389. -- if isBreak then --找到结果 停止继续查找下个文件
  390. -- break;
  391. -- end
  392. filehandle:close() -- 关闭文件
  393. end
  394. -- print(table_leng(selectRes),json.encode(selectRes))
  395. if table_leng(selectRes) > 0 then
  396. --去重
  397. local result = table.unique(selectRes,true,'id')
  398. return result,count,'select success'
  399. else
  400. return false,count,'this data is not found'
  401. end
  402. end
  403. -- 查询文件内容复数_模糊查询
  404. function getBaseInfo_ununique_dim( filename,param,dirPath)
  405. local selectRes = {} --查询结果
  406. local count = 0 --查询文件个数
  407. if not filename then
  408. log.error('func:getBaseInfo','filename not fount')
  409. return false,count,'filename not fount'
  410. end
  411. if not dirPath then
  412. log.error('func:getBaseInfo','dirPath not fount')
  413. return false,count,'dirPath not fount'
  414. end
  415. --获取所有该文件及副本文件
  416. local files,desc = getAllFiles(dirPath,filename)
  417. local filevals = nil --文件内容
  418. if not files then
  419. return false,count,desc
  420. end
  421. for i,file in pairs(files) do
  422. local path = dirPath..'/'..file
  423. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  424. if not filehandle then -- 判断文件是否存在
  425. break --不存在 跳过 查询下个文件
  426. end
  427. fileval = filehandle:read("*all") -- 读出文件内容
  428. -- print(fileval)
  429. if not fileval then
  430. break --空文件 跳过读取下一个文件
  431. end
  432. local filevaltable = split(fileval,delimiter)
  433. local table_length = table_leng(filevaltable)
  434. if table_length <= 1 then
  435. break --无'-'标识符 跳过读取下一个文件
  436. end
  437. local isBreak = false
  438. for a = 1,table_length-1 do
  439. local val = json.decode(filevaltable[a])
  440. if param then --有查询参数
  441. local p_length = table_leng(param) --条件长度
  442. local succ_cnt = 0; --比较成功个数
  443. local pattern = ""
  444. for p,s in pairs(param) do
  445. if succ_cnt > 19 then
  446. -- body
  447. break
  448. end
  449. local wholeStr = val[p]
  450. if type(val[p]) ~= "userdata" then
  451. -- body
  452. pattern = string.gsub(s, "[%[%]&=+%%%c%(%)%-%/%+]", function(c)
  453. return "%"..c
  454. end)
  455. local a,b = string.find(wholeStr,pattern)
  456. local transMatchres = string.match(wholeStr,pattern)
  457. if a and (b-a+1) == #transMatchres then
  458. succ_cnt = succ_cnt + 1
  459. table.insert(selectRes, val)
  460. end
  461. end
  462. end
  463. -- if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  464. -- table.insert(selectRes, val)
  465. -- end
  466. else --无查询参数 查询所有
  467. table.insert(selectRes, val)
  468. end
  469. end
  470. count = i
  471. -- if isBreak then --找到结果 停止继续查找下个文件
  472. -- break;
  473. -- end
  474. filehandle:close() -- 关闭文件
  475. end
  476. -- print(table_leng(selectRes),json.encode(selectRes))
  477. if table_leng(selectRes) > 0 then
  478. --去重
  479. local result = table.unique(selectRes,true,'id')
  480. return result,count,'select success'
  481. else
  482. return false,count,'this data is not found'
  483. end
  484. end
  485. -- local res,cnt,desc = getBaseInfo('user',{pwd="123456",name="小亮113"},'/sdcard0/wind/12345')
  486. -- log.info('select result',res,cnt,desc)
  487. -- for i,j in ipairs(res) do
  488. -- for m,n in pairs(j) do
  489. -- print(m,n)
  490. -- end
  491. -- end
  492. --删除基本信息
  493. --参数[filename] 文件名称 如wrench、wind、unit。。。[id] 删除数据id
  494. function delBaseInfo(filename,id,dirPath)
  495. local res = false --删除结果
  496. local count = 0 --查询文件个数
  497. log.info("del process:",filename,id,dirPath)
  498. if not filename then
  499. log.error('func:delBaseInfo','filename not fount')
  500. return res,count,'filename not fount'
  501. end
  502. if not id then
  503. log.error('func:delBaseInfo','id not fount')
  504. return res,count,'id not fount'
  505. end
  506. if not dirPath then
  507. log.error('func:delBaseInfo','dirPath not fount')
  508. return res,count,'dirPath not fount'
  509. end
  510. --获取所有该文件及副本文件
  511. local files,desc = getAllFiles(dirPath,filename)
  512. local filevals = nil --文件内容
  513. if not files then
  514. return res,count,desc
  515. end
  516. local isCurFile = false --要删除文件是否找到
  517. for i,file in pairs(files) do
  518. local path = dirPath..'/'..file
  519. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  520. if not filehandle then -- 判断文件是否存在
  521. break --不存在 跳过 查询下个文件
  522. end
  523. fileval = filehandle:read("*all") -- 读出文件内容
  524. if not fileval then
  525. break --空文件 跳过读取下一个文件
  526. end
  527. local filevaltable = split(fileval,delimiter)
  528. local table_lenght = table_leng(filevaltable)
  529. if table_lenght <= 1 then
  530. break --无'-'标识符 跳过读取下一个文件
  531. end
  532. local newfileval = "" --删除后文件内容
  533. for a = 1,table_lenght-1 do
  534. local val = json.decode(filevaltable[a])
  535. if val.id ~= id then
  536. data = json.encode(val)
  537. if newfileval ~= "" then
  538. newfileval = newfileval..data..delimiter
  539. else
  540. newfileval = data..delimiter
  541. end
  542. else
  543. isCurFile = true
  544. end
  545. end
  546. filehandle:close() -- 关闭文件
  547. if isCurFile then
  548. --删除旧文件
  549. local del = os.remove(path)
  550. log.debug('func:delBaseInfo del file', del)
  551. --保存删除后的内容
  552. local c = io.writeFile(path,newfileval, "a+b")
  553. if c then
  554. count = i
  555. log.info('func:delBaseInfo save file', c)
  556. return true,count,'del success'
  557. else
  558. log.error('error', '基础信息写入失败')
  559. return false,count,'del ok,but save fail'
  560. end
  561. break
  562. else
  563. count = i
  564. end
  565. end
  566. if not isCurFile then
  567. return false,count,'this data not found where id='..id
  568. end
  569. end
  570. function delOfflineCacheTemp( dirPath )
  571. -- body
  572. return rtos.remove_dir(dirPath)
  573. end
  574. -- local res,cnt,desc = delBaseInfo('user',113,'/sdcard0/wind/12345')
  575. -- log.info('del result',res,cnt,desc)
  576. --修改基本信息
  577. --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 要修改的数据 table类型 要包含数据id
  578. --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述
  579. function updateBaseInfo( filename,data,dirPath)
  580. local res = false --修改结果
  581. local count = 0 --查询文件个数
  582. if not filename then
  583. log.error('func:updateBaseInfo','filename not fount')
  584. return res,count,'filename not fount'
  585. end
  586. if not data.id then
  587. log.error('func:updateBaseInfo','data not fount')
  588. return res,count,'the id of data is not fount'
  589. end
  590. --获取所有该文件及副本文件
  591. local files,desc = getAllFiles(dirPath,filename)
  592. local filevals = nil --文件内容
  593. if not files then
  594. return res,count,desc
  595. end
  596. local isCurFile = false --要修改数据文件是否找到
  597. for i,file in pairs(files) do
  598. local path = dirPath..'/'..file
  599. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  600. if not filehandle then -- 判断文件是否存在
  601. break --不存在 跳过 查询下个文件
  602. end
  603. fileval = filehandle:read("*all") -- 读出文件内容
  604. if not fileval then
  605. break --空文件 跳过读取下一个文件
  606. end
  607. local filevaltable = split(fileval,delimiter)
  608. local table_length = table_leng(filevaltable)
  609. if table_length <= 1 then
  610. break --无'-'标识符 跳过读取下一个文件
  611. end
  612. local newfileval = nil --删除后文件内容
  613. for a = 1,table_length-1 do
  614. local val = json.decode(filevaltable[a])
  615. if val.id ~= data.id then --过滤要修改的那条数据
  616. dataStr = json.encode(val)
  617. if newfileval then
  618. newfileval = newfileval..dataStr..delimiter
  619. else
  620. newfileval = dataStr..delimiter
  621. end
  622. else
  623. --将新数据放入新的文件内容中
  624. local newData = json.encode(data)
  625. if newfileval then
  626. newfileval = newfileval..newData..delimiter
  627. else
  628. newfileval = newData..delimiter
  629. end
  630. isCurFile = true
  631. end
  632. end
  633. filehandle:close() -- 关闭文件
  634. if isCurFile then
  635. if newfileval then
  636. --删除旧文件
  637. local del = os.remove(path)
  638. if not del then
  639. return false,count,'func:updateBaseInfo del fail,please reoperate'
  640. end
  641. --保存删除后的内容
  642. local c = io.writeFile(path,newfileval, "a+b")
  643. if c then
  644. count = i
  645. return true,count,'func:updateBaseInfo update success'
  646. else
  647. return false,count,'func:updateBaseInfo del success,but save fail'
  648. end
  649. else
  650. return false,count,'the file content after del create fail'
  651. end
  652. break
  653. else
  654. count = i
  655. end
  656. end
  657. if not isCurFile then
  658. return false,count,'this data not found where id='..data.id
  659. end
  660. end
  661. sys.taskInit(function()
  662. -- while true do
  663. -- log.info("test00")
  664. -- led.blinkPwm(blueLed,2000,1000)
  665. -- led.blinkPwm(redLed,3000,1000)
  666. -- sys.wait(2000)
  667. -- end
  668. sys.wait(5000)
  669. local moutedFlag = io.mount(io.SDCARD)
  670. log.info('mount sd card', moutedFlag)
  671. local sdCardTotalSize = rtos.get_fs_total_size(1,1)
  672. log.info("sd card total size "..sdCardTotalSize.." KB")
  673. if moutedFlag == 1 then
  674. -- body
  675. sys.publish("IO_SDCARD_MOUTED")
  676. end
  677. end)