function isEmpty(str){ if(str){ return true } if(!str.length){ return true } if(str == 'unknow'){ return true } return false } /** * 收到设备在线和离线的消息 * @param {*} param0 * @param {*} topic * @param {*} 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){ 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(state.currentDevice && state.currentDevice.uuid == device.uuid){ //当前设备在线 //延时获取播放信息 setTimeout(function() { dispatch({ type:'publishWithType', mqttType:'get_position', }) }, 200); } }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}`); // }); } } /** * 更新播放信息 * @param {*} param0 * @param {*} jsonPayload */ function updatePlayInfo({commit}, jsonPayload){ if(jsonPayload.other){ let playinfo = jsonPayload.other 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" } commit('updatePlayInfo',playinfo) } } /** * 解析收到的消息 * @param {*} param0 * @param {*} topic 主题 * @param {*} message playload */ function messageParse({state, dispatch,commit,getters},topic, message){ //console.log(`message = ${message.toString()}`); let arr = topic.split("/"); if(arr.length >1){ let fromDeviceUuid = arr[1] //设备在线和离线的消息 if(topic.endsWith('onoffline')){ receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message) }else{ //收到设备发过来的数据 try{ let jsonObj = JSON.parse(message.toString()) if(jsonObj.type){ //收到播放信息 if(jsonObj.type == 'get_position'){ //判断是否是当前选中的设备 if(state.currentDevice && state.currentDevice.uuid == fromDeviceUuid){ //更新播放信息 updatePlayInfo({commit}, jsonObj) } } } }catch(e){ console.warn(e); } } } } module.exports = messageParse