Browse Source

:bug: 修复基站定位失效, 更换为 `lbsLoc`, fix #16

Mizore 2 years ago
parent
commit
e8b8a5a693
2 changed files with 17 additions and 100 deletions
  1. 3 2
      script/main.lua
  2. 14 98
      script/util_location.lua

+ 3 - 2
script/main.lua

@@ -99,7 +99,7 @@ sys.taskInit(
 
         -- 开机通知
         if config.BOOT_NOTIFY then
-            util_notify.add("#BOOT")
+            sys.timerStart(util_notify.add, 1000 * 5, "#BOOT")
         end
 
         -- 定时查询流量
@@ -109,7 +109,8 @@ sys.taskInit(
 
         -- 定时基站定位
         if config.LOCATION_INTERVAL and config.LOCATION_INTERVAL >= 1000 * 30 then
-            sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL, 30)
+            util_location.refresh(nil, true)
+            sys.timerLoopStart(util_location.refresh, config.LOCATION_INTERVAL)
         end
 
         -- 电源键短按发送测试通知

+ 14 - 98
script/util_location.lua

@@ -1,11 +1,9 @@
 local util_location = {}
 
--- 基站定位接口类型, 支持 openluat 和 cellocation
-local api_type = "openluat"
+PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
+local lbsLoc = require("lbsLoc")
 
 local cache = {
-    cell_info_raw = {},
-    cell_info_formatted = "",
     lbs_data = {
         lat = 0,
         lng = 0
@@ -35,48 +33,24 @@ local function getMapLink(lat, lng)
     return map_link
 end
 
---- 格式化基站信息
--- @param cell_info_raw 基站信息
--- @return 格式化后的基站信息
-local function formatCellInfo(cell_info_raw)
-    if api_type == "openluat" then
-        local cell_info_arr = {}
-        for i, v in ipairs(cell_info_raw) do
-            table.insert(cell_info_arr, {mcc = v.mcc, mnc = v.mnc, lac = v.tac, ci = v.cid, rxlevel = v.rsrp, hex = 10})
-        end
-        local cell_info_json = json.encode(cell_info_arr)
-        log.debug("util_location.formatCellInfo", api_type .. ":", cell_info_json)
-        return cell_info_json
-    end
-
-    if api_type == "cellocation" then
-        local str = ""
-        for i, v in ipairs(cell_info_raw) do
-            str = str .. (i == 1 and "" or ";")
-            str = str .. v.mcc .. "," .. v.mnc .. "," .. v.tac .. "," .. v.cid .. "," .. v.rsrp
-        end
-        log.debug("util_location.formatCellInfo", api_type .. ":", str)
-        return str
+--- lbsLoc.request 回调
+local function getLocCb(result, lat, lng, addr, time, locType)
+    log.info("util_location.getLocCb", "result,lat,lng,time,locType:", result, lat, lng, time:toHex(), locType)
+    -- 获取经纬度成功, 坐标系WGS84
+    if result == 0 then
+        cache.lbs_data = {lat, lng}
     end
 end
 
---- 获取基站信息
--- @return 基站信息 or ""
-local function getCellInfo()
-    local cell_info_formatted = formatCellInfo(mobile.getCellInfo())
-    cache.cell_info_formatted = cell_info_formatted
-    return cell_info_formatted
-end
-
 --- 刷新基站信息
 -- @param timeout 超时时间(单位: 秒)
-function util_location.refreshCellInfo(timeout)
+local function refreshCellInfo(timeout)
     log.info("util_location.refreshCellInfo", "start")
     if cache.is_req_cell_info_running then
         log.info("util_location.refreshCellInfo", "running, wait...")
     else
         cache.is_req_cell_info_running = true
-        mobile.reqCellInfo(timeout or 30) -- 单位: 秒
+        mobile.reqCellInfo(timeout or 20) -- 单位: 秒
     end
     sys.waitUntil("CELL_INFO_UPDATE")
     cache.is_req_cell_info_running = false
@@ -84,74 +58,16 @@ function util_location.refreshCellInfo(timeout)
 end
 
 --- 刷新基站定位信息
--- @param timeout 超时时间(单位: 秒)
--- @return 刷新成功返回 true
+-- @param timeout 超时时间(单位: 毫秒)
 function util_location.refresh(timeout, is_refresh_cell_info_disabled)
-    timeout = type(timeout) == "number" and timeout * 1000 or nil
-
-    local openluat = function(cell_info_formatted)
-        local lbs_api = "http://bs.openluat.com/get_gpss"
-        local header = {
-            ["Content-Type"] = "application/x-www-form-urlencoded"
-        }
-        local body = "data=" .. cell_info_formatted
-        local code, headers, body = util_http.fetch(timeout, "POST", lbs_api, header, body)
-        log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body)
-
-        if code ~= 200 or body == nil or body == "" then
-            return
-        end
-
-        local lbs_data = json.decode(body) or {}
-        local status, lat, lng = lbs_data.status, lbs_data.lat, lbs_data.lng
-
-        if status ~= 0 or lat == nil or lng == nil or lat == "" or lng == "" then
-            return
-        end
-
-        return lat, lng
-    end
-
-    local cellocation = function(cell_info_formatted)
-        local lbs_api = "http://api.cellocation.com:83/loc/?output=json&cl=" .. cell_info_formatted
-        local code, headers, body = util_http.fetch(timeout, "GET", lbs_api)
-        log.info("util_location.refresh", api_type .. ":", "code:", code, "body:", body)
-
-        if code ~= 200 or body == nil or body == "" then
-            return
-        end
-
-        local lbs_data = json.decode(body) or {}
-        local errcode, lat, lng = lbs_data.errcode, lbs_data.lat, lbs_data.lon
-        if errcode ~= 0 or lat == nil or lng == nil or lat == "0.0" or lng == "0.0" then
-            return
-        end
-
-        return lat, lng
-    end
+    timeout = type(timeout) == "number" and timeout or nil
 
     sys.taskInit(
         function()
             if not is_refresh_cell_info_disabled then
-                util_location.refreshCellInfo(timeout)
-            end
-            local old_cell_info_formatted = cache.cell_info_formatted
-            local cell_info_formatted = getCellInfo()
-
-            if cell_info_formatted == old_cell_info_formatted then
-                log.info("util_location.refresh", api_type .. ":", "cell_info 无变化, 不重新请求")
-                return
-            end
-
-            local lat, lng
-            if api_type == "openluat" then
-                lat, lng = openluat(cell_info_formatted)
-            elseif api_type == "cellocation" then
-                lat, lng = cellocation(cell_info_formatted)
-            end
-            if lat and lng then
-                cache.lbs_data = {lat, lng}
+                refreshCellInfo()
             end
+            lbsLoc.request(getLocCb, nil, timeout, nil, "bs.air32.cn")
         end
     )
 end