123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- function isEmpty(str) {
- if (str) {
- return true
- }
- if (!str.length) {
- return true
- }
- if (str == 'unknow') {
- return true
- }
- return false
- }
- function sleep(time) {
- return new Promise((resolve) => setTimeout(resolve, time))
- }
- function indexOfUuid(deviceList,uuid){
- let index = -1
- if(deviceList == null || uuid == null){
- return index
- }
- for (var i = 0; i < deviceList.length; i++) {
- if (deviceList[i].uuid === uuid) {
- index = i;
- break
- }
- }
- return index
- }
- /**
- * 当设备在线
- * @param {*} param0
- * @param {*} fromDeviceUuid
- */
- async function onDeviceOnline({ state, dispatch }, fromDeviceUuid){
- try{
- //订阅消息
- await dispatch('subscribe',`/${fromDeviceUuid}/user/pub_response`)
- await sleep(200)
- //获取设备信息
- await dispatch({
- type: "publishWithType",
- mqttType: "get_dev_info",
- })
- if(state.currentDevice && state.currentDevice.uuid == fromDeviceUuid){
- //获取播放信息
- dispatch({
- type: "publishWithType",
- mqttType: "get_position",
- })
- }
- }catch(e){
- console.warn(e);
- }
- }
- /**
- * 收到设备在线和离线的消息
- * @param {*} param0
- * @param {*} topic
- * @param {*} message
- */
- function receiveOnOffline({ state, dispatch, commit }, fromDeviceUuid, message) {
- let index = indexOfUuid(state.deviceList,fromDeviceUuid)
- 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 })
- //设备在线订阅获取设备信息等
- onDeviceOnline({state, dispatch},fromDeviceUuid)
- } 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 = indexOfUuid(state.deviceList,jsonPayload.SrcDeviceName)
- 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,indexOfUuid}
|