mqttParse.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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[index];
  32. //设备在线
  33. if (stateJson.state === 'online') {
  34. device.online = true
  35. //更新设备状态信息
  36. commit('updateDevice', { index, device })
  37. if (state.currentDevice && state.currentDevice.uuid == device.uuid) { //当前设备在线
  38. //延时获取设备信息和播放信息
  39. setTimeout(async function () {
  40. await dispatch({
  41. type: 'publishWithType',
  42. mqttType: 'get_dev_info',
  43. })
  44. dispatch({
  45. type: "publishWithType",
  46. mqttType: "get_position",
  47. })
  48. }, 200);
  49. }
  50. } else {
  51. device.online = false
  52. //更新设备状态信息
  53. commit('updateDevice', { index, device })
  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. } else if (jsonObj.type == 'get_dev_info') {
  106. //更新设备信息
  107. updateDeviceInfo({ state,commit }, jsonObj)
  108. }
  109. }
  110. } catch (e) {
  111. console.warn(e);
  112. }
  113. }
  114. }
  115. }
  116. function updateDeviceInfo({ state, commit }, jsonPayload) {
  117. let index = -1
  118. for (var i = 0; i < state.deviceList.length; i++) {
  119. if (state.deviceList[i].uuid === jsonPayload.SrcDeviceName) {
  120. index = i;
  121. break
  122. }
  123. }
  124. console.log(`state index = ${index}`);
  125. if (index >= 0 && jsonPayload.other) {
  126. let device = state.deviceList[index];
  127. let nowDevice = jsonPayload.other
  128. nowDevice.uuid = device.uuid
  129. nowDevice.online = device.online
  130. console.log(nowDevice);
  131. //更新设备状态信息
  132. commit('updateDevice', { index, device:nowDevice })
  133. state.deviceList.forEach(element => {
  134. console.log(element);
  135. });
  136. //更新当前的设备状态信息
  137. if(state.currentDevice && state.currentDevice.uuid === device.uuid){
  138. commit('setCurrentDevice', nowDevice)
  139. }
  140. }
  141. }
  142. module.exports = messageParse