lbsLoc2.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. --[[
  2. @module lbsLoc2
  3. @summary 基站定位v2
  4. @version 1.0
  5. @date 2023.5.23
  6. @author wendal
  7. @demo lbsLoc2
  8. @usage
  9. -- 注意:
  10. -- 1. 因使用了sys.wait()所有api需要在协程中使用
  11. -- 2. 仅支持单基站定位, 即当前联网的基站
  12. -- 3. 本服务当前处于测试状态
  13. sys.taskInit(function()
  14. sys.waitUntil("IP_READY", 30000)
  15. -- mobile.reqCellInfo(60)
  16. -- sys.wait(1000)
  17. while mobile do -- 没有mobile库就没有基站定位
  18. mobile.reqCellInfo(15)
  19. sys.waitUntil("CELL_INFO_UPDATE", 3000)
  20. local lat, lng, t = lbsLoc2.request(5000)
  21. -- local lat, lng, t = lbsLoc2.request(5000, "bs.openluat.com")
  22. log.info("lbsLoc2", lat, lng, (json.encode(t or {})))
  23. sys.wait(60000)
  24. end
  25. end)
  26. ]]
  27. local sys = require "sys"
  28. local lbsLoc2 = {}
  29. local function numToBcdNum(inStr,destLen)
  30. local l,t,num = string.len(inStr or ""),{}
  31. destLen = destLen or (inStr:len()+1)/2
  32. for i=1,l,2 do
  33. num = tonumber(inStr:sub(i,i+1),16)
  34. if i==l then
  35. num = 0xf0+num
  36. else
  37. num = (num%0x10)*0x10 + (num-(num%0x10))/0x10
  38. end
  39. table.insert(t,num)
  40. end
  41. local s = string.char(unpack(t))
  42. l = string.len(s)
  43. if l < destLen then
  44. s = s .. string.rep("\255",destLen-l)
  45. elseif l > destLen then
  46. s = string.sub(s,1,destLen)
  47. end
  48. return s
  49. end
  50. --- BCD编码格式字符串 转化为 号码ASCII字符串(仅支持数字)
  51. -- @string num 待转换字符串
  52. -- @return string data,转换后的字符串
  53. -- @usage
  54. local function bcdNumToNum(num)
  55. local byte,v1,v2
  56. local t = {}
  57. for i=1,num:len() do
  58. byte = num:byte(i)
  59. v1,v2 = bit.band(byte,0x0f),bit.band(bit.rshift(byte,4),0x0f)
  60. if v1 == 0x0f then break end
  61. table.insert(t,v1)
  62. if v2 == 0x0f then break end
  63. table.insert(t,v2)
  64. end
  65. return table.concat(t)
  66. end
  67. lbsLoc2.imei = numToBcdNum(mobile.imei())
  68. local function enCellInfo(s)
  69. -- 改造成单基站, 反正服务器也只认单基站
  70. local v = s[1]
  71. log.info("cell", json.encode(v))
  72. local ret = pack.pack(">HHbbi",v.tac,v.mcc,v.mnc,31,v.cid)
  73. return string.char(1)..ret
  74. end
  75. local function trans(str)
  76. local s = str
  77. if str:len()<10 then
  78. s = str..string.rep("0",10-str:len())
  79. end
  80. return s:sub(1,3).."."..s:sub(4,10)
  81. end
  82. --[[
  83. 执行定位请求
  84. @api lbsLoc2.request(timeout, host, port, reqTime)
  85. @number 请求超时时间,单位毫秒,默认15000
  86. @number 服务器地址,有默认值,可以是域名,一般不需要填
  87. @number 服务器端口,默认12411,一般不需要填
  88. @bool 是否要求返回服务器时间
  89. @return string 若成功,返回定位坐标的纬度,否则会返还nil
  90. @return string 若成功,返回定位坐标的经度,否则会返还nil
  91. @return table 服务器时间,东八区时间. 当reqTime为true且定位成功才会返回
  92. @usage
  93. -- 关于坐标系
  94. -- 部分情况下会返回GCJ02坐标系, 部分情况返回的是WGS84坐标
  95. -- 历史数据已经无法分辨具体坐标系
  96. -- 鉴于两种坐标系之间的误差并不大,小于基站定位本身的误差, 纠偏的意义不大
  97. sys.taskInit(function()
  98. sys.waitUntil("IP_READY", 30000)
  99. -- mobile.reqCellInfo(60)
  100. -- sys.wait(1000)
  101. while mobile do -- 没有mobile库就没有基站定位
  102. mobile.reqCellInfo(15)
  103. sys.waitUntil("CELL_INFO_UPDATE", 3000)
  104. local lat, lng, t = lbsLoc2.request(5000)
  105. -- local lat, lng, t = lbsLoc2.request(5000, "bs.openluat.com")
  106. log.info("lbsLoc2", lat, lng, (json.encode(t or {})))
  107. sys.wait(60000)
  108. end
  109. end)
  110. ]]
  111. function lbsLoc2.request(timeout, host, port, reqTime)
  112. if mobile.status() == 0 then
  113. return
  114. end
  115. local hosts = host and {host} or {"free.bs.air32.cn", "bs.openluat.com"}
  116. port = port and tonumber(port) or 12411
  117. local sc = socket.create(nil, function(sc, event)
  118. -- log.info("lbsLoc", "event", event, socket.ON_LINE, socket.TX_OK, socket.EVENT)
  119. if event == socket.ON_LINE then
  120. --log.info("lbsLoc", "已连接")
  121. sys.publish("LBS_CONACK")
  122. elseif event == socket.TX_OK then
  123. --log.info("lbsLoc", "发送完成")
  124. sys.publish("LBS_TX")
  125. elseif event == socket.EVENT then
  126. --log.info("lbsLoc", "有数据来")
  127. sys.publish("LBS_RX")
  128. end
  129. end)
  130. if sc == nil then
  131. return
  132. end
  133. -- socket.debug(sc, true)
  134. socket.config(sc, nil, true)
  135. local rxbuff = zbuff.create(64)
  136. for k, rhost in pairs(hosts) do
  137. local reqStr = string.char(0, (reqTime and 4 or 0) +8) .. lbsLoc2.imei
  138. local tmp = nil
  139. if mobile.scell then
  140. local scell = mobile.scell()
  141. if scell and scell.mcc then
  142. -- log.debug("lbsLoc2", "使用当前驻网基站的信息")
  143. tmp = pack.pack(">bHHbbi", 1, scell.tac, scell.mcc, scell.mnc, 31, scell.eci)
  144. end
  145. end
  146. if tmp == nil then
  147. local cells = mobile.getCellInfo()
  148. if cells == nil or #cells == 0 then
  149. socket.release(sc)
  150. return
  151. end
  152. reqStr = reqStr .. enCellInfo(cells)
  153. else
  154. reqStr = reqStr .. tmp
  155. end
  156. -- log.debug("lbsLoc2", "待发送数据", (reqStr:toHex()))
  157. log.debug("lbsLoc2", rhost, port)
  158. if socket.connect(sc, rhost, port) and sys.waitUntil("LBS_CONACK", 1000) then
  159. if socket.tx(sc, reqStr) and sys.waitUntil("LBS_TX", 1000) then
  160. socket.wait(sc)
  161. if sys.waitUntil("LBS_RX", timeout or 15000) then
  162. local succ, data_len = socket.rx(sc, rxbuff)
  163. -- log.debug("lbsLoc", "rx", succ, data_len)
  164. if succ and data_len > 0 then
  165. socket.close(sc)
  166. break
  167. else
  168. log.debug("lbsLoc", "rx数据失败", rhost)
  169. end
  170. else
  171. log.debug("lbsLoc", "等待数据超时", rhost)
  172. end
  173. else
  174. log.debug("lbsLoc", "tx调用失败或TX_ACK超时", rhost)
  175. end
  176. else
  177. log.debug("lbsLoc", "connect调用失败或CONACK超时", rhost)
  178. end
  179. socket.close(sc)
  180. --sys.wait(100)
  181. end
  182. sys.wait(100)
  183. socket.release(sc)
  184. if rxbuff:used() > 0 then
  185. local resp = rxbuff:toStr(0, rxbuff:used())
  186. log.debug("lbsLoc2", "rx", (resp:toHex()))
  187. if resp:len() >= 11 and(resp:byte(1) == 0 or resp:byte(1) == 0xFF) then
  188. local lat = trans(bcdNumToNum(resp:sub(2, 6)))
  189. local lng = trans(bcdNumToNum(resp:sub(7, 11)))
  190. local t = nil
  191. if resp:len() >= 17 then
  192. t = {
  193. year=resp:byte(12) + 2000,
  194. month=resp:byte(13),
  195. day=resp:byte(14),
  196. hour=resp:byte(15),
  197. min=resp:byte(16),
  198. sec=resp:byte(17),
  199. }
  200. end
  201. return lat, lng, t
  202. end
  203. end
  204. rxbuff:del()
  205. end
  206. return lbsLoc2