|
@@ -1,6 +1,6 @@
|
|
|
import mqtt from '../../common/mqtt.js'
|
|
|
import Vue from 'vue'
|
|
|
-import parse from './mqttParse.js'
|
|
|
+import {messageParse,indexOfUuid} from './mqttParse.js'
|
|
|
//import mqtt from 'mqtt/dist/mqtt.js'
|
|
|
|
|
|
const options = {
|
|
@@ -19,20 +19,20 @@ url = 'wxs://mqtt.test.radio1964.com'
|
|
|
|
|
|
|
|
|
//订阅设备在线状态
|
|
|
-function subscribeDevicesStatus(dispatch,deviceList){
|
|
|
- deviceList.forEach((value) =>{
|
|
|
+function subscribeDevicesStatus(dispatch, deviceList) {
|
|
|
+ deviceList.forEach((value) => {
|
|
|
let topic = `/${value.uuid}/status/onoffline`
|
|
|
- dispatch('subscribe',topic)
|
|
|
+ dispatch('subscribe', topic)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
//订阅消息
|
|
|
-function subscribeCurrDevice(dispatch,device){
|
|
|
- if(device){
|
|
|
- let topic = `/${device.uuid}/user/pub_response`
|
|
|
- dispatch('subscribe',topic)
|
|
|
- }
|
|
|
-}
|
|
|
+// function subscribeCurrDevice(dispatch, device) {
|
|
|
+// if (device) {
|
|
|
+// let topic = `/${device.uuid}/user/pub_response`
|
|
|
+// dispatch('subscribe', topic)
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
export default {
|
|
|
namespaced: true,
|
|
@@ -44,19 +44,19 @@ export default {
|
|
|
//mqtt客户端
|
|
|
mqttClient: null,
|
|
|
//收到的消息的历史记录
|
|
|
- msgList:[],
|
|
|
+ msgList: [],
|
|
|
//设备的播放信息
|
|
|
- playInfo:{
|
|
|
- title:"",
|
|
|
- artist:"",
|
|
|
- albumURI:"",
|
|
|
- songInfoID:"",
|
|
|
- songAlbumID:"",
|
|
|
- Duration:0,
|
|
|
- channel:1,
|
|
|
- audioType:1,
|
|
|
- PlayState:3,
|
|
|
- PlayMode:0
|
|
|
+ playInfo: {
|
|
|
+ title: "",
|
|
|
+ artist: "",
|
|
|
+ albumURI: "",
|
|
|
+ songInfoID: "",
|
|
|
+ songAlbumID: "",
|
|
|
+ Duration: 0,
|
|
|
+ channel: 1,
|
|
|
+ audioType: 1,
|
|
|
+ PlayState: 3,
|
|
|
+ PlayMode: 0
|
|
|
|
|
|
}
|
|
|
|
|
@@ -76,7 +76,7 @@ export default {
|
|
|
//设置当前选择的设备
|
|
|
setCurrentDevice(state, device) {
|
|
|
//state.currentDevice = device
|
|
|
- Vue.set(state,'currentDevice',device)
|
|
|
+ Vue.set(state, 'currentDevice', device)
|
|
|
},
|
|
|
//移除设备
|
|
|
removeDevice(state, device) {
|
|
@@ -96,77 +96,77 @@ export default {
|
|
|
state.mqttClient = null
|
|
|
},
|
|
|
//更新设备信息
|
|
|
- updateDevice(state,data){
|
|
|
- Vue.set(state.deviceList,data.index,data.device)
|
|
|
+ updateDevice(state, data) {
|
|
|
+ Vue.set(state.deviceList, data.index, data.device)
|
|
|
},
|
|
|
//更新播放信息
|
|
|
- updatePlayInfo(state,playinfo){
|
|
|
+ updatePlayInfo(state, playinfo) {
|
|
|
state.playInfo = playinfo
|
|
|
}
|
|
|
|
|
|
},
|
|
|
actions: {
|
|
|
//添加设备
|
|
|
- addDevice({state, dispatch,commit, getters },data){
|
|
|
+ addDevice({ state, dispatch, commit, getters }, data) {
|
|
|
let clientId = (data.clientId) ? data.clientId : options.clientId
|
|
|
let device = data.device
|
|
|
|
|
|
console.log(`deviceList = ${state.deviceList} device = ${device}`);
|
|
|
//判断设备是否存在deviceList中
|
|
|
- let findDevice = state.deviceList.find((item) =>{
|
|
|
+ let findDevice = state.deviceList.find((item) => {
|
|
|
return item.uuid === device.uuid
|
|
|
})
|
|
|
- if(findDevice == null){
|
|
|
- if(state.deviceList.length === 0){
|
|
|
+ if (findDevice == null) {
|
|
|
+ if (state.deviceList.length === 0) {
|
|
|
//设置当前的设备为添加的设备
|
|
|
- commit('setCurrentDevice',device)
|
|
|
+ commit('setCurrentDevice', device)
|
|
|
}
|
|
|
//添加设备
|
|
|
state.deviceList.push(device)
|
|
|
//如果已经连接
|
|
|
- if(getters.connected){
|
|
|
+ if (getters.connected) {
|
|
|
//订阅设备是否在线
|
|
|
- subscribeDevicesStatus(dispatch,[device])
|
|
|
- if(state.currentDevice && state.currentDevice.uuid === device.uuid){
|
|
|
+ subscribeDevicesStatus(dispatch, [device])
|
|
|
+ //if (state.currentDevice && state.currentDevice.uuid === device.uuid) {
|
|
|
//订阅当前设备的消息
|
|
|
- subscribeCurrDevice(dispatch,state.currentDevice)
|
|
|
- }
|
|
|
+ //subscribeCurrDevice(dispatch, state.currentDevice)
|
|
|
+ //}
|
|
|
|
|
|
- }else{//如果没有连接
|
|
|
+ } else {//如果没有连接
|
|
|
//连接服务器
|
|
|
- dispatch('connect',clientId)
|
|
|
+ dispatch('connect', clientId)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
},
|
|
|
//删除设备
|
|
|
- removeDevice({state, dispatch,commit, getters },device){
|
|
|
+ removeDevice({ state, dispatch, commit, getters }, device) {
|
|
|
//判断设备是否存在deviceList中
|
|
|
- let findDevice = state.deviceList.find((item) =>{
|
|
|
+ let findDevice = state.deviceList.find((item) => {
|
|
|
return item.uuid === device.uuid
|
|
|
})
|
|
|
- if(findDevice){
|
|
|
+ if (findDevice) {
|
|
|
//取消此设备的订阅
|
|
|
let topic = `/${device.uuid}/status/onoffline`
|
|
|
- dispatch('unsubscribe',topic)
|
|
|
+ dispatch('unsubscribe', topic)
|
|
|
topic = `/${device.uuid}/status/pub_response`
|
|
|
- dispatch('unsubscribe',topic)
|
|
|
+ dispatch('unsubscribe', topic)
|
|
|
//在设备列表中删除此设备
|
|
|
- commit('removeDevice',findDevice)
|
|
|
+ commit('removeDevice', findDevice)
|
|
|
//如果删除的是选中的设备
|
|
|
- if(currentDevice && state.currentDevice.uuid == device.uuid){
|
|
|
+ if (currentDevice && state.currentDevice.uuid == device.uuid) {
|
|
|
//清除当前选中的设备 todo 还是自动选中在线的设备
|
|
|
- commit('setCurrentDevice',null)
|
|
|
+ commit('setCurrentDevice', null)
|
|
|
}
|
|
|
-
|
|
|
- if(state.deviceList.length === 0 ){//所有的设备都已经删除,关闭连接
|
|
|
+
|
|
|
+ if (state.deviceList.length === 0) {//所有的设备都已经删除,关闭连接
|
|
|
dispatch('disconnect')
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
//连接设备
|
|
|
- connect({ state, dispatch,commit, getters }, clientId) {
|
|
|
+ connect({ state, dispatch, commit, getters }, clientId) {
|
|
|
if (state.mqttClient == null) {
|
|
|
//连接服务器
|
|
|
commit('connect', clientId)
|
|
@@ -174,9 +174,9 @@ export default {
|
|
|
state.mqttClient.on("connect", function () {
|
|
|
console.log("connect success!");
|
|
|
//订阅设备是否在线
|
|
|
- subscribeDevicesStatus(dispatch,state.deviceList)
|
|
|
+ subscribeDevicesStatus(dispatch, state.deviceList)
|
|
|
//订阅当前设备的消息
|
|
|
- subscribeCurrDevice(dispatch,state.currentDevice)
|
|
|
+ //subscribeCurrDevice(dispatch, state.currentDevice)
|
|
|
});
|
|
|
|
|
|
//异常回调
|
|
@@ -201,71 +201,110 @@ export default {
|
|
|
state.mqttClient.on('message', function (topic, message) {
|
|
|
console.log(`message = ${message.toString()}`);
|
|
|
//解析收到的消息
|
|
|
- parse({state, dispatch,commit, getters },topic,message)
|
|
|
-
|
|
|
+ messageParse({ state, dispatch, commit, getters }, topic, message)
|
|
|
+
|
|
|
//这是测试的
|
|
|
state.msgList.push({
|
|
|
topic: topic.toString(),
|
|
|
msg: message.toString(),
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
|
|
|
|
|
|
})
|
|
|
- }else{
|
|
|
- if(!state.mqttClient.connected){
|
|
|
+ } else {
|
|
|
+ if (!state.mqttClient.connected) {
|
|
|
console.log("reconnect");
|
|
|
state.mqttClient.reconnect()
|
|
|
//重连成功的回调
|
|
|
- state.mqttClient.on("reconnect", function() {
|
|
|
+ state.mqttClient.on("reconnect", function () {
|
|
|
console.log("reconnect success!");
|
|
|
-
|
|
|
+
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
//断开mqtt的连接
|
|
|
- disconnect({state,commit},isRelease) {
|
|
|
+ disconnect({ state, commit }, isRelease) {
|
|
|
console.log('disconnect')
|
|
|
if (state.mqttClient) {
|
|
|
- state.mqttClient.end(false,() =>{
|
|
|
+ state.mqttClient.end(false, () => {
|
|
|
console.log(`state.mqttClient.connected = ${state.mqttClient.connected}`);
|
|
|
})
|
|
|
}
|
|
|
- if(isRelease){
|
|
|
+ if (isRelease) {
|
|
|
//清除数据
|
|
|
commit('release')
|
|
|
}
|
|
|
},
|
|
|
//发布消息
|
|
|
- publishWithType({state, dispatch,getters},payload){
|
|
|
+ publishMsg({ state, dispatch, getters }, msg) {
|
|
|
+ let uuid = msg.uuid
|
|
|
+ let type = msg.mqttType;
|
|
|
+ let other = msg.other;
|
|
|
+ if (!getters.connected) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ reject('连接已断开')
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ let index = indexOfUuid(state.deviceList,uuid)
|
|
|
+ if (index >= 0) {
|
|
|
+ let device = state.deviceList[index];
|
|
|
+ if(device.online){
|
|
|
+ let topic = `/${uuid}/user/sub_control`;
|
|
|
+ let json = {
|
|
|
+ 'DstDeviceName': uuid,
|
|
|
+ 'SrcDeviceName': options.clientId,
|
|
|
+ 'type': type,
|
|
|
+ }
|
|
|
+ if (other) {
|
|
|
+ json.other = other
|
|
|
+ }
|
|
|
+ let payload = JSON.stringify(json)
|
|
|
+ console.log(`publishWithType topic = ${topic} payload = ${payload}`);
|
|
|
+ return dispatch('publish', { topic, payload })
|
|
|
+ }else{
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ reject('设备已经离线')
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ reject('没有此设备')
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ //发布消息
|
|
|
+ publishWithType({ state, dispatch, getters }, payload) {
|
|
|
//console.log(payload);
|
|
|
//console.log(options);
|
|
|
let type = payload.mqttType;
|
|
|
let other = payload.other;
|
|
|
- if(!getters.connected){
|
|
|
+ if (!getters.connected) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
reject('连接已断开')
|
|
|
});
|
|
|
- }else if(!(state.currentDevice && state.currentDevice.online)){
|
|
|
+ } else if (!(state.currentDevice && state.currentDevice.online)) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
reject('设备已经离线')
|
|
|
});
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
let topic = `/${state.currentDevice.uuid}/user/sub_control`;
|
|
|
//console.log(payload);
|
|
|
//console.log(payload.mqttType);
|
|
|
let json = {
|
|
|
- 'DstDeviceName':state.currentDevice.uuid,
|
|
|
- 'SrcDeviceName':options.clientId,
|
|
|
- 'type':type,
|
|
|
+ 'DstDeviceName': state.currentDevice.uuid,
|
|
|
+ 'SrcDeviceName': options.clientId,
|
|
|
+ 'type': type,
|
|
|
}
|
|
|
- if(other){
|
|
|
+ if (other) {
|
|
|
json.other = other
|
|
|
}
|
|
|
let payload = JSON.stringify(json)
|
|
|
console.log(`publishWithType topic = ${topic} payload = ${payload}`);
|
|
|
- return dispatch('publish',{topic,payload})
|
|
|
+ return dispatch('publish', { topic, payload })
|
|
|
|
|
|
}
|
|
|
|
|
@@ -311,7 +350,7 @@ export default {
|
|
|
})
|
|
|
},
|
|
|
//解除订阅消息
|
|
|
- unsubscribe({ state, commit, getters }, topic){
|
|
|
+ unsubscribe({ state, commit, getters }, topic) {
|
|
|
console.log(`unsubscribe topic = ${topic}`);
|
|
|
return new Promise((resolve, reject) => {
|
|
|
if (getters.connected) {
|