lbsLoc.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. --- 模块功能:根据基站信息查询经纬度
  2. -- @module lbsLoc
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.25
  7. require"socket"
  8. require"utils"
  9. require"common"
  10. require"misc"
  11. module(..., package.seeall)
  12. local function enCellInfo(s)
  13. local ret,t,mcc,mnc,lac,ci,rssi,k,v,m,n,cntrssi = "",{}
  14. log.info("lbsLoc.enCellInfo",s)
  15. for mcc,mnc,lac,ci,rssi in string.gmatch(s,"(%d+)%.(%d+)%.(%d+)%.(%d+)%.(%d+);") do
  16. mcc,mnc,lac,ci,rssi = tonumber(mcc),tonumber(mnc),tonumber(lac),tonumber(ci),(tonumber(rssi) > 31) and 31 or tonumber(rssi)
  17. local handle = nil
  18. for k,v in pairs(t) do
  19. if v.lac == lac and v.mcc == mcc and v.mnc == mnc then
  20. if #v.rssici < 8 then
  21. table.insert(v.rssici,{rssi=rssi,ci=ci})
  22. end
  23. handle = true
  24. break
  25. end
  26. end
  27. if not handle then
  28. table.insert(t,{mcc=mcc,mnc=mnc,lac=lac,rssici={{rssi=rssi,ci=ci}}})
  29. end
  30. end
  31. for k,v in pairs(t) do
  32. ret = ret .. pack.pack(">HHb",v.lac,v.mcc,v.mnc)
  33. for m,n in pairs(v.rssici) do
  34. cntrssi = bit.bor(bit.lshift(((m == 1) and (#v.rssici-1) or 0),5),n.rssi)
  35. ret = ret .. pack.pack(">bi",cntrssi,n.ci)
  36. end
  37. end
  38. return string.char(#t)..ret
  39. end
  40. local function enWifiInfo(tWifi)
  41. local ret,cnt,k,v = "",0
  42. if tWifi then
  43. for k,v in pairs(tWifi) do
  44. log.info("lbsLoc.enWifiInfo",k,v)
  45. ret = ret..pack.pack("Ab",(k:gsub(":","")):fromHex(),(v<0) and (v+255) or v)
  46. cnt = cnt+1
  47. end
  48. end
  49. return string.char(cnt)..ret
  50. end
  51. local function enMuid()
  52. local muid = misc.getMuid()
  53. return string.char(muid:len())..muid
  54. end
  55. local function trans(str)
  56. local s = str
  57. if str:len()<10 then
  58. s = str..string.rep("0",10-str:len())
  59. end
  60. return s:sub(1,3).."."..s:sub(4,10)
  61. end
  62. local function taskClient(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  63. while not socket.isReady() do
  64. if not sys.waitUntil("IP_READY_IND",timeout) then return cbFnc(1) end
  65. end
  66. local retryCnt,sck = 0
  67. local reqStr = pack.pack("bAbAAAAA",
  68. productKey:len(),
  69. productKey,
  70. (reqAddr and 2 or 0)+(reqTime and 4 or 0)+8+(reqWifi and 16 or 0)+32,
  71. "",
  72. common.numToBcdNum(misc.getImei()),
  73. enMuid(),
  74. enCellInfo(net.getCellInfoExt()),
  75. enWifiInfo(reqWifi))
  76. log.info("reqStr",reqStr:toHex())
  77. while true do
  78. sck = socket.udp()
  79. if not sck then cbFnc(6) return end
  80. if sck:connect(host,port) then
  81. while true do
  82. if sck:send(reqStr) then
  83. local result,data = sck:recv(timeout)
  84. if result then
  85. sck:close()
  86. log.info("lbsLoc receive",data:toHex())
  87. if data:len()>=11 and (data:byte(1)==0 or data:byte(1)==0xFF) then
  88. local locType = data:byte(1)
  89. cbFnc(0,
  90. trans(common.bcdNumToNum(data:sub(2,6))),
  91. trans(common.bcdNumToNum(data:sub(7,11))),
  92. reqAddr and data:sub(13,12+data:byte(12)) or nil,
  93. data:sub(reqAddr and (13+data:byte(12)) or 12,-1),
  94. locType)
  95. else
  96. log.warn("lbsLoc.query","根据基站查询经纬度失败")
  97. if data:byte(1)==2 then
  98. log.warn("lbsLoc.query","main.lua中的PRODUCT_KEY和此设备在iot.openluat.com中所属项目的ProductKey必须一致,请去检查")
  99. else
  100. log.warn("lbsLoc.query","基站数据库查询不到所有小区的位置信息")
  101. log.warn("lbsLoc.query","在trace中向上搜索encellinfo,然后在电脑浏览器中打开http://bs.openluat.com/,手动查找encellinfo后的所有小区位置")
  102. log.warn("lbsLoc.query","如果手动可以查到位置,则服务器存在BUG,直接向技术人员反映问题")
  103. log.warn("lbsLoc.query","如果手动无法查到位置,则基站数据库还没有收录当前设备的小区位置信息,向技术人员反馈,我们会尽快收录")
  104. end
  105. cbFnc(5)
  106. end
  107. return
  108. else
  109. sck:close()
  110. retryCnt = retryCnt+1
  111. if retryCnt>=3 then return cbFnc(4) end
  112. break
  113. end
  114. else
  115. sck:close()
  116. retryCnt = retryCnt+1
  117. if retryCnt>=3 then return cbFnc(3) end
  118. break
  119. end
  120. end
  121. else
  122. sck:close()
  123. retryCnt = retryCnt+1
  124. if retryCnt>=3 then return cbFnc(2) end
  125. end
  126. end
  127. end
  128. --- 发送基站/WIFI定位请求(仅支持中国区域的位置查询)
  129. -- @function cbFnc 用户回调函数,回调函数的调用形式为:
  130. -- cbFnc(result,lat,lng,addr,time,locType)
  131. -- result:number类型
  132. -- 0表示成功
  133. -- 1表示网络环境尚未就绪
  134. -- 2表示连接服务器失败
  135. -- 3表示发送数据失败
  136. -- 4表示接收服务器应答超时
  137. -- 5表示服务器返回查询失败
  138. -- 6表示socket已满,创建socket失败
  139. -- 为0时,后面的5个参数才有意义
  140. -- lat:string类型或者nil,纬度,整数部分3位,小数部分7位,例如"031.2425864"
  141. -- lng:string类型或者nil,经度,整数部分3位,小数部分7位,例如"121.4736522"
  142. -- addr:目前无意义
  143. -- time:string类型或者nil,服务器返回的时间,6个字节,年月日时分秒,需要转为十六进制读取
  144. -- 第一个字节:年减去2000,例如2017年,则为0x11
  145. -- 第二个字节:月,例如7月则为0x07,12月则为0x0C
  146. -- 第三个字节:日,例如11日则为0x0B
  147. -- 第四个字节:时,例如18时则为0x12
  148. -- 第五个字节:分,例如59分则为0x3B
  149. -- 第六个字节:秒,例如48秒则为0x30
  150. -- locType:numble类型或者nil,定位类型,0表示基站定位成功,255表示WIFI定位成功
  151. -- @bool[opt=nil] reqAddr 是否请求服务器返回具体的位置字符串信息,目前此功能不完善
  152. -- @number[opt=20000] timeout 请求超时时间,单位毫秒,默认20000毫秒
  153. -- @string[opt=nil] productKey IOT网站上的产品证书,如果在main.lua中定义了PRODUCT_KEY变量,则此参数可以传nil
  154. -- @string[opt=nil] host 服务器域名,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  155. -- @string[opt=nil] port 服务器端口,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  156. -- @bool[opt=nil] reqTime 是否需要服务器返回时间信息,true返回,false或者nil不返回,此参数可选,目前仅lib中agps.lua使用此参数。应用脚本可以直接传nil
  157. -- @table[opt=nil] reqWifi 搜索到的WIFI热点信息(MAC地址和信号强度),如果传入了此参数,后台会查询WIFI热点对应的经纬度,此参数格式如下:
  158. -- {
  159. -- ["1a:fe:34:9e:a1:77"] = -63,
  160. -- ["8c:be:be:2d:cd:e9"] = -81,
  161. -- ["20:4e:7f:82:c2:c4"] = -70,
  162. -- }
  163. -- @return nil
  164. -- @usage lbsLoc.request(cbFnc)
  165. -- @usage lbsLoc.request(cbFnc,true)
  166. -- @usage lbsLoc.request(cbFnc,nil,20000)
  167. function request(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  168. assert(_G.PRODUCT_KEY or productKey,"undefine PRODUCT_KEY in main.lua")
  169. sys.taskInit(taskClient,cbFnc,reqAddr,timeout or 20000,productKey or _G.PRODUCT_KEY,host or "bs.openluat.com",port or "12411",reqTime,reqWifi)
  170. end