electricityMessage.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // import { mqttConnect, mqttSub } from '@/pages/mqtt-connet';
  2. // const parseTopic = (topic: string): DeviceMessage => {
  3. // const result: DeviceMessage = {};
  4. // const arr = topic.split('/');
  5. // if (arr.length > 4) {
  6. // result.DeviceCode = arr[3];
  7. // result.SubDeviceId = arr[4];
  8. // }
  9. // return result;
  10. // };
  11. export default {
  12. namespace: 'MQTTMessage',
  13. state: {
  14. message: undefined,
  15. connected: false,
  16. },
  17. subscriptions: {
  18. setupHistory({ dispatch, history }: any) {
  19. history.listen((location: any) => {
  20. if (location.pathname === '/DataBoard') {
  21. console.log(dispatch);
  22. // const client = mqttConnect();
  23. // client.on('connect', () => {
  24. // dispatch({ type: 'connected', payload: true });
  25. // mqttSub(client, {
  26. // topic: ['/device_message/1ps9djpswi0cds7cofynkso300eql4iu/#'],
  27. // qos: 0,
  28. // });
  29. // });
  30. // client.on('error', () => {
  31. // client.end();
  32. // });
  33. //
  34. // client.on('message', (topic, message: Buffer) => {
  35. // // console.log('收到的消息', topic, message.toString());
  36. // const data = JSON.parse(message.toString());
  37. // const msg = parseTopic(topic);
  38. // msg.Data = data;
  39. // dispatch({ type: 'message', payload: msg });
  40. // });
  41. }
  42. });
  43. },
  44. },
  45. reducers: {
  46. connected(state: any, { payload }: any) {
  47. return { ...state, connected: payload };
  48. },
  49. message(state: any, { payload }: any) {
  50. return { ...state, message: payload };
  51. },
  52. },
  53. };
  54. export type DeviceMessage = {
  55. DeviceCode?: string;
  56. SubDeviceId?: string;
  57. Data?: any;
  58. };