From 51ea988ffde4fcc074ba1bc55a686eab7afeb273 Mon Sep 17 00:00:00 2001 From: Dozingfiretruck <1473454180@qq.com> Date: Thu, 17 Oct 2024 10:17:34 +0800 Subject: [PATCH] add:airLbs.lua --- script/libs/airLbs.lua | 147 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 script/libs/airLbs.lua diff --git a/script/libs/airLbs.lua b/script/libs/airLbs.lua new file mode 100644 index 00000000..03ddc121 --- /dev/null +++ b/script/libs/airLbs.lua @@ -0,0 +1,147 @@ + + +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 location_data = 0 +local connect_failed = -1 + +local airLbs ={} + + +local function airlbs_task(task_name, buff, timeout) + local netc = socket.create(nil, lib_name) + socket.config(netc, nil, true) -- udp + + sysplus.cleanMsg(lib_name) + local result = libnet.connect(lib_name, 15000, netc, airlbs_host, airlbs_port) + if result then + log.info(lib_name, "服务器连上了") + libnet.tx(lib_name, 0, netc, buff) + else + log.info(lib_name, "服务器没连上了!!!") + sys.publish(lib_topic,connect_failed) + libnet.close(lib_name, 5000, netc) + return + end + buff:del() + while result do + local succ, param = socket.rx(netc, buff) + if not succ then + log.error(lib_name,"服务器断开了", succ, param) + sys.publish(lib_topic,connect_failed) + break + end + if buff:used() > 0 then + local data = buff:query(0,1) -- 获取数据 + if data:toHex() == '00' then + local location = json.decode(buff:query(1)) + sys.publish(lib_topic, location_data, location) + else + log.error(lib_name,"not json data") + end + buff:del() + break + end + result, param, param2 = libnet.wait(lib_name, timeout, netc) + log.info(lib_name, "wait", result, param, param2) + if param == false then + break + end + end + libnet.close(lib_name, 5000, netc) +end + +-- 处理未识别的网络消息 +local function netCB(msg) + log.info("未处理消息", msg[1], msg[2], msg[3], msg[4]) +end + +function airLbs.request(param) + if not mobile then + log.error(lib_name,"no mobile") + return false + end + if mobile.status() == 0 then + log.error(lib_name,"网络未注册") + return false + end + if param.project_id ==nil or param.project_key == nil then + log.error(lib_name,"param error") + return false + end + local timeout = + socket.sntp() + sys.waitUntil("NTP_UPDATE", 1000) + + local udp_buff = zbuff.create(1024) + local auth_type = 0x01 + 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") + end + local imei = mobile.imei() + local muid = mobile.muid() + local timestamp = os.time() + local project_key = param.project_key + local nonce = string.char(math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(0, 255), math.random(0, 255)) + + local hmac_data = crypto.hmac_sha1(project_id .. imei .. muid .. timestamp .. nonce, project_key) + -- log.debug(lib_name,"hmac_sha1", hmac_data) + + mobile.reqCellInfo(60) + sys.waitUntil("CELL_INFO_UPDATE", param.timeout or 15000) + -- log.info("cell", json.encode(mobile.getCellInfo())) + + local lbs_data = {cells={}} + for k, v in pairs(mobile.getCellInfo()) do + lbs_data.cells[k] = {} + lbs_data.cells[k].mnc = v.mnc + lbs_data.cells[k].mcc = v.mcc + lbs_data.cells[k].rssi = v.rssi + lbs_data.cells[k].pci = v.pci + lbs_data.cells[k].rsrp = v.rsrp + lbs_data.cells[k].tac = v.tac + lbs_data.cells[k].cid = v.cid + lbs_data.cells[k].rsrq = v.rsrq + lbs_data.cells[k].snr = v.snr + lbs_data.cells[k].earfcn = v.earfcn + end + local lbs_jdata = json.encode(lbs_data) + + 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 15000) + + while 1 do + local result, tp, data = sys.waitUntil(lib_topic, param.timeout or 15000) + log.info("event", result, tp, data) + if not result then + -- 等很久了,没数据上传/下发, 发个日期心跳包吧 + return false + elseif tp == location_data then + -- result 0-找不到 1-成功 2-qps超限 3-欠费? 4-其他错误 + if data.result == 1 then + return true,{lon = data.lon,lat = data.lat} + end + -- print("location", json.encode(data)) + -- return true,{} + else + + end + end + +end + + +return airLbs +