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[index]; //设备在线 if (stateJson.state === 'online') { device.online = true //更新设备状态信息 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", }) }, 200); } } else { device.online = false //更新设备状态信息 commit('updateDevice', { index, device }) } //发送通知 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) } } else if (jsonObj.type == 'get_dev_info') { //更新设备信息 updateDeviceInfo({ state,commit }, jsonObj) } } } 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