moduleMqtt.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import mqtt from '../../common/mqtt.js'
  2. import Vue from 'vue'
  3. import parse from './mqttParse.js'
  4. //import mqtt from 'mqtt/dist/mqtt.js'
  5. const options = {
  6. clientId: "wx_" + parseInt(Math.random() * 100 + 800, 10),
  7. keepalive: 28,//每28秒发送一次心跳
  8. connectTimeout: 15 * 1000,//连接超时的时间
  9. };
  10. let url = 'wss://mqtt.test.radio1964.com'
  11. // #ifdef MP-WEIXIN
  12. url = 'wxs://mqtt.test.radio1964.com'
  13. // #endif
  14. //订阅设备在线状态
  15. function subscribeDevicesStatus(dispatch,deviceList){
  16. deviceList.forEach((value) =>{
  17. let topic = `/${value.uuid}/status/onoffline`
  18. dispatch('subscribe',topic)
  19. })
  20. }
  21. //订阅消息
  22. function subscribeCurrDevice(dispatch,device){
  23. if(device){
  24. let topic = `/${device.uuid}/user/pub_response`
  25. dispatch('subscribe',topic)
  26. }
  27. }
  28. export default {
  29. namespaced: true,
  30. state: {
  31. //当前选择的设备
  32. currentDevice: null,
  33. //用户设备列表
  34. deviceList: [],
  35. //mqtt客户端
  36. mqttClient: null,
  37. //收到的消息的历史记录
  38. msgList:[],
  39. //设备的播放信息
  40. playInfo:{
  41. title:"",
  42. artist:"",
  43. albumURI:"",
  44. songInfoID:"",
  45. songAlbumID:"",
  46. Duration:0,
  47. channel:1,
  48. audioType:1,
  49. PlayState:3,
  50. PlayMode:0
  51. }
  52. },
  53. getters: {
  54. //mqtt连接状态
  55. connected: state => {
  56. return state.mqttClient && state.mqttClient.connected
  57. },
  58. },
  59. mutations: {
  60. //添加设备
  61. addDevice(state, device) {
  62. state.deviceList.push(device)
  63. },
  64. //设置当前选择的设备
  65. setCurrentDevice(state, device) {
  66. state.currentDevice = device
  67. },
  68. //移除设备
  69. removeDevice(state, device) {
  70. let index = state.deviceList.indexOf(device)
  71. state.deviceList.slice(index, 1)
  72. },
  73. //连接mqtt服务器 clientId为wx_加上用户的uid
  74. connect(state, clientId) {
  75. console.log(`connect clientId = ${clientId}`);
  76. options.clientId = clientId
  77. state.mqttClient = mqtt.connect(url, options)
  78. },
  79. //token失效,退出登录,需要清除数据
  80. release(state) {
  81. state.deviceList = []
  82. state.currentDevice = null
  83. state.mqttClient = null
  84. },
  85. //设置设备的在线状态
  86. updateDeviceState(state,data){
  87. Vue.set(state.deviceList,data.index,data.device)
  88. //state.deviceList[data.index] = {...state.deviceList[data.index], online:data.online}
  89. //state.deviceList[data.index].online = data.online
  90. },
  91. //更新播放信息
  92. updatePlayInfo(state,playinfo){
  93. state.playInfo = playinfo
  94. }
  95. },
  96. actions: {
  97. //添加设备
  98. addDevice({state, dispatch,commit, getters },data){
  99. let clientId = (data.clientId) ? data.clientId : options.clientId
  100. let device = data.device
  101. console.log(`deviceList = ${state.deviceList} device = ${device}`);
  102. //判断设备是否存在deviceList中
  103. let findDevice = state.deviceList.find((item) =>{
  104. return item.uuid === device.uuid
  105. })
  106. if(findDevice == null){
  107. if(state.deviceList.length === 0){
  108. //设置当前的设备为添加的设备
  109. commit('setCurrentDevice',device)
  110. }
  111. //添加设备
  112. state.deviceList.push(device)
  113. //如果已经连接
  114. if(getters.connected){
  115. //订阅设备是否在线
  116. subscribeDevicesStatus(dispatch,[device])
  117. if(state.currentDevice && state.currentDevice.uuid === device.uuid){
  118. //订阅当前设备的消息
  119. subscribeCurrDevice(dispatch,state.currentDevice)
  120. }
  121. }else{//如果没有连接
  122. //连接服务器
  123. dispatch('connect',clientId)
  124. }
  125. }
  126. },
  127. //删除设备
  128. removeDevice({state, dispatch,commit, getters },device){
  129. //判断设备是否存在deviceList中
  130. let findDevice = state.deviceList.find((item) =>{
  131. return item.uuid === device.uuid
  132. })
  133. if(findDevice){
  134. //取消此设备的订阅
  135. let topic = `/${device.uuid}/status/onoffline`
  136. dispatch('unsubscribe',topic)
  137. topic = `/${device.uuid}/status/pub_response`
  138. dispatch('unsubscribe',topic)
  139. //在设备列表中删除此设备
  140. commit('removeDevice',findDevice)
  141. //如果删除的是选中的设备
  142. if(currentDevice && state.currentDevice.uuid == device.uuid){
  143. //清除当前选中的设备 todo 还是自动选中在线的设备
  144. commit('setCurrentDevice',null)
  145. }
  146. if(state.deviceList.length === 0 ){//所有的设备都已经删除,关闭连接
  147. dispatch('disconnect')
  148. }
  149. }
  150. },
  151. //连接设备
  152. connect({ state, dispatch,commit, getters }, clientId) {
  153. if (state.mqttClient == null) {
  154. //连接服务器
  155. commit('connect', clientId)
  156. //连接成功回调
  157. state.mqttClient.on("connect", function () {
  158. console.log("connect success!");
  159. //订阅设备是否在线
  160. subscribeDevicesStatus(dispatch,state.deviceList)
  161. //订阅当前设备的消息
  162. subscribeCurrDevice(dispatch,state.currentDevice)
  163. });
  164. //异常回调
  165. state.mqttClient.on("error", function (err) {
  166. console.log(err);
  167. });
  168. state.mqttClient.on('offline', function () {
  169. console.log(`offline callback`);
  170. })
  171. state.mqttClient.on('end', function () {
  172. console.log(`end callback`);
  173. })
  174. //网络断开的回调
  175. state.mqttClient.on('close', function () {
  176. console.log(`close callback`);
  177. })
  178. //收到消息的回调
  179. state.mqttClient.on('message', function (topic, message) {
  180. console.log(`message = ${message.toString()}`);
  181. //解析收到的消息
  182. parse({state, dispatch,commit, getters },topic,message)
  183. //这是测试的
  184. state.msgList.push({
  185. topic: topic.toString(),
  186. msg: message.toString(),
  187. });
  188. })
  189. }else{
  190. if(!state.mqttClient.connected){
  191. console.log("reconnect");
  192. state.mqttClient.reconnect()
  193. //重连成功的回调
  194. state.mqttClient.on("reconnect", function() {
  195. console.log("reconnect success!");
  196. });
  197. }
  198. }
  199. },
  200. //断开mqtt的连接
  201. disconnect({state,commit},isRelease) {
  202. console.log('disconnect')
  203. if (state.mqttClient) {
  204. state.mqttClient.end(false,() =>{
  205. console.log(`state.mqttClient.connected = ${state.mqttClient.connected}`);
  206. })
  207. }
  208. if(isRelease){
  209. //清除数据
  210. commit('release')
  211. }
  212. },
  213. //发布消息
  214. publishWithType({state, dispatch,getters},payload){
  215. //console.log(payload);
  216. //console.log(options);
  217. let type = payload.mqttType;
  218. let other = payload.other;
  219. if(!getters.connected){
  220. return new Promise((resolve, reject) => {
  221. reject('连接已断开')
  222. });
  223. }else if(!(state.currentDevice && state.currentDevice.online)){
  224. return new Promise((resolve, reject) => {
  225. reject('设备已经离线')
  226. });
  227. }else{
  228. let topic = `/${state.currentDevice.uuid}/user/sub_control`;
  229. //console.log(payload);
  230. //console.log(payload.mqttType);
  231. let json = {
  232. 'DstDeviceName':state.currentDevice.uuid,
  233. 'SrcDeviceName':options.clientId,
  234. 'type':type,
  235. }
  236. if(other){
  237. json.other = other
  238. }
  239. let payload = JSON.stringify(json)
  240. console.log(`publishWithType topic = ${topic} payload = ${payload}`);
  241. return dispatch('publish',{topic,payload})
  242. }
  243. },
  244. //发布消息
  245. publish({ state, commit, getters }, message) {
  246. console.log('publish');
  247. console.log(message);
  248. return new Promise((resolve, reject) => {
  249. if (getters.connected) {
  250. //发布消息
  251. state.mqttClient.publish(message.topic, message.payload, (err) => {
  252. if (err) {
  253. console.warn(err);
  254. reject(err)
  255. } else {
  256. resolve()
  257. }
  258. })
  259. } else {
  260. reject("mqttClient is not connected")
  261. }
  262. })
  263. },
  264. //订阅消息
  265. subscribe({ state, commit, getters }, topic) {
  266. console.log(`subscribe topic = ${topic}`);
  267. return new Promise((resolve, reject) => {
  268. if (getters.connected) {
  269. //发布消息
  270. state.mqttClient.subscribe(topic, (err) => {
  271. if (err) {
  272. console.log(err);
  273. reject(err)
  274. } else {
  275. resolve()
  276. }
  277. })
  278. } else {
  279. reject("mqttClient is not connected")
  280. }
  281. })
  282. },
  283. //解除订阅消息
  284. unsubscribe({ state, commit, getters }, topic){
  285. console.log(`unsubscribe topic = ${topic}`);
  286. return new Promise((resolve, reject) => {
  287. if (getters.connected) {
  288. //发布消息
  289. state.mqttClient.unsubscribe(topic, (err) => {
  290. if (err) {
  291. console.log(err);
  292. reject(err)
  293. } else {
  294. resolve()
  295. }
  296. })
  297. } else {
  298. reject("mqttClient is not connected")
  299. }
  300. })
  301. }
  302. }
  303. }