agpsHxxt.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. --- 模块功能:GPS辅助定位以及星历更新服务.
  2. -- 本功能模块只能配合Air820UX系列的模块以及Air530H模块,和芯星通GPS芯片使用;
  3. -- require"agpsHxxt"后,会自动开启本功能模块的任务;
  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 agpsHxxt
  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"gpsHxxt"
  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 = 3*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("agpsHxxt.writeEphEnd")
  29. sys.timerStart(gps.close,3000,gps.TIMER,{tag="lib.agpsHxxt.lua.eph"})
  30. sEphData = nil
  31. end
  32. local function writeEph()
  33. log.info("agpsHxxt.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("agpsHxxt.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. -- io.writeFile(EPH_TIME_FILE,tostring(os.time()))
  48. else
  49. sEphData = body
  50. gps.open(gps.TIMER,{tag="lib.agpsHxxt.lua.eph",val=10,cb=writeEphEnd})
  51. sys.timerStart(writeEph,2000)
  52. return
  53. end
  54. end
  55. end
  56. --连接服务器下载星历
  57. function updateEph()
  58. if gps.isFix() then runTimer() return end
  59. http.request("GET","download.openluat.com:80/9501-xingli/HXXT_GPS_BDS_AGNSS_DATA.dat",nil,nil,nil,20000,downloadEphCb)
  60. end
  61. --JWL在星历的时间范围内,如果有星历文件,重启模块就直接把本地的星历文件写入GPS芯片
  62. --在AGPS_LOCATED 的订阅事件中,调用此接口。
  63. function upd_xingli()
  64. if not gps.isFix() then
  65. local lstm = io.readFile(EPH_TIME_FILE)
  66. if not lstm or lstm=="" then return end
  67. log.info("agpsHxxt.upd_xingli lstm=",lstm)
  68. if os.time()-tonumber(lstm) < EPH_UPDATE_INTERVAL then
  69. local body = io.readFile(EPH_DATA_FILE)
  70. if body and #body >0 then
  71. log.info("agpsHxxt.upd_xingli length=",#body)
  72. sEphData = body
  73. gps.open(gps.TIMER,{tag="lib.agpsHxxt.lua.eph",val=10,cb=writeEphEnd})
  74. sys.timerStart(writeEph,2000)
  75. else
  76. log.info("agpsHxxt.upd_xingli wanto update xingli data")
  77. updateEph()
  78. end
  79. end
  80. end
  81. end
  82. --检查是否需要更新星历
  83. local function checkEph()
  84. local result
  85. if not gps.isFix() then
  86. lastTm = io.readFile(EPH_TIME_FILE)
  87. if not lastTm or lastTm=="" then return true end
  88. log.info("agpsHxxt.checkEph",os.time(),tonumber(lastTm)," DELTA=",os.time()-tonumber(lastTm))
  89. result = (os.time()-tonumber(lastTm) >= EPH_UPDATE_INTERVAL)
  90. end
  91. if not result then runTimer() end
  92. return result
  93. end
  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("agpsHxxt.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.agpsHxxt.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. local function ipReady()
  127. if gps.isFix() then
  128. runTimer()
  129. else
  130. if not lbsLocRequesting then
  131. lbsLocRequesting = true
  132. lbsLoc.request(getLocCb,nil,30000,"0","bs.openluat.com","12412",true)
  133. end
  134. log.info("agpsHxxt.ipready to updateEph")
  135. if checkEph() then
  136. updateEph()
  137. else
  138. sys.timerStart(upd_xingli,3000)
  139. end
  140. end
  141. end
  142. local function gpsState(evt,para)
  143. log.info("agpsHxxt.GPS_STATE",evt,para)
  144. if evt=="LOCATION_SUCCESS" or (evt=="CLOSE" and para==true) then
  145. runTimer()
  146. elseif evt=="OPEN" then
  147. local lng,lat = gps.getLastLocation()
  148. if lng=="" or lat=="" then
  149. lng,lat = lastLbsLng,lastLbsLat
  150. end
  151. if lng~="" and lat~="" then
  152. gps.open(gps.TIMERORSUC,{tag="lib.agpsHxxt.lua.fastFix",val=4})
  153. local tm = os.date("*t")
  154. sys.timerStart(gps.setFastFix,2000,lat,lng,common.timeZoneConvert(tm.year,tm.month,tm.day,tm.hour,tm.min,tm.sec,8,0))
  155. end
  156. end
  157. end
  158. function init()
  159. sys.subscribe("GPS_STATE",gpsState)
  160. sys.subscribe("IP_READY_IND",ipReady)
  161. log.info("agpsHxxt.init")
  162. end
  163. function unInit()
  164. sys.unsubscribe("GPS_STATE",gpsState)
  165. sys.unsubscribe("IP_READY_IND",ipReady)
  166. log.info("agpsHxxt.unInit")
  167. end
  168. init()