agps.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --- 模块功能:星历更新服务
  2. -- @module agps
  3. -- @author 稀饭放姜
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2017.10.23
  7. require "http"
  8. require "net"
  9. module(..., package.seeall)
  10. -- 星历数据本地文件名
  11. local GPD_FILE = "/GPD.txt"
  12. local LBS_FILE = "/LBS.txt"
  13. local GPD_URL = "http://download.openluat.com/9501-xingli/brdcGPD.dat_rda"
  14. local LBS_URL = "http://api.openluat.com/iot/cell_location"
  15. local LBS_KEY = "zLakaGugFktk2sgZ"
  16. local LBS_SECRET = "BKW3tSMv0YBqomUlqen3DjyIbg0hvuj6P3EHFeRKCBmVkVjDYy7NQcIcWw1rTd9C"
  17. --- 设置基站定位需要登陆的服务器,验证账号和密码
  18. -- @string host,LBS基站定位服务器地址
  19. -- @string key, HTTP Basic Authorization 认证用户名
  20. -- @string secret,HTTP Basic Authorization 认证密码
  21. -- @return 无
  22. -- @usage agps.LBS_Setup("api.openluat.com","user","123345")
  23. function LBS_Setup(host, key, secret)
  24. LBS_HOST = host or LBS_HOST
  25. LBS_KEY = key or LBS_KEY
  26. LBS_SECRET = secret or LBS_SECRET
  27. end
  28. --- 下载星历数据
  29. -- @number timeout,下载星历超时等待时间
  30. -- @return string,星历数据的HEX字符串
  31. -- @usage agps.refresh(30000)
  32. function refresh(timeout)
  33. while not socket.isReady() do sys.wait(1000) end
  34. local code, head, data = http.request("GET", GPD_URL, timeout)
  35. if code == "200" then
  36. local data, len = data:tohex()
  37. log.info("agps.gpd length,file:", len, io.writefile(GPD_FILE, data))
  38. return data
  39. end
  40. end
  41. --- 获取星历数据
  42. -- @return string,星历数据的HEX字符串
  43. -- @usage agps.getGPD()
  44. function getGPD()
  45. return io.readfile(GPD_FILE) or refresh(30000)
  46. end
  47. --- 下载基站坐标
  48. -- @number timeout,下载基站信息超时等待时间
  49. -- @return string,基站坐标字符串,基站没准备好返回nil
  50. -- @usage agps.cellTrack()
  51. function cellTrack(timeout)
  52. local ct, info = {['cell'] = {}}
  53. while not socket.isReady() do sys.wait(1000) end
  54. info = net.getCellInfoExt()
  55. if info == "" then return end
  56. for mcc, mnc, lac, ci, rssi in info:gmatch("(%d+)%.(%d+)%.(%d+)%.(%d+)%.(%d+);") do
  57. local tmp = {}
  58. tmp.mcc = tonumber(mcc)
  59. tmp.mnc = tonumber(mnc)
  60. tmp.lac = tonumber(lac)
  61. tmp.ci = tonumber(ci)
  62. tmp.hex = "10"
  63. tmp.csq = (tonumber(rssi) > 31) and 31 or tonumber(rssi)
  64. table.insert(ct.cell, tmp)
  65. end
  66. ct.lng_lat_format = "string"
  67. -- 发送请求报文
  68. local code, head, data = http.request("POST", LBS_URL, timeout, nil, ct, 2, LBS_KEY .. ":" .. LBS_SECRET)
  69. if code == "200" then
  70. -- data = json.decode(data)
  71. log.info("agps.lbs length,file:", io.writefile(LBS_FILE, data))
  72. return data
  73. end
  74. end
  75. --- 获取基站坐标
  76. -- @return string,基站定位的坐标字符串
  77. -- @usage agps.getLBS()
  78. function getLBS()
  79. return io.readfile(LBS_FILE) or cellTrack(30000)
  80. end