util_location.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. local util_location = {}
  2. -- 基站定位接口类型, 支持 openluat 和 cellocation
  3. local api_type = "openluat"
  4. local cache = {
  5. cell_info_raw = {},
  6. cell_info_formatted = "",
  7. lbs_data = {
  8. lat = 0,
  9. lng = 0
  10. }
  11. }
  12. --- 格式化经纬度 (保留小数点后 6 位, 去除末尾的 0)
  13. -- @param value 经纬度
  14. -- @return 格式化后的经纬度
  15. local function formatCoord(value)
  16. local str = string.format("%.6f", tonumber(value) or 0)
  17. str = str:gsub("%.?0+$", "")
  18. return tonumber(str)
  19. end
  20. --- 生成地图链接
  21. -- @param lat 纬度
  22. -- @param lng 经度
  23. -- @return 地图链接 or ""
  24. local function getMapLink(lat, lng)
  25. lat, lng = lat or 0, lng or 0
  26. local map_link = ""
  27. if lat ~= 0 and lng ~= 0 then
  28. map_link = "http://apis.map.qq.com/uri/v1/marker?coord_type=1&marker=title:+;coord:" .. lat .. "," .. lng
  29. end
  30. log.debug("util_location.getMapLink", map_link)
  31. return map_link
  32. end
  33. --- 格式化基站信息
  34. -- @param cell_info_raw 基站信息
  35. -- @return 格式化后的基站信息
  36. local function formatCellInfo(cell_info_raw)
  37. if api_type == "openluat" then
  38. local cell_info_arr = {}
  39. for i, v in ipairs(cell_info_raw) do
  40. table.insert(cell_info_arr, {mcc = v.mcc, mnc = v.mnc, lac = v.tac, ci = v.cid, rxlevel = v.rsrp, hex = 10})
  41. end
  42. local cell_info_json = json.encode(cell_info_arr)
  43. log.debug("util_location.formatCellInfo", api_type .. ":", cell_info_json)
  44. return cell_info_json
  45. end
  46. if api_type == "cellocation" then
  47. local str = ""
  48. for i, v in ipairs(cell_info_raw) do
  49. str = str .. (i == 1 and "" or ";")
  50. str = str .. v.mcc .. "," .. v.mnc .. "," .. v.tac .. "," .. v.cid .. "," .. v.rsrp
  51. end
  52. log.debug("util_location.formatCellInfo", api_type .. ":", str)
  53. return str
  54. end
  55. end
  56. --- 获取基站信息
  57. -- @return 基站信息 or ""
  58. local function getCellInfo()
  59. local cell_info_formatted = formatCellInfo(mobile.getCellInfo())
  60. cache.cell_info_formatted = cell_info_formatted
  61. return cell_info_formatted
  62. end
  63. --- 刷新基站信息
  64. -- @param timeout 超时时间(单位: 秒)
  65. function util_location.refreshCellInfo(timeout)
  66. log.info("util_location.refreshCellInfo", "start")
  67. if cache.is_req_cell_info_running then
  68. log.info("util_location.refreshCellInfo", "running, wait...")
  69. else
  70. cache.is_req_cell_info_running = true
  71. mobile.reqCellInfo(timeout or 30) -- 单位: 秒
  72. end
  73. sys.waitUntil("CELL_INFO_UPDATE")
  74. cache.is_req_cell_info_running = false
  75. log.info("util_location.refreshCellInfo", "end")
  76. end
  77. --- 刷新基站定位信息
  78. -- @param timeout 超时时间(单位: 秒)
  79. -- @return 刷新成功返回 true
  80. function util_location.refresh(timeout, is_refresh_cell_info_disabled)
  81. timeout = type(timeout) == "number" and timeout * 1000 or nil
  82. local openluat = function(cell_info_formatted)
  83. local lbs_api = "http://bs.openluat.com/get_gpss"
  84. local header = {
  85. ["Content-Type"] = "application/x-www-form-urlencoded"
  86. }
  87. local body = "data=" .. cell_info_formatted
  88. local code, headers, body = util_http.fetch(timeout, "POST", lbs_api, header, body)
  89. log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body)
  90. if code ~= 200 or body == nil or body == "" then
  91. return
  92. end
  93. local lbs_data = json.decode(body) or {}
  94. local status, lat, lng = lbs_data.status, lbs_data.lat, lbs_data.lng
  95. if status ~= 0 or lat == nil or lng == nil or lat == "" or lng == "" then
  96. return
  97. end
  98. return lat, lng
  99. end
  100. local cellocation = function(cell_info_formatted)
  101. local lbs_api = "http://api.cellocation.com:83/loc/?output=json&cl=" .. cell_info_formatted
  102. local code, headers, body = util_http.fetch(timeout, "GET", lbs_api)
  103. log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body)
  104. if code ~= 200 or body == nil or body == "" then
  105. return
  106. end
  107. local lbs_data = json.decode(body) or {}
  108. local errcode, lat, lng = lbs_data.errcode, lbs_data.lat, lbs_data.lon
  109. if errcode ~= 0 or lat == nil or lng == nil or lat == "0.0" or lng == "0.0" then
  110. return
  111. end
  112. return lat, lng
  113. end
  114. sys.taskInit(
  115. function()
  116. if not is_refresh_cell_info_disabled then
  117. util_location.refreshCellInfo(timeout)
  118. end
  119. local old_cell_info_formatted = cache.cell_info_formatted
  120. local cell_info_formatted = getCellInfo()
  121. if cell_info_formatted == old_cell_info_formatted then
  122. log.info("util_location.refresh", api_type .. ":", "cell_info 无变化, 不重新请求")
  123. return
  124. end
  125. local lat, lng
  126. if api_type == "openluat" then
  127. lat, lng = openluat(cell_info_formatted)
  128. elseif api_type == "cellocation" then
  129. lat, lng = cellocation(cell_info_formatted)
  130. end
  131. if lat and lng then
  132. cache.lbs_data = {lat, lng}
  133. end
  134. end
  135. )
  136. end
  137. --- 获取位置信息
  138. -- @return lat
  139. -- @return lng
  140. -- @return map_link
  141. function util_location.get()
  142. local lat, lng = unpack(cache.lbs_data)
  143. lat, lng = formatCoord(lat), formatCoord(lng)
  144. return lat, lng, getMapLink(lat, lng)
  145. end
  146. sys.subscribe(
  147. "CELL_INFO_UPDATE",
  148. function()
  149. log.debug("EVENT.CELL_INFO_UPDATE")
  150. end
  151. )
  152. return util_location