lbsLoc.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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仅供演示使用,不保证一直能用
  11. --量产项目中一定要使用自己在iot.openluat.com中创建的项目productKey,项目详情里
  12. --基站定位的坐标系是 WSG84
  13. PRODUCT_KEY = "v32xEAKsGTIEQxtqgwCldp5aPlcnPs3K"
  14. local lbsLoc = require("lbsLoc")
  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. -- 获取经纬度成功, 坐标系WGS84
  31. if result == 0 then
  32. log.info("服务器返回的时间", time:toHex())
  33. log.info("定位类型,基站定位成功返回0", locType)
  34. end
  35. end
  36. lbsLoc.request(getLocCb)
  37. ]]
  38. local sys = require "sys"
  39. local sysplus = require("sysplus")
  40. local libnet = require("libnet")
  41. local lbsLoc = {}
  42. local d1Name = "lbsLoc"
  43. --- ASCII字符串 转化为 BCD编码格式字符串(仅支持数字)
  44. -- @string inStr 待转换字符串
  45. -- @number destLen 转换后的字符串期望长度,如果实际不足,则填充F
  46. -- @return string data,转换后的字符串
  47. -- @usage
  48. local function numToBcdNum(inStr,destLen)
  49. local l,t,num = string.len(inStr or ""),{}
  50. destLen = destLen or (inStr:len()+1)/2
  51. for i=1,l,2 do
  52. num = tonumber(inStr:sub(i,i+1),16)
  53. if i==l then
  54. num = 0xf0+num
  55. else
  56. num = (num%0x10)*0x10 + (num-(num%0x10))/0x10
  57. end
  58. table.insert(t,num)
  59. end
  60. local s = string.char(unpack(t))
  61. l = string.len(s)
  62. if l < destLen then
  63. s = s .. string.rep("\255",destLen-l)
  64. elseif l > destLen then
  65. s = string.sub(s,1,destLen)
  66. end
  67. return s
  68. end
  69. --- BCD编码格式字符串 转化为 号码ASCII字符串(仅支持数字)
  70. -- @string num 待转换字符串
  71. -- @return string data,转换后的字符串
  72. -- @usage
  73. local function bcdNumToNum(num)
  74. local byte,v1,v2
  75. local t = {}
  76. for i=1,num:len() do
  77. byte = num:byte(i)
  78. v1,v2 = bit.band(byte,0x0f),bit.band(bit.rshift(byte,4),0x0f)
  79. if v1 == 0x0f then break end
  80. table.insert(t,v1)
  81. if v2 == 0x0f then break end
  82. table.insert(t,v2)
  83. end
  84. return table.concat(t)
  85. end
  86. local function netCB(msg)
  87. --log.info("未处理消息", msg[1], msg[2], msg[3], msg[4])
  88. end
  89. local function enCellInfo(s)
  90. local ret,t,mcc,mnc,lac,ci,rssi,k,v,m,n,cntrssi = "",{}
  91. for k,v in pairs(s) do
  92. mcc,mnc,lac,ci,rssi = v.mcc,v.mnc,v.tac,v.cid,((v.rsrq + 144) >31) and 31 or (v.rsrq + 144)
  93. local handle = nil
  94. for k,v in pairs(t) do
  95. if v.lac == lac and v.mcc == mcc and v.mnc == mnc then
  96. if #v.rssici < 8 then
  97. table.insert(v.rssici,{rssi=rssi,ci=ci})
  98. end
  99. handle = true
  100. break
  101. end
  102. end
  103. if not handle then
  104. table.insert(t,{mcc=mcc,mnc=mnc,lac=lac,rssici={{rssi=rssi,ci=ci}}})
  105. end
  106. log.debug("rssi,mcc,mnc,lac,ci", rssi,mcc,mnc,lac,ci)
  107. end
  108. for k,v in pairs(t) do
  109. ret = ret .. pack.pack(">HHb",v.lac,v.mcc,v.mnc)
  110. for m,n in pairs(v.rssici) do
  111. cntrssi = bit.bor(bit.lshift(((m == 1) and (#v.rssici-1) or 0),5),n.rssi)
  112. ret = ret .. pack.pack(">bi",cntrssi,n.ci)
  113. end
  114. end
  115. return string.char(#t)..ret
  116. end
  117. local function enWifiInfo(tWifi)
  118. local ret,cnt = "", 0
  119. if tWifi then
  120. for k,v in pairs(tWifi) do
  121. -- log.info("lbsLoc.enWifiInfo",k,v)
  122. ret = ret..pack.pack("Ab",(k:gsub(":","")):fromHex(),(v<0) and (v+255) or v)
  123. cnt = cnt+1
  124. end
  125. end
  126. return string.char(cnt)..ret
  127. end
  128. local function enMuid() --获取模块MUID
  129. local muid = mobile.muid()
  130. return string.char(muid:len())..muid
  131. end
  132. local function trans(str)
  133. local s = str
  134. if str:len()<10 then
  135. s = str..string.rep("0",10-str:len())
  136. end
  137. return s:sub(1,3).."."..s:sub(4,10)
  138. end
  139. local function taskClient(cbFnc, reqAddr, timeout, productKey, host, port,reqTime, reqWifi)
  140. if mobile.status() == 0 then
  141. if not sys.waitUntil("IP_READY", timeout) then return cbFnc(1) end
  142. sys.wait(500)
  143. end
  144. if productKey == nil then
  145. productKey = ""
  146. end
  147. local retryCnt = 0
  148. local reqStr = pack.pack("bAbAAAAA", productKey:len(), productKey,
  149. (reqAddr and 2 or 0) + (reqTime and 4 or 0) + 8 +(reqWifi and 16 or 0) + 32, "",
  150. numToBcdNum(mobile.imei()), enMuid(),
  151. enCellInfo(mobile.getCellInfo()),
  152. enWifiInfo(reqWifi))
  153. log.debug("reqStr", reqStr:toHex())
  154. local rx_buff = zbuff.create(17)
  155. -- sys.wait(5000)
  156. while true do
  157. local result,succ,param
  158. local netc = socket.create(nil, d1Name) -- 创建socket对象
  159. if not netc then cbFnc(6) return end -- 创建socket失败
  160. socket.debug(netc, false)
  161. socket.config(netc, nil, true, nil)
  162. --result = libnet.waitLink(d1Name, 0, netc)
  163. result = libnet.connect(d1Name, 5000, netc, host, port)
  164. if result then
  165. while true do
  166. -- log.info(" lbsloc socket_service connect true")
  167. result = libnet.tx(d1Name, 0, netc, reqStr) ---发送数据
  168. if result then
  169. result, param = libnet.wait(d1Name, 15000 + retryCnt * 5, netc)
  170. if not result then
  171. socket.close(netc)
  172. socket.release(netc)
  173. retryCnt = retryCnt+1
  174. if retryCnt>=3 then return cbFnc(4) end
  175. break
  176. end
  177. succ, param = socket.rx(netc, rx_buff) -- 接收数据
  178. -- log.info("是否接收和数据长度", succ, param)
  179. if param ~= 0 then -- 如果接收成功
  180. socket.close(netc) -- 关闭连接
  181. socket.release(netc)
  182. local read_buff = rx_buff:toStr(0, param)
  183. rx_buff:clear()
  184. log.debug("lbsLoc receive", read_buff:toHex())
  185. if read_buff:len() >= 11 and(read_buff:byte(1) == 0 or read_buff:byte(1) == 0xFF) then
  186. local locType = read_buff:byte(1)
  187. cbFnc(0, trans(bcdNumToNum(read_buff:sub(2, 6))),
  188. trans(bcdNumToNum(read_buff:sub(7, 11))), reqAddr and
  189. read_buff:sub(13, 12 + read_buff:byte(12)) or nil,
  190. reqTime and read_buff:sub(reqAddr and (13 + read_buff:byte(12)) or 12, -1) or "",
  191. locType)
  192. else
  193. log.warn("lbsLoc.query", "根据基站查询经纬度失败")
  194. if read_buff:byte(1) == 2 then
  195. log.warn("lbsLoc.query","main.lua中的PRODUCT_KEY和此设备在iot.openluat.com中所属项目的ProductKey必须一致,请去检查")
  196. else
  197. log.warn("lbsLoc.query","基站数据库查询不到所有小区的位置信息")
  198. -- log.warn("lbsLoc.query","在trace中向上搜索encellinfo,然后在电脑浏览器中打开http://bs.openluat.com/,手动查找encellinfo后的所有小区位置")
  199. -- log.warn("lbsLoc.query","如果手动可以查到位置,则服务器存在BUG,直接向技术人员反映问题")
  200. -- log.warn("lbsLoc.query","如果手动无法查到位置,则基站数据库还没有收录当前设备的小区位置信息,向技术人员反馈,我们会尽快收录")
  201. end
  202. cbFnc(5)
  203. end
  204. return
  205. else
  206. socket.close(netc)
  207. socket.release(netc)
  208. retryCnt = retryCnt+1
  209. if retryCnt>=3 then return cbFnc(4) end
  210. break
  211. end
  212. else
  213. socket.close(netc)
  214. socket.release(netc)
  215. retryCnt = retryCnt+1
  216. if retryCnt>=3 then return cbFnc(3) end
  217. break
  218. end
  219. end
  220. else
  221. socket.close(netc)
  222. socket.release(netc)
  223. retryCnt = retryCnt + 1
  224. if retryCnt >= 3 then return cbFnc(2) end
  225. end
  226. end
  227. end
  228. --[[
  229. 发送基站定位请求
  230. @api lbsLoc.request(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  231. @function cbFnc 用户回调函数,回调函数的调用形式为:cbFnc(result,lat,lng,addr,time,locType)
  232. @bool reqAddr 是否请求服务器返回具体的位置字符串信息,已经不支持,填false或者nil
  233. @number timeout 请求超时时间,单位毫秒,默认20000毫秒
  234. @string productKey IOT网站上的产品KEY,如果在main.lua中定义了PRODUCT_KEY变量,则此参数可以传nil
  235. @string host 服务器域名, 默认 "bs.openluat.com" ,可选备用服务器(不保证可用) "bs.air32.cn"
  236. @string port 服务器端口,默认"12411",一般不需要设置
  237. @return nil
  238. @usage
  239. -- 提醒: 返回的坐标值, 是WGS84坐标系
  240. ]]
  241. function lbsLoc.request(cbFnc,reqAddr,timeout,productKey,host,port,reqTime,reqWifi)
  242. sysplus.taskInitEx(taskClient, d1Name, netCB, cbFnc, reqAddr,timeout or 20000,productKey or _G.PRODUCT_KEY,host or "bs.openluat.com",port or "12411", reqTime == nil and true or reqTime)
  243. end
  244. return lbsLoc