sdModuel.lua 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. local tname = split(fName,'-')
  190. -- log.debug('func:getAllFiles tname',tname,tname[1],tname[2])
  191. if tname[1] == file then
  192. table.insert(fs,fName)
  193. end
  194. elseif fType == nil then
  195. break
  196. end
  197. end
  198. else
  199. io.closedir()
  200. return false,'fun:getAllFiles,desc:dir not found'
  201. end
  202. io.closedir()
  203. return fs,'fun:getAllFiles,desc:get all files is ok'
  204. end
  205. -- 保存基础信息所有扳手信息
  206. -- 文件 所有扳手信息 查询条件扳手编号 wrench
  207. -- 文件 所有风场信息 wind
  208. -- 文件 部件列表 unit
  209. -- 文件 用户信息 user 查询条件用户名+密码
  210. -- 文件 任务计划 task 查询条件任务编号
  211. -- 文件 工作位置列表 workspace 查询条件风机型号+部件号
  212. -- 文件 待上传信息
  213. -- 文件 配置信息 比如最后一次使用时间 用于存储空间不够时删除
  214. --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 保存数据 table类型
  215. function saveBaseInfo( filename,data,dirPath)
  216. -- print(filename)
  217. if not filename then
  218. log.error('savebaseinfo','filename not fount')
  219. return false,'filename not fount'
  220. end
  221. if not data then
  222. log.error('savebaseinfo','data not fount')
  223. return false,'data not fount'
  224. end
  225. if not dirPath then
  226. log.error('savebaseinfo','dirPath not fount')
  227. return false,'dirPath not fount'
  228. end
  229. --最新文件名称
  230. local filename = getComLastFile(dirPath,filename)
  231. log.debug('func:saveBaseInfo last file',filename)
  232. local path = dirPath..'/'..filename
  233. log.info('save path',path)
  234. data = json.encode(data)..delimiter
  235. local c = io.writeFile(path,data, "a+b")
  236. if c then
  237. log.info('write result', c)
  238. return true
  239. else
  240. log.error('error', '基础信息写入失败')
  241. return false,'基础信息写入失败'
  242. end
  243. end
  244. -- 保存基础信息
  245. -- local wrench = {id=1,name='测试设备'}
  246. -- local res,desc= saveBaseInfo('wrench',wrench)
  247. -- log.debug('save result',res,desc)
  248. -- 模拟数据
  249. -- for i=113,150 do
  250. -- local user = {id=i,name='小亮'..i,pwd='49BA59ABBE56E057',time=os.time()} --password md5加密 16位大写 123456
  251. -- local res,desc= saveBaseInfo('user',user,'/sdcard0/wind/12345')
  252. -- log.debug('save result',res,desc)
  253. -- break
  254. -- end
  255. --查询设备信息
  256. --参数[filename] 文件名称 如wrench、wind、unit。。。[param] 保存数据 table类型 nil查询所有数据
  257. --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述
  258. function getBaseInfo( filename,param,dirPath)
  259. local selectRes = {} --查询结果
  260. local count = 0 --查询文件个数
  261. if not filename then
  262. log.error('func:getBaseInfo','filename not fount')
  263. return false,count,'filename not fount'
  264. end
  265. if not dirPath then
  266. log.error('func:getBaseInfo','dirPath not fount')
  267. return false,count,'dirPath not fount'
  268. end
  269. --获取所有该文件及副本文件
  270. local files,desc = getAllFiles(dirPath,filename)
  271. local filevals = nil --文件内容
  272. if not files then
  273. return false,count,desc
  274. end
  275. local succ_cnt = 0; --比较成功个数
  276. for i,file in pairs(files) do
  277. local isbreak = false
  278. local path = dirPath..'/'..file
  279. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  280. if not filehandle then -- 判断文件是否存在
  281. isbreak = true
  282. end
  283. fileval = filehandle:read("*all") -- 读出文件内容
  284. -- print(fileval)
  285. if not fileval then
  286. isbreak = true
  287. end
  288. local filevaltable = split(fileval,delimiter)
  289. local table_length = table_leng(filevaltable)
  290. if table_length <= 1 then
  291. isbreak = true
  292. end
  293. -- log.info("isbreak:", isbreak, table_length)
  294. if not isbreak then
  295. -- body
  296. for a = 1,table_length-1 do
  297. local val = json.decode(filevaltable[a])
  298. if param then --有查询参数
  299. local p_length = table_leng(param) --条件长度
  300. for p,s in pairs(param) do
  301. if filename == "Users" then
  302. if p == 'pwd' then
  303. s = string.sub(string.upper(crypto.md5(s,#s,16)),9,24) --密码加密 加密结果32位 取16位(第9-24)字符
  304. end
  305. end
  306. -- log.info("val[p] ", val[p] , "s", s, succ_cnt)
  307. if val[p] == s then
  308. succ_cnt = succ_cnt + 1
  309. else
  310. break
  311. end
  312. end
  313. if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  314. selectRes[i] = val
  315. isbreak = true
  316. break
  317. end
  318. else --无查询参数 查询所有
  319. selectRes[i] = val
  320. end
  321. end
  322. end
  323. count = i
  324. -- if isbreak then --找到结果 停止继续查找下个文件
  325. -- break;
  326. -- end
  327. filehandle:close() -- 关闭文件
  328. end
  329. -- print(table_leng(selectRes),json.encode(selectRes))
  330. if table_leng(selectRes) > 0 then
  331. --去重
  332. local result = table.unique(selectRes,true,'id')
  333. return result,count,'select success'
  334. else
  335. return false,count,'this data is not found'
  336. end
  337. end
  338. -- 查询文件内容复数
  339. function getBaseInfo_ununique( filename,param,dirPath)
  340. local selectRes = {} --查询结果
  341. local count = 0 --查询文件个数
  342. if not filename then
  343. log.error('func:getBaseInfo','filename not fount')
  344. return false,count,'filename not fount'
  345. end
  346. if not dirPath then
  347. log.error('func:getBaseInfo','dirPath not fount')
  348. return false,count,'dirPath not fount'
  349. end
  350. --获取所有该文件及副本文件
  351. local files,desc = getAllFiles(dirPath,filename)
  352. local filevals = nil --文件内容
  353. if not files then
  354. return false,count,desc
  355. end
  356. local succ_cnt = 0; --比较成功个数
  357. for i,file in pairs(files) do
  358. local isbreak = false
  359. local path = dirPath..'/'..file
  360. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  361. if not filehandle then -- 判断文件是否存在
  362. isbreak = true
  363. end
  364. fileval = filehandle:read("*all") -- 读出文件内容
  365. -- print(fileval)
  366. if not fileval then
  367. isbreak = true
  368. end
  369. local filevaltable = split(fileval,delimiter)
  370. local table_length = table_leng(filevaltable)
  371. if table_length <= 1 then
  372. isbreak = true
  373. end
  374. if not isbreak then
  375. -- body
  376. for a = 1,table_length-1 do
  377. local val = json.decode(filevaltable[a])
  378. succ_cnt = 0
  379. if param then --有查询参数
  380. local p_length = table_leng(param) --条件长度
  381. for p,s in pairs(param) do
  382. if val[p] == s then
  383. -- log.info("val[p]:", val[p], "s:",s)
  384. succ_cnt = succ_cnt + 1
  385. else
  386. break
  387. end
  388. end
  389. if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  390. table.insert(selectRes, val)
  391. end
  392. else --无查询参数 查询所有
  393. table.insert(selectRes, val)
  394. end
  395. end
  396. end
  397. count = i
  398. -- if isbreak then --找到结果 停止继续查找下个文件
  399. -- break;
  400. -- end
  401. filehandle:close() -- 关闭文件
  402. end
  403. -- print(table_leng(selectRes),json.encode(selectRes))
  404. if table_leng(selectRes) > 0 then
  405. --去重
  406. local result = table.unique(selectRes,true,'id')
  407. return result,count,'select success'
  408. else
  409. return false,count,'this data is not found'
  410. end
  411. end
  412. -- 查询文件内容复数_模糊查询
  413. function getBaseInfo_ununique_dim( filename,param,dirPath)
  414. local selectRes = {} --查询结果
  415. local count = 0 --查询文件个数
  416. if not filename then
  417. log.error('func:getBaseInfo','filename not fount')
  418. return false,count,'filename not fount'
  419. end
  420. if not dirPath then
  421. log.error('func:getBaseInfo','dirPath not fount')
  422. return false,count,'dirPath not fount'
  423. end
  424. --获取所有该文件及副本文件
  425. local files,desc = getAllFiles(dirPath,filename)
  426. local filevals = nil --文件内容
  427. if not files then
  428. return false,count,desc
  429. end
  430. local succ_cnt = 0; --比较成功个数
  431. for i,file in pairs(files) do
  432. local isbreak = false
  433. local path = dirPath..'/'..file
  434. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  435. if not filehandle then -- 判断文件是否存在
  436. isbreak = true
  437. -- isbreak = true
  438. end
  439. fileval = filehandle:read("*all") -- 读出文件内容
  440. -- print(fileval)
  441. if not fileval then
  442. isbreak = true
  443. -- isbreak = true
  444. end
  445. local filevaltable = split(fileval,delimiter)
  446. local table_length = table_leng(filevaltable)
  447. if table_length <= 1 then
  448. isbreak = true
  449. -- isbreak = true
  450. end
  451. if not isbreak then
  452. -- body
  453. for a = 1,table_length-1 do
  454. local val = json.decode(filevaltable[a])
  455. if param then --有查询参数
  456. local p_length = table_leng(param) --条件长度
  457. local pattern = ""
  458. for p,s in pairs(param) do
  459. if succ_cnt > 49 then
  460. -- body
  461. break
  462. end
  463. -- log.info("val[p]:", val[p], "s:",s)
  464. local wholeStr = val[p]
  465. if type(val[p]) ~= "userdata" then
  466. -- body
  467. pattern = string.gsub(s, "[%[%]&=+%%%c%(%)%-%/%+]", function(c)
  468. return "%"..c
  469. end)
  470. local a,b = string.find(wholeStr,pattern)
  471. local transMatchres = string.match(wholeStr,pattern)
  472. if a and (b-a+1) == #transMatchres then
  473. succ_cnt = succ_cnt + 1
  474. table.insert(selectRes, val)
  475. end
  476. end
  477. end
  478. -- if p_length == succ_cnt then --找到结果 停止继续查找下个文件
  479. -- table.insert(selectRes, val)
  480. -- end
  481. else --无查询参数 查询所有
  482. if succ_cnt > 49 then
  483. -- body
  484. else
  485. if filename == "Wind" then
  486. -- body
  487. local fieldDisplayRadius = nvm.get("fieldDisplayRadius") or 200
  488. local gpsLng, gpsLat = nvm.get("gpsLng"), nvm.get("gpsLat")
  489. local dist = devTool.getDistance( val.lat, val.lon, gpsLat, gpsLng)
  490. -- log.info("dist:",dist, json.encode(v))
  491. if dist < fieldDisplayRadius then
  492. -- body
  493. -- log.info("cicle",v.lat)
  494. val.dist = dist
  495. table.insert(selectRes, val)
  496. succ_cnt = succ_cnt+1
  497. end
  498. else
  499. table.insert(selectRes, val)
  500. succ_cnt = succ_cnt+1
  501. end
  502. end
  503. end
  504. end
  505. end
  506. count = i
  507. -- if isbreak then --找到结果 停止继续查找下个文件
  508. -- break;
  509. -- end
  510. filehandle:close() -- 关闭文件
  511. end
  512. -- print(table_leng(selectRes),json.encode(selectRes))
  513. if table_leng(selectRes) > 0 then
  514. --去重
  515. local result = table.unique(selectRes,true,'id')
  516. return result,count,'select success'
  517. else
  518. return false,count,'this data is not found'
  519. end
  520. end
  521. -- local res,cnt,desc = getBaseInfo('user',{pwd="123456",name="小亮113"},'/sdcard0/wind/12345')
  522. -- log.info('select result',res,cnt,desc)
  523. -- for i,j in ipairs(res) do
  524. -- for m,n in pairs(j) do
  525. -- print(m,n)
  526. -- end
  527. -- end
  528. --删除基本信息
  529. --参数[filename] 文件名称 如wrench、wind、unit。。。[id] 删除数据id
  530. function delBaseInfo(filename,id,dirPath)
  531. local res = false --删除结果
  532. local count = 0 --查询文件个数
  533. log.info("del process:",filename,id,dirPath)
  534. if not filename then
  535. log.error('func:delBaseInfo','filename not fount')
  536. return res,count,'filename not fount'
  537. end
  538. if not id then
  539. log.error('func:delBaseInfo','id not fount')
  540. return res,count,'id not fount'
  541. end
  542. if not dirPath then
  543. log.error('func:delBaseInfo','dirPath not fount')
  544. return res,count,'dirPath not fount'
  545. end
  546. --获取所有该文件及副本文件
  547. local files,desc = getAllFiles(dirPath,filename)
  548. local filevals = nil --文件内容
  549. if not files then
  550. return res,count,desc
  551. end
  552. local isCurFile = false --要删除文件是否找到
  553. for i,file in pairs(files) do
  554. local isbreak = false
  555. local path = dirPath..'/'..file
  556. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  557. if not filehandle then -- 判断文件是否存在
  558. isbreak = true
  559. end
  560. fileval = filehandle:read("*all") -- 读出文件内容
  561. if not fileval then
  562. isbreak = true
  563. end
  564. local filevaltable = split(fileval,delimiter)
  565. local table_lenght = table_leng(filevaltable)
  566. if table_lenght <= 1 then
  567. isbreak = true
  568. end
  569. local newfileval = "" --删除后文件内容
  570. if not isbreak then
  571. -- body
  572. for a = 1,table_lenght-1 do
  573. local val = json.decode(filevaltable[a])
  574. if val.id ~= id then
  575. data = json.encode(val)
  576. if newfileval ~= "" then
  577. newfileval = newfileval..data..delimiter
  578. else
  579. newfileval = data..delimiter
  580. end
  581. else
  582. isCurFile = true
  583. end
  584. end
  585. end
  586. filehandle:close() -- 关闭文件
  587. if isCurFile then
  588. --删除旧文件
  589. local del = os.remove(path)
  590. log.debug('func:delBaseInfo del file', del)
  591. --保存删除后的内容
  592. local c = io.writeFile(path,newfileval, "a+b")
  593. if c then
  594. count = i
  595. log.info('func:delBaseInfo save file', c)
  596. return true,count,'del success'
  597. else
  598. log.error('error', '基础信息写入失败')
  599. return false,count,'del ok,but save fail'
  600. end
  601. break
  602. else
  603. count = i
  604. end
  605. end
  606. if not isCurFile then
  607. return false,count,'this data not found where id='..id
  608. end
  609. end
  610. function delOfflineCacheTemp( dirPath )
  611. -- body
  612. return rtos.remove_dir(dirPath)
  613. end
  614. -- local res,cnt,desc = delBaseInfo('user',113,'/sdcard0/wind/12345')
  615. -- log.info('del result',res,cnt,desc)
  616. --修改基本信息
  617. --参数[filename] 文件名称 如wrench、wind、unit。。。[data] 要修改的数据 table类型 要包含数据id
  618. --返回结果 result 结果 值true/false count--查询文件数量 desc --结果描述
  619. function updateBaseInfo( filename,data,dirPath)
  620. local res = false --修改结果
  621. local count = 0 --查询文件个数
  622. if not filename then
  623. log.error('func:updateBaseInfo','filename not fount')
  624. return res,count,'filename not fount'
  625. end
  626. if not data.id then
  627. log.error('func:updateBaseInfo','data not fount')
  628. return res,count,'the id of data is not fount'
  629. end
  630. --获取所有该文件及副本文件
  631. local files,desc = getAllFiles(dirPath,filename)
  632. local filevals = nil --文件内容
  633. if not files then
  634. return res,count,desc
  635. end
  636. local isCurFile = false --要修改数据文件是否找到
  637. for i,file in pairs(files) do
  638. local isbreak = false
  639. local path = dirPath..'/'..file
  640. local filehandle = io.open(path, "r") -- 第一个参数是文件名,第二个是打开方式,'r'读模式,'w'写模式,对数据进行覆盖,'a'附加模式,'b'加在模式后面表示以二进制形式打开
  641. if not filehandle then -- 判断文件是否存在
  642. isbreak = true
  643. end
  644. fileval = filehandle:read("*all") -- 读出文件内容
  645. if not fileval then
  646. isbreak = true
  647. end
  648. local filevaltable = split(fileval,delimiter)
  649. local table_length = table_leng(filevaltable)
  650. if table_length <= 1 then
  651. isbreak = true
  652. end
  653. local newfileval = nil --删除后文件内容
  654. if not isbreak then
  655. -- body
  656. for a = 1,table_length-1 do
  657. local val = json.decode(filevaltable[a])
  658. if val.id ~= data.id then --过滤要修改的那条数据
  659. dataStr = json.encode(val)
  660. if newfileval then
  661. newfileval = newfileval..dataStr..delimiter
  662. else
  663. newfileval = dataStr..delimiter
  664. end
  665. else
  666. --将新数据放入新的文件内容中
  667. local newData = json.encode(data)
  668. if newfileval then
  669. newfileval = newfileval..newData..delimiter
  670. else
  671. newfileval = newData..delimiter
  672. end
  673. isCurFile = true
  674. end
  675. end
  676. end
  677. filehandle:close() -- 关闭文件
  678. if isCurFile then
  679. if newfileval then
  680. --删除旧文件
  681. local del = os.remove(path)
  682. if not del then
  683. return false,count,'func:updateBaseInfo del fail,please reoperate'
  684. end
  685. --保存删除后的内容
  686. local c = io.writeFile(path,newfileval, "a+b")
  687. if c then
  688. count = i
  689. return true,count,'func:updateBaseInfo update success'
  690. else
  691. return false,count,'func:updateBaseInfo del success,but save fail'
  692. end
  693. else
  694. return false,count,'the file content after del create fail'
  695. end
  696. break
  697. else
  698. count = i
  699. end
  700. end
  701. if not isCurFile then
  702. return false,count,'this data not found where id='..data.id
  703. end
  704. end
  705. sys.taskInit(function()
  706. -- while true do
  707. -- log.info("test00")
  708. -- led.blinkPwm(blueLed,2000,1000)
  709. -- led.blinkPwm(redLed,3000,1000)
  710. -- sys.wait(2000)
  711. -- end
  712. sys.wait(5000)
  713. local moutedFlag = io.mount(io.SDCARD)
  714. log.info('mount sd card', moutedFlag)
  715. local sdCardTotalSize = rtos.get_fs_total_size(1,1)
  716. log.info("sd card total size "..sdCardTotalSize.." KB")
  717. if moutedFlag == 1 then
  718. -- body
  719. sys.publish("IO_SDCARD_MOUTED")
  720. end
  721. end)