123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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
|