airlbs.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. --[[
  2. @module airlbs
  3. @summary airlbs 定位服务(收费服务,需自行联系销售申请)
  4. @version 1.0
  5. @date 2024.11.01
  6. @author Dozingfiretruck
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. --注意:使用前需同步时间
  10. -- 用法实例
  11. local airlbs = require "airlbs"
  12. sys.taskInit(function()
  13. sys.waitUntil("IP_READY")
  14. socket.sntp()
  15. sys.waitUntil("NTP_UPDATE", 1000)
  16. while 1 do
  17. local result , data = airlbs.request({project_id = "xxx",project_key = 'xxx',timeout = 1000})
  18. if result then
  19. print("airlbs", json.encode(data))
  20. end
  21. sys.wait(20000)
  22. end
  23. end)
  24. ]] sys = require("sys")
  25. sysplus = require("sysplus")
  26. libnet = require "libnet"
  27. local airlbs_host = "airlbs.openluat.com"
  28. local airlbs_port = 12413
  29. local lib_name = "airlbs"
  30. local lib_topic = lib_name .. "topic"
  31. local location_data = 0
  32. local disconnect = -1
  33. local airlbs_timeout = 15000
  34. local airlbs = {}
  35. local function airlbs_task(task_name, buff, timeout)
  36. local netc = socket.create(nil, lib_name)
  37. socket.config(netc, nil, true) -- udp
  38. sysplus.cleanMsg(lib_name)
  39. local result = libnet.connect(lib_name, 15000, netc, airlbs_host, airlbs_port)
  40. if result then
  41. log.info(lib_name, "服务器连上了")
  42. libnet.tx(lib_name, 0, netc, buff)
  43. else
  44. log.info(lib_name, "服务器没连上了!!!")
  45. sys.publish(lib_topic, disconnect)
  46. libnet.close(lib_name, 5000, netc)
  47. return
  48. end
  49. buff:del()
  50. while result do
  51. local succ, param = socket.rx(netc, buff)
  52. if not succ then
  53. log.error(lib_name, "服务器断开了", succ, param)
  54. sys.publish(lib_topic, disconnect)
  55. break
  56. end
  57. if buff:used() > 0 then
  58. local location = nil
  59. local data = buff:query(0, 1) -- 获取数据
  60. if data:toHex() == '00' then
  61. location = json.decode(buff:query(1))
  62. else
  63. log.error(lib_name, "not json data")
  64. end
  65. sys.publish(lib_topic, location_data, location)
  66. buff:del()
  67. break
  68. end
  69. result, param, param2 = libnet.wait(lib_name, timeout, netc)
  70. log.info(lib_name, "wait", result, param, param2)
  71. if param == false then
  72. log.error(lib_name, "服务器断开了", succ, param)
  73. sys.publish(lib_topic, disconnect)
  74. break
  75. end
  76. end
  77. libnet.close(lib_name, 5000, netc)
  78. end
  79. -- 处理未识别的网络消息
  80. local function netCB(msg)
  81. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  82. end
  83. --[[
  84. 获取定位数据
  85. @api airlbs.request(param)
  86. @param table 参数(联系销售获取id与key) project_id:项目ID project_key:项目密钥 timeout:超时时间,单位毫秒 默认15000
  87. @return bool 成功返回true,失败会返回false
  88. @return table 定位成功生效,成功返回定位数据
  89. @usage
  90. local result , data = airlbs.request({project_id = airlbs_project_id,project_key = airlbs_project_key})
  91. if result then
  92. print("airlbs", json.encode(data))
  93. end
  94. ]]
  95. function airlbs.request(param)
  96. if not mobile then
  97. log.error(lib_name, "no mobile")
  98. return false
  99. end
  100. if mobile.status() == 0 then
  101. log.error(lib_name, "网络未注册")
  102. return false
  103. end
  104. if param.project_id == nil or param.project_key == nil then
  105. log.error(lib_name, "param error")
  106. return false
  107. end
  108. local udp_buff = zbuff.create(1500)
  109. local auth_type = 0x01
  110. local lbs_data_type = 0x00
  111. local project_id = param.project_id
  112. if project_id:len() ~= 6 then
  113. log.error("airlbs", "project_id len not 6")
  114. end
  115. local imei = mobile.imei()
  116. local muid = mobile.muid()
  117. local timestamp = os.time()
  118. local project_key = param.project_key
  119. local nonce = crypto.trng(6)
  120. local hmac_data = crypto.hmac_sha1(project_id .. imei .. muid .. timestamp .. nonce, project_key)
  121. -- log.debug(lib_name,"hmac_sha1", hmac_data)
  122. mobile.reqCellInfo(60)
  123. sys.waitUntil("CELL_INFO_UPDATE", param.timeout or airlbs_timeout)
  124. -- log.info("cell", json.encode(mobile.getCellInfo()))
  125. local lbs_data = {
  126. cells = {}
  127. }
  128. for k, v in pairs(mobile.getCellInfo()) do
  129. lbs_data.cells[k] = {}
  130. lbs_data.cells[k][1] = v.mcc
  131. lbs_data.cells[k][2] = v.mnc
  132. lbs_data.cells[k][3] = v.tac
  133. lbs_data.cells[k][4] = v.cid
  134. lbs_data.cells[k][5] = v.rssi
  135. lbs_data.cells[k][6] = v.snr
  136. lbs_data.cells[k][7] = v.pci
  137. lbs_data.cells[k][8] = v.rsrp
  138. lbs_data.cells[k][9] = v.rsrq
  139. lbs_data.cells[k][10] = v.earfcn
  140. end
  141. if param.wifi_info and #param.wifi_info > 0 then
  142. lbs_data.macs = {}
  143. for k, v in pairs(param.wifi_info) do
  144. lbs_data.macs[k] = {}
  145. lbs_data.macs[k][1] = v.bssid:toHex():gsub("(%x%x)", "%1:"):sub(1, -2)
  146. lbs_data.macs[k][2] = v.rssi
  147. end
  148. end
  149. local lbs_jdata = json.encode(lbs_data)
  150. log.info("扫描出的数据",lbs_jdata)
  151. udp_buff:write(string.char(auth_type) .. project_id .. imei .. muid .. timestamp .. nonce .. hmac_data:fromHex() .. string.char(lbs_data_type) .. lbs_jdata)
  152. sysplus.taskInitEx(airlbs_task, lib_name, netCB, lib_name, udp_buff, param.timeout or airlbs_timeout)
  153. while 1 do
  154. local result, tp, data = sys.waitUntil(lib_topic, param.timeout or airlbs_timeout)
  155. log.info("定位请求的结果", result, "超时时间", tp, data)
  156. if not result then
  157. return false, "timeout"
  158. elseif tp == location_data then
  159. if not data then
  160. log.error(lib_name, "无数据, 请检查project_id和project_key")
  161. return false
  162. -- data.result 0-找不到 1-成功 2-qps超限 3-欠费 4-其他错误
  163. elseif data.result == 0 then
  164. log.error(lib_name, "no location(基站定位服务器查询当前地址失败)")
  165. return false
  166. elseif data.result == 1 then
  167. log.info("多基站请求成功,服务器返回的原始数据", data)
  168. return true, {
  169. lng = data.lng,
  170. lat = data.lat
  171. }
  172. elseif data.result == 2 then
  173. log.error(lib_name, "qps limit(当前请求已到达限制,请检查当前请求是否过于频繁))")
  174. return false
  175. elseif data.result == 3 then
  176. log.error(lib_name, "当前设备已欠费,请联系销售充值")
  177. return false
  178. elseif data.result == 4 then
  179. log.error(lib_name, "other error")
  180. return false
  181. else
  182. log.error("其他错误,错误码", data.result, lib_name)
  183. end
  184. else
  185. log.error(lib_name, "net error")
  186. return false
  187. end
  188. end
  189. end
  190. return airlbs