gnss.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. sys.taskInit(function()
  2. log.info("GPS", "start")
  3. -- gnss的复位
  4. local gpsRst = gpio.setup(27, 1)
  5. local uartId = 2
  6. libgnss.clear() -- 清空数据,兼初始化
  7. uart.setup(uartId, 115200)
  8. sys.wait(200) -- GPNSS芯片启动需要时间
  9. -- 调试日志,可选
  10. --libgnss.debug(true)
  11. libgnss.bind(2)
  12. end)
  13. -- 订阅GNSS状态编码
  14. sys.subscribe("GNSS_STATE", function(event, ticks)
  15. -- event取值有
  16. -- FIXED 定位成功
  17. -- LOSE 定位丢失
  18. -- ticks是事件发生的时间,一般可以忽略
  19. log.info("gnss", "state", event, ticks)
  20. if event == "FIXED" then
  21. local locStr = libgnss.locStr()
  22. log.info("gnss", "locStr", locStr)
  23. elseif event == "LOSE" then
  24. log.info("gnss", "no fix")
  25. end
  26. end)
  27. --基站定位数据
  28. local latLbs, lngLbs, typeLbs
  29. sys.timerLoopStart(function ()
  30. local isFixed = libgnss.isFix()
  31. if isFixed then--优先使用gps数据
  32. local loc = libgnss.getRmc(2)
  33. attributes.set("isFixed", "已定位")
  34. attributes.set("lat", tostring(loc.lat))
  35. attributes.set("lng", tostring(loc.lng))
  36. attributes.set("location", {
  37. lat = loc.lat,
  38. lng = loc.lng,
  39. })
  40. elseif latLbs and lngLbs then
  41. attributes.set("isFixed", typeLbs)
  42. attributes.set("lat", tostring(latLbs))
  43. attributes.set("lng", tostring(lngLbs))
  44. attributes.set("location", {
  45. lat = tonumber(latLbs),
  46. lng = tonumber(lngLbs),
  47. })
  48. else
  49. attributes.set("isFixed", "获取中")
  50. attributes.set("lat", "无数据")
  51. attributes.set("lng", "无数据")
  52. end
  53. end,3000)
  54. local lbsLoc = require("lbsLoc")
  55. local function getLocCb(result, lat, lng, addr, time, locType)
  56. log.info("testLbsLoc.getLocCb", result, lat, lng)
  57. -- 基站定位获取经纬度成功
  58. if result == 0 then
  59. latLbs, lngLbs = lat, lng
  60. typeLbs = locType == 0 and "基站定位" or "WIFI定位"
  61. end
  62. end
  63. sys.taskInit(function()
  64. sys.waitUntil("IP_READY", 30000)
  65. while mobile do -- 没有mobile库就没有基站定位
  66. --基站定位信息
  67. mobile.reqCellInfo(15)
  68. sys.waitUntil("CELL_INFO_UPDATE", 3000)
  69. --wifi定位信息
  70. wlan.scan()
  71. local reqWifi
  72. local r = sys.waitUntil("WLAN_SCAN_DONE", 60000)
  73. if r then
  74. local results = wlan.scanResult()
  75. log.info("wifi scan", "count", #results)
  76. if #results > 0 then
  77. local reqWifi = {}
  78. for k,v in pairs(results) do
  79. log.info("scan", v["ssid"], v["rssi"], v["bssid"]:toHex())
  80. local bssid = v["bssid"]:toHex()
  81. bssid = string.format ("%s:%s:%s:%s:%s:%s", bssid:sub(1,2), bssid:sub(3,4), bssid:sub(5,6), bssid:sub(7,8), bssid:sub(9,10), bssid:sub(11,12))
  82. reqWifi[bssid]=v["rssi"]
  83. end
  84. end
  85. end
  86. if not libgnss.isFix() then--没定位成功再去获取
  87. lbsLoc.request(getLocCb,nil,nil,nil,nil,nil,nil,reqWifi)
  88. end
  89. sys.wait(60000)
  90. end
  91. end)