modbusTT.lua 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. --- modbus模块功能
  2. -- @module modbus
  3. -- @author Dozingfiretruck
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2020.12.17
  7. module(...,package.seeall)
  8. require"utils"
  9. require"common"
  10. require"pm"
  11. require"pins"
  12. -- require "sdModuel"
  13. -- require "logModuel"
  14. --保持系统处于唤醒状态,此处只是为了测试需要,所以此模块没有地方调用pm.sleep("testUart")休眠,不会进入低功耗休眠状态
  15. --在开发“要求功耗低”的项目时,一定要想办法保证pm.wake("modbusrtu")后,在不需要串口时调用pm.sleep("testUart")
  16. local uart_id = 1
  17. -- local uart_baud = 9600
  18. local uart_baud = 115200
  19. -- pmd.ldoset(2,pmd.LDO_VMMC)
  20. local BAT_ADC_ID = 2
  21. adc.open(BAT_ADC_ID)
  22. local queryCondReg = nil --1000
  23. local queryCondRegValue = {} --
  24. local queryFlagReg = {}
  25. queryFlagReg["0064"] = "0000"
  26. queryFlagReg["0065"] = "0000"
  27. queryFlagReg["0066"] = "0000"
  28. queryFlagReg["0067"] = "0000"
  29. queryFlagReg["0068"] = "0000"
  30. queryFlagReg["0069"] = "0000"
  31. queryFlagReg["006a"] = "0000"
  32. queryFlagReg["006b"] = "0000"
  33. queryFlagReg["006c"] = "0000"
  34. queryFlagReg["006d"] = "0000"
  35. queryFlagReg["006e"] = "0000"
  36. queryFlagReg["006f"] = "0000"
  37. queryFlagReg["0070"] = "0000"
  38. local queryFlagResReg = {} -- 200
  39. queryFlagResReg["00c8"] = "0000"
  40. queryFlagResReg["00c9"] = "0000"
  41. queryFlagResReg["00ca"] = "0000"
  42. queryFlagResReg["00cb"] = "0000"
  43. queryFlagResReg["00cc"] = "0000"
  44. queryFlagResReg["00cd"] = "0000"
  45. queryFlagResReg["00ce"] = "0000"
  46. queryFlagResReg["00cf"] = "0000"
  47. queryFlagResReg["00d0"] = "0000"
  48. queryFlagResReg["00d1"] = "0000"
  49. queryFlagResReg["00d2"] = "0000"
  50. queryFlagResReg["00d3"] = "0000"
  51. queryFlagResReg["00d4"] = "0000" -- 1400
  52. local queryRespData = {} --
  53. local newPumbModelT = ""
  54. local newPumbNumberT = ""
  55. local oldPumbModelT = ""
  56. local oldPumbNumberT = ""
  57. local sensorNumberT = ""
  58. -- 获取table长度
  59. function table_leng(t)
  60. local leng=0
  61. for k, v in pairs(t) do
  62. leng=leng+1
  63. end
  64. return leng;
  65. end
  66. -- 将sting字节流转为字符串57 53 2D 54 2E 36 2D 30 35 31 00 34
  67. function getByte2AsciiStr( data )
  68. -- body
  69. local str = ""
  70. for i=1,#data, 2 do
  71. if string.char(data:byte(i)) == "\0" then
  72. -- body
  73. if string.char(data:byte(i+1)) == "\0" then
  74. -- body
  75. else
  76. str = str..string.char(data:byte(i+1))
  77. end
  78. break
  79. end
  80. str = str.. string.char(data:byte(i+1)) .. string.char(data:byte(i)) -- 011003E8000A14727731653332353437363938003700000000000000003774
  81. end
  82. return str
  83. end
  84. -- 将utf8str转16进制字符串,按宽度补0
  85. function getAscii2StringHexBe( str, width )
  86. -- body
  87. local hexStr = ""
  88. -- log.info(str)
  89. if type(str) == "userdata" then
  90. -- body
  91. str = "\0"
  92. end
  93. if width == 0 then
  94. -- body
  95. return hexStr
  96. end
  97. for i=1,#str, 2 do
  98. if i+1 > #str then
  99. -- body
  100. hexStr = hexStr .. string.format("%02x", string.byte("\0")) ..string.format("%02x", str:byte(i))
  101. break
  102. end
  103. hexStr = hexStr .. string.format("%02x", str:byte(i+1)) .. string.format("%02x", str:byte(i))
  104. end
  105. if (width - #str) > 0 then
  106. -- body
  107. local clen = 0
  108. if not IsOuNumber(#str) then
  109. clen = width - #str -1
  110. else
  111. clen = width - #str
  112. end
  113. for i=1,clen do
  114. hexStr = hexStr .. string.format("%02x", 0)
  115. end
  116. end
  117. return hexStr
  118. end
  119. -- 获取格式化日期时间
  120. function getDateTimeStr( )
  121. local tm = os.date("*t")
  122. return string.format("%04d%02d%02d%02d%02d%02d", tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec)
  123. end
  124. function IsOuNumber(num)
  125. local num1,num2=math.modf(num/2)--返回整数和小数部分
  126. if(num2==0)then
  127. return true
  128. else
  129. return false
  130. end
  131. end
  132. function ucs2deleteZero( ucs2Byte )
  133. -- body
  134. local ustr = ""
  135. for i=1,#ucs2Byte, 2 do
  136. local ascii1, ascii2 = ucs2Byte:byte(i),ucs2Byte:byte(i+1)
  137. if ascii1 == 0 and ascii2 ==0 then
  138. -- body
  139. break
  140. else
  141. ustr = ustr..common.ucs2beToUtf8(ucs2Byte:sub(i, i+1))
  142. end
  143. end
  144. return ustr
  145. end
  146. local function handlePumbHeart( startAddress, regNumber, data )
  147. local startAddDec = tonumber(startAddress, 16)
  148. local regNumberDec = tonumber(regNumber, 16)
  149. -- 01 10 0191 0001 02 0001 6bd1
  150. for i=1, 2*regNumberDec , 2 do
  151. local value = tonumber(data:sub(i,i+1):toHex(), 16)
  152. if startAddDec == 401 then
  153. -- body
  154. -- log.info("pumbStatus", startAddDec, value)
  155. if nvm.get("pumbStatus") ~= value then
  156. -- body
  157. nvm.set("pumbStatus", value)
  158. end
  159. end
  160. if startAddDec == 410 then
  161. -- body
  162. -- log.info("hydOil", startAddDec, value)
  163. if nvm.get("hydOil") ~= value then
  164. -- body
  165. nvm.set("hydOil", value)
  166. end
  167. end
  168. if startAddDec == 411 then
  169. -- body
  170. -- log.info("scaleValve", startAddDec, value)
  171. if nvm.get("scaleValve") ~= value then
  172. -- body
  173. nvm.set("scaleValve", value)
  174. end
  175. end
  176. if startAddDec == 412 then
  177. -- body
  178. -- log.info("dianci", startAddDec, value)
  179. if nvm.get("elecValve") ~= value then
  180. -- body
  181. nvm.set("elecValve", value)
  182. end
  183. end
  184. if startAddDec == 413 then
  185. -- body
  186. -- log.info("dianci", startAddDec, value)
  187. if nvm.get("filterValue") ~= value then
  188. -- body
  189. nvm.set("elecValve", value)
  190. end
  191. end
  192. if startAddDec > 419 and startAddDec < 430 then
  193. -- body
  194. -- log.info("dianci", startAddDec, value)
  195. newPumbModelT = newPumbModelT .. data:sub(i,i+1)
  196. if #newPumbModelT == 20 then
  197. -- body
  198. newPumbModelT = getByte2AsciiStr(newPumbModelT)
  199. nvm.set("newPumbModel", newPumbModelT)
  200. newPumbModelT = ""
  201. end
  202. end
  203. if startAddDec > 429 and startAddDec < 440 then
  204. -- body
  205. -- log.info("dianci", startAddDec, value)
  206. newPumbNumberT = newPumbNumberT .. data:sub(i,i+1)
  207. if #newPumbNumberT == 20 then
  208. -- body
  209. newPumbNumberT = getByte2AsciiStr(newPumbNumberT)
  210. nvm.set("newPumbNumber", newPumbNumberT)
  211. newPumbNumberT = ""
  212. end
  213. end
  214. if startAddDec > 439 and startAddDec < 450 then
  215. -- body
  216. -- log.info("dianci", startAddDec, value)
  217. oldPumbModelT = oldPumbModelT .. data:sub(i,i+1)
  218. if #oldPumbModelT == 20 then
  219. -- body
  220. oldPumbModelT = getByte2AsciiStr(oldPumbModelT)
  221. nvm.set("oldPumbModel", oldPumbModelT)
  222. oldPumbModelT = ""
  223. end
  224. end
  225. if startAddDec > 449 and startAddDec < 460 then
  226. -- body
  227. -- log.info("dianci", startAddDec, value)
  228. oldPumbNumberT = oldPumbNumberT .. data:sub(i,i+1)
  229. if #oldPumbNumberT == 20 then
  230. -- body
  231. oldPumbNumberT = getByte2AsciiStr(oldPumbNumberT)
  232. nvm.set("oldPumbNumber", oldPumbNumberT)
  233. oldPumbNumberT = ""
  234. end
  235. end
  236. if startAddDec > 459 and startAddDec < 470 then
  237. -- body
  238. -- log.info("dianci", startAddDec, value)
  239. sensorNumberT = sensorNumberT .. data:sub(i,i+1)
  240. if #sensorNumberT == 20 then
  241. -- body
  242. sensorNumberT = getByte2AsciiStr(sensorNumberT)
  243. nvm.set("sensorNumber", sensorNumberT)
  244. sensorNumberT = ""
  245. end
  246. end
  247. startAddDec = startAddDec +1
  248. end
  249. end
  250. local function mobBussErrSend (slaveAdd, functionCode, errcode )
  251. -- body
  252. local errRespData, errRespCrc
  253. errRespData = (slaveAdd..functionCode..errcode):fromHex()
  254. errRespCrc = pack.pack('<h', crypto.crc16("MODBUS",errRespData))
  255. logModuel.debug_log("send 485:"..(errRespData..errRespCrc):toHex() )
  256. uart.write(uart_id, errRespData..errRespCrc)
  257. end
  258. -- 处理接收到的数据
  259. local function proc( data )
  260. -- body 010600CF00017835
  261. orginDataHex =string.toHex(data)
  262. if not orginDataHex then
  263. -- body
  264. return false
  265. end
  266. if #data < 8 then
  267. -- body
  268. return false
  269. end
  270. if not crc16Compare(orginDataHex) then
  271. -- body
  272. logModuel.debug_log("data:"..orginDataHex..",CRC check failed!")
  273. return false
  274. end
  275. local slaveAdd = string.format("%02x",data:byte(1))
  276. local functionCode = string.format("%02x",data:byte(2))
  277. local startRegAddress = string.format("%02x",data:byte(3)) ..string.format("%02x",data:byte(4))
  278. local multiRespData = ""
  279. local respCrc = ""
  280. local errFunctionCode = ""
  281. if not slaveAdd or not functionCode then
  282. -- body
  283. logModuel.debug_log("data:"..orginDataHex..",没有从机码和功能码!")
  284. return false
  285. end
  286. if functionCode ~= "06" and functionCode ~= "10" and functionCode ~= "03" then
  287. -- body
  288. if not tonumber(functionCode) then
  289. -- body
  290. errFunctionCode = functionCode
  291. else
  292. errFunctionCode = tonumber(functionCode) + 80
  293. end
  294. multiRespData = (slaveAdd..errFunctionCode.."01"):fromHex()
  295. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  296. uart.write(uart_id, multiRespData..respCrc)
  297. logModuel.debug_log("data:"..orginDataHex..",非 03, 06, 10功能码,不允许操作!")
  298. return false
  299. end
  300. local regAddHex = tonumber(startRegAddress, 16)
  301. if functionCode == "06" then
  302. -- body
  303. local value = string.format("%02x",data:byte(5)).. string.format("%02x",data:byte(6))
  304. if not value then
  305. -- body
  306. mobBussErrSend(slaveAdd, "86", "03")
  307. return false
  308. end
  309. if regAddHex >= 100 and regAddHex < 200 then
  310. -- body
  311. queryFlagReg[startRegAddress] = "0001"
  312. setQueryFlagData(startRegAddress, data)
  313. end
  314. if regAddHex >= 200 and regAddHex < 300 then
  315. mobBussErrSend(slaveAdd, "86", "01")
  316. return false
  317. end
  318. if regAddHex >= 1400 then
  319. mobBussErrSend(slaveAdd, "86", "01")
  320. return false
  321. end
  322. multiRespData = (slaveAdd..functionCode..startRegAddress..value):fromHex()
  323. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  324. uart.write(uart_id, multiRespData..respCrc)
  325. end
  326. if functionCode == "10" then
  327. -- body
  328. if #data < 11 then
  329. -- body
  330. mobBussErrSend(slaveAdd, "90", "01")
  331. return false
  332. end
  333. local regNumber = string.format("%02x",data:byte(5)) .. string.format("%02x",data:byte(6))
  334. if regAddHex >= 1000 and regAddHex < 1400 then
  335. -- body 01 10 03e8 0001 02 0000 aaaa
  336. setQueryCondRegData(startRegAddress, data)
  337. end
  338. if regAddHex >= 100 and regAddHex < 200 then
  339. -- body
  340. if regAddHex > 212 then
  341. -- body
  342. mobBussErrSend(slaveAdd, "90", "01")
  343. return false
  344. end
  345. queryFlagReg[startRegAddress] = "0001"
  346. setQueryFlagData(startRegAddress, data)
  347. end
  348. if regAddHex >= 200 and regAddHex < 300 then
  349. -- body
  350. mobBussErrSend(slaveAdd, "90", "01")
  351. return false
  352. end
  353. if regAddHex >= 400 and regAddHex < 500 then --泵心跳相关
  354. -- queryFlagResReg[startRegAddress] = data
  355. local byteCount = string.format("%02x",data:byte(7))
  356. handlePumbHeart(startRegAddress, regNumber,data:sub(8, tonumber(byteCount, 16)+7))
  357. end
  358. if regAddHex >= 1400 then
  359. mobBussErrSend(slaveAdd, "90", "01")
  360. return false
  361. end
  362. multiRespData = (slaveAdd..functionCode..startRegAddress..regNumber):fromHex()
  363. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  364. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  365. uart.write(uart_id, multiRespData..respCrc)
  366. end
  367. if functionCode == "03" then
  368. -- body
  369. local regNumber = string.format("%02x",data:byte(5)) .. string.format("%02x",data:byte(6))
  370. local respValue = ""
  371. log.info("regAddHex:", regAddHex, "startAdd:",startRegAddress)
  372. if regAddHex >= 100 and regAddHex < 200 then
  373. -- body
  374. respValue = queryFlagReg[startRegAddress]
  375. if not respValue then
  376. -- body
  377. mobBussErrSend(slaveAdd, "83", "01")
  378. return false
  379. end
  380. byteCount = "02"
  381. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  382. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  383. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  384. uart.write(uart_id, multiRespData..respCrc)
  385. return true
  386. end
  387. if regAddHex >= 200 and regAddHex < 300 then
  388. -- body
  389. respValue = queryFlagResReg[startRegAddress]
  390. if not respValue then
  391. -- body
  392. mobBussErrSend(slaveAdd, "83", "01")
  393. return false
  394. end
  395. byteCount = "02"
  396. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  397. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  398. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  399. uart.write(uart_id, multiRespData..respCrc)
  400. return true
  401. end
  402. if regAddHex >= 1400 then
  403. -- body
  404. if tonumber(regNumber, 16) == 1 then
  405. -- body
  406. log.info("startRegAddress:",startRegAddress, "value:", queryRespData[startRegAddress])
  407. respValue = queryRespData[startRegAddress]
  408. if not respValue then
  409. -- body
  410. mobBussErrSend(slaveAdd, "83", "01")
  411. return false
  412. end
  413. else
  414. local regNumberDec,startRegAddressDec = tonumber(regNumber,16), tonumber(startRegAddress,16)
  415. if regNumberDec > table_leng(queryRespData) then
  416. -- body
  417. logModuel.debug_log("read regNumber > store queryRespData table length!")
  418. mobBussErrSend(slaveAdd, "83", "03")
  419. return false
  420. end
  421. for i=startRegAddressDec, (startRegAddressDec + regNumberDec-1) do
  422. local regHex = string.format("%04x", i)
  423. if queryRespData[regHex] then
  424. -- body
  425. respValue = respValue..queryRespData[regHex]
  426. else
  427. logModuel.debug_log("for in regNumber > store queryRespData table max regNumber!")
  428. mobBussErrSend(slaveAdd, "83", "03")
  429. return false
  430. end
  431. end
  432. end
  433. if not respValue then
  434. -- body
  435. logModuel.debug_log("modbus.proc error! maybe receive data unusual or sequence not correct! regAddHex=" .. regAddHex)
  436. return
  437. end
  438. byteCount = string.format("%02x", tonumber(regNumber, 16)*2)
  439. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  440. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  441. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex() )
  442. uart.write(uart_id, multiRespData..respCrc)
  443. return true
  444. end
  445. if regAddHex >= 600 and regAddHex < 700 then
  446. -- body
  447. local regNumberDec,startRegAddressDec = tonumber(regNumber,16), tonumber(startRegAddress,16)
  448. local networkInfo = getNetWorkInfoHex()
  449. local stap = (regAddHex-600)*4+1
  450. local endp = regNumberDec*4 + stap-1
  451. for i=stap,endp, 4 do
  452. respValue = respValue.. networkInfo:sub(i, i+3)
  453. end
  454. byteCount = string.format("%02x", tonumber(regNumber, 16)*2)
  455. multiRespData = (slaveAdd..functionCode..byteCount..respValue):fromHex()
  456. respCrc = pack.pack('<h', crypto.crc16("MODBUS",multiRespData))
  457. logModuel.debug_log("send 485:"..(multiRespData..respCrc):toHex())
  458. uart.write(uart_id, multiRespData..respCrc)
  459. return true
  460. end
  461. mobBussErrSend(slaveAdd, "83", "01")
  462. return false
  463. end
  464. end
  465. function getNetWorkInfoHex( )
  466. -- body
  467. netWorkDeviceData = ""
  468. local dateObj = os.date("*t")
  469. local temp = ""
  470. for i=600,619 do
  471. local regHex = string.format("%04x", i)
  472. if i == 600 then
  473. -- body
  474. temp = "0001"
  475. elseif i == 601 then
  476. -- body
  477. local csq = math.floor( (net.getRssi() / 31 * 100) )
  478. temp = string.format("%04x", csq)
  479. elseif i == 602 then
  480. temp = string.format("%04x", 0)
  481. elseif i == 610 then
  482. temp = string.format("%04x", dateObj.year)
  483. elseif i == 611 then
  484. temp = string.format("%04x", dateObj.month)
  485. elseif i == 612 then
  486. temp = string.format("%04x", dateObj.day)
  487. elseif i == 613 then
  488. temp = string.format("%04x", dateObj.hour)
  489. elseif i == 614 then
  490. temp = string.format("%04x", dateObj.min)
  491. elseif i == 615 then
  492. temp = string.format("%04x", dateObj.sec)
  493. else
  494. temp = "0000"
  495. end
  496. netWorkDeviceData = netWorkDeviceData .. temp
  497. end
  498. local dataVersion = getAscii2StringHexBe(nvm.get("localCntVersion"), 20)
  499. local updateTime = getAscii2StringHexBe(nvm.get("localCntUpdateTime"), 20)
  500. local imei = getAscii2StringHexBe(misc.getImei(), 20)
  501. return netWorkDeviceData .. dataVersion ..updateTime ..imei
  502. end
  503. -- 工具可以自定义
  504. -- 机位号
  505. -- 所属部件
  506. -- 工作位置
  507. -- 设置查询条件相关寄存器数据
  508. function setQueryCondRegData( startRegAddress, data )
  509. -- body
  510. if queryCondReg and tonumber(startRegAddress, 16) <= tonumber(queryCondReg, 16) then
  511. -- body
  512. initRegVariable()
  513. log.info("queryReg variable init!")
  514. end
  515. queryCondReg = startRegAddress
  516. local byteCount = string.format("%02x",data:byte(7))
  517. table.insert(queryCondRegValue, data:sub(8, tonumber(byteCount, 16)+7))
  518. log.info("queryCondRegValue first:", queryCondRegValue[1]:toHex() )
  519. end
  520. -- 设置查询标识寄存器数据
  521. function setQueryFlagData( startRegAddress, data )
  522. -- body
  523. -- queryFlagReg = string.format("%02x",data:byte(8)) .. string.format("%02x",data:byte(9))
  524. if startRegAddress then
  525. -- body
  526. sys.publish("pub_queryflag_reg", startRegAddress)
  527. end
  528. log.info("quryflag wrote done:", startRegAddress)
  529. end
  530. -- crc16校验
  531. function crc16Compare( wholeData )
  532. -- body
  533. -- local crc16get = string.format("%02x",wholeData:byte(-2))..string.format("%02x",wholeData:byte(-1))
  534. local crc16get = string.sub(wholeData, -4 )
  535. local dataExceptCrc =string.sub(wholeData, 1, -5 )
  536. log.info("dataExceptCrc :", dataExceptCrc)
  537. local caculateCrc = pack.pack('<h', crypto.crc16("MODBUS",dataExceptCrc:fromHex()))
  538. if crc16get:fromHex() ~= caculateCrc then
  539. -- body
  540. log.info("crc16 compare, crc16get =",crc16get,",caculateCrc=",string.toHex(caculateCrc) )
  541. return false
  542. end
  543. return true
  544. end
  545. -- 寄存器相关变量初始化
  546. function initRegVariable( )
  547. -- body
  548. queryCondReg = nil
  549. queryCondRegValue = {} --
  550. queryFlagReg["0064"] = "0000"
  551. queryFlagReg["0065"] = "0000"
  552. queryFlagReg["0066"] = "0000"
  553. queryFlagReg["0067"] = "0000"
  554. queryFlagReg["0068"] = "0000"
  555. queryFlagReg["0069"] = "0000"
  556. queryFlagReg["006a"] = "0000"
  557. queryFlagReg["006b"] = "0000"
  558. queryFlagReg["006c"] = "0000"
  559. queryFlagReg["006d"] = "0000"
  560. queryFlagReg["006e"] = "0000"
  561. queryFlagReg["006f"] = "0000"
  562. queryFlagReg["0070"] = "0000"
  563. queryFlagResReg["00c8"] = "0000"
  564. queryFlagResReg["00c9"] = "0000"
  565. queryFlagResReg["00ca"] = "0000"
  566. queryFlagResReg["00cb"] = "0000"
  567. queryFlagResReg["00cc"] = "0000"
  568. queryFlagResReg["00cd"] = "0000"
  569. queryFlagResReg["00ce"] = "0000"
  570. queryFlagResReg["00cf"] = "0000"
  571. queryFlagResReg["00d0"] = "0000"
  572. queryFlagResReg["00d1"] = "0000"
  573. queryFlagResReg["00d2"] = "0000"
  574. queryFlagResReg["00d3"] = "0000"
  575. queryFlagResReg["00d4"] = "0000" -- 1400
  576. queryRespData = {}
  577. end
  578. local function modbus_read()
  579. local cacheData = ""
  580. while true do
  581. local s = uart.read(uart_id,1)
  582. if s == "" then
  583. -- if not sys.waitUntil("UART_RECEIVE",35000/uart_baud) then
  584. if not sys.waitUntil("UART_RECEIVE",35000/uart_baud) then
  585. -- 3.5个字符的时间间隔,只是用在RTU模式下面,因为RTU模式没有开始符和结束符,
  586. -- 两个数据包之间只能靠时间间隔来区分,Modbus定义在不同的波特率下,间隔时间是不一样的,
  587. -- 所以就是3.5个字符的时间,波特率高,这个时间间隔就小,波特率低,这个时间间隔相应就大
  588. -- 4800 = 7.297ms
  589. -- 9600 = 3.646ms
  590. -- 19200 = 1.771ms
  591. -- 38400 = 0.885ms
  592. --uart接收数据,如果 35000/uart_baud 毫秒没有收到数据,则打印出来所有已收到的数据,清空数据缓冲区,等待下次数据接收
  593. --注意:
  594. --因为在整个GSM模块软件系统中,软件定时器的精确性无法保证,例如本demo配置的是100毫秒,在系统繁忙时,实际延时可能远远超过100毫秒,达到200毫秒、300毫秒、400毫秒等
  595. --设置的延时时间越短,误差越大
  596. if cacheData:len()>0 then
  597. local a,_ = string.toHex(cacheData)
  598. logModuel.debug_log("read 485:"..a)
  599. --用户逻辑处理代码
  600. --
  601. local resp = proc(cacheData)
  602. cacheData = ""
  603. end
  604. end
  605. else
  606. cacheData = cacheData..s
  607. end
  608. end
  609. end
  610. local function writeOk()
  611. log.info("testUart.writeOk")
  612. end
  613. --10 写多个寄存器
  614. local function writeMultiReg( slaveaddr, functionCode, startAddress, regNumber, values )
  615. -- body
  616. local byteCount = 2*regNumber
  617. local prefix = (string.format("%02x",slaveaddr)..string.format("%02x",functionCode)..string.format("%04x",startAddress)..string.format("%04x",regNumber)..string.format("%04x",byteCount)):fromHex()
  618. local valueHex = ""
  619. for i=1,math.ceil(values:len()/4) do
  620. local t = "0x"..string.sub(values, (i-1)*4+1, 4*i)
  621. valueHex = valueHex.. string.format("%04x", t)
  622. end
  623. local data = prefix..valueHex:fromHex()
  624. local modbus_crc_data= pack.pack('<h', crypto.crc16("MODBUS",data))
  625. local data_tx = data..modbus_crc_data
  626. log.info("uart2Task.write",data_tx)
  627. uart.write(uart_id,data_tx)
  628. end
  629. --06 写单个寄存器
  630. local function writeSingleReg( slaveaddr, functionCode, regAddress, value )
  631. -- body
  632. local data = (string.format("%02x",slaveaddr)..string.format("%02x",functionCode)..string.format("%04x",regAddress)..string.format("%04x",value)):fromHex()
  633. local modbus_crc_data= pack.pack('<h', crypto.crc16("MODBUS",data))
  634. local data_tx = data..modbus_crc_data
  635. log.info("uart2Task.write",data_tx)
  636. uart.write(uart_id,data_tx)
  637. end
  638. -- 查询扳手信息并写入结果寄存器
  639. local function query0064Res()
  640. -- body
  641. respRegAddress = "00c8"
  642. local conds = table.concat(queryCondRegValue)
  643. if #conds < 20 then
  644. -- body
  645. queryFlagReg["0064"] = "0000"
  646. return false
  647. end
  648. local number = getByte2AsciiStr(conds:sub(1, 20))--查询条件
  649. local result, count, desc = getBaseInfo("Wrench", {number=number} , "/sdcard0/common")
  650. queryFlagReg["0064"] = "0000"
  651. queryCondRegValue = {}--清空查询条件
  652. if not result then
  653. -- body
  654. queryFlagResReg[respRegAddress] = "0002"
  655. log.info("查询扳手信息失败, desc:", desc)
  656. logModuel.debug_log("查询扳手信息失败,count:"..count..",desc:"..desc..",querycond_number:"..number)
  657. return false
  658. end
  659. local singleRes = result[1]
  660. local minnj = pack.pack('>I', singleRes.mint)
  661. local maxnj = pack.pack('>I', singleRes.maxt)
  662. local fbl = pack.pack('>I', singleRes.angres)
  663. local resT = {
  664. string.format("%04x", singleRes.qualified),
  665. string.format("%04x", singleRes.type),
  666. string.format("%04x", singleRes.minp),
  667. string.format("%04x", singleRes.maxp),
  668. minnj:sub(3, 4):toHex() .. minnj:sub(1, 2):toHex(),
  669. maxnj:sub(3, 4):toHex() .. maxnj:sub(1, 2):toHex(),
  670. fbl:sub(3, 4):toHex() .. fbl:sub(1, 2):toHex(),
  671. getAscii2StringHexBe(singleRes.clt, 20),
  672. getAscii2StringHexBe(singleRes.model, 20),
  673. getAscii2StringHexBe(singleRes.fixed, 20)
  674. -- 线性方程系数和常数
  675. -- pack.pack(">f", singleRes.eq_a):toHex(),
  676. -- pack.pack(">f", singleRes.eq_b):toHex()
  677. }
  678. local res64Hex = table.concat(resT)
  679. local j = 1
  680. for i=1400,1439 do
  681. local regHex = string.format("%04x", i)
  682. queryRespData[regHex] = string.sub(res64Hex, j, j+3)
  683. j = j+4
  684. end
  685. log.info("queryRespData lenth:", table_leng(queryRespData))
  686. queryFlagResReg[respRegAddress] = "0001"
  687. end
  688. -- 查询法兰历史紧固数据并写入结果寄存器
  689. local function query0065Res()
  690. -- body
  691. respRegAddress = "00c9"
  692. local conds = table.concat(queryCondRegValue)
  693. if #conds < 90 then
  694. -- body
  695. queryFlagReg["0065"] = "0000"
  696. return false
  697. end
  698. local cond = {
  699. wnum = getByte2AsciiStr(conds:sub(1, 10)),
  700. fnum = getByte2AsciiStr(conds:sub(11, 30)),
  701. fmodel = getByte2AsciiStr(conds:sub(31, 50)),
  702. parts = ucs2deleteZero(conds:sub(51, 70)),
  703. work = ucs2deleteZero(conds:sub(71, 110))
  704. }
  705. log.info("wnum:", cond.wnum, "wnum_hex:", conds:sub(1, 10):toHex())
  706. log.info("record cond:",json.encode(cond))
  707. local result, count, desc = getBaseInfo("WorkRecord", cond , "/sdcard0/"..cond.wnum .."/".. cond.fnum)
  708. queryFlagReg["0065"] = "0000"
  709. queryCondRegValue = {}--清空查询条件
  710. if not result then
  711. -- body
  712. queryFlagResReg[respRegAddress] = "0002"
  713. logModuel.debug_log("法兰历史紧固记录查询失败,count:"..count..",desc:"..desc..",querycond:"..json.encode(cond))
  714. return false
  715. end
  716. -- WorkRecord
  717. local res = result[1]
  718. local regValueHex = ""
  719. local total = string.format("%04x", res.bnum)
  720. local lsnj = pack.pack('>I', res.torque)
  721. regValueHex = total.. lsnj:sub(3, 4):toHex() .. lsnj:sub(1, 2):toHex()
  722. -- queryRespData["057c"] = pack.pack('>I', 100)
  723. -- queryRespData["057d"] = pack.pack('>I', 500)
  724. local j = 1
  725. for i=1400,1402 do
  726. local regHex = string.format("%04x", i)
  727. queryRespData[regHex] = regValueHex:sub(j, j+3)
  728. j = j+4
  729. end
  730. queryFlagResReg[respRegAddress] = "0001"
  731. end
  732. -- 用户查询认证
  733. local function query0066Res()
  734. -- body
  735. respRegAddress = "00ca"
  736. -- "0001" 用户名错误 "0002" 密码错误
  737. -- "000a" 操作员 "000b" 管理员
  738. local conds = table.concat(queryCondRegValue)
  739. if #conds < 40 then
  740. -- body
  741. queryFlagReg["0066"] = "0000"
  742. return false
  743. end
  744. local username = getByte2AsciiStr(conds:sub(1, 20))--查询条件--用户名
  745. local password = getByte2AsciiStr(conds:sub(21, 40))--查询条件--密码
  746. local ures, count, desc = getBaseInfo("Users", {name=username} , "/sdcard0/common")
  747. queryFlagReg["0066"] = "0000"
  748. queryCondRegValue = {}--清空查询条件
  749. if not ures then
  750. -- body
  751. logModuel.debug_log("用户名查询失败,count:"..count..",desc:"..desc..",querycond:"..json.encode({name=username}))
  752. queryFlagResReg[respRegAddress] = "0001"
  753. return false
  754. end
  755. local result, rcount, rdesc = getBaseInfo("Users", {name=username, pwd=password} , "/sdcard0/common")
  756. if not result then
  757. -- body
  758. logModuel.debug_log("用户查询认证失败,密码不匹配,count:"..rcount..",desc:"..rdesc..",querycond:"..json.encode({name=username, pwd=password}))
  759. queryFlagResReg[respRegAddress] = "0002"
  760. return false
  761. end
  762. queryFlagResReg[respRegAddress] = string.format("%04x", result[1].perm)
  763. end
  764. -- 查询风场列表并写入结果寄存器
  765. local function query0067Res()
  766. -- body
  767. respRegAddress = "00cb"
  768. local conds = table.concat(queryCondRegValue)
  769. local wnum = ""
  770. if #conds == 10 then
  771. wnum = getByte2AsciiStr(conds:sub(1, 10))
  772. end
  773. local result, count, desc
  774. if wnum ~= "" then
  775. -- body
  776. result, count, desc = getBaseInfo_ununique_dim("Wind", {number=wnum}, "/sdcard0/common")
  777. else
  778. result, count, desc = getBaseInfo_ununique_dim("Wind", nil, "/sdcard0/common")
  779. end
  780. queryFlagReg["0067"] = "0000"
  781. queryCondRegValue = {}--清空查询条件
  782. local lastWorkField = nvm.get("lastWorkField")
  783. if not result and lastWorkField == "" then
  784. -- body
  785. queryFlagResReg[respRegAddress] = "0002"
  786. logModuel.debug_log("查询风场列表失败,count:"..count..",desc:"..desc)
  787. return false
  788. end
  789. local fitWindTable, gpsLng, gpsLat, gpsTime = {}, nvm.get("gpsLng"), nvm.get("gpsLat"), nvm.get("gpsTime")
  790. local fieldDisplayRadius = nvm.get("fieldDisplayRadius") or 200
  791. print("aaaaaaaaaaaa")
  792. if not result then
  793. --无全风场列表显示上次作业风场
  794. -- body
  795. print("bbbbbbbbbbb")
  796. result = getBaseInfo("Wind", {number=lastWorkField}, "/sdcard0/common")
  797. else
  798. if gpsTime == 0 or gpsTime == "0" or wnum ~= "" then
  799. --有风场列表,但GPS未定位成功过显示上次作业风场
  800. -- body
  801. table.sort(result, function(l,t)
  802. return l.number < t.number
  803. end)
  804. else
  805. print("eeeeeeeeeeeeeee")
  806. -- for k,v in pairs(result) do
  807. -- --过滤风场列表中不在方圆20Km内的风场
  808. -- local dist = devTool.getDistance( v.lat, v.lon, gpsLat, gpsLng)
  809. -- log.info("dist:",dist, json.encode(v))
  810. -- if dist < fieldDisplayRadius then
  811. -- -- body
  812. -- -- log.info("cicle",v.lat)
  813. -- v.dist = dist
  814. -- table.insert(fitWindTable, v)
  815. -- end
  816. -- end
  817. fitWindTable = result
  818. if table_leng(fitWindTable) == 0 then
  819. --若过滤后无风场则显示上次作业风场
  820. -- body
  821. print("fffffffffffffffff")
  822. if lastWorkField ~= "" then
  823. -- body
  824. print("ggggggggggggggggggg")
  825. result = getBaseInfo("Wind", {number=lastWorkField}, "/sdcard0/common")
  826. end
  827. else
  828. print("hhhhhhhhhhhhhhhhhhhhhhhh")
  829. table.sort(fitWindTable, function(l,t)
  830. return l.dist < t.dist
  831. end)
  832. result = fitWindTable
  833. -- log.info("fittable:", json.encode(fitWindTable))
  834. end
  835. end
  836. end
  837. print("iiiiiiiiiiiiiiiiiii")
  838. if not result then
  839. -- body
  840. print("jjjjjjjjjjjjjjj")
  841. queryFlagResReg[respRegAddress] = "0002"
  842. logModuel.debug_log("查询风场列表失败过滤逻辑之后")
  843. return false
  844. end
  845. queryRespData["0578"] = string.format("%04x", table_leng(result))
  846. local regValueHex = ""
  847. for k,v in pairs(result) do
  848. local numberHex = getAscii2StringHexBe(v.number, 10)
  849. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  850. local afterHandleNameByte = ""
  851. if #ucs2Bytes > 40 then
  852. -- body
  853. ucs2Bytes = ucs2Bytes:sub(1, 40)
  854. end
  855. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  856. -- log.info("namehex:",v.nameHex, "numberhex:",v.numberHex, "dist:", v.dist, "number", v.number, "name:", v.name)
  857. regValueHex = regValueHex..numberHex..nameHex
  858. end
  859. local j = 1
  860. for i=1410, 1409 + 25*table_leng(result) do
  861. local regHex = string.format("%04x", i)
  862. queryRespData[regHex] = regValueHex:sub(j, j+3)
  863. j = j+4
  864. end
  865. queryFlagResReg[respRegAddress] = "0001"
  866. end
  867. -- 查询风机型号列表并写入结果寄存器
  868. local function query0068Res()
  869. -- body
  870. respRegAddress = "00cc"
  871. local conds = table.concat(queryCondRegValue)
  872. if #conds < 10 then
  873. -- body
  874. queryFlagReg["0068"] = "0000"
  875. return false
  876. end
  877. local wnum = getByte2AsciiStr(conds:sub(1, 10)) --查询条件
  878. local result, count, desc = getBaseInfo_ununique("FanModel", {wnum=wnum} , "/sdcard0/"..wnum)
  879. queryFlagReg["0068"] = "0000"
  880. queryCondRegValue = {}--清空查询条件
  881. if not result then
  882. -- body
  883. queryFlagResReg[respRegAddress] = "0002"
  884. logModuel.debug_log("查询风机型号列表失败,count:"..count..",desc:"..desc..",querycond_wnum:"..wnum)
  885. return false
  886. end
  887. queryRespData["0578"] = string.format("%04x", table_leng(result))
  888. local regHexValue = ""
  889. for k, v in pairs(result) do
  890. regHexValue = regHexValue .. getAscii2StringHexBe(v.model, 20)
  891. end
  892. local j = 1
  893. for i=1410,1409 + table_leng(result) * 10 do
  894. local regHex = string.format("%04x", i)
  895. queryRespData[regHex] = string.sub(regHexValue, j, j+3)
  896. j = j+4
  897. end
  898. queryFlagResReg[respRegAddress] = "0001"
  899. end
  900. -- 查询风机机位号列表并写入结果寄存器
  901. local function query0069Res()
  902. -- body
  903. respRegAddress = "00cd"
  904. local conds = table.concat(queryCondRegValue)
  905. if #conds < 10 then
  906. -- body
  907. queryFlagReg["0069"] = "0000"
  908. return false
  909. end
  910. local wnum = getByte2AsciiStr(conds:sub(1, 10)) --查询条件
  911. -- local result, count, desc = getBaseInfo("Fan", {wnum=wnum} , "/sdcard0/"..wnum)
  912. local result, count, desc = getBaseInfo_ununique("Fan", {wnum=wnum} , "/sdcard0/"..wnum)
  913. queryFlagReg["0069"] = "0000"
  914. queryCondRegValue = {}--清空查询条件
  915. if not result then
  916. -- body
  917. queryFlagResReg[respRegAddress] = "0002"
  918. logModuel.debug_log("查询机位列表失败,count:"..count..",desc:"..desc..",querycond_number:"..wnum)
  919. return false
  920. end
  921. queryRespData["0578"] = string.format("%04x", table_leng(result))
  922. local regValueHex = ""
  923. for k,v in pairs(result) do
  924. local number = v.number
  925. if type(number) =="number" then
  926. -- body
  927. number = tostring(number)
  928. end
  929. local numberHex = getAscii2StringHexBe(number, 20)
  930. regValueHex = regValueHex..numberHex
  931. end
  932. local j = 1
  933. for i=1410, 1409 + 10*table_leng(result) do
  934. local regHex = string.format("%04x", i)
  935. queryRespData[regHex] = regValueHex:sub(j, j+3)
  936. j = j+4
  937. end
  938. queryFlagResReg[respRegAddress] = "0001"
  939. end
  940. -- 查询部件列表并写入结果寄存器
  941. local function query006aRes()
  942. -- body
  943. respRegAddress = "00ce"
  944. local result, count, desc = getBaseInfo_ununique("WorkingParts", nil, "/sdcard0/common")
  945. queryFlagReg["006a"] = "0000"
  946. queryCondRegValue = {}--清空查询条件
  947. if not result then
  948. -- body
  949. queryFlagResReg[respRegAddress] = "0002"
  950. logModuel.debug_log("查询部件列表失败,count:"..count..",desc:"..desc)
  951. return false
  952. end
  953. -- local result = {
  954. -- {id=123, name="测试部件1"},
  955. -- {id=124, name="测试部件2"},
  956. -- {id=125, name="测试部件3"}
  957. -- }
  958. queryRespData["0578"] = string.format("%04x", table_leng(result))
  959. local regValueHex = ""
  960. for k,v in pairs(result) do
  961. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  962. if #ucs2Bytes > 20 then
  963. -- body
  964. ucs2Bytes = ucs2Bytes:sub(1, 20)
  965. end
  966. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 20-#ucs2Bytes)
  967. regValueHex = regValueHex..nameHex
  968. end
  969. local j = 1
  970. for i=1410, 1409 + 10*table_leng(result) do
  971. local regHex = string.format("%04x", i)
  972. queryRespData[regHex] = regValueHex:sub(j, j+3)
  973. j = j+4
  974. end
  975. queryFlagResReg[respRegAddress] = "0001"
  976. end
  977. -- 查询工作位置并写入结果寄存器
  978. local function query006bRes()
  979. respRegAddress = "00cf"
  980. -- query body
  981. local conds = table.concat(queryCondRegValue)
  982. if #conds < 40 then
  983. -- body
  984. queryFlagReg["006b"] = "0000"
  985. return false
  986. end
  987. local fmdel = getByte2AsciiStr(conds:sub(1, 20))
  988. local ucs2Byte = conds:sub(21, 40)
  989. local Parts = ucs2deleteZero(ucs2Byte)
  990. -- local result, count, desc = getBaseInfo_ununique("WorkingPosition", { fmodel=fmdel, Parts=Parts }, "/sdcard0/common")
  991. local result, count, desc = getBaseInfo_ununique("WorkingPosition", { Parts=Parts }, "/sdcard0/common")
  992. -- log.info("ddddddddddddddddddddddd--------",json.encode(result))
  993. queryFlagReg["006b"] = "0000"
  994. queryCondRegValue = {}--清空查询条件
  995. if not result then
  996. -- body
  997. queryFlagResReg[respRegAddress] = "0002"
  998. logModuel.debug_log("查询工作位置列表失败,count:"..count..",desc:"..desc..",querycond:"..json.encode({ fmodel=fmdel, Parts=Parts }))
  999. return false
  1000. end
  1001. queryRespData["0578"] = string.format("%04x", table_leng(result))
  1002. local regValueHex = ""
  1003. for k,v in pairs(result) do
  1004. local ucs2Bytes = common.utf8ToUcs2be(v.name)
  1005. if #ucs2Bytes > 40 then
  1006. -- body
  1007. ucs2Bytes = ucs2Bytes:sub(1, 40)
  1008. end
  1009. local nameHex = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  1010. regValueHex = regValueHex..nameHex
  1011. end
  1012. local j = 1
  1013. for i=1410, 1409 + 20*table_leng(result) do
  1014. local regHex = string.format("%04x", i)
  1015. queryRespData[regHex] = regValueHex:sub(j, j+3)
  1016. j = j+4
  1017. end
  1018. queryFlagResReg[respRegAddress] = "0001"
  1019. end
  1020. -- 查询自定义的3各文件中是否存在,不存在则新增一个
  1021. local function queryCustomAndAdd( fanNumber, part, workPos , pt )
  1022. -- body
  1023. local fanExist = getBaseInfo("Fan", {wnmu=pt.WindFieldNumber, number=fanNumber,model=pt.Model}, "/sdcard0/"..pt.WindFieldNumber)
  1024. if not fanExist then
  1025. -- body
  1026. saveBaseInfo("Fan",{id=0,wnmu=pt.WindFieldNumber, number=fanNumber,model=pt.Model}, "/sdcard0/"..pt.WindFieldNumber)
  1027. end
  1028. local partExist = getBaseInfo("WorkingParts", {name=part}, "/sdcard0/common")
  1029. if not partExist then
  1030. -- body
  1031. saveBaseInfo("WorkingParts",{id=0,name=part}, "/sdcard0/common")
  1032. end
  1033. local workPosExist = getBaseInfo("WorkingPosition", {Parts=part,fmodel=pt.Model, name=workPos}, "/sdcard0/common")
  1034. if not workPosExist then
  1035. -- body
  1036. saveBaseInfo("WorkingPosition",{id=0,Parts=part,fmodel=pt.Model, name=workPos}, "/sdcard0/common")
  1037. end
  1038. end
  1039. -- 上传紧固模式操作记录
  1040. local function query006cRes( )
  1041. respRegAddress = "00d0"
  1042. local conds = table.concat(queryCondRegValue)
  1043. if #conds < 180 then
  1044. -- body
  1045. queryFlagReg["006c"] = "0000"
  1046. return false
  1047. end
  1048. local msgid = devTool.guid()
  1049. local upTable = {
  1050. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1051. WrenchNumber = getByte2AsciiStr(conds:sub(21, 40)), --扳手编号 string
  1052. WindFieldNumber = getByte2AsciiStr(conds:sub(41, 50)), --风场编号 string
  1053. TurbineNumber = getByte2AsciiStr(conds:sub(51, 70)), --机位号 string 自定义
  1054. Model = getByte2AsciiStr(conds:sub(71, 90)), --风机型号 string
  1055. -- PartNumber = common.ucs2beToUtf8(conds:sub(91, 110)), --部件号 Unicode 自定义
  1056. PartNumber = ucs2deleteZero(conds:sub(91, 110)), --部件号 Unicode 自定义
  1057. -- WorkPos = common.ucs2beToUtf8(conds:sub(111, 130)), --工作位置 Unicode 自定义
  1058. WorkPos = ucs2deleteZero(conds:sub(111, 150)), --工作位置 Unicode 自定义
  1059. BoltModel = getByte2AsciiStr(conds:sub(151, 160)), --螺栓型号 string
  1060. BoltTotal = tonumber(conds:sub(161, 162):toHex(), 16), --螺栓总数 u16
  1061. BoltNumber = tonumber(conds:sub(163, 164):toHex(), 16), --当前螺栓编号 u16
  1062. SetTorque = tonumber(conds:sub(167,168):toHex()..conds:sub(165,166):toHex(), 16), --设定扭矩Nm u32
  1063. FasteningTorque = tonumber(conds:sub(171,172):toHex()..conds:sub(169,170):toHex(), 16), --紧固扭矩Nm u32
  1064. SetStress = tonumber(conds:sub(173,174):toHex(), 16), --设定压力bar u16
  1065. FasteningStress = tonumber(conds:sub(175,176):toHex(), 16), --紧固压力bar u16
  1066. FasteningStatus = tonumber(conds:sub(177,178):toHex(), 16), --紧固状态 u16
  1067. Time = tonumber(conds:sub(179,180):toHex(), 16), --紧固时间 u16
  1068. netWorkSaveTime = os.time(),
  1069. msgid = msgid,
  1070. id = msgid
  1071. }
  1072. -- 查询自定义的3字段是否存在,不存在则新增一个
  1073. queryCustomAndAdd(upTable.TurbineNumber, upTable.PartNumber, upTable.WorkPos, {Model=upTable.Model, WindFieldNumber = upTable.WindFieldNumber})
  1074. -- 待上传 upTable 全部
  1075. -- if socket.isReady() then
  1076. -- -- body
  1077. -- sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordMA",["data"]=upTable}))
  1078. -- local uploadRes,msgid = sys.waitUntil("WAIT_UPDATA_RESP_SUCCESS")
  1079. -- else
  1080. saveBaseInfo("wait2upload", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordFA",["data"]=upTable,["id"]=msgid}, "/sdcard0/common/temp")
  1081. logModuel.debug_log("保存本地待上传作业记录FA, msgid:"..msgid)
  1082. -- end
  1083. -- 工作记录表 boltTotal, fastenNm 风场编号+机位号+风机型号+部件号+工作位置
  1084. local localRecord = {
  1085. id = upTable.WindFieldNumber..upTable.TurbineNumber..upTable.Model..upTable.PartNumber..upTable.WorkPos,
  1086. wnum = upTable.WindFieldNumber,
  1087. fnum = upTable.TurbineNumber,
  1088. fmodel = upTable.Model,
  1089. parts = upTable.PartNumber,
  1090. work = upTable.WorkPos,
  1091. bnum = upTable.BoltTotal,
  1092. torque = upTable.FasteningTorque
  1093. }
  1094. log.info("local record", json.encode(localRecord))
  1095. local result,count, desc = updateBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum .."/".. localRecord.fnum)
  1096. if not result or desc:sub(1, 29) == "this data not found where id=" then
  1097. -- body
  1098. result, desc = saveBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum .."/".. localRecord.fnum )
  1099. end
  1100. log.info("local result", result)
  1101. queryFlagReg["006c"] = "0000"
  1102. if not result then
  1103. -- body
  1104. logModuel.debug_log("上传紧固模式操作记录--写本地工作记录文件失败, desc:"..desc)
  1105. queryFlagResReg[respRegAddress] = "0002"
  1106. else
  1107. nvm.set("lastWorkField", localRecord.wnum)
  1108. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1109. end
  1110. end
  1111. -- 上传维护模式操作记录
  1112. local function query006dRes()
  1113. respRegAddress = "00d1"
  1114. local conds = table.concat(queryCondRegValue)
  1115. if #conds < 162 then
  1116. -- body
  1117. queryFlagReg["006d"] = "0000"
  1118. return false
  1119. end
  1120. local msgid = devTool.guid()
  1121. local upTable = {
  1122. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1123. WrenchNumber = getByte2AsciiStr(conds:sub(21, 40)), --扳手编号 string
  1124. WindFieldNumber = getByte2AsciiStr(conds:sub(41, 50)), --风场编号 string
  1125. TurbineNumber = getByte2AsciiStr(conds:sub(51, 70)), --机位号 string 自定义
  1126. Model = getByte2AsciiStr(conds:sub(71, 90)), --风机型号 string
  1127. -- PartNumber = common.ucs2beToUtf8(conds:sub(91, 110)), --部件号 Unicode 自定义
  1128. PartNumber = ucs2deleteZero(conds:sub(91, 110)), --部件号 Unicode 自定义
  1129. -- WorkPos = common.ucs2beToUtf8(conds:sub(111, 130)), --工作位置 Unicode 自定义
  1130. WorkPos = ucs2deleteZero(conds:sub(111, 150)), --工作位置 Unicode 自定义
  1131. BoltModel = getByte2AsciiStr(conds:sub(151, 160)), --螺栓型号 string
  1132. BoltTotal = tonumber(conds:sub(161, 162):toHex(), 16), --螺栓总数 u16
  1133. BoltNumber = tonumber(conds:sub(163, 164):toHex(), 16), --当前螺栓编号 u16
  1134. SetTorque = tonumber(conds:sub(167,168):toHex()..conds:sub(165,166):toHex(), 16), --设定扭矩Nm u32
  1135. FasteningTorque = tonumber(conds:sub(171,172):toHex()..conds:sub(169,170):toHex(), 16), --紧固扭矩Nm u32
  1136. SetStress = tonumber(conds:sub(173,174):toHex(), 16), --设定压力bar u16
  1137. FasteningStress = tonumber(conds:sub(175,176):toHex(), 16), --紧固压力bar u16
  1138. FasteningStatus = tonumber(conds:sub(177,178):toHex(), 16), --紧固状态 u16
  1139. Time = tonumber(conds:sub(179,180):toHex(), 16), --紧固时间 u16
  1140. WrenchAngle = tonumber(conds:sub(181,182):toHex(), 16), --扳手转动角度 u16
  1141. netWorkSaveTime = os.time(),
  1142. msgid = msgid,
  1143. id = msgid
  1144. }
  1145. -- 查询自定义的3字段是否存在,不存在则新增一个
  1146. queryCustomAndAdd(upTable.TurbineNumber, upTable.PartNumber, upTable.WorkPos, {Model=upTable.Model, WindFieldNumber = upTable.WindFieldNumber})
  1147. -- 待上传 upTable 全部
  1148. -- if socket.isReady() and (net.getRssi() > 14) then
  1149. -- -- body
  1150. -- sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordFA",["data"]=upTable}))
  1151. -- else
  1152. saveBaseInfo("wait2upload", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordMA",["data"]=upTable,["id"]=msgid}, "/sdcard0/common/temp")
  1153. logModuel.debug_log("保存本地待上传作业记录MA, msgid:"..msgid)
  1154. -- end
  1155. -- 工作记录表 boltTotal, fastenNm 风场编号+机位号+风机型号+部件号+工作位置
  1156. local localRecord = {
  1157. id = upTable.WindFieldNumber..upTable.TurbineNumber..upTable.Model..upTable.PartNumber..upTable.WorkPos,
  1158. wnum = upTable.WindFieldNumber,
  1159. fnum = upTable.TurbineNumber,
  1160. fmodel = upTable.Model,
  1161. parts = upTable.PartNumber,
  1162. work = upTable.WorkPos,
  1163. bnum = upTable.BoltTotal,
  1164. torque = upTable.FasteningTorque
  1165. }
  1166. local result,count, desc = updateBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum )
  1167. if not result and desc:sub(1, 29) == "this data not found where id=" then
  1168. -- body
  1169. result, desc = saveBaseInfo("WorkRecord", localRecord, "/sdcard0/"..localRecord.wnum )
  1170. end
  1171. log.info("local result", result)
  1172. queryFlagReg["006d"] = "0000"
  1173. if not result then
  1174. -- body
  1175. queryFlagResReg[respRegAddress] = "0002"
  1176. logModuel.debug_log("上传维护模式操作记录--写本地工作记录文件失败, desc:"..desc)
  1177. else
  1178. nvm.set("lastWorkField", localRecord.wnum)
  1179. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1180. end
  1181. end
  1182. -- 上传维护模式操作记录
  1183. local function query006eRes()
  1184. respRegAddress = "00d2"
  1185. local conds = table.concat(queryCondRegValue)
  1186. if #conds < 24 then
  1187. -- body
  1188. queryFlagReg["006e"] = "0000"
  1189. return false
  1190. end
  1191. local msgid = devTool.guid()
  1192. local upTable = {
  1193. Username =getByte2AsciiStr(conds:sub(1, 20)), --用户名 string
  1194. SetStress = tonumber(conds:sub(21,22):toHex(), 16), --设定压力bar u16
  1195. FasteningStress = tonumber(conds:sub(23,24):toHex(), 16), --紧固压力bar u16
  1196. msgid = msgid,
  1197. netWorkSaveTime = os.time(),
  1198. id = msgid
  1199. }
  1200. -- 待上传 upTable 全部
  1201. local result = saveBaseInfo("wait2upload", {["topic"]="SHEGCL/IntelligenTool/UploadWorkRecordSimple",["data"]=upTable,["id"]=msgid}, "/sdcard0/common/temp")
  1202. queryFlagReg["006e"] = "0000"
  1203. if not result then
  1204. -- body
  1205. queryFlagResReg[respRegAddress] = "0002"
  1206. logModuel.debug_log("上传维护模式操作(仅次数)记录--写本地工作记录文件失败, desc:"..desc)
  1207. else
  1208. logModuel.debug_log("保存本地待上传作业记录SA, msgid:"..msgid)
  1209. queryFlagResReg[respRegAddress] = "0001" --结果置1表示操作成功
  1210. end
  1211. end
  1212. -- 获取任务编号列表
  1213. local function query006fRes()
  1214. -- body
  1215. respRegAddress = "00d3"
  1216. local conds = table.concat(queryCondRegValue)
  1217. if #conds ~= 30 then
  1218. -- body
  1219. queryFlagReg["006f"] = "0000"
  1220. return false
  1221. end
  1222. local wind = getByte2AsciiStr(conds:sub(1, 10))--查询条件
  1223. local fmodel = getByte2AsciiStr(conds:sub(11, 30))--查询条件
  1224. local result, count, desc = getBaseInfo_ununique("WorkPlan", {wnum=wind,fmodel=fmodel}, "/sdcard0/common")
  1225. queryFlagReg["006f"] = "0000"
  1226. queryCondRegValue = {}--清空查询条件
  1227. if not result then
  1228. -- body
  1229. queryFlagResReg[respRegAddress] = "0002"
  1230. logModuel.debug_log("查询任务编号列表失败,count:"..count..",desc:"..desc)
  1231. return false
  1232. end
  1233. queryRespData["0578"] = string.format("%04x", table_leng(result))
  1234. local regValueHex = ""
  1235. for k,v in pairs(result) do
  1236. local pnum = getAscii2StringHexBe(v.pnum, 20)
  1237. local ucs2Bytes = common.utf8ToUcs2be(v.info)
  1238. if #ucs2Bytes > 40 then
  1239. -- body
  1240. ucs2Bytes = ucs2Bytes:sub(1, 40)
  1241. end
  1242. local info = ucs2Bytes:toHex()..getAscii2StringHexBe("\0", 40-#ucs2Bytes)
  1243. log.info("pnum:", pnum, "info:", info)
  1244. regValueHex = regValueHex..pnum..info
  1245. end
  1246. local j = 1
  1247. for i=1410, 1409 + 30*table_leng(result) do
  1248. local regHex = string.format("%04x", i)
  1249. queryRespData[regHex] = regValueHex:sub(j, j+3)
  1250. j = j+4
  1251. end
  1252. queryFlagResReg[respRegAddress] = "0001"
  1253. end
  1254. -- 获取任务编号内容
  1255. local function query0070Res()
  1256. -- body
  1257. respRegAddress = "00d4"
  1258. queryFlagResReg[respRegAddress] = "0002"
  1259. local conds = table.concat(queryCondRegValue)
  1260. if #conds < 20 then
  1261. -- body
  1262. queryFlagReg["0070"] = "0000"
  1263. return false
  1264. end
  1265. local cond = getByte2AsciiStr(conds:sub(1,20))
  1266. local result, count, desc = getBaseInfo("WorkPlan", {pnum=cond}, "/sdcard0/common")
  1267. queryFlagReg["0070"] = "0000"
  1268. queryCondRegValue = {}--清空查询条件
  1269. if not result then
  1270. -- body
  1271. logModuel.debug_log("查询任务具体详情失败,count:"..count..",desc:"..desc,",cond_pnum="..cond)
  1272. return false
  1273. end
  1274. local singleRes = result[1]
  1275. local lt = pack.pack('>I', singleRes.lt)
  1276. local torque = pack.pack('>I', singleRes.torque)
  1277. local wname = common.utf8ToUcs2be(singleRes.wname)
  1278. local parts = common.utf8ToUcs2be(singleRes.parts)
  1279. local work = common.utf8ToUcs2be(singleRes.work)
  1280. if #wname > 40 then
  1281. -- body
  1282. wname = wname:sub(1, 40)
  1283. end
  1284. if #parts > 20 then
  1285. -- body
  1286. parts = parts:sub(1, 20)
  1287. end
  1288. if #work > 40 then
  1289. -- body
  1290. work = work:sub(1, 40)
  1291. end
  1292. local resT = {
  1293. getAscii2StringHexBe(singleRes.wnum, 10),
  1294. wname:toHex()..getAscii2StringHexBe("\0", 40-#wname),
  1295. getAscii2StringHexBe(singleRes.fnum, 20),
  1296. getAscii2StringHexBe(singleRes.fmodel, 20),
  1297. parts:toHex()..getAscii2StringHexBe("\0", 20-#parts),
  1298. work:toHex()..getAscii2StringHexBe("\0", 40-#work),
  1299. getAscii2StringHexBe(singleRes.bm, 20),
  1300. string.format("%04x", singleRes.bn),
  1301. string.format("%04x", singleRes.tm),
  1302. string.format("%04x", singleRes.bias),
  1303. lt:sub(3, 4):toHex() .. lt:sub(1, 2):toHex(),
  1304. torque:sub(3, 4):toHex() .. torque:sub(1, 2):toHex()
  1305. }
  1306. local resValueHex = table.concat(resT)
  1307. local j = 1
  1308. for i=1400,1491 do
  1309. local regHex = string.format("%04x", i)
  1310. queryRespData[regHex] = string.sub(resValueHex, j, j+3)
  1311. j = j+4
  1312. end
  1313. queryFlagResReg[respRegAddress] = "0001"
  1314. end
  1315. -- 获取扳手编号列表_模糊查询
  1316. local function query0071Res()
  1317. -- body
  1318. respRegAddress = "00d5"
  1319. local conds = table.concat(queryCondRegValue)
  1320. -- if #conds < 20 then
  1321. -- -- body
  1322. -- queryFlagReg["0064"] = "0000"
  1323. -- return false
  1324. -- end
  1325. local seachStr = getByte2AsciiStr(conds)--查询条件
  1326. local result, count, desc = getBaseInfo_ununique_dim("Wrench", {number = seachStr ,fixed = seachStr}, "/sdcard0/common")
  1327. queryFlagReg["0071"] = "0000"
  1328. queryCondRegValue = {}--清空查询条件
  1329. if not result then
  1330. -- body
  1331. queryFlagResReg[respRegAddress] = "0002"
  1332. logModuel.debug_log("查询扳手编号列表失败,count:"..count..",desc:"..desc)
  1333. return false
  1334. end
  1335. queryRespData["0578"] = string.format("%04x", table_leng(result))
  1336. local regValueHex = ""
  1337. for k,v in pairs(result) do
  1338. local number = getAscii2StringHexBe(v.number, 20)
  1339. log.info("number:", v.number)
  1340. regValueHex = regValueHex..number
  1341. end
  1342. local j = 1
  1343. for i=1410, 1409 + 10*table_leng(result) do
  1344. local regHex = string.format("%04x", i)
  1345. queryRespData[regHex] = regValueHex:sub(j, j+3)
  1346. j = j+4
  1347. end
  1348. queryFlagResReg[respRegAddress] = "0001"
  1349. end
  1350. -- 泵到网关心跳
  1351. function query0191Res()
  1352. -- body
  1353. if condition then
  1354. -- body
  1355. end
  1356. end
  1357. -- 网关到泵心跳
  1358. function query0258Res()
  1359. -- body
  1360. if condition then
  1361. -- body
  1362. end
  1363. end
  1364. local function queryByRegAdd(regAddress)
  1365. queryRespData = {}--清空之前的查询结果
  1366. if regAddress == "0064" then
  1367. -- body
  1368. query0064Res()
  1369. end
  1370. if regAddress == "0065" then
  1371. -- body
  1372. query0065Res()
  1373. end
  1374. if regAddress == "0066" then
  1375. -- body
  1376. query0066Res()
  1377. end
  1378. if regAddress == "0067" then
  1379. -- body
  1380. query0067Res()
  1381. end
  1382. if regAddress == "0068" then
  1383. -- body
  1384. query0068Res()
  1385. end
  1386. if regAddress == "0069" then
  1387. -- body
  1388. query0069Res()
  1389. end
  1390. if regAddress == "006a" then
  1391. -- body
  1392. query006aRes()
  1393. end
  1394. if regAddress == "006b" then
  1395. -- body
  1396. query006bRes()
  1397. end
  1398. if regAddress == "006c" then
  1399. -- body
  1400. query006cRes()
  1401. end
  1402. if regAddress == "006d" then
  1403. -- body
  1404. query006dRes()
  1405. end
  1406. if regAddress == "006e" then
  1407. -- body
  1408. query006eRes()
  1409. end
  1410. if regAddress == "006f" then
  1411. -- body
  1412. query006fRes()
  1413. end
  1414. if regAddress == "0070" then
  1415. -- body
  1416. query0070Res()
  1417. end
  1418. if regAddress == "0071" then
  1419. -- body
  1420. query0071Res()
  1421. end
  1422. end
  1423. local function queryByQueryflag()
  1424. while true do
  1425. local result, regAddress = sys.waitUntil("pub_queryflag_reg", 1000)
  1426. if result then
  1427. -- body
  1428. queryByRegAdd(regAddress)
  1429. end
  1430. end
  1431. end
  1432. pm.wake("modbusrtu")
  1433. --注册串口的数据发送通知函数
  1434. uart.on(uart_id,"receive",function() sys.publish("UART_RECEIVE") end)
  1435. uart.on(uart_id,"sent",writeOk)
  1436. --配置并且打开串口
  1437. -- uart.setup(uart_id,uart_baud,8,uart.PAR_NONE,uart.STOP_1)
  1438. uart.setup(uart_id,uart_baud,8,uart.PAR_NONE,uart.STOP_1,nil,1)
  1439. uart.set_rs485_oe(uart_id, pio.P0_19)
  1440. --启动串口数据接收任务
  1441. sys.taskInit(modbus_read)
  1442. sys.taskInit(queryByQueryflag)
  1443. function getHeartbeatContent( )
  1444. -- body
  1445. local gpsStr = nvm.get("gpsLng")..","..nvm.get("gpsLat")
  1446. -- local vbatt = readAdc() or "0"
  1447. local adcval,voltval = adc.read(BAT_ADC_ID)
  1448. local vbatt = math.ceil( 1.68 * voltval )
  1449. local ram = _G.collectgarbage("count")
  1450. local flash = rtos.get_fs_free_size(0,1)
  1451. -- local SDInfo = rtos.get_fs_free_size(1,1)
  1452. local Status = 0
  1453. if powerManage.setGpio11Fnc() == 1 then
  1454. -- body
  1455. Status = 1
  1456. end
  1457. local heartBeat = {
  1458. IMEI = misc.getImei(),
  1459. IMSI = sim.getImsi(),
  1460. CCID = sim.getIccid(),
  1461. CSQ = net.getRssi(),
  1462. GPS = gpsStr,
  1463. Status = Status,
  1464. lngType = nvm.get("lngType"),
  1465. latType = nvm.get("latType"),
  1466. LastGPSLocTime = nvm.get("gpsTime") ,
  1467. Power = vbatt,
  1468. RamInfo = ram..","..flash,
  1469. SoftVersion = _G.VERSION,
  1470. HeartTimeInterval = nvm.get("heartbeatInterval"),
  1471. CntVersion = nvm.get("localCntVersion"),
  1472. pumbStatus = nvm.get("pumbStatus"),
  1473. newPumbModel = nvm.get("newPumbModel"),
  1474. newPumbNumber = nvm.get("newPumbNumber"),
  1475. oldPumbModel = nvm.get("oldPumbModel"),
  1476. oldPumbNumber = nvm.get("oldPumbNumber"),
  1477. sensorNumber = nvm.get("sensorNumber"),
  1478. msgid = devTool.guid(),
  1479. time = os.time()
  1480. }
  1481. return heartBeat
  1482. end
  1483. local function checkPumbHeartbeatExtra( )
  1484. -- body
  1485. local pumbStatus = nvm.get("pumbStatus")
  1486. local hydOil, hydOilThr = nvm.get("hydOil"), nvm.get("hydOilThr")
  1487. local scaleValve, scaleValveThr = nvm.get("scaleValve"), nvm.get("scaleValveThr")
  1488. local elecValve, elecValveThr = nvm.get("elecValve"), nvm.get("elecValveThr")
  1489. local alarmType, alarmReason = "device", {}
  1490. if pumbStatus > 2 then
  1491. -- body
  1492. table.insert(alarmReason, "pumb_unusual_status")
  1493. end
  1494. if hydOil > hydOilThr then
  1495. -- body
  1496. table.insert(alarmReason, "low_oil")
  1497. end
  1498. if scaleValve > scaleValveThr then
  1499. -- body
  1500. table.insert(alarmReason, "low_scale_valve")
  1501. end
  1502. if elecValve > elecValveThr then
  1503. -- body
  1504. table.insert(alarmReason, "low_elec_valve")
  1505. end
  1506. if table_leng(alarmReason) == 0 then
  1507. -- body
  1508. return
  1509. end
  1510. local alarmData = {
  1511. Type = alarmType,
  1512. Reason = alarmReason,
  1513. Data = {pumbStatus=pumbStatus, hydOil= hydOil, scaleValve=scaleValve,elecValve=elecValve},
  1514. Time = os.time()
  1515. }
  1516. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadAlarmInfo",["data"]=alarmData}))
  1517. end
  1518. function checkNetWorkHealth( msgType )
  1519. local alarmType, alarmReason = "gateway", {}
  1520. local adcval,voltval = adc.read(BAT_ADC_ID)
  1521. local vbatt = math.ceil( 1.68 * voltval )
  1522. local lowBatThd = nvm.get("lowBatThd") or 3300
  1523. if msgType then
  1524. -- body
  1525. table.insert(alarmReason, msgType)
  1526. else
  1527. if vbatt < lowBatThd then
  1528. -- body
  1529. table.insert(alarmReason, "network_bat_lower")
  1530. end
  1531. end
  1532. if table_leng(alarmReason) == 0 then
  1533. -- body
  1534. return
  1535. end
  1536. local alarmData = {
  1537. Type = alarmType,
  1538. Reason = alarmReason,
  1539. Data = {battery=vbatt},
  1540. Time = os.time()
  1541. }
  1542. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/UploadAlarmInfo",["data"]=alarmData}))
  1543. end
  1544. sdcardMouted = false
  1545. sys.taskInit(function( )
  1546. -- body
  1547. local result = sys.waitUntil("IO_SDCARD_MOUTED", 10000)
  1548. if result then
  1549. -- body
  1550. sdcardMouted = true
  1551. end
  1552. end)
  1553. sys.timerLoopStart(checkPumbHeartbeatExtra,60000)
  1554. local heartbeatInterval = nvm.get("heartbeatInterval") or 600
  1555. -- 定时心跳
  1556. sys.taskInit(function()
  1557. sys.wait(6000)
  1558. while true do
  1559. if socket.isReady() then --连上网再开始运行
  1560. -- sys.waitUntil("pub_gps_location_success_msg", 120*1000)
  1561. local heartBeatData = getHeartbeatContent()
  1562. log.info("pblish heartbeat msg!!!")
  1563. sys.publish("LOCAL_PUB_MSG", json.encode({["topic"]="SHEGCL/IntelligenTool/Heart",["data"]=heartBeatData}))
  1564. sys.wait(1000)
  1565. checkNetWorkHealth(nil)
  1566. sys.wait(heartbeatInterval *1000)
  1567. else --没连上网别忘了延时!不然会陷入while true死循环,导致模块无法运行其他代码
  1568. -- log.info("socket not ready")
  1569. sys.wait(5000) --等待5秒
  1570. end
  1571. end
  1572. end)