mqttParse.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. function isEmpty(str){
  2. if(str){
  3. return true
  4. }
  5. if(!str.length){
  6. return true
  7. }
  8. if(str == 'unknow'){
  9. return true
  10. }
  11. return false
  12. }
  13. /**
  14. * 收到设备在线和离线的消息
  15. * @param {*} param0
  16. * @param {*} topic
  17. * @param {*} message
  18. */
  19. function receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message){
  20. let index = -1
  21. for (var i = 0; i < state.deviceList.length; i++) {
  22. if(state.deviceList[i].uuid === fromDeviceUuid){
  23. index = i;
  24. break
  25. }
  26. }
  27. console.log(`state index = ${index}`);
  28. if(index >= 0){
  29. let stateJson = JSON.parse(message)
  30. //console.log(stateJson);
  31. let device = state.deviceList[i];
  32. //设备在线
  33. if(stateJson.state === 'online'){
  34. device.online = true
  35. //更新设备状态信息
  36. commit('updateDeviceState',{index,device})
  37. //commit('updateDeviceState',{index,online:true})
  38. //state.deviceList[index].online = true
  39. if(state.currentDevice && state.currentDevice.uuid == device.uuid){ //当前设备在线
  40. //延时获取播放信息
  41. setTimeout(function() {
  42. dispatch({
  43. type:'publishWithType',
  44. mqttType:'get_position',
  45. })
  46. }, 200);
  47. }
  48. }else{
  49. device.online = false
  50. //更新设备状态信息
  51. commit('updateDeviceState',{index,device})
  52. //commit('updateDeviceState',{index,online:true})
  53. //state.deviceList[index].online = false
  54. }
  55. //发送通知
  56. uni.$emit('mqtt_onoffline',stateJson)
  57. // state.deviceList.forEach(element => {
  58. // console.log(`online = ${element.online}`);
  59. // });
  60. }
  61. }
  62. /**
  63. * 更新播放信息
  64. * @param {*} param0
  65. * @param {*} jsonPayload
  66. */
  67. function updatePlayInfo({commit}, jsonPayload){
  68. if(jsonPayload.other){
  69. let playinfo = jsonPayload.other
  70. if(isEmpty(playinfo.title)){
  71. playinfo.title = '猫王妙播'
  72. }
  73. if(isEmpty(playinfo.albumURI)){
  74. playinfo.albumURI = "https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/wx/bg_place_holder.png"
  75. }
  76. commit('updatePlayInfo',playinfo)
  77. }
  78. }
  79. /**
  80. * 解析收到的消息
  81. * @param {*} param0
  82. * @param {*} topic 主题
  83. * @param {*} message playload
  84. */
  85. function messageParse({state, dispatch,commit,getters},topic, message){
  86. //console.log(`message = ${message.toString()}`);
  87. let arr = topic.split("/");
  88. if(arr.length >1){
  89. let fromDeviceUuid = arr[1]
  90. //设备在线和离线的消息
  91. if(topic.endsWith('onoffline')){
  92. receiveOnOffline({state, dispatch,commit},fromDeviceUuid, message)
  93. }else{
  94. //收到设备发过来的数据
  95. try{
  96. let jsonObj = JSON.parse(message.toString())
  97. if(jsonObj.type){
  98. //收到播放信息
  99. if(jsonObj.type == 'get_position'){
  100. //判断是否是当前选中的设备
  101. if(state.currentDevice && state.currentDevice.uuid == fromDeviceUuid){
  102. //更新播放信息
  103. updatePlayInfo({commit}, jsonObj)
  104. }
  105. }
  106. }
  107. }catch(e){
  108. console.warn(e);
  109. }
  110. }
  111. }
  112. }
  113. module.exports = messageParse