From 16e8f47269d242e6a4628053d9847664da652bec Mon Sep 17 00:00:00 2001 From: OneLifeHowDo Date: Fri, 22 Nov 2024 12:07:52 +0800 Subject: [PATCH] =?utf8?q?add:=E5=A2=9E=E5=8A=A0=E5=AE=9A=E4=BD=8D?= =?utf8?q?=E8=AF=B7=E6=B1=82=E5=A4=B1=E8=B4=A5=E6=97=B6=E5=80=99=E7=9A=84?= =?utf8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- script/libs/airlbs.lua | 94 ++++++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/script/libs/airlbs.lua b/script/libs/airlbs.lua index dae4262a..3459167d 100644 --- a/script/libs/airlbs.lua +++ b/script/libs/airlbs.lua @@ -1,8 +1,8 @@ --[[ @module airlbs @summary airlbs 定位服务(收费服务,需自行联系销售申请) -@version 1.1 -@date 2024.11.18 +@version 1.0 +@date 2024.11.01 @author Dozingfiretruck @usage --注意:因使用了sys.wait()所有api需要在协程中使用 @@ -13,22 +13,11 @@ local airlbs = require "airlbs" sys.taskInit(function() sys.waitUntil("IP_READY") - -- 如需wifi定位,需要硬件以及固件支持wifi扫描功能 - local wifi_info = nil - if wlan then - sys.wait(3000) -- 网络可用后等待一段时间才再调用wifi扫描功能,否则可能无法获取wifi信息 - wlan.init() - wlan.scan() - sys.waitUntil("WLAN_SCAN_DONE", 15000) - wifi_info = wlan.scanResult() - log.info("scan", "wifi_info", #wifi_info) - end - socket.sntp() sys.waitUntil("NTP_UPDATE", 1000) while 1 do - local result , data = airlbs.request({project_id = "xxx",project_key = 'xxx',wifi_info = wifi_info,timeout = 1000}) + local result , data = airlbs.request({project_id = "xxx",project_key = 'xxx',timeout = 1000}) if result then print("airlbs", json.encode(data)) end @@ -36,25 +25,21 @@ sys.taskInit(function() end end) -]] - -sys = require("sys") +]] sys = require("sys") sysplus = require("sysplus") libnet = require "libnet" - local airlbs_host = "airlbs.openluat.com" local airlbs_port = 12413 local lib_name = "airlbs" -local lib_topic = lib_name.."topic" +local lib_topic = lib_name .. "topic" local location_data = 0 local disconnect = -1 local airlbs_timeout = 15000 -local airlbs ={} - +local airlbs = {} local function airlbs_task(task_name, buff, timeout) local netc = socket.create(nil, lib_name) @@ -67,7 +52,7 @@ local function airlbs_task(task_name, buff, timeout) libnet.tx(lib_name, 0, netc, buff) else log.info(lib_name, "服务器没连上了!!!") - sys.publish(lib_topic,disconnect) + sys.publish(lib_topic, disconnect) libnet.close(lib_name, 5000, netc) return end @@ -75,17 +60,17 @@ local function airlbs_task(task_name, buff, timeout) while result do local succ, param = socket.rx(netc, buff) if not succ then - log.error(lib_name,"服务器断开了", succ, param) - sys.publish(lib_topic,disconnect) + log.error(lib_name, "服务器断开了", succ, param) + sys.publish(lib_topic, disconnect) break end if buff:used() > 0 then local location = nil - local data = buff:query(0,1) -- 获取数据 + local data = buff:query(0, 1) -- 获取数据 if data:toHex() == '00' then location = json.decode(buff:query(1)) else - log.error(lib_name,"not json data") + log.error(lib_name, "not json data") end sys.publish(lib_topic, location_data, location) buff:del() @@ -94,8 +79,8 @@ local function airlbs_task(task_name, buff, timeout) result, param, param2 = libnet.wait(lib_name, timeout, netc) log.info(lib_name, "wait", result, param, param2) if param == false then - log.error(lib_name,"服务器断开了", succ, param) - sys.publish(lib_topic,disconnect) + log.error(lib_name, "服务器断开了", succ, param) + sys.publish(lib_topic, disconnect) break end end @@ -104,13 +89,13 @@ end -- 处理未识别的网络消息 local function netCB(msg) - log.info("未处理消息", msg[1], msg[2], msg[3], msg[4]) + log.info("未处理消息", msg[1], msg[2], msg[3], msg[4]) end --[[ 获取定位数据 @api airlbs.request(param) -@param table 参数(联系销售获取id与key) project_id:项目ID project_key:项目密钥 wifi_info:wifi扫描结果(wlan.scanResult()的返回值,wifi定位使用)timeout:超时时间,单位毫秒 默认15000 +@param table 参数(联系销售获取id与key) project_id:项目ID project_key:项目密钥 timeout:超时时间,单位毫秒 默认15000 @return bool 成功返回true,失败会返回false @return table 定位成功生效,成功返回定位数据 @usage @@ -120,16 +105,16 @@ if result then end ]] function airlbs.request(param) - if not mobile then - log.error(lib_name,"no mobile") + if not mobile then + log.error(lib_name, "no mobile") return false end if mobile.status() == 0 then - log.error(lib_name,"网络未注册") + log.error(lib_name, "网络未注册") return false end - if param.project_id ==nil or param.project_key == nil then - log.error(lib_name,"param error") + if param.project_id == nil or param.project_key == nil then + log.error(lib_name, "param error") return false end @@ -138,7 +123,7 @@ function airlbs.request(param) local lbs_data_type = 0x00 local project_id = param.project_id if project_id:len() ~= 6 then - log.error("airlbs","project_id len not 6") + log.error("airlbs", "project_id len not 6") end local imei = mobile.imei() local muid = mobile.muid() @@ -152,7 +137,9 @@ function airlbs.request(param) sys.waitUntil("CELL_INFO_UPDATE", param.timeout or airlbs_timeout) -- log.info("cell", json.encode(mobile.getCellInfo())) - local lbs_data = {cells={}} + local lbs_data = { + cells = {} + } for k, v in pairs(mobile.getCellInfo()) do lbs_data.cells[k] = {} lbs_data.cells[k][1] = v.mcc @@ -166,55 +153,58 @@ function airlbs.request(param) lbs_data.cells[k][9] = v.rsrq lbs_data.cells[k][10] = v.earfcn end - if param.wifi_info and #param.wifi_info > 0 then - lbs_data.macs={} + lbs_data.macs = {} for k, v in pairs(param.wifi_info) do lbs_data.macs[k] = {} lbs_data.macs[k][1] = v.bssid:toHex():gsub("(%x%x)", "%1:"):sub(1, -2) lbs_data.macs[k][2] = v.rssi end end - local lbs_jdata = json.encode(lbs_data) - + log.info("扫描出的数据",lbs_jdata) udp_buff:write(string.char(auth_type) .. project_id .. imei .. muid .. timestamp .. nonce .. hmac_data:fromHex() .. string.char(lbs_data_type) .. lbs_jdata) sysplus.taskInitEx(airlbs_task, lib_name, netCB, lib_name, udp_buff, param.timeout or airlbs_timeout) while 1 do local result, tp, data = sys.waitUntil(lib_topic, param.timeout or airlbs_timeout) - log.info("event", result, tp, data) + log.info("定位请求的结果", result, "超时时间", tp, data) if not result then - return false,"timeout" + return false, "timeout" elseif tp == location_data then if not data then - log.error(lib_name,"no data, please check project_id and key") + log.error(lib_name, "无数据, 请检查project_id和project_key") return false - -- data.result 0-找不到 1-成功 2-qps超限 3-欠费? 4-其他错误 + -- data.result 0-找不到 1-成功 2-qps超限 3-欠费 4-其他错误 elseif data.result == 0 then - log.error(lib_name,"no location") + log.error(lib_name, "no location(基站定位服务器查询当前地址失败)") return false elseif data.result == 1 then - return true,{lng = data.lng,lat = data.lat} + log.info("多基站请求成功,服务器返回的原始数据", data) + return true, { + lng = data.lng, + lat = data.lat + } elseif data.result == 2 then - log.error(lib_name,"qps limit") + log.error(lib_name, "qps limit(当前请求已到达限制,请检查当前请求是否过于频繁))") return false elseif data.result == 3 then - log.error(lib_name,"no money") + log.error(lib_name, "当前设备已欠费,请联系销售充值") return false elseif data.result == 4 then - log.error(lib_name,"other error") + log.error(lib_name, "other error") return false + else + log.error("其他错误,错误码", data.result, lib_name) end else - log.error(lib_name,"net error") + log.error(lib_name, "net error") return false end end end - return airlbs -- 2.11.4.GIT