home.js 17 KB

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