|
@@ -1,11 +1,11 @@
|
|
|
-function isEmpty(str){
|
|
|
- if(str){
|
|
|
+function isEmpty(str) {
|
|
|
+ if (str) {
|
|
|
return true
|
|
|
}
|
|
|
- if(!str.length){
|
|
|
+ if (!str.length) {
|
|
|
return true
|
|
|
}
|
|
|
- if(str == 'unknow'){
|
|
|
+ if (str == 'unknow') {
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
@@ -18,46 +18,46 @@ function isEmpty(str){
|
|
|
* @param {*} topic
|
|
|
* @param {*} message
|
|
|
*/
|
|
|
-function receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message){
|
|
|
-
|
|
|
+function receiveOnOffline({ state, dispatch, commit }, fromDeviceUuid, message) {
|
|
|
+
|
|
|
let index = -1
|
|
|
for (var i = 0; i < state.deviceList.length; i++) {
|
|
|
- if(state.deviceList[i].uuid === fromDeviceUuid){
|
|
|
+ if (state.deviceList[i].uuid === fromDeviceUuid) {
|
|
|
index = i;
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
console.log(`state index = ${index}`);
|
|
|
- if(index >= 0){
|
|
|
+ if (index >= 0) {
|
|
|
let stateJson = JSON.parse(message)
|
|
|
//console.log(stateJson);
|
|
|
- let device = state.deviceList[i];
|
|
|
+ let device = state.deviceList[index];
|
|
|
//设备在线
|
|
|
- if(stateJson.state === 'online'){
|
|
|
+ if (stateJson.state === 'online') {
|
|
|
device.online = true
|
|
|
//更新设备状态信息
|
|
|
- commit('updateDeviceState',{index,device})
|
|
|
- //commit('updateDeviceState',{index,online:true})
|
|
|
- //state.deviceList[index].online = true
|
|
|
- if(state.currentDevice && state.currentDevice.uuid == device.uuid){ //当前设备在线
|
|
|
- //延时获取播放信息
|
|
|
- setTimeout(function() {
|
|
|
+ commit('updateDevice', { index, device })
|
|
|
+ if (state.currentDevice && state.currentDevice.uuid == device.uuid) { //当前设备在线
|
|
|
+ //延时获取设备信息和播放信息
|
|
|
+ setTimeout(async function () {
|
|
|
+ await dispatch({
|
|
|
+ type: 'publishWithType',
|
|
|
+ mqttType: 'get_dev_info',
|
|
|
+ })
|
|
|
dispatch({
|
|
|
- type:'publishWithType',
|
|
|
- mqttType:'get_position',
|
|
|
+ type: "publishWithType",
|
|
|
+ mqttType: "get_position",
|
|
|
})
|
|
|
}, 200);
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
device.online = false
|
|
|
//更新设备状态信息
|
|
|
- commit('updateDeviceState',{index,device})
|
|
|
- //commit('updateDeviceState',{index,online:true})
|
|
|
- //state.deviceList[index].online = false
|
|
|
+ commit('updateDevice', { index, device })
|
|
|
}
|
|
|
//发送通知
|
|
|
- uni.$emit('mqtt_onoffline',stateJson)
|
|
|
+ uni.$emit('mqtt_onoffline', stateJson)
|
|
|
// state.deviceList.forEach(element => {
|
|
|
// console.log(`online = ${element.online}`);
|
|
|
// });
|
|
@@ -69,18 +69,18 @@ function receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message){
|
|
|
* @param {*} param0
|
|
|
* @param {*} jsonPayload
|
|
|
*/
|
|
|
-function updatePlayInfo({commit}, jsonPayload){
|
|
|
- if(jsonPayload.other){
|
|
|
+function updatePlayInfo({ commit }, jsonPayload) {
|
|
|
+ if (jsonPayload.other) {
|
|
|
let playinfo = jsonPayload.other
|
|
|
-
|
|
|
- if(isEmpty(playinfo.title)){
|
|
|
+
|
|
|
+ if (isEmpty(playinfo.title)) {
|
|
|
playinfo.title = '猫王妙播'
|
|
|
}
|
|
|
- if(isEmpty(playinfo.albumURI)){
|
|
|
- playinfo.albumURI = "https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/wx/bg_place_holder.png"
|
|
|
+ if (isEmpty(playinfo.albumURI)) {
|
|
|
+ playinfo.albumURI = "https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/wx/bg_place_holder.png"
|
|
|
}
|
|
|
|
|
|
- commit('updatePlayInfo',playinfo)
|
|
|
+ commit('updatePlayInfo', playinfo)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -92,38 +92,69 @@ function updatePlayInfo({commit}, jsonPayload){
|
|
|
* @param {*} topic 主题
|
|
|
* @param {*} message playload
|
|
|
*/
|
|
|
-function messageParse({state, dispatch,commit,getters},topic, message){
|
|
|
+function messageParse({ state, dispatch, commit, getters }, topic, message) {
|
|
|
//console.log(`message = ${message.toString()}`);
|
|
|
+
|
|
|
let arr = topic.split("/");
|
|
|
- if(arr.length >1){
|
|
|
+ if (arr.length > 1) {
|
|
|
let fromDeviceUuid = arr[1]
|
|
|
//设备在线和离线的消息
|
|
|
- if(topic.endsWith('onoffline')){
|
|
|
- receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message)
|
|
|
-
|
|
|
- }else{
|
|
|
+ if (topic.endsWith('onoffline')) {
|
|
|
+ receiveOnOffline({ state, dispatch, commit }, fromDeviceUuid, message)
|
|
|
+ }else {
|
|
|
//收到设备发过来的数据
|
|
|
- try{
|
|
|
+ try {
|
|
|
let jsonObj = JSON.parse(message.toString())
|
|
|
- if(jsonObj.type){
|
|
|
+ if (jsonObj.type) {
|
|
|
//收到播放信息
|
|
|
- if(jsonObj.type == 'get_position'){
|
|
|
+ if (jsonObj.type == 'get_position') {
|
|
|
//判断是否是当前选中的设备
|
|
|
- if(state.currentDevice && state.currentDevice.uuid == fromDeviceUuid){
|
|
|
+ if (state.currentDevice && state.currentDevice.uuid == fromDeviceUuid) {
|
|
|
//更新播放信息
|
|
|
- updatePlayInfo({commit}, jsonObj)
|
|
|
+ updatePlayInfo({ commit }, jsonObj)
|
|
|
}
|
|
|
+ } else if (jsonObj.type == 'get_dev_info') {
|
|
|
+ //更新设备信息
|
|
|
+ updateDeviceInfo({ state,commit }, jsonObj)
|
|
|
}
|
|
|
}
|
|
|
- }catch(e){
|
|
|
+ } catch (e) {
|
|
|
console.warn(e);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function updateDeviceInfo({ state, commit }, jsonPayload) {
|
|
|
+ let index = -1
|
|
|
+ for (var i = 0; i < state.deviceList.length; i++) {
|
|
|
+ if (state.deviceList[i].uuid === jsonPayload.SrcDeviceName) {
|
|
|
+ index = i;
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(`state index = ${index}`);
|
|
|
+ if (index >= 0 && jsonPayload.other) {
|
|
|
+ let device = state.deviceList[index];
|
|
|
+ let nowDevice = jsonPayload.other
|
|
|
+ nowDevice.uuid = device.uuid
|
|
|
+ nowDevice.online = device.online
|
|
|
+ console.log(nowDevice);
|
|
|
+ //更新设备状态信息
|
|
|
+ commit('updateDevice', { index, device:nowDevice })
|
|
|
+ state.deviceList.forEach(element => {
|
|
|
+ console.log(element);
|
|
|
+ });
|
|
|
+ //更新当前的设备状态信息
|
|
|
+ if(state.currentDevice && state.currentDevice.uuid === device.uuid){
|
|
|
+ commit('setCurrentDevice', nowDevice)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
module.exports = messageParse
|