mqttParse.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. function sleep(time) {
  14. return new Promise((resolve) => setTimeout(resolve, time))
  15. }
  16. function indexOfUuid(deviceList,uuid){
  17. let index = -1
  18. if(deviceList == null || uuid == null){
  19. return index
  20. }
  21. for (var i = 0; i < deviceList.length; i++) {
  22. if (deviceList[i].uuid === uuid) {
  23. index = i;
  24. break
  25. }
  26. }
  27. return index
  28. }
  29. /**
  30. * 当设备在线
  31. * @param {*} param0
  32. * @param {*} fromDeviceUuid
  33. */
  34. async function onDeviceOnline({ state, dispatch }, fromDeviceUuid){
  35. try{
  36. //订阅消息
  37. await dispatch('subscribe',`/${fromDeviceUuid}/user/pub_response`)
  38. await sleep(200)
  39. //获取设备信息
  40. await dispatch({
  41. type: "publishWithType",
  42. mqttType: "get_dev_info",
  43. })
  44. if(state.currentDevice && state.currentDevice.uuid == fromDeviceUuid){
  45. //获取播放信息
  46. dispatch({
  47. type: "publishWithType",
  48. mqttType: "get_position",
  49. })
  50. }
  51. }catch(e){
  52. console.warn(e);
  53. }
  54. }
  55. /**
  56. * 收到设备在线和离线的消息
  57. * @param {*} param0
  58. * @param {*} topic
  59. * @param {*} message
  60. */
  61. function receiveOnOffline({ state, dispatch, commit }, fromDeviceUuid, message) {
  62. let index = indexOfUuid(state.deviceList,fromDeviceUuid)
  63. console.log(`state index = ${index}`);
  64. if (index >= 0) {
  65. let stateJson = JSON.parse(message)
  66. //console.log(stateJson);
  67. let device = state.deviceList[index];
  68. //设备在线
  69. if (stateJson.state === 'online') {
  70. device.online = true
  71. //更新设备状态信息
  72. commit('updateDevice', { index, device })
  73. //设备在线订阅获取设备信息等
  74. onDeviceOnline({state, dispatch},fromDeviceUuid)
  75. } else {
  76. device.online = false
  77. //更新设备状态信息
  78. commit('updateDevice', { index, device })
  79. }
  80. //发送通知
  81. uni.$emit('mqtt_onoffline', stateJson)
  82. // state.deviceList.forEach(element => {
  83. // console.log(`online = ${element.online}`);
  84. // });
  85. }
  86. }
  87. /**
  88. * 更新播放信息
  89. * @param {*} param0
  90. * @param {*} jsonPayload
  91. */
  92. function updatePlayInfo({ commit }, jsonPayload) {
  93. if (jsonPayload.other) {
  94. let playinfo = jsonPayload.other
  95. if (isEmpty(playinfo.title)) {
  96. playinfo.title = '猫王妙播'
  97. }
  98. if (isEmpty(playinfo.albumURI)) {
  99. playinfo.albumURI = "https://airsmart-photo1.oss-cn-shanghai.aliyuncs.com/wx/bg_place_holder.png"
  100. }
  101. commit('updatePlayInfo', playinfo)
  102. }
  103. }
  104. /**
  105. * 解析收到的消息
  106. * @param {*} param0
  107. * @param {*} topic 主题
  108. * @param {*} message playload
  109. */
  110. function messageParse({ state, dispatch, commit, getters }, topic, message) {
  111. //console.log(`message = ${message.toString()}`);
  112. let arr = topic.split("/");
  113. if (arr.length > 1) {
  114. let fromDeviceUuid = arr[1]
  115. //设备在线和离线的消息
  116. if (topic.endsWith('onoffline')) {
  117. receiveOnOffline({ state, dispatch, commit }, fromDeviceUuid, message)
  118. }else {
  119. //收到设备发过来的数据
  120. try {
  121. let jsonObj = JSON.parse(message.toString())
  122. if (jsonObj.type) {
  123. //收到播放信息
  124. if (jsonObj.type == 'get_position') {
  125. //判断是否是当前选中的设备
  126. if (state.currentDevice && state.currentDevice.uuid == fromDeviceUuid) {
  127. //更新播放信息
  128. updatePlayInfo({ commit }, jsonObj)
  129. }
  130. } else if (jsonObj.type == 'get_dev_info') {
  131. //更新设备信息
  132. updateDeviceInfo({ state,commit }, jsonObj)
  133. }
  134. }
  135. } catch (e) {
  136. console.warn(e);
  137. }
  138. }
  139. }
  140. }
  141. function updateDeviceInfo({ state, commit }, jsonPayload) {
  142. let index = indexOfUuid(state.deviceList,jsonPayload.SrcDeviceName)
  143. console.log(`state index = ${index}`);
  144. if (index >= 0 && jsonPayload.other) {
  145. let device = state.deviceList[index];
  146. let nowDevice = jsonPayload.other
  147. nowDevice.uuid = device.uuid
  148. nowDevice.online = device.online
  149. console.log(nowDevice);
  150. //更新设备状态信息
  151. commit('updateDevice', { index, device:nowDevice })
  152. state.deviceList.forEach(element => {
  153. console.log(element);
  154. });
  155. //更新当前的设备状态信息
  156. if(state.currentDevice && state.currentDevice.uuid === device.uuid){
  157. commit('setCurrentDevice', nowDevice)
  158. }
  159. }
  160. }
  161. module.exports = {messageParse,indexOfUuid}