|
@@ -1,6 +1,16 @@
|
|
import mqtt from './mqtt';
|
|
import mqtt from './mqtt';
|
|
|
|
|
|
|
|
+let mqttClientCmd = null; // 全局 CMD MQTT
|
|
|
|
+let mqttClientData = null; // 全局 DATA MQTT
|
|
|
|
+
|
|
|
|
+let mqttCmdConnected = false;
|
|
|
|
+let mqttDataConnected = false;
|
|
|
|
+
|
|
export function createMqttClient() {
|
|
export function createMqttClient() {
|
|
|
|
+ if (mqttCmdConnected) {
|
|
|
|
+ console.log("CMD MQTT 已连接");
|
|
|
|
+ return mqttClientCmd;
|
|
|
|
+ }
|
|
const params = {
|
|
const params = {
|
|
keepalive: 6000,
|
|
keepalive: 6000,
|
|
clean: true,
|
|
clean: true,
|
|
@@ -23,7 +33,6 @@ export function createMqttClient() {
|
|
},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
};
|
|
-
|
|
|
|
let client = "";
|
|
let client = "";
|
|
// const selectedService = uni.getStorageSync("sercviceChoice");
|
|
// const selectedService = uni.getStorageSync("sercviceChoice");
|
|
// if (!selectedService || selectedService == "aloneServe") {
|
|
// if (!selectedService || selectedService == "aloneServe") {
|
|
@@ -36,8 +45,8 @@ export function createMqttClient() {
|
|
// client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
|
|
// client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
|
|
// }
|
|
// }
|
|
client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
|
|
client = mqtt.connect("wxs://cmd.radar-power.cn/mqtt/", params);
|
|
-
|
|
|
|
client.on("connect", () => {
|
|
client.on("connect", () => {
|
|
|
|
+ mqttCmdConnected = true;
|
|
console.log("MQTTCMD连接成功");
|
|
console.log("MQTTCMD连接成功");
|
|
const userId = uni.getStorageSync("userId");
|
|
const userId = uni.getStorageSync("userId");
|
|
client.subscribe(`/mps/wx_${userId}/notice`, (err) => {
|
|
client.subscribe(`/mps/wx_${userId}/notice`, (err) => {
|
|
@@ -48,7 +57,6 @@ export function createMqttClient() {
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
-
|
|
|
|
client.on("message", (topic, message) => {
|
|
client.on("message", (topic, message) => {
|
|
console.log("topic", topic, message);
|
|
console.log("topic", topic, message);
|
|
console.log("接收到消息:", JSON.parse(message.toString()));
|
|
console.log("接收到消息:", JSON.parse(message.toString()));
|
|
@@ -56,10 +64,9 @@ export function createMqttClient() {
|
|
const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
const match = topic.match(noticeMatch);
|
|
const match = topic.match(noticeMatch);
|
|
if (!match) return;
|
|
if (!match) return;
|
|
- // 处理消息
|
|
|
|
});
|
|
});
|
|
-
|
|
|
|
client.on("error", (err) => {
|
|
client.on("error", (err) => {
|
|
|
|
+ mqttCmdConnected = false;
|
|
console.error("MQTT连接错误:", err);
|
|
console.error("MQTT连接错误:", err);
|
|
// 连接失败时重新尝试连接
|
|
// 连接失败时重新尝试连接
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
@@ -68,8 +75,58 @@ export function createMqttClient() {
|
|
});
|
|
});
|
|
|
|
|
|
client.on("disconnect", () => {
|
|
client.on("disconnect", () => {
|
|
|
|
+ mqttCmdConnected = false;
|
|
console.log("MQTT断开连接");
|
|
console.log("MQTT断开连接");
|
|
});
|
|
});
|
|
-
|
|
|
|
return client;
|
|
return client;
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 创建data连接
|
|
|
|
+export function createMqttData(clientIdProp) {
|
|
|
|
+ if (!clientIdProp) {
|
|
|
|
+ console.warn("DATA MQTT 创建失败:缺少 clientIdProp");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (mqttDataConnected) return mqttClientData;
|
|
|
|
+
|
|
|
|
+ const params = {
|
|
|
|
+ keepalive: 6000,
|
|
|
|
+ clean: true,
|
|
|
|
+ connectTimeout: 10000,
|
|
|
|
+ clientId: "xcx_mqtt_data1_" + clientIdProp + "_" + Date.now(),
|
|
|
|
+ username: "lnradar",
|
|
|
|
+ password: "lnradar",
|
|
|
|
+ WebSocket: function (url) {
|
|
|
|
+ return wx.connectSocket({
|
|
|
|
+ url: url,
|
|
|
|
+ header: {
|
|
|
|
+ "content-type": "application/json",
|
|
|
|
+ },
|
|
|
|
+ protocols: ["mqtt"],
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ reconnectPeriod: 2000
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ mqttClientData = mqtt.connect("wxs://data.radar-power.cn/mqtt/", params);
|
|
|
|
+
|
|
|
|
+ mqttClientData.on("connect", () => {
|
|
|
|
+ console.log("DATA MQTT 连接成功");
|
|
|
|
+ mqttDataConnected = true;
|
|
|
|
+ mqttClientData.subscribe(`/dev/${clientIdProp}/tracker_targets`, (err) => {
|
|
|
|
+ if (err) console.error("DATA MQTT 订阅失败", err);
|
|
|
|
+ else console.log(`DATA MQTT 订阅成功: /dev/${clientIdProp}/tracker_targets`);
|
|
|
|
+ });
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ mqttClientData.on("message", (topic, message) => {
|
|
|
|
+ console.log("DATA MQTT 消息:", topic, message.toString());
|
|
|
|
+ // handleDataMessage(topic, message, clientIdProp);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ mqttClientData.on("error", (err) => { mqttDataConnected = false; console.error(err); });
|
|
|
|
+ mqttClientData.on("disconnect", () => { mqttDataConnected = false; console.log("DATA MQTT 断开"); });
|
|
|
|
+
|
|
|
|
+ return mqttClientData;
|
|
|
|
+}
|