diff --git a/src/store/index.js b/src/store/index.js index b5c4dc1..e20ed9a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -303,6 +303,8 @@ const store = new Vuex.Store({ const res = await api.get('getAirList') res.data.airList.forEach(plane => { plane.planeState = { // 飞机状态初始化字段 + flyDataMark: false, // 飞机解锁标记成真 + flyDataSave: [], // 飞机加锁截至 待上传飞行数据之后 再清空此值 heartBeat: null, // 心跳 heartRandom: null, // 每次接收到心跳创建一个随机数 用于watch监听 voltagBattery: null, // 电压信息 diff --git a/src/utils/api/table.js b/src/utils/api/table.js index 683dd52..13d78b7 100644 --- a/src/utils/api/table.js +++ b/src/utils/api/table.js @@ -178,3 +178,9 @@ export async function getFlyData (idArr, startTime, endTime) { const res = await api.post('getFlyDataByIdArr', params, 'Plane') return res } +/** + *@abstract 上传保存飞机 飞行数据 检测心跳 解锁开始记录数据 加锁上传数据 + */ +export async function saveFlyData () { + +} diff --git a/src/utils/mqtt/index.js b/src/utils/mqtt/index.js index 59580fe..c67b83a 100644 --- a/src/utils/mqtt/index.js +++ b/src/utils/mqtt/index.js @@ -23,21 +23,19 @@ export default { * @Description: mqtt对象初始化 */ function mqttConf () { - if (!this.client) { - // 链接mqtt + if (!this.client || (this.client && !this.client.connected)) { + // 如果 client 不存在,或存在但未连接,重新连接 this.client = mqtt.connect(this.url, this.options) - // mqtt服务器中断 + this.client.on('error', () => { store.dispatch('fetchLog', { content: '指令服务器异常中断' }) }) - // 端口之后复连 this.client.on('reconnect', () => { store.dispatch('fetchLog', { content: '重新连接指令服务器' }) }) - // 监听断开连接事件 this.client.on('offline', () => { store.dispatch('fetchLog', { content: '指令服务器异常中断' }) - store.state.app.mqttState = false // 标记mqtt链接失败 掉线 + store.state.app.mqttState = false }) } } diff --git a/src/views/layout/index.vue b/src/views/layout/index.vue index eeadc84..56cc9d5 100644 --- a/src/views/layout/index.vue +++ b/src/views/layout/index.vue @@ -55,6 +55,8 @@ export default { } }, mounted () { + /* mqtt */ + mqtt.mqttConf()// 连接mqtt }, created () { /* init */ @@ -77,9 +79,6 @@ export default { * @param {*} res 飞机列表 */ airList (res) { - /* mqtt */ - mqtt.mqttDestroy()// 先断开mqtt - mqtt.mqttConf()// 连接mqtt // 订阅飞机信息 mqtt.doSubscribe('planeState/+', (mqttRes) => { res.forEach(plane => { @@ -91,8 +90,29 @@ export default { // 更新mqtt信息 选择性更新状态 for (const key in jsonData) { if (key === 'heartBeat') { - plane.planeState.heartRandom = Math.random()// 每次接收到心跳 heartRandom属性 创建一个随机数 用于watch监听 - plane.planeState.heartBeat = jsonData.heartBeat // 按订阅信息 刷新飞机状态 + const oldMark = plane.planeState.flyDataMark + const newMark = (Number(jsonData.heartBeat) & 128) !== 0 + + // 每次接收到心跳时,更新随机数以触发 watch 监听 + plane.planeState.heartRandom = Math.random() + plane.planeState.heartBeat = jsonData.heartBeat + + // 解锁状态变更检测:用于控制飞行数据的采集与上传 + if (!oldMark && newMark) { + // 从加锁切换为解锁:开始记录飞行轨迹 + plane.planeState.flyDataMark = true + } else if (oldMark && !newMark) { + // 从解锁切换为加锁:停止记录并上传飞行轨迹 + plane.planeState.flyDataMark = false + + const uploadData = [...plane.planeState.flyDataSave] + console.log(`上传飞机 ${plane.macadd} 的飞行数据:`, uploadData) + // TODO: 后续实现:上传飞行数据接口 + // await api.post('uploadFlightPath', { macadd: plane.macadd, data: uploadData }) + + // 上传完成后清空记录缓存 + plane.planeState.flyDataSave = [] + } } else if (key === 'position') { // 如果是飞机位置信息 则不是直接刷新状态 而是累计 到数组 以便于画出飞机路径 const position = JSON.parse(jsonData.position) @@ -105,6 +125,10 @@ export default { if (plane.planeState.position.length > 1000) { plane.planeState.position.shift() // 删除最早的经纬度 } + // 如果是解锁状态,记录轨迹用于上传 + if (plane.planeState.flyDataMark) { + plane.planeState.flyDataSave.push([position.lon / 10e6, position.lat / 10e6, Number(position.alt)]) + } } else if (key === 'homePosition') { const homePosition = JSON.parse(jsonData.homePosition)// home点反序列化再赋值 homePosition.lon = homePosition.lon / 10e6