|
@@ -1,5 +1,6 @@
|
|
|
import mqtt from '../../common/mqtt.js'
|
|
|
import Vue from 'vue'
|
|
|
+import parse from './mqttParse.js'
|
|
|
//import mqtt from 'mqtt/dist/mqtt.js'
|
|
|
|
|
|
const options = {
|
|
@@ -27,7 +28,7 @@ function subscribeDevicesStatus(dispatch,deviceList){
|
|
|
|
|
|
//订阅消息
|
|
|
function subscribeCurrDevice(dispatch,device){
|
|
|
- if(typeof device !== 'undefined'){
|
|
|
+ if(device){
|
|
|
let topic = `/${device.uuid}/user/pub_response`
|
|
|
dispatch('subscribe',topic)
|
|
|
}
|
|
@@ -37,19 +38,33 @@ export default {
|
|
|
namespaced: true,
|
|
|
state: {
|
|
|
//当前选择的设备
|
|
|
- currentDevice: undefined,
|
|
|
+ currentDevice: null,
|
|
|
//用户设备列表
|
|
|
deviceList: [],
|
|
|
//mqtt客户端
|
|
|
- mqttClient: undefined,
|
|
|
+ mqttClient: null,
|
|
|
//收到的消息的历史记录
|
|
|
msgList:[],
|
|
|
+ //设备的播放信息
|
|
|
+ playInfo:{
|
|
|
+ title:"",
|
|
|
+ artist:"",
|
|
|
+ albumURI:"",
|
|
|
+ songInfoID:"",
|
|
|
+ songAlbumID:"",
|
|
|
+ Duration:0,
|
|
|
+ channel:1,
|
|
|
+ audioType:1,
|
|
|
+ PlayState:3,
|
|
|
+ PlayMode:0
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
},
|
|
|
getters: {
|
|
|
//mqtt连接状态
|
|
|
connected: state => {
|
|
|
- return state.mqttClient !== undefined && state.mqttClient.connected
|
|
|
+ return state.mqttClient && state.mqttClient.connected
|
|
|
},
|
|
|
|
|
|
},
|
|
@@ -75,15 +90,19 @@ export default {
|
|
|
},
|
|
|
//token失效,退出登录,需要清除数据
|
|
|
release(state) {
|
|
|
- deviceList = []
|
|
|
- currentDevice = undefined
|
|
|
- state.mqttClient = undefined
|
|
|
+ state.deviceList = []
|
|
|
+ state.currentDevice = null
|
|
|
+ state.mqttClient = null
|
|
|
},
|
|
|
//设置设备的在线状态
|
|
|
updateDeviceState(state,data){
|
|
|
Vue.set(state.deviceList,data.index,data.device)
|
|
|
//state.deviceList[data.index] = {...state.deviceList[data.index], online:data.online}
|
|
|
//state.deviceList[data.index].online = data.online
|
|
|
+ },
|
|
|
+ //更新播放信息
|
|
|
+ updatePlayInfo(state,playinfo){
|
|
|
+ state.playInfo = playinfo
|
|
|
}
|
|
|
|
|
|
},
|
|
@@ -98,7 +117,7 @@ export default {
|
|
|
let findDevice = state.deviceList.find((item) =>{
|
|
|
return item.uuid === device.uuid
|
|
|
})
|
|
|
- if(typeof findDevice === 'undefined'){
|
|
|
+ if(findDevice == null){
|
|
|
if(state.deviceList.length === 0){
|
|
|
//设置当前的设备为添加的设备
|
|
|
commit('setCurrentDevice',device)
|
|
@@ -109,7 +128,7 @@ export default {
|
|
|
if(getters.connected){
|
|
|
//订阅设备是否在线
|
|
|
subscribeDevicesStatus(dispatch,[device])
|
|
|
- if(typeof state.currentDevice !== 'undefined' && state.currentDevice.uuid === device.uuid){
|
|
|
+ if(state.currentDevice && state.currentDevice.uuid === device.uuid){
|
|
|
//订阅当前设备的消息
|
|
|
subscribeCurrDevice(dispatch,state.currentDevice)
|
|
|
}
|
|
@@ -127,7 +146,7 @@ export default {
|
|
|
let findDevice = state.deviceList.find((item) =>{
|
|
|
return item.uuid === device.uuid
|
|
|
})
|
|
|
- if(typeof findDevice !== 'undefined'){
|
|
|
+ if(findDevice){
|
|
|
//取消此设备的订阅
|
|
|
let topic = `/${device.uuid}/status/onoffline`
|
|
|
dispatch('unsubscribe',topic)
|
|
@@ -136,20 +155,20 @@ export default {
|
|
|
//在设备列表中删除此设备
|
|
|
commit('removeDevice',findDevice)
|
|
|
//如果删除的是选中的设备
|
|
|
- if(typeof state.currentDevice !== 'undefined' && state.currentDevice.uuid == device.uuid){
|
|
|
+ if(currentDevice && state.currentDevice.uuid == device.uuid){
|
|
|
//清除当前选中的设备 todo 还是自动选中在线的设备
|
|
|
- commit('setCurrentDevice',undefined)
|
|
|
+ commit('setCurrentDevice',null)
|
|
|
}
|
|
|
|
|
|
- if(state.deviceList.length === 0 ){//所有的设备都已经删除
|
|
|
- dispatch()
|
|
|
+ if(state.deviceList.length === 0 ){//所有的设备都已经删除,关闭连接
|
|
|
+ dispatch('disconnect')
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
//连接设备
|
|
|
connect({ state, dispatch,commit, getters }, clientId) {
|
|
|
- if (typeof state.mqttClient === 'undefined') {
|
|
|
+ if (state.mqttClient == null) {
|
|
|
//连接服务器
|
|
|
commit('connect', clientId)
|
|
|
//连接成功回调
|
|
@@ -182,49 +201,9 @@ export default {
|
|
|
//收到消息的回调
|
|
|
state.mqttClient.on('message', function (topic, message) {
|
|
|
console.log(`message = ${message.toString()}`);
|
|
|
- let arr = topic.split("/");
|
|
|
- if(arr.length >1){
|
|
|
- let fromDeviceUuid = arr[1]
|
|
|
-
|
|
|
- //设备在线和离线的消息
|
|
|
- if(topic.endsWith('onoffline')){
|
|
|
- let index = -1
|
|
|
- for (var i = 0; i < state.deviceList.length; i++) {
|
|
|
- if(state.deviceList[i].uuid === fromDeviceUuid){
|
|
|
- index = i;
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- console.log(`state index = ${index}`);
|
|
|
- if(index >= 0){
|
|
|
- let stateJson = JSON.parse(message)
|
|
|
- //console.log(stateJson);
|
|
|
- let device = state.deviceList[i];
|
|
|
- if(stateJson.state === 'online'){
|
|
|
- device.online = true
|
|
|
- commit('updateDeviceState',{index,device})
|
|
|
- //commit('updateDeviceState',{index,online:true})
|
|
|
- //state.deviceList[index].online = true
|
|
|
- if(typeof state.currentDevice !== 'undefined' && state.currentDevice.uuid == device.uuid){
|
|
|
- dispatch({
|
|
|
- type:'publishWithType',
|
|
|
- mqttType:'get_position',
|
|
|
- })
|
|
|
- }
|
|
|
- }else{
|
|
|
- device.online = false
|
|
|
- commit('updateDeviceState',{index,device})
|
|
|
- //commit('updateDeviceState',{index,online:true})
|
|
|
- //state.deviceList[index].online = false
|
|
|
- }
|
|
|
- uni.$emit('mqtt_onoffline',stateJson)
|
|
|
- // state.deviceList.forEach(element => {
|
|
|
- // console.log(`online = ${element.online}`);
|
|
|
- // });
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ //解析收到的消息
|
|
|
+ parse({state, dispatch,commit, getters },topic,message)
|
|
|
+
|
|
|
//这是测试的
|
|
|
state.msgList.push({
|
|
|
topic: topic.toString(),
|
|
@@ -249,7 +228,7 @@ export default {
|
|
|
//断开mqtt的连接
|
|
|
disconnect({state,commit},isRelease) {
|
|
|
console.log('disconnect')
|
|
|
- if (state.mqttClient !== undefined) {
|
|
|
+ if (state.mqttClient) {
|
|
|
state.mqttClient.end(false,() =>{
|
|
|
console.log(`state.mqttClient.connected = ${state.mqttClient.connected}`);
|
|
|
})
|
|
@@ -269,7 +248,7 @@ export default {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
reject('连接已断开')
|
|
|
});
|
|
|
- }else if(typeof state.currentDevice === undefined || !state.currentDevice.online){
|
|
|
+ }else if(!(state.currentDevice && state.currentDevice.online)){
|
|
|
return new Promise((resolve, reject) => {
|
|
|
reject('设备已经离线')
|
|
|
});
|
|
@@ -282,7 +261,7 @@ export default {
|
|
|
'SrcDeviceName':options.clientId,
|
|
|
'type':type,
|
|
|
}
|
|
|
- if(typeof other !== 'undefined'){
|
|
|
+ if(other){
|
|
|
json.other = other
|
|
|
}
|
|
|
let payload = JSON.stringify(json)
|
|
@@ -302,7 +281,7 @@ export default {
|
|
|
//发布消息
|
|
|
state.mqttClient.publish(message.topic, message.payload, (err) => {
|
|
|
if (err) {
|
|
|
- console.log(err);
|
|
|
+ console.warn(err);
|
|
|
reject(err)
|
|
|
} else {
|
|
|
resolve()
|