airlbs.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. --[[
  2. @module airlbs
  3. @summary airlbs 定位服务(收费服务,需自行联系销售申请)
  4. @version 1.0
  5. @date 2024.11.01
  6. @author Dozingfiretruck
  7. ]]
  8. sys = require("sys")
  9. sysplus = require("sysplus")
  10. libnet = require "libnet"
  11. local airlbs_host = "airlbs.openluat.com"
  12. local airlbs_port = 12413
  13. local lib_name = "airlbs"
  14. local lib_topic = lib_name .. "topic"
  15. local location_data = 0
  16. local disconnect = -1
  17. local airlbs_timeout = 15000
  18. local airlbs = {}
  19. local function airlbs_task(task_name, buff, timeout, adapter)
  20. local netc = socket.create(nil, lib_name)
  21. socket.config(netc, adapter, true) -- udp
  22. sysplus.cleanMsg(lib_name)
  23. local result = libnet.connect(lib_name, 15000, netc, airlbs_host, airlbs_port)
  24. if result then
  25. log.info(lib_name, "服务器连上了")
  26. libnet.tx(lib_name, 0, netc, buff)
  27. else
  28. log.info(lib_name, "服务器没连上了!!!")
  29. sys.publish(lib_topic, disconnect)
  30. libnet.close(lib_name, 5000, netc)
  31. return
  32. end
  33. buff:del()
  34. while result do
  35. local succ, param = socket.rx(netc, buff)
  36. if not succ then
  37. log.error(lib_name, "服务器断开了", succ, param)
  38. sys.publish(lib_topic, disconnect)
  39. break
  40. end
  41. if buff:used() > 0 then
  42. local location = nil
  43. local data = buff:query(0, 1) -- 获取数据
  44. if data:toHex() == '00' then
  45. location = json.decode(buff:query(1))
  46. else
  47. log.error(lib_name, "not json data")
  48. end
  49. sys.publish(lib_topic, location_data, location)
  50. buff:del()
  51. break
  52. end
  53. result, param, param2 = libnet.wait(lib_name, timeout, netc)
  54. log.info(lib_name, "wait", result, param, param2)
  55. if param == false then
  56. log.error(lib_name, "服务器断开了", succ, param)
  57. sys.publish(lib_topic, disconnect)
  58. break
  59. end
  60. end
  61. libnet.close(lib_name, 5000, netc)
  62. end
  63. -- 处理未识别的网络消息
  64. local function netCB(msg)
  65. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  66. end
  67. --[[
  68. 获取定位数据
  69. @api airlbs.request(param)
  70. @param table 参数(联系销售获取id与key) project_id:项目ID project_key:项目密钥 timeout:超时时间,单位毫秒 默认15000 adapter: 网络适配器id,可选,默认是平台自带的网络协议栈
  71. @return bool 成功返回true,失败会返回false
  72. @return table 定位成功生效,成功返回定位数据
  73. @usage
  74. --注意:函数内因使用了sys.waitUntil阻塞接口,所以api需要在协程中使用
  75. --注意:使用前需同步时间
  76. local airlbs = require "airlbs"
  77. sys.taskInit(function()
  78. -- 等待网络就绪
  79. sys.waitUntil("IP_READY")
  80. -- 执行时间同步
  81. socket.sntp()
  82. sys.waitUntil("NTP_UPDATE", 10000)
  83. while 1 do
  84. -- airlbs请求定位
  85. local result ,data = airlbs.request({
  86. project_id = airlbs_project_id,
  87. project_key = airlbs_project_key,
  88. timeout = 10000,
  89. adapter = socket.LWIP_STA
  90. })
  91. if result then
  92. log.info("airlbs", json.encode(data))
  93. end
  94. sys.wait(20000)
  95. end
  96. end)
  97. ]]
  98. function airlbs.request(param)
  99. if not param or param.project_id == nil or param.project_key == nil then
  100. log.error(lib_name, "param error")
  101. return false
  102. end
  103. if not mobile and not param.wifi_info then
  104. log.error(lib_name, "no mobile and no wifi_info")
  105. return false
  106. end
  107. local udp_buff = zbuff.create(1500)
  108. local auth_type = 0x01
  109. local lbs_data_type = 0x00
  110. local project_id = param.project_id
  111. if project_id:len() ~= 6 then
  112. log.error("airlbs", "project_id len not 6")
  113. end
  114. local imei = mobile.imei()
  115. local muid = mobile.muid()
  116. local timestamp = os.time()
  117. local project_key = param.project_key
  118. local nonce = crypto.trng(6)
  119. local hmac_data = crypto.hmac_sha1(project_id .. imei .. muid .. timestamp .. nonce, project_key)
  120. -- log.debug(lib_name,"hmac_sha1", hmac_data)
  121. local lbs_data = {}
  122. if mobile then
  123. mobile.reqCellInfo(60)
  124. sys.waitUntil("CELL_INFO_UPDATE", param.timeout or airlbs_timeout)
  125. lbs_data.cells = {}
  126. -- log.info("cell", json.encode(mobile.getCellInfo()))
  127. for k, v in pairs(mobile.getCellInfo()) do
  128. lbs_data.cells[k] = {}
  129. lbs_data.cells[k][1] = v.mcc
  130. lbs_data.cells[k][2] = v.mnc
  131. lbs_data.cells[k][3] = v.tac
  132. lbs_data.cells[k][4] = v.cid
  133. lbs_data.cells[k][5] = v.rssi
  134. lbs_data.cells[k][6] = v.snr
  135. lbs_data.cells[k][7] = v.pci
  136. lbs_data.cells[k][8] = v.rsrp
  137. lbs_data.cells[k][9] = v.rsrq
  138. lbs_data.cells[k][10] = v.earfcn
  139. end
  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, param.adapter)
  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