home.js 15 KB

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