lbsLoc.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. --[[
  2. @module lbsLoc
  3. @summary lbsLoc 发送基站定位请求
  4. @version 1.0
  5. @date 2022.12.16
  6. @author luatos
  7. @usage
  8. --注意:因使用了sys.wait()所有api需要在协程中使用
  9. --用法实例
  10. PRODUCT_KEY = "VmhtOb81EgZau6YyuuZJzwF6oUNGCbXi"
  11. local lbsLoc = require("lbsLoc")
  12. local function reqLbsLoc()
  13. lbsLoc.request(getLocCb)
  14. end
  15. -- 功能:获取基站对应的经纬度后的回调函数
  16. -- 参数:-- result:number类型,0表示成功,1表示网络环境尚未就绪,2表示连接服务器失败,3表示发送数据失败,4表示接收服务器应答超时,5表示服务器返回查询失败;为0时,后面的5个参数才有意义
  17. -- lat:string类型,纬度,整数部分3位,小数部分7位,例如031.2425864
  18. -- lng:string类型,经度,整数部分3位,小数部分7位,例如121.4736522
  19. -- addr:目前无意义
  20. -- time:string类型或者nil,服务器返回的时间,6个字节,年月日时分秒,需要转为十六进制读取
  21. -- 第一个字节:年减去2000,例如2017年,则为0x11
  22. -- 第二个字节:月,例如7月则为0x07,12月则为0x0C
  23. -- 第三个字节:日,例如11日则为0x0B
  24. -- 第四个字节:时,例如18时则为0x12
  25. -- 第五个字节:分,例如59分则为0x3B
  26. -- 第六个字节:秒,例如48秒则为0x30
  27. -- locType:numble类型或者nil,定位类型,0表示基站定位成功,255表示WIFI定位成功
  28. function getLocCb(result, lat, lng, addr, time, locType)
  29. log.info("testLbsLoc.getLocCb", result, lat, lng)
  30. -- 获取经纬度成功
  31. if result == 0 then
  32. log.info("服务器返回的时间", time:toHex())
  33. log.info("定位类型,基站定位成功返回0", locType)
  34. end
  35. sys.timerStart(lbsLoc,20000)
  36. end
  37. reqLbsLoc()
  38. ]]
  39. local lbsLoc = {}
  40. local d1Name = "D1_TASKL"
  41. --- 阻塞等待网卡的网络连接上,只能用于任务函数中
  42. -- @string 任务标志
  43. -- @int 超时时间,如果==0或者空,则没有超时一致等待
  44. -- @... 其他参数和socket.linkup一致
  45. -- @return 失败或者超时返回false 成功返回true
  46. local function waitLink(taskName, timeout, ...)
  47. local is_err, result = socket.linkup(...)
  48. if is_err then
  49. return false
  50. end
  51. if not result then
  52. result = sys_wait(taskName, socket.LINK, timeout)
  53. else
  54. return true
  55. end
  56. if type(result) == 'table' and result[2] == 0 then
  57. return true
  58. else
  59. return false
  60. end
  61. end
  62. --- 阻塞等待IP或者域名连接上,如果加密连接还要等握手完成,只能用于任务函数中
  63. -- @string 任务标志
  64. -- @int 超时时间,如果==0或者空,则没有超时一致等待
  65. -- @... 其他参数和socket.connect一致
  66. -- @return 失败或者超时返回false 成功返回true
  67. local function connect(taskName,timeout, ... )
  68. local is_err, result = socket.connect(...)
  69. if is_err then
  70. return false
  71. end
  72. if not result then
  73. result = sys_wait(taskName, socket.ON_LINE, timeout)
  74. else
  75. return true
  76. end
  77. if type(result) == 'table' and result[2] == 0 then
  78. return true
  79. else
  80. return false
  81. end
  82. end
  83. --- 阻塞等待数据发送完成,只能用于任务函数中
  84. -- @string 任务标志
  85. -- @int 超时时间,如果==0或者空,则没有超时一致等待
  86. -- @... 其他参数和socket.tx一致
  87. -- @return
  88. -- @boolean 失败或者超时返回false,缓冲区满了或者成功返回true
  89. -- @boolean 缓存区是否满了
  90. local function tx(taskName,timeout, ...)
  91. local is_err, is_full, result = socket.tx(...)
  92. if is_err then
  93. return false, is_full
  94. end
  95. if is_full then
  96. return true, true
  97. end
  98. if not result then
  99. result = sys_wait(taskName, socket.TX_OK, timeout)
  100. else
  101. return true, is_full
  102. end
  103. if type(result) == 'table' and result[2] == 0 then
  104. return true, false
  105. else
  106. return false, is_full
  107. end
  108. end
  109. --- 阻塞等待新的网络事件或者特定事件,只能用于任务函数中
  110. -- @string 任务标志
  111. -- @int 超时时间,如果==0或者空,则没有超时一致等待
  112. -- @... 其他参数和socket.wait一致
  113. -- @return
  114. -- @boolean 网络异常返回false,其他返回true
  115. -- @table or boolean 超时返回false,有新的数据到返回true,被其他事件退出的,返回接收到的事件
  116. local function wait(taskName,timeout, netc)
  117. local is_err, result = socket.wait(netc)
  118. if is_err then
  119. return false,false
  120. end
  121. if not result then
  122. result = sys_wait(taskName, socket.EVENT, timeout)
  123. else
  124. return true,true
  125. end
  126. if type(result) == 'table' then
  127. if result[2] == 0 then
  128. return true, true
  129. else
  130. return false, false
  131. end
  132. else
  133. return true, false
  134. end
  135. end
  136. --- ASCII字符串 转化为 BCD编码格式字符串(仅支持数字)
  137. -- @string inStr 待转换字符串
  138. -- @number destLen 转换后的字符串期望长度,如果实际不足,则填充F
  139. -- @return string data,转换后的字符串
  140. -- @usage
  141. local function numToBcdNum(inStr,destLen)
  142. local l,t,num = string.len(inStr or ""),{}
  143. destLen = destLen or (inStr:len()+1)/2
  144. for i=1,l,2 do
  145. num = tonumber(inStr:sub(i,i+1),16)
  146. if i==l then
  147. num = 0xf0+num
  148. else
  149. num = (num%0x10)*0x10 + (num-(num%0x10))/0x10
  150. end
  151. table.insert(t,num)
  152. end
  153. local s = string.char(unpack(t))
  154. l = string.len(s)
  155. if l < destLen then
  156. s = s .. string.rep("\255",destLen-l)
  157. elseif l > destLen then
  158. s = string.sub(s,1,destLen)
  159. end
  160. return s
  161. end
  162. --- BCD编码格式字符串 转化为 号码ASCII字符串(仅支持数字)
  163. -- @string num 待转换字符串
  164. -- @return string data,转换后的字符串
  165. -- @usage
  166. local function bcdNumToNum(num)
  167. local byte,v1,v2
  168. local t = {}
  169. for i=1,num:len() do
  170. byte = num:byte(i)
  171. v1,v2 = bit.band(byte,0x0f),bit.band(bit.rshift(byte,4),0x0f)
  172. if v1 == 0x0f then break end
  173. table.insert(t,v1)
  174. if v2 == 0x0f then break end
  175. table.insert(t,v2)
  176. end
  177. return table.concat(t)
  178. end
  179. local function netCB(msg)
  180. log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  181. end
  182. local function enCellInfo(s)
  183. local ret,t,mcc,mnc,lac,ci,rssi,k,v,m,n,cntrssi = "",{}
  184. for k,v in pairs(s) do
  185. mcc,mnc,lac,ci,rssi = v.mcc,v.mnc,v.tac,v.cid,((v.rsrq + 144) >31) and 31 or (v.rsrq + 144)
  186. local handle = nil
  187. for k,v in pairs(t) do
  188. if v.lac == lac and v.mcc == mcc and v.mnc == mnc then
  189. if #v.rssici < 8 then
  190. table.insert(v.rssici,{rssi=rssi,ci=ci})
  191. end
  192. handle = true
  193. break
  194. end
  195. end
  196. if not handle then
  197. table.insert(t,{mcc=mcc,mnc=mnc,lac=lac,rssici={{rssi=rssi,ci=ci}}})
  198. end
  199. log.info("rssi、mcc、mnc、lac、ci", rssi,mcc,mnc,lac,ci)
  200. end
  201. for k,v in pairs(t) do
  202. ret = ret .. pack.pack(">HHb",v.lac,v.mcc,v.mnc)
  203. for m,n in pairs(v.rssici) do
  204. cntrssi = bit.bor(bit.lshift(((m == 1) and (#v.rssici-1) or 0),5),n.rssi)
  205. ret = ret .. pack.pack(">bi",cntrssi,n.ci)
  206. end
  207. end
  208. return string.char(#t)..ret
  209. end
  210. local function enWifiInfo(tWifi)
  211. local ret,cnt,k,v = "",0
  212. if tWifi then
  213. for k,v in pairs(tWifi) do
  214. log.info("lbsLoc.enWifiInfo",k,v)
  215. ret = ret..pack.pack("Ab",(k:gsub(":","")):fromHex(),(v<0) and (v+255) or v)
  216. cnt = cnt+1
  217. end
  218. end
  219. return string.char(cnt)..ret
  220. end
  221. local function enMuid() --获取模块MUID
  222. local muid = mobile.muid()
  223. return string.char(muid:len())..muid
  224. end
  225. local function trans(str)
  226. local s = str
  227. if str:len()<10 then
  228. s = str..string.rep("0",10-str:len())
  229. end
  230. return s:sub(1,3).."."..s:sub(4,10)
  231. end
  232. local function taskClient(cbFnc, reqAddr, timeout, productKey, host, port,reqTime, reqWifi)
  233. while mobile.status() == 0 do
  234. if not sys.waitUntil("IP_READY", timeout) then return cbFnc(1) end
  235. end
  236. local retryCnt = 0
  237. local reqStr = pack.pack("bAbAAAAA", productKey:len(), productKey,
  238. (reqAddr and 2 or 0) + (reqTime and 4 or 0) + 8 +(reqWifi and 16 or 0) + 32, "",
  239. numToBcdNum(mobile.imei()), enMuid(),
  240. enCellInfo(mobile.getCellInfo()),
  241. enWifiInfo(reqWifi))
  242. log.info("reqStr", reqStr:toHex())
  243. local rx_buff = zbuff.create(17)
  244. -- sys.wait(5000)
  245. while true do
  246. netc = socket.create(nil, d1Name) -- 创建socket对象
  247. if not netc then cbFnc(6) return end -- 创建socket失败
  248. socket.debug(netc, false)
  249. socket.config(netc, nil, true, nil)
  250. waitLink(d1Name, 0, netc)
  251. local result = connect(d1Name, 15000, netc, host, port)
  252. if result then
  253. while true do
  254. log.info(" lbsloc socket_service connect true")
  255. local result, _ = tx(d1Name, 0, netc, reqStr) ---发送数据
  256. if result then
  257. wait(d1Name, timeout - 100, netc)
  258. local is_err, param, _, _ = socket.rx(netc, rx_buff) -- 接收数据
  259. log.info("是否接收和数据长度", not is_err, param)
  260. if not is_err then -- 如果接收成功
  261. socket.close(netc) -- 关闭连接
  262. socket.release(netc)
  263. local read_buff = rx_buff:toStr(0, param)
  264. rx_buff:clear()
  265. log.info("lbsLoc receive", read_buff:toHex())
  266. if read_buff:len() >= 11 and(read_buff:byte(1) == 0 or read_buff:byte(1) == 0xFF) then
  267. local locType = read_buff:byte(1)
  268. cbFnc(0, trans(bcdNumToNum(read_buff:sub(2, 6))),
  269. trans(bcdNumToNum(read_buff:sub(7, 11))), reqAddr and
  270. read_buff:sub(13, 12 + read_buff:byte(12)) or nil,
  271. read_buff:sub(reqAddr and (13 + read_buff:byte(12)) or 12, -1),
  272. locType)
  273. else
  274. log.warn("lbsLoc.query", "根据基站查询经纬度失败")
  275. if read_buff:byte(1) == 2 then
  276. log.warn("lbsLoc.query","main.lua中的PRODUCT_KEY和此设备在iot.openluat.com中所属项目的ProductKey必须一致,请去检查")
  277. else
  278. log.warn("lbsLoc.query","基站数据库查询不到所有小区的位置信息")
  279. log.warn("lbsLoc.query","在trace中向上搜索encellinfo,然后在电脑浏览器中打开http://bs.openluat.com/,手动查找encellinfo后的所有小区位置")
  280. log.warn("lbsLoc.query","如果手动可以查到位置,则服务器存在BUG,直接向技术人员反映问题")
  281. log.warn("lbsLoc.query","如果手动无法查到位置,则基站数据库还没有收录当前设备的小区位置信息,向技术人员反馈,我们会尽快收录")
  282. end
  283. cbFnc(5)
  284. end
  285. return
  286. else
  287. socket.close(netc)
  288. socket.release(netc)
  289. retryCnt = retryCnt+1
  290. if retryCnt>=3 then return cbFnc(4) end
  291. break
  292. end
  293. else
  294. socket.close(netc)
  295. socket.release(netc)
  296. retryCnt = retryCnt+1
  297. if retryCnt>=3 then return cbFnc(3) end
  298. break
  299. end
  300. end
  301. else
  302. socket.close(netc)
  303. socket.release(netc)
  304. retryCnt = retryCnt + 1
  305. if retryCnt >= 3 then return cbFnc(2) end
  306. end
  307. end
  308. end
  309. --[[
  310. 发送基站/WIFI定位请求(仅支持中国区域的位置查询)
  311. @api lbsLoc.request(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  312. @function cbFnc 用户回调函数,回调函数的调用形式为:cbFnc(result,lat,lng,addr,time,locType)
  313. @bool reqAddr 是否请求服务器返回具体的位置字符串信息,目前此功能不完善,参数可以传nil
  314. @number timeout 请求超时时间,单位毫秒,默认20000毫秒
  315. @string productKey IOT网站上的产品证书,如果在main.lua中定义了PRODUCT_KEY变量,则此参数可以传nil
  316. @string host 服务器域名,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  317. @string port 服务器端口,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  318. @bool reqTime 是否需要服务器返回时间信息,true返回,false或者nil不返回,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  319. @table reqWifi 搜索到的WIFI热点信息(MAC地址和信号强度),如果传入了此参数,后台会查询WIFI热点对应的经纬度,此参数格式如下:
  320. {["1a:fe:34:9e:a1:77"] = -63,["8c:be:be:2d:cd:e9"] = -81,["20:4e:7f:82:c2:c4"] = -70,}
  321. @return nil
  322. ]]
  323. function lbsLoc.request(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  324. sysplus.taskInitEx(taskClient, d1Name, netCB, cbFnc,reqAddr or nil,timeout or 20000,productKey or _G.PRODUCT_KEY,host or "bs.openluat.com",port or "12411",reqTime,reqWifi)
  325. end
  326. return lbsLoc