|
|
|
@ -1,5 +1,11 @@
|
|
|
|
|
import mqtt from 'mqtt';
|
|
|
|
|
import { useMessage } from '@/hooks/web/useMessage';
|
|
|
|
|
import { getRedisUser, addOrUpdateRedisUser } from '@/api/workmanagement/airportMaintenance';
|
|
|
|
|
import { useUserStore } from '@/store/modules/user';
|
|
|
|
|
import { timestampToFormattedDate } from '@/utils/index';
|
|
|
|
|
|
|
|
|
|
const userStore = useUserStore();
|
|
|
|
|
const userInfo = userStore.getUserInfo;
|
|
|
|
|
|
|
|
|
|
const { createMessage } = useMessage();
|
|
|
|
|
const connection = {
|
|
|
|
@ -12,8 +18,8 @@ const connection = {
|
|
|
|
|
clean: true,
|
|
|
|
|
connectTimeout: 30 * 1000, // ms
|
|
|
|
|
reconnectPeriod: 4000, // ms
|
|
|
|
|
// clientId: 'mqttx_' + Math.random().toString(16).substring(2, 8),
|
|
|
|
|
clientId: 'mqtt_client_1581F8HGX254V00A0BUY',
|
|
|
|
|
clientId: '',
|
|
|
|
|
// clientId: 'mqtt_client_1581F8HGX254V00A0BUY',
|
|
|
|
|
// auth
|
|
|
|
|
username: 'sdhc',
|
|
|
|
|
password: '',
|
|
|
|
@ -44,32 +50,41 @@ const handleOnReConnect = () => {
|
|
|
|
|
};
|
|
|
|
|
// 创建连接函数
|
|
|
|
|
const createConnection = (callback?) => {
|
|
|
|
|
console.log('创建连接');
|
|
|
|
|
// 连接mqtt的时候,先用GetRedisUser查一下有没有,
|
|
|
|
|
// 然后AddOrUpdateRedisUser更新或添加,
|
|
|
|
|
// 控制的时候先查一下有没有锁定的用户GetLockedClients
|
|
|
|
|
try {
|
|
|
|
|
getRedisUser(userInfo.id).then((res) => {
|
|
|
|
|
console.log(res);
|
|
|
|
|
if (res) {
|
|
|
|
|
connection.clientId = res.clientId;
|
|
|
|
|
} else {
|
|
|
|
|
const clientId = 'mqttx_' + Math.random().toString(16).substring(2, 8);
|
|
|
|
|
connection.clientId = clientId;
|
|
|
|
|
const querys = {
|
|
|
|
|
clientId: clientId,
|
|
|
|
|
userId: userInfo.id,
|
|
|
|
|
userName: userInfo.name,
|
|
|
|
|
isLock: false,
|
|
|
|
|
connectTime: timestampToFormattedDate(new Date().getTime()),
|
|
|
|
|
};
|
|
|
|
|
addOrUpdateRedisUser(querys);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
// const clientId = 'mqttx_' + Math.random().toString(16).substring(2, 8);
|
|
|
|
|
// connection.clientId = clientId;
|
|
|
|
|
const { protocol, host, port, endpoint, ...options } = connection;
|
|
|
|
|
const connectUrl = `${protocol}://${host}:${port}${endpoint}`;
|
|
|
|
|
client = mqtt.connect(connectUrl, options);
|
|
|
|
|
client.on('connect', () => {
|
|
|
|
|
// console.log('✅ 已连接');
|
|
|
|
|
if(callback){
|
|
|
|
|
callback()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
client.on('close', () => {
|
|
|
|
|
// console.log('❌ 连接已关闭');
|
|
|
|
|
});
|
|
|
|
|
client.on('error', (err) => {
|
|
|
|
|
// console.error('❌ 出错了:', err);
|
|
|
|
|
});
|
|
|
|
|
if (client.on) {
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('mqtt.connect error', error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const getClient = () => {
|
|
|
|
|
if (!client || !client.connected) {
|
|
|
|
|
createConnection();
|
|
|
|
|
}
|
|
|
|
|
// if (!client || !client.connected) {
|
|
|
|
|
// createConnection();
|
|
|
|
|
// }
|
|
|
|
|
return client;
|
|
|
|
|
};
|
|
|
|
|
// 断开连接
|
|
|
|
@ -87,20 +102,16 @@ const destroyConnection = () => {
|
|
|
|
|
};
|
|
|
|
|
// 订阅事件
|
|
|
|
|
const clientSubscribe = (topic: string, options?: any) => {
|
|
|
|
|
console.log(client);
|
|
|
|
|
if (!client || !client.connected) {
|
|
|
|
|
createConnection();
|
|
|
|
|
}
|
|
|
|
|
getClient().subscribe(topic, { qos: 0 }, (error, res) => {
|
|
|
|
|
console.log('订阅');
|
|
|
|
|
console.log(error, res);
|
|
|
|
|
});
|
|
|
|
|
// if (!client || !client.connected) {
|
|
|
|
|
// createConnection();
|
|
|
|
|
// }
|
|
|
|
|
getClient().subscribe(topic, { qos: 0 }, (error, res) => {});
|
|
|
|
|
};
|
|
|
|
|
// 发送消息
|
|
|
|
|
const clientPublish = (topic: string, querys: any) => {
|
|
|
|
|
if (!client || !client.connected) {
|
|
|
|
|
createConnection();
|
|
|
|
|
}
|
|
|
|
|
// if (!client || !client.connected) {
|
|
|
|
|
// createConnection();
|
|
|
|
|
// }
|
|
|
|
|
getClient().publish(topic, JSON.stringify(querys), { qos: 0 }, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error('Publish error:', err);
|
|
|
|
@ -109,12 +120,11 @@ const clientPublish = (topic: string, querys: any) => {
|
|
|
|
|
};
|
|
|
|
|
// 获取连接状态
|
|
|
|
|
const getConnectStatus = () => {
|
|
|
|
|
console.log('client',client)
|
|
|
|
|
if (!client.connected) {
|
|
|
|
|
return false
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
// on 事件
|
|
|
|
|
// connect 连接
|
|
|
|
|
// reconnect 重新连接
|
|
|
|
@ -143,18 +153,31 @@ const createSeizeConnection = () => {
|
|
|
|
|
console.log('mqtt.connect error', error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const getReizeClient = () => {
|
|
|
|
|
if (!client_seize || !client_seize.connected) {
|
|
|
|
|
createSeizeConnection();
|
|
|
|
|
const destroySeizeConnection = () => {
|
|
|
|
|
if (client_seize.connected) {
|
|
|
|
|
try {
|
|
|
|
|
client_seize.end(false, () => {
|
|
|
|
|
client_seize = {
|
|
|
|
|
connected: false,
|
|
|
|
|
};
|
|
|
|
|
console.log('Successfully disconnected!');
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log('Disconnect failed', error.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const getReizeClient = () => {
|
|
|
|
|
// if (!client_seize || !client_seize.connected) {
|
|
|
|
|
// createSeizeConnection();
|
|
|
|
|
// }
|
|
|
|
|
return client_seize;
|
|
|
|
|
};
|
|
|
|
|
// 订阅事件
|
|
|
|
|
const clientReizeSubscribe = (topic: string, options?: any) => {
|
|
|
|
|
console.log(client_seize);
|
|
|
|
|
if (!client_seize || !client_seize.connected) {
|
|
|
|
|
createConnection();
|
|
|
|
|
}
|
|
|
|
|
// if (!client_seize || !client_seize.connected) {
|
|
|
|
|
// createSeizeConnection();
|
|
|
|
|
// }
|
|
|
|
|
getReizeClient().subscribe(topic, { qos: 0 }, (error, res) => {
|
|
|
|
|
console.log('订阅');
|
|
|
|
|
console.log(error, res);
|
|
|
|
@ -162,9 +185,9 @@ const clientReizeSubscribe = (topic: string, options?: any) => {
|
|
|
|
|
};
|
|
|
|
|
// 发送消息
|
|
|
|
|
const clientReizePublish = (topic: string, querys: any) => {
|
|
|
|
|
if (!client_seize || !client_seize.connected) {
|
|
|
|
|
createConnection();
|
|
|
|
|
}
|
|
|
|
|
// if (!client_seize || !client_seize.connected) {
|
|
|
|
|
// createSeizeConnection();
|
|
|
|
|
// }
|
|
|
|
|
getReizeClient().publish(topic, JSON.stringify(querys), { qos: 0 }, (err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.error('Publish error:', err);
|
|
|
|
@ -179,6 +202,7 @@ export {
|
|
|
|
|
clientSubscribe,
|
|
|
|
|
clientPublish,
|
|
|
|
|
createSeizeConnection,
|
|
|
|
|
destroySeizeConnection,
|
|
|
|
|
getReizeClient,
|
|
|
|
|
clientReizeSubscribe,
|
|
|
|
|
clientReizePublish,
|
|
|
|
|