agpsZkw.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. --- 模块功能:GPS辅助定位以及星历更新服务.
  2. -- 本功能模块只能配合Air820UX系列的模块以及Air530Z模块,中科微GPS芯片使用;
  3. -- require"agpsZkw"后,会自动开启本功能模块的任务;
  4. -- 会定期更新GPS星历,星历更新算法如下:
  5. -- 从最后一次GPS定位成功的时间算起,每隔4小时连接星历服务器下载一次星历数据(大概4K字节),写入GPS芯片。
  6. -- 例如01:00分开机后,更新了一次星历文件,截止到05:00,“一直没有开启过GPS”或者“开启过GPS,但是GPS从来没有定位成功”,在05:00就会下载星历数据然后写入GPS芯片;
  7. -- 05:00更新星历数据后,在06:00打开了GPS,并且GPS定位成功,然后在07:00关闭了GPS,关闭前GPS仍然处于定位成功状态;
  8. -- 截止到11:00,“一直没有开启过GPS”或者“开启过GPS,但是GPS从来没有定位成功”,在11:00就会下载星历数据然后写入GPS芯片;
  9. -- @module agpsZkw
  10. -- @author openLuat
  11. -- @license MIT
  12. -- @copyright openLuat
  13. -- @release 2020.10.28
  14. require"http"
  15. require"lbsLoc"
  16. require"net"
  17. local gps = require"gpsZkw"
  18. module(..., package.seeall)
  19. local EPH_TIME_FILE = "/ephTime.txt"
  20. local EPH_DATA_FILE = "/ephData.bin"
  21. local sEphData
  22. local EPH_UPDATE_INTERVAL = 4*3600
  23. local lastLbsLng,lastLbsLat = "",""
  24. local function runTimer()
  25. sys.timerStart(updateEph,EPH_UPDATE_INTERVAL*1000)
  26. end
  27. local function writeEphEnd()
  28. log.info("agpsZkw.writeEphEnd")
  29. sys.timerStart(gps.close,3000,gps.TIMER,{tag="lib.agpsZkw.lua.eph"})
  30. sEphData = nil
  31. end
  32. local function writeEph()
  33. log.info("agpsZkw.writeEph")
  34. gps.writeData(sEphData)
  35. --gps.writeData(io.readFile("/lua/aid_data.bin"))
  36. --uart.setup(2, 9600, 8, uart.PAR_NONE, uart.STOP_1)
  37. --uart.write(2,io.readFile("/lua/aid_data.bin"))
  38. writeEphEnd()
  39. end
  40. local function downloadEphCb(result,prompt,head,body)
  41. log.info("agpsZkw.downloadEphCb",result,prompt)
  42. runTimer()
  43. if result and prompt=="200" and body then
  44. io.writeFile(EPH_DATA_FILE,body)
  45. io.writeFile(EPH_TIME_FILE,tostring(os.time()))
  46. if gps.isFix() then
  47. else
  48. sEphData = body
  49. gps.open(gps.TIMER,{tag="lib.agpsZkw.lua.eph",val=10,cb=writeEphEnd})
  50. sys.timerStart(writeEph,2000)
  51. return
  52. end
  53. end
  54. end
  55. --连接服务器下载星历
  56. function updateEph()
  57. if gps.isFix() then runTimer() return end
  58. http.request("GET","http://download.openluat.com/9501-xingli/CASIC_data.dat",nil,nil,nil,20000,downloadEphCb)
  59. end
  60. --JWL在星历的时间范围内,如果有星历文件,重启模块就直接把本地的星历文件写入GPS芯片
  61. --在AGPS_LOCATED 的订阅事件中,调用此接口。
  62. function upd_xingli()
  63. if not gps.isFix() then
  64. local lstm = io.readFile(EPH_TIME_FILE)
  65. if not lstm or lstm=="" then return end
  66. log.info("agpsZkw.upd_xingli lstm=",lstm)
  67. if os.time()-tonumber(lstm) < EPH_UPDATE_INTERVAL then
  68. local body = io.readFile(EPH_DATA_FILE)
  69. if body and #body >0 then
  70. log.info("agpsZkw.upd_xingli length=",#body)
  71. sEphData = body
  72. gps.open(gps.TIMER,{tag="lib.agpsZkw.lua.eph",val=10,cb=writeEphEnd})
  73. sys.timerStart(writeEph,2000)
  74. else
  75. log.info("agpsZkw.upd_xingli wanto update xingli data")
  76. updateEph()
  77. end
  78. end
  79. end
  80. end
  81. --检查是否需要更新星历
  82. local function checkEph()
  83. local result
  84. if not gps.isFix() then
  85. lastTm = io.readFile(EPH_TIME_FILE)
  86. if not lastTm or lastTm=="" then return true end
  87. log.info("agpsZkw.checkEph",os.time(),tonumber(lastTm)," DELTA=",os.time()-tonumber(lastTm))
  88. result = (os.time()-tonumber(lastTm) >= EPH_UPDATE_INTERVAL)
  89. end
  90. if not result then runTimer() end
  91. return result
  92. end
  93. --[[
  94. local function setFastFix(lng,lat,tm)
  95. gps.setFastFix(lat,lng,tm)
  96. if checkEph() then updateEph() end
  97. end
  98. local getloc = 0
  99. local lbsLocRequesting
  100. --获取到基站对应的经纬度,写到GPS芯片中
  101. local function getLocCb(result,lat,lng,addr,time)
  102. log.info("agpsZkw.getLocCb",result,lat,lng,time and time:len() or 0)
  103. lbsLocRequesting = false
  104. if result==0 then
  105. lastLbsLng,lastLbsLat = lng,lat
  106. if not gps.isFix() then
  107. local tm = {year=0,month=0,day=0,hour=0,min=0,sec=0}
  108. if time:len()==6 then
  109. tm = {year=time:byte(1)+2000,month=time:byte(2),day=time:byte(3),hour=time:byte(4),min=time:byte(5),sec=time:byte(6)}
  110. misc.setClock(tm)
  111. tm = common.timeZoneConvert(tm.year,tm.month,tm.day,tm.hour,tm.min,tm.sec,8,0)
  112. end
  113. gps.open(gps.TIMERORSUC,{tag="lib.agpsZkw.lua.fastFix",val=4})
  114. sys.timerStart(setFastFix,2000,lng,lat,tm)
  115. getloc = 1
  116. end
  117. end
  118. if result~=0 or gps.isFix() then
  119. if checkEph() then updateEph() end
  120. end
  121. end
  122. --是否获取到基站对应的经纬度
  123. function isgetloc()
  124. return getloc
  125. end
  126. ]]
  127. local function ipReady()
  128. if gps.isFix() then
  129. runTimer()
  130. else
  131. --[[
  132. if not lbsLocRequesting then
  133. lbsLocRequesting = true
  134. lbsLoc.request(getLocCb,nil,30000,"0","bs.openluat.com","12412",true)
  135. end]]
  136. log.info("agpsZkw.ipready to updateEph")
  137. if checkEph() then
  138. updateEph()
  139. else
  140. sys.timerStart(upd_xingli,3000)
  141. end
  142. end
  143. end
  144. local function gpsState(evt,para)
  145. log.info("agpsZkw.GPS_STATE",evt,para)
  146. if evt=="LOCATION_SUCCESS" or (evt=="CLOSE" and para==true) then
  147. runTimer()
  148. elseif evt=="OPEN" then
  149. --[[
  150. local lng,lat = gps.getLastLocation()
  151. if lng=="" or lat=="" then
  152. lng,lat = lastLbsLng,lastLbsLat
  153. end
  154. if lng~="" and lat~="" then
  155. gps.open(gps.TIMERORSUC,{tag="lib.agpsZkw.lua.fastFix",val=4})
  156. local tm = os.date("*t")
  157. sys.timerStart(gps.setFastFix,2000,lat,lng,common.timeZoneConvert(tm.year,tm.month,tm.day,tm.hour,tm.min,tm.sec,8,0))
  158. end]]
  159. end
  160. end
  161. function init()
  162. sys.subscribe("GPS_STATE",gpsState)
  163. sys.subscribe("IP_READY_IND",ipReady)
  164. log.info("agpsZkw.unInit")
  165. end
  166. function unInit()
  167. sys.unsubscribe("GPS_STATE",gpsState)
  168. sys.unsubscribe("IP_READY_IND",ipReady)
  169. log.info("agpsZkw.unInit")
  170. end
  171. init()