123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- const app = getApp()
- // pages/home/home.js
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- bestFrame: 0,
- bestMultiFrame: 0,
- bleDataList: [],//蓝牙设备队列,包括mac和rssi项
- installBtsList: [],//安装基站列表
- btsMinCoordX: 0,
- btsMaxCoordX: 0,
- btsMinCoordY: 0,
- btsMaxCoordY: 0,
- canvasCtx: null, //画布
- canvasWidth: 0, //画布宽度
- canvasHeight: 0, //画布高度
- canvasPixelRatio: 1,//画布像素比例
- coordDataList: [],//定位坐标数据队列
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- //创建线程
- app.createWorker(this.workerMessageListenerHome)
- app.createRecorder()//创建录音设备
- //TODO,测试用接口,后期改成从服务器上读取安装基站数据
- this.generateMultiInstallBts() //生成定位基站
- this.initCanvas()//初始化画布
- //启动三轴加速计
- app.startImu()
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- if (app.data.worker != null) {
- app.data.worker.postMessage({
- message: 'MAIN_WORKER_FREE_MEMORY',
- data: ''
- })
- }
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- },
- //线程监听服务程序
- workerMessageListenerHome(res) {
- switch (res.message) {
- case 'WORKER_MAIN_START_MONITOR_BLE_BTS':
- app.openBleAdapter()//打开蓝牙设备
- break
- case 'WORKER_MAIN_ORGANIZE_INSTALL_BTS_DATA':
- this.organizeInstallBtsData()//组织安装基站
- break
- case 'WORKER_MAIN_START_REC'://开始录音
- app.createRecorder()
- break
- case 'WORKER_MAIN_CURRENT_LOCATION_COORD'://当前定位坐标
- this.drawLocationTrace(res.data)
- break
- case 'WORKER_MAIN_NOTICE_DOWNLOAD_AUDIO_FILE':
- this.downloadAudioFile()
- break
- case 'WORKER_MAIN_NOTICE_READ_AUDIO_FILE':
- this.readAudioFile()
- break
- case 'WORKER_MAIN_SAVE_AUDIO_DATA':
- app.saveAduioDataToBin(res.data)
- break
- default:
- break
- }
- },
- //初始化画布
- initCanvas() {
- //获得屏幕数据
- let self = this
- wx.getSystemInfo({
- success: (res) => {
- self.data.canvasPixelRatio = res.pixelRatio
- let myCanvasWidth = res.windowWidth - 5
- self.setData({
- canvasWidth: myCanvasWidth,
- canvasHeight: myCanvasWidth
- })
- }
- })
- //为画布选择一个容器
- wx.createSelectorQuery().select('#homeCanvas').fields({ node: true, size: true }).exec((res) => {
- let canvas = res[0].node
- if (canvas) {
- this.data.canvasCtx = canvas.getContext('2d')
- canvas.width = res[0].width * this.data.canvasPixelRatio
- canvas.height = res[0].height * this.data.canvasPixelRatio
- this.data.canvasCtx.scale(this.data.canvasPixelRatio, this.data.canvasPixelRatio)
- this.drawPosition(0, 0, 0, 0)
- }
- })
- },
- //组织安装基站数据
- organizeInstallBtsData() {
- let dataList = []//数据队列
- let oneItem = {}
- //按照wasm模块要求的数据格式组织数据
- dataList.push(this.data.installBtsList.length)
- for (let i = 0; i < this.data.installBtsList.length; i++) {
- oneItem = this.data.installBtsList[i]
- dataList.push(oneItem.dwBuildId)
- dataList.push(oneItem.dwLayerId)
- dataList.push(oneItem.dwBtsId)
- dataList.push(oneItem.dwFreqIndex)
- dataList.push(oneItem.dwSlot)
- dataList.push(oneItem.dwCoordX)
- dataList.push(oneItem.dwCoordY)
- dataList.push(oneItem.dwCoordZ)
- //一维定位数据
- dataList.push(oneItem.dwLctType)
- dataList.push(oneItem.dwTwoBtsNum)
- for (let i = 0; i < oneItem.dwTwoBtsNum; i++) {
- dataList.push(oneItem.adwTwoBtsList[i])
- }
- for (let i = oneItem.dwTwoBtsNum; i < 6; i++) {
- dataList.push(0)
- }
- //蓝牙mac
- for (let j = 0; j < 12; j++) {
- dataList.push(oneItem.adwBluetoothMac[j])
- }
- //wifi mac
- for (let k = 0; k < 12; k++) {
- dataList.push(oneItem.adwWifiMac[k])
- }
- }
- //将基站数据发送给worker
- if (app.data.worker != null) {
- app.data.worker.postMessage({
- message: 'MAIN_WORKER_INSTALL_BTS_DATA',
- data: dataList//安装基站数据
- })
- }
- },
- //生成多个安装基站
- generateMultiInstallBts() {
- let oneItem = {}
- let maxTmpX = -10000000
- let minTmpX = 10000000
- let maxTmpY = -10000000
- let minTmpY = 10000000
- let xTmp = 0
- let yTmp = 0
- let btsNum = 5;//基站数量
- //清空基站队列
- this.data.installBtsList = []
- // a81710d94e04 1号基站
- // a81710d9a1c8 2号基站
- // a81710d8f9be 3号基站
- // a81710d91a5e 4号基站
- // 1号基站,基站ID,频率ID(0 - 8),时隙ID(0 - 3),X坐标(单位厘米),Y坐标(单位厘米),MAC地址,定位类型,1维定位基站数,1维定位基站ID队列
- oneItem = this.generateOneInstallBts(10001, 0, 0, 0, 0, "685377178192", 2, 0, "")
- this.data.installBtsList.push(oneItem)
- //2号基站
- oneItem = this.generateOneInstallBts(10002, 1, 1, 2500, 0, "685377183652", 3, 1, "10005")
- this.data.installBtsList.push(oneItem)
- //3号基站
- oneItem = this.generateOneInstallBts(10003, 2, 2, 2500, 800, "6853771679b0", 2, 0, "")
- this.data.installBtsList.push(oneItem)
- //4号基站
- oneItem = this.generateOneInstallBts(10004, 3, 3, 0, 800, "68537716c686", 2, 0, "")
- this.data.installBtsList.push(oneItem)
- //5号基站
- oneItem = this.generateOneInstallBts(10005, 4, 0, 5000, 0, "685377183238", 1, 1, "10002")
- this.data.installBtsList.push(oneItem)
- // //6号基站
- // oneItem = this.generateOneInstallBts(10006, 5, 0, 5500, 0, "685377174ea4", 1, 1, "10005")
- // this.data.installBtsList.push(oneItem)
- // //7号基站
- // oneItem = this.generateOneInstallBts(2097114, 6, 0, 500, 180, "a81710d94198", 2, 1, "0")
- // this.data.installBtsList.push(oneItem)
- // // //8号基站
- // // oneItem = this.generateOneInstallBts(2097110, 7, 0, 800, 1800, "a81710d951dc")
- // // this.data.installBtsList.push(oneItem)
- for (let i = 0; i < btsNum; i++) {
- xTmp = this.data.installBtsList[i].dwCoordX
- if (xTmp > maxTmpX) {
- maxTmpX = xTmp
- }
- if (xTmp < minTmpX) {
- minTmpX = xTmp
- }
- yTmp = this.data.installBtsList[i].dwCoordY
- if (yTmp > maxTmpY) {
- maxTmpY = yTmp
- }
- if (yTmp < minTmpY) {
- minTmpY = yTmp
- }
- }
- //保存值
- this.setData({
- btsMinCoordX: minTmpX,
- btsMaxCoordX: maxTmpX,
- btsMinCoordY: minTmpY,
- btsMaxCoordY: maxTmpY
- })
- },
- //生成一个安装基站数据
- generateOneInstallBts(dwBtsId, dwFreqIndex, dwSlot, dwCoordX, dwCoordY, macAddressList, dwLctType, dwTwoBtsNum, adwTwoBtsList) {
- let installBts = {}
- let oneCharHex = ''
- let oneCharHexValue = 0
- installBts.dwBuildId = 1
- installBts.dwLayerId = 1
- installBts.dwBtsId = dwBtsId
- installBts.dwFreqIndex = dwFreqIndex
- installBts.dwSlot = dwSlot
- installBts.dwCoordX = dwCoordX
- installBts.dwCoordY = dwCoordY
- installBts.dwCoordZ = 270
- //定位类型,1:一维定位,2:二维定位,3:既支持一维定位也支持二维定位
- installBts.dwLctType = dwLctType
- //一维定位基站数量
- installBts.dwTwoBtsNum = dwTwoBtsNum;
- //一维定位基站ID队列
- installBts.adwTwoBtsList = []
- let btsIdList = adwTwoBtsList.split(",")
- let btsId = 0
- // if (btsIdList.length != dwTwoBtsNum) {
- // console.log("长度错误")
- // return;
- // }
- for (let k = 0; k < dwTwoBtsNum; k++) {
- btsId = parseInt(btsIdList[k])
- console.log("基站ID:", btsId)
- installBts.adwTwoBtsList.push(btsId)
- }
- //蓝牙mac
- installBts.adwBluetoothMac = []
- for (let i = 0; i < 12; i++) {
- oneCharHex = macAddressList[i]
- oneCharHexValue = app.hexCharToValue(oneCharHex)
- installBts.adwBluetoothMac.push(oneCharHexValue)
- }
- //wifi mac
- installBts.adwWifiMac = []
- for (let j = 0; j < 12; j++) {
- oneCharHex = macAddressList[j]
- oneCharHexValue = app.hexCharToValue(oneCharHex)
- installBts.adwWifiMac.push(oneCharHexValue)
- }
- console.log(installBts)
- return installBts
- },
- //TODO,生成蓝牙基站数据
- generateBleBtsData() {
- let mac = "123456789ABC"
- let rssi = -58
- let item = {}
- item['mac'] = mac;
- item['rssi'] = rssi
- let dataList = []
- dataList.push(item)
- app.data.worker.postMessage({
- message: 'MAIN_WORKER_BLE_BTS_DATA',
- code: 100,
- data: dataList
- })
- },
- //从服务器上下载文件
- downloadAudioFile() {
- const url = "https://www.stp.intourism.cn/tdsadmin/line390-zt.wav"
- wx.downloadFile({
- url: url,
- success: (res) => {
- if (res.statusCode == 200) {
- app.data.wavFilePath = res.tempFilePath
- this.readAudioFile();
- }
- }
- })
- },
- // 读取音频文件
- readAudioFile() {
- let filePath = app.data.wavFilePath
- if (app.data.readFileTimes == null) {
- return
- }
- //90秒数据
- if (app.data.readFileTimes >= 360) {
- app.data.readFileTimes = 0
- return
- }
- const times = app.data.readFileTimes
- const fs = wx.getFileSystemManager();
- fs.readFile({
- filePath: `${filePath}`,
- position: 44 + times * 12000 * 2,
- length: 12000 * 2,//12000点,每点2个字节
- success: (res) => {
- app.data.readFileTimes++
- app.data.worker.postMessage({
- message: 'MAIN_WORKER_AUDIO_DATA',
- data: res.data
- })
- },
- fail(res) {
- console.log(res);
- }
- })
- },
- drawPosition(type, coordinateX, coordinateY, coordDataList) {
- let i = 0
- let pos = 0
- let space = 10
- let lineCount = 40
- let ctxMinX = this.data.btsMinCoordX - 500
- let ctxMaxX = this.data.btsMaxCoordX + 500
- let ctxMinY = this.data.btsMinCoordY - 500
- let ctxMaxY = this.data.btsMaxCoordY + 500
- let ctxWidth = ctxMaxX - ctxMinX
- let ctxHeight = ctxMaxY - ctxMinY
- let radio = this.data.canvasWidth / ctxWidth
- let radioHeight = this.data.canvasHeight / ctxHeight
- if (radioHeight < radio) {
- radio = radioHeight
- }
- let positionCtx = this.data.canvasCtx
- positionCtx.fillStyle = "#0066FF"
- positionCtx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
- positionCtx.lineWidth = 0.5
- //// 画网格
- positionCtx.beginPath()
- positionCtx.strokeStyle = '#CCCCCC'
- //1、画网格横线
- space = parseFloat(this.data.canvasWidth) / lineCount
- for (i = 0; i < lineCount; i++) {
- pos = i * space
- positionCtx.moveTo(0, pos)
- positionCtx.lineTo(this.data.canvasWidth, pos)
- }
- //2、画网格竖线
- space = parseFloat(this.data.canvasHeight) / lineCount
- for (i = 0; i < lineCount; i++) {
- pos = i * space
- positionCtx.moveTo(pos, 0)
- positionCtx.lineTo(pos, this.data.canvasHeight)
- }
- positionCtx.stroke()
- if (type === 0) { //仅画网格
- return
- }
- // 画基站
- positionCtx.beginPath()
- positionCtx.fillStyle = "#FF0000"
- positionCtx.strokeStyle = '#FF0000'
- for (i = 0; i < this.data.installBtsList.length; i++) {
- let x = (this.data.installBtsList[i].dwCoordX - ctxMinX) * radio
- let y = this.data.canvasHeight - (this.data.installBtsList[i].dwCoordY - ctxMinY) * radio
- positionCtx.fillRect(x - 5, y - 5, 6, 6);
- // if (masterFreqFrequency === app.store.signal.ubsList[i].device_frequency) {
- // positionCtx.fillRect(x - 8, y - 8, 16, 16);
- // } else {
- // positionCtx.fillRect(x - 5, y - 5, 10, 10);
- // }
- }
- // 画轨迹线
- positionCtx.lineWidth = 1.2
- positionCtx.beginPath()
- positionCtx.strokeStyle = '#0A0A0A'
- for (i = 2; i < coordDataList.length; i++) {
- let point = coordDataList[i - 1]
- let x = (point.x - ctxMinX) * radio
- let y = this.data.canvasHeight - (point.y - ctxMinY) * radio
- positionCtx.moveTo(x, y)
- point = coordDataList[i]
- x = (point.x - ctxMinX) * radio
- y = this.data.canvasHeight - (point.y - ctxMinY) * radio
- positionCtx.lineTo(x, y)
- }
- positionCtx.stroke()
- // 画当前位置
- positionCtx.beginPath()
- positionCtx.fillStyle = "#0000FF"
- positionCtx.strokeStyle = '#0000FF'
- let x = (coordinateX - ctxMinX) * radio
- let y = this.data.canvasHeight - (coordinateY - ctxMinY) * radio
- positionCtx.arc(x, y, 3, 0, 2 * Math.PI);
- positionCtx.fill()
- },
- drawLocationTrace(coord) {
- let x = coord.x
- let y = coord.y
- //保存定位坐标
- this.data.coordDataList.push(coord)
- this.drawPosition(1, x, y, this.data.coordDataList)
- }
- })
|