suerprisePlus
2024-05-30 d4c810f9803aa13d806724a5f4f71fe85abac38e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import mqtt from 'mqtt';
import { getToken } from '@/utils/auth';
 
let mqttTool = {
  client: null,
};
 
/** 连接Mqtt */
mqttTool.connect = function () {
  let options = {
    username: 'FastBee',
    password: getToken(),
    cleanSession: true,
    keepAlive: 30,
    clientId: 'web-' + Math.random().toString(16).substr(2),
    connectTimeout: 60000,
  };
  // 配置Mqtt地址
  let url = process.env.VUE_APP_MQTT_SERVER_URL;
  if (url == '') {
    console.log('自动获取mqtt连接地址');
    if (window.location.protocol === 'http:') {
      url = 'ws://' + window.location.hostname + ':8083/mqtt';
    } else {
      url = 'wss://' + window.location.hostname + '/mqtt';
    }
  }
  console.log('mqtt地址:', url);
  mqttTool.client = mqtt.connect(url, options);
  mqttTool.client.on('connect', (e) => {
    console.log('mqtt连接成功');
  });
  // 重新连接
  mqttTool.client.on('reconnect', (error) => {
    console.log('正在重连:', error);
  });
  // 发生错误
  mqttTool.client.on('error', (error) => {
    console.log('Mqtt客户端连接失败:', error);
    mqttTool.client.end();
  });
  // 断开连接
  mqttTool.client.on('close', function (res) {
    console.log('已断开Mqtt连接');
  });
};
/** 断开连接 */
mqttTool.end = function () {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接');
      console.log('未连接');
      return;
    }
    mqttTool.client.end();
    mqttTool.client = null;
    console.log('Mqtt服务器已断开连接!');
    resolve('连接终止');
  });
};
/** 重新连接 */
mqttTool.reconnect = function () {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      // 调用resolve方法,Promise变为操作成功状态(fulfilled)
      resolve('未连接');
      console.log('未连接');
      return;
    }
    console.log('正在重连...', res);
    mqttTool.client.reconnect();
  });
};
/** 消息订阅 */
mqttTool.subscribe = function (topics) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接');
      console.log('未连接');
      uni.showToast({
        icon: 'none',
        title: 'mqtt未连接',
      });
      return;
    }
    mqttTool.client.subscribe(
      topics,
      {
        qos: 1,
      },
      function (err, res) {
        console.log('订阅主题:', topics);
        if (!err) {
          console.log('订阅成功');
          resolve('订阅成功');
        } else {
          console.log('订阅失败,主题可能已经订阅');
          resolve('订阅失败');
          return;
        }
      }
    );
  });
};
/** 取消订阅 */
mqttTool.unsubscribe = function (topics) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('未连接');
      console.log('未连接');
      return;
    }
    mqttTool.client.unsubscribe(topics, function (err) {
      if (!err) {
        resolve('取消订阅成功');
        console.log('取消订阅成功');
      } else {
        resolve('取消订阅失败');
        console.log('取消订阅失败');
        return;
      }
    });
  });
};
mqttTool.publish = function (topic, message, name) {
  return new Promise((resolve, reject) => {
    if (mqttTool.client == null) {
      resolve('Mqtt客户端未连接');
      console.log('Mqtt客户端未连接');
      return;
    }
    mqttTool.client.publish(topic, message, { qos: 1 }, function (err) {
      console.log('发送主题:', topic);
      console.log('发送内容:', message);
      if (!err) {
        if (topic.indexOf('offline') > 0) {
          console.log('[ ' + name + ' ] 影子指令发送成功');
          resolve('[ ' + name + ' ] 影子指令发送成功');
        } else {
          console.log('[ ' + name + ' ] 指令发送成功');
          resolve('[ ' + name + ' ] 指令发送成功');
        }
      } else {
        console.log('[ ' + name + ' ] 指令发送失败');
        reject('[ ' + name + ' ] 指令发送失败');
        return;
      }
    });
  });
};
 
export default mqttTool;