home.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. //注意,注意,""../"表示退回上一级目录
  2. import { userMobileLogin } from '../../api/wxlogin.js'
  3. import { getMobileBtsParamList } from '../../api/btsparam.js'
  4. import sysconfig from '../../sysconfig/sysconfig.js'
  5. const app = getApp()
  6. Page({
  7. data: {
  8. bestFrame: 0,
  9. bestMultiFrame: 0,
  10. bleDataList: [],//蓝牙设备队列,包括mac和rssi项
  11. btsParamList: [],//基站参数列表
  12. btsDataList: [],//基站数据列表
  13. btsMinCoordX: 0,
  14. btsMaxCoordX: 0,
  15. btsMinCoordY: 0,
  16. btsMaxCoordY: 0,
  17. canvasCtx: null, //画布
  18. canvasWidth: 0, //画布宽度
  19. canvasHeight: 0, //画布高度
  20. canvasPixelRatio: 1,//画布像素比例
  21. coordDataList: [],//定位坐标数据队列(所有定位坐标)
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad(options) {
  27. //创建线程
  28. app.createWorker(this.workerMessageListenerHome)
  29. //创建录音设备
  30. //app.createRecorder()
  31. //用户登录
  32. this.userLogin()
  33. //初始化画布
  34. this.initCanvas()
  35. //启动三轴加速计
  36. app.startImu()
  37. },
  38. /**
  39. * 生命周期函数--监听页面初次渲染完成
  40. */
  41. onReady() {
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow() {
  47. },
  48. /**
  49. * 生命周期函数--监听页面隐藏
  50. */
  51. onHide() {
  52. },
  53. /**
  54. * 生命周期函数--监听页面卸载
  55. */
  56. onUnload() {
  57. //页面关闭时释放wasm模块内存
  58. if (app.data.worker != null) {
  59. app.data.worker.postMessage({
  60. message: 'MAIN_WORKER_FREE_MEMORY',
  61. data: ''
  62. })
  63. }
  64. },
  65. /**
  66. * 页面相关事件处理函数--监听用户下拉动作
  67. */
  68. onPullDownRefresh() {
  69. },
  70. /**
  71. * 页面上拉触底事件的处理函数
  72. */
  73. onReachBottom() {
  74. },
  75. /**
  76. * 用户点击右上角分享
  77. */
  78. onShareAppMessage() {
  79. },
  80. //线程监听服务程序
  81. workerMessageListenerHome(res) {
  82. switch (res.message) {
  83. case 'WORKER_MAIN_START_MONITOR_BLE_BTS':
  84. app.openBleAdapter()//打开蓝牙设备
  85. break
  86. case 'WORKER_MAIN_ORGANIZE_INSTALL_BTS_DATA':
  87. this.organizeBtsDataForWasm()//组织基站数据
  88. break
  89. case 'WORKER_MAIN_START_REC'://开始录音
  90. app.createRecorder()
  91. break
  92. case 'WORKER_MAIN_CURRENT_LOCATION_COORD'://当前定位坐标
  93. this.saveLocateCoord(res.data)
  94. break
  95. case 'WORKER_MAIN_NOTICE_DOWNLOAD_AUDIO_FILE':
  96. this.downloadAudioFile()
  97. break
  98. case 'WORKER_MAIN_NOTICE_READ_AUDIO_FILE':
  99. this.readAudioFile()
  100. break
  101. case 'WORKER_MAIN_SAVE_AUDIO_DATA':
  102. app.saveAduioDataToBin(res.data)
  103. break
  104. default:
  105. break
  106. }
  107. },
  108. //用户登录,登录成功后拉取基站参数
  109. userLogin() {
  110. // 用户登录登录(发送登录凭证[res.code]到后台换取openId、sessionKey和unionId)
  111. wx.login({
  112. success: (res) => {
  113. var paramData = {};
  114. paramData.code = res.code; //登录凭证
  115. paramData.domain_name = sysconfig.domanName; //系统二级域名(用二级域名换取项目编号)
  116. userMobileLogin(paramData).then((resp) => {
  117. //保存token
  118. const tokenKey = sysconfig.user.tokenKey
  119. wx.setStorageSync(tokenKey, resp.data.data.token)
  120. //保存用户数据
  121. const userInfoKey = sysconfig.user.userInfoKey
  122. wx.setStorageSync(userInfoKey, resp.data.data)
  123. //拉取基站参数(注意:需要在用户登录成功后才拉取参数)
  124. this.getBtsParamList()
  125. }).catch((err) => {
  126. console.log("用户登录失败", err)
  127. })
  128. }
  129. })
  130. },
  131. //拉取基站参数(注意:需要在用户登录成功后才拉取参数)
  132. getBtsParamList() {
  133. //拉取基站参数
  134. getMobileBtsParamList().then((resBtsPram) => {
  135. this.data.btsParamList = resBtsPram.data.data
  136. //组织基站数据队列
  137. this.data.btsDataList = this.generateBtsDataList(this.data.btsParamList)
  138. //计算最大最小坐标值
  139. this.calMaxMinCoordXY(this.data.btsDataList)
  140. }).catch((err) => {
  141. console.log("查询基站参数失败", err)
  142. })
  143. },
  144. //初始化画布
  145. initCanvas() {
  146. //获得屏幕数据
  147. let self = this
  148. wx.getSystemInfo({
  149. success: (res) => {
  150. self.data.canvasPixelRatio = res.pixelRatio
  151. let myCanvasWidth = res.windowWidth - 5
  152. self.setData({
  153. canvasWidth: myCanvasWidth,
  154. canvasHeight: myCanvasWidth
  155. })
  156. }
  157. })
  158. //为画布选择一个容器
  159. wx.createSelectorQuery().select('#homeCanvas').fields({ node: true, size: true }).exec((res) => {
  160. let canvas = res[0].node
  161. if (canvas) {
  162. this.data.canvasCtx = canvas.getContext('2d')
  163. canvas.width = res[0].width * this.data.canvasPixelRatio
  164. canvas.height = res[0].height * this.data.canvasPixelRatio
  165. this.data.canvasCtx.scale(this.data.canvasPixelRatio, this.data.canvasPixelRatio)
  166. //画网格
  167. this.drawLocateTrack(0, 0, 0, this.data.coordDataList)
  168. }
  169. })
  170. },
  171. //为wasm模块组织基站数据
  172. organizeBtsDataForWasm() {
  173. let dataList = []//数据队列
  174. let oneItem = {}
  175. //按照wasm模块要求的数据格式组织数据
  176. dataList.push(this.data.btsDataList.length)
  177. for (let i = 0; i < this.data.btsDataList.length; i++) {
  178. oneItem = this.data.btsDataList[i]
  179. dataList.push(oneItem.dwBuildId)
  180. dataList.push(oneItem.dwLayerId)
  181. dataList.push(oneItem.dwBtsId)
  182. dataList.push(oneItem.dwFreqIndex)
  183. dataList.push(oneItem.dwSlot)
  184. dataList.push(oneItem.dwCoordX)
  185. dataList.push(oneItem.dwCoordY)
  186. dataList.push(oneItem.dwCoordZ)
  187. //一维定位数据
  188. dataList.push(oneItem.dwLctType)
  189. dataList.push(oneItem.dwTwoBtsNum)
  190. for (let i = 0; i < oneItem.dwTwoBtsNum; i++) {
  191. dataList.push(oneItem.adwTwoBtsList[i])
  192. }
  193. for (let i = oneItem.dwTwoBtsNum; i < 6; i++) {//补零占位
  194. dataList.push(0)
  195. }
  196. //蓝牙mac
  197. for (let j = 0; j < 12; j++) {
  198. dataList.push(oneItem.adwBluetoothMac[j])
  199. }
  200. //wifi mac
  201. for (let k = 0; k < 12; k++) {
  202. dataList.push(oneItem.adwWifiMac[k])
  203. }
  204. }
  205. //将基站数据发送给worker
  206. if (app.data.worker != null) {
  207. app.data.worker.postMessage({
  208. message: 'MAIN_WORKER_INSTALL_BTS_DATA',
  209. data: dataList//安装基站数据
  210. })
  211. }
  212. },
  213. //生成基站数据队列
  214. generateBtsDataList(btsParamList) {
  215. let dataList = []
  216. ////组织基站数据
  217. let btsNum = btsParamList.length;//基站数量
  218. for (let i = 0; i < btsNum; i++) {
  219. let btsParam = btsParamList[i]
  220. let oneItem = this.generateOneBtsData(btsParam.bts_id, btsParam.carrier_id, btsParam.timeslot, btsParam.coord_x, btsParam.coord_y, btsParam.coord_z, btsParam.ble_name, btsParam.location_type, btsParam.two_bts_num, btsParam.two_bts_list)
  221. dataList.push(oneItem)
  222. }
  223. return dataList
  224. },
  225. //生成一个基站数据
  226. //基站ID,频率ID(0 - 8),时隙ID(0 - 3),X坐标(单位厘米),Y坐标(单位厘米),Z坐标(单位厘米),蓝牙MAC地址,定位类型,1维定位基站数,1维定位基站ID队列
  227. generateOneBtsData(dwBtsId, dwFreqIndex, dwSlot, dwCoordX, dwCoordY, dwCoordZ, macAddressList, dwLctType, dwTwoBtsNum, adwTwoBtsList) {
  228. let installBts = {}
  229. let oneCharHex = ''
  230. let oneCharHexValue = 0
  231. installBts.dwBuildId = 1
  232. installBts.dwLayerId = 1
  233. installBts.dwBtsId = dwBtsId
  234. installBts.dwFreqIndex = dwFreqIndex - 1
  235. installBts.dwSlot = dwSlot - 1
  236. installBts.dwCoordX = dwCoordX
  237. installBts.dwCoordY = dwCoordY
  238. installBts.dwCoordZ = dwCoordZ
  239. //定位类型,1:一维定位,2:二维定位,3:既支持一维定位也支持二维定位
  240. installBts.dwLctType = dwLctType
  241. //一维定位基站数量
  242. installBts.dwTwoBtsNum = dwTwoBtsNum;
  243. //一维定位基站ID队列
  244. installBts.adwTwoBtsList = []
  245. let btsIdList = adwTwoBtsList.split(",")
  246. let btsId = 0
  247. for (let k = 0; k < dwTwoBtsNum; k++) {
  248. btsId = parseInt(btsIdList[k])
  249. installBts.adwTwoBtsList.push(btsId)
  250. }
  251. //蓝牙mac
  252. installBts.adwBluetoothMac = []
  253. for (let i = 0; i < 12; i++) {
  254. oneCharHex = macAddressList[i]
  255. oneCharHexValue = app.hexCharToValue(oneCharHex)
  256. installBts.adwBluetoothMac.push(oneCharHexValue)
  257. }
  258. //wifi mac
  259. installBts.adwWifiMac = []
  260. for (let j = 0; j < 12; j++) {
  261. oneCharHex = macAddressList[j]
  262. oneCharHexValue = app.hexCharToValue(oneCharHex)
  263. installBts.adwWifiMac.push(oneCharHexValue)
  264. }
  265. return installBts
  266. },
  267. //计算基站围成的多边形最大最小坐标
  268. calMaxMinCoordXY(btsDataList) {
  269. let maxTmpX = -10000000
  270. let minTmpX = 10000000
  271. let maxTmpY = -10000000
  272. let minTmpY = 10000000
  273. let xTmp = 0
  274. let yTmp = 0
  275. let btsNum = btsDataList.length
  276. //计算基站围成的多边形坐标最大最小值
  277. for (let i = 0; i < btsNum; i++) {
  278. xTmp = btsDataList[i].dwCoordX
  279. if (xTmp > maxTmpX) {
  280. maxTmpX = xTmp
  281. }
  282. if (xTmp < minTmpX) {
  283. minTmpX = xTmp
  284. }
  285. yTmp = btsDataList[i].dwCoordY
  286. if (yTmp > maxTmpY) {
  287. maxTmpY = yTmp
  288. }
  289. if (yTmp < minTmpY) {
  290. minTmpY = yTmp
  291. }
  292. }
  293. //保存值
  294. this.setData({
  295. btsMinCoordX: minTmpX,
  296. btsMaxCoordX: maxTmpX,
  297. btsMinCoordY: minTmpY,
  298. btsMaxCoordY: maxTmpY
  299. })
  300. //画基站坐标
  301. this.drawLocateTrack(1, 0, 0, this.data.coordDataList)
  302. },
  303. //保存定位坐标
  304. saveLocateCoord(coord) {
  305. //保存定位坐标
  306. this.data.coordDataList.push(coord)
  307. //画定位轨迹
  308. this.drawLocateTrack(2, coord.x, coord.y, this.data.coordDataList)
  309. },
  310. //画定位轨迹,包括网格线、基站坐标、定位轨迹和当前位置
  311. drawLocateTrack(type, coordinateX, coordinateY, coordDataList) {
  312. let i = 0
  313. let pos = 0
  314. let space = 10
  315. let lineCount = 40
  316. let ctxMinX = this.data.btsMinCoordX - 500
  317. let ctxMaxX = this.data.btsMaxCoordX + 500
  318. let ctxMinY = this.data.btsMinCoordY - 500
  319. let ctxMaxY = this.data.btsMaxCoordY + 500
  320. let ctxWidth = ctxMaxX - ctxMinX
  321. let ctxHeight = ctxMaxY - ctxMinY
  322. let radio = this.data.canvasWidth / ctxWidth
  323. let radioHeight = this.data.canvasHeight / ctxHeight
  324. if (radioHeight < radio) {
  325. radio = radioHeight
  326. }
  327. let positionCtx = this.data.canvasCtx
  328. positionCtx.fillStyle = "#0066FF"
  329. positionCtx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
  330. positionCtx.lineWidth = 0.5
  331. //// 画网格
  332. positionCtx.beginPath()
  333. positionCtx.strokeStyle = '#CCCCCC'
  334. //1、画网格横线
  335. space = parseFloat(this.data.canvasWidth) / lineCount
  336. for (i = 0; i < lineCount; i++) {
  337. pos = i * space
  338. positionCtx.moveTo(0, pos)
  339. positionCtx.lineTo(this.data.canvasWidth, pos)
  340. }
  341. //2、画网格竖线
  342. space = parseFloat(this.data.canvasHeight) / lineCount
  343. for (i = 0; i < lineCount; i++) {
  344. pos = i * space
  345. positionCtx.moveTo(pos, 0)
  346. positionCtx.lineTo(pos, this.data.canvasHeight)
  347. }
  348. positionCtx.stroke()
  349. if (type === 0) { //仅画网格
  350. return
  351. }
  352. //// 画基站
  353. positionCtx.beginPath()
  354. positionCtx.fillStyle = "#FF0000"
  355. positionCtx.strokeStyle = '#FF0000'
  356. for (i = 0; i < this.data.btsDataList.length; i++) {
  357. let x = (this.data.btsDataList[i].dwCoordX - ctxMinX) * radio
  358. let y = this.data.canvasHeight - (this.data.btsDataList[i].dwCoordY - ctxMinY) * radio
  359. positionCtx.fillRect(x - 5, y - 5, 6, 6);
  360. }
  361. if (type === 1) {//仅画基站
  362. return
  363. }
  364. //// 画轨迹线
  365. positionCtx.lineWidth = 1.2
  366. positionCtx.beginPath()
  367. positionCtx.strokeStyle = '#0A0A0A'
  368. for (i = 2; i < coordDataList.length; i++) {
  369. let point = coordDataList[i - 1]
  370. let x = (point.x - ctxMinX) * radio
  371. let y = this.data.canvasHeight - (point.y - ctxMinY) * radio
  372. positionCtx.moveTo(x, y)
  373. point = coordDataList[i]
  374. x = (point.x - ctxMinX) * radio
  375. y = this.data.canvasHeight - (point.y - ctxMinY) * radio
  376. positionCtx.lineTo(x, y)
  377. }
  378. positionCtx.stroke()
  379. //// 画当前位置
  380. positionCtx.beginPath()
  381. positionCtx.fillStyle = "#0000FF"
  382. positionCtx.strokeStyle = '#0000FF'
  383. let x = (coordinateX - ctxMinX) * radio
  384. let y = this.data.canvasHeight - (coordinateY - ctxMinY) * radio
  385. positionCtx.arc(x, y, 3, 0, 2 * Math.PI);
  386. positionCtx.fill()
  387. },
  388. //////////////////////////////////////////////////////////////////////////////////
  389. //////////////////////////////////下面是测试接口,已舍弃////////////////////////////
  390. //////////////////////////////////////////////////////////////////////////////////
  391. //生成蓝牙基站数据
  392. generateBleBtsData() {
  393. let mac = "123456789ABC"
  394. let rssi = -58
  395. let item = {}
  396. item['mac'] = mac;
  397. item['rssi'] = rssi
  398. let dataList = []
  399. dataList.push(item)
  400. app.data.worker.postMessage({
  401. message: 'MAIN_WORKER_BLE_BTS_DATA',
  402. code: 100,
  403. data: dataList
  404. })
  405. },
  406. //从服务器上下载文件
  407. downloadAudioFile() {
  408. const url = "https://www.stp.intourism.cn/tdsadmin/line390-zt.wav"
  409. wx.downloadFile({
  410. url: url,
  411. success: (res) => {
  412. if (res.statusCode == 200) {
  413. app.data.wavFilePath = res.tempFilePath
  414. this.readAudioFile();
  415. }
  416. }
  417. })
  418. },
  419. // 读取音频文件
  420. readAudioFile() {
  421. let filePath = app.data.wavFilePath
  422. if (app.data.readFileTimes == null) {
  423. return
  424. }
  425. //90秒数据
  426. if (app.data.readFileTimes >= 360) {
  427. app.data.readFileTimes = 0
  428. return
  429. }
  430. const times = app.data.readFileTimes
  431. const fs = wx.getFileSystemManager();
  432. fs.readFile({
  433. filePath: `${filePath}`,
  434. position: 44 + times * 12000 * 2,
  435. length: 12000 * 2,//12000点,每点2个字节
  436. success: (res) => {
  437. app.data.readFileTimes++
  438. app.data.worker.postMessage({
  439. message: 'MAIN_WORKER_AUDIO_DATA',
  440. data: res.data
  441. })
  442. },
  443. fail(res) {
  444. console.log(res);
  445. }
  446. })
  447. },
  448. })