去掉前端关自动停逻辑

master
xce 2026-01-17 01:49:10 +08:00
parent d01b66256b
commit 997cd9426b
3 changed files with 39 additions and 129 deletions

View File

@ -2,6 +2,7 @@
import config from './config'
import { getToken } from '@/utils/auth'
import mqttUtil from '@/utils/mqtt'
import {startMqttOnlinePing, stopMqttOnlinePing} from "./utils/mqtt";
export default {
globalData: {
@ -55,6 +56,9 @@ export default {
console.info("token存在mqtt重新连接中。。")
this.reconnectMqtt()
}
// mqtt client connected start
startMqttOnlinePing( 20000);
}
},
onHide() {
@ -65,6 +69,7 @@ export default {
// ========== localStorage ==========
uni.setStorageSync('mqtt_subscribe_list', this.globalData.mqtt.subscribeList)
mqttUtil.disconnectMqtt()
stopMqttOnlinePing();
}
},
methods: {

View File

@ -230,8 +230,6 @@ export default {
currentCard: {}, //
currentCardTime: 0, //
newLimitTime: 0, //
// key=typevalue={timerId, snapInfo}
timers: {}
};
},
onLoad() {
@ -260,8 +258,6 @@ export default {
onUnload() {
// MQTT
mqttUtil.removeOnMessageCallback();
//
this.clearAllTimers();
},
methods: {
@ -274,15 +270,6 @@ export default {
}
})
},
//
clearAllTimers() {
Object.keys(this.timers).forEach(type => {
if (this.timers[type]?.timerId) {
clearTimeout(this.timers[type].timerId);
}
});
this.timers = {};
},
change(e) {
this.imei = e;
if ((e === 'A' || e==='B' || e==='C')
@ -480,43 +467,6 @@ export default {
testAuto(type) {
this.$set(this.status, type, this.status[type] === 0 ? 1 : 0);
this.$set(this.show, type, this.status[type] === 0 ? "暂停" : "运行");
// ========== ==========
const isStartCommand = status === 1; // 1=0=
if (isStartCommand) {
// 1. type
if (this.timers[type]?.timerId) {
clearTimeout(this.timers[type].timerId);
delete this.timers[type];
}
// 2.
const snapInfo = {
imei: this.imei, // imei
publishTopic: this.publishTopic, // Topic
delayTime: Number(this.limitTimes[`${type}Limit`] || 0) * 1000, //
deviceType: type, //
connected: this.connected //
};
// 3.
if (snapInfo.delayTime && snapInfo.delayTime > 0) {
console.info(`定时记录:大棚:${snapInfo.imei}; 指令:${this.message}`)
// 4. timerId
const timerId = setTimeout(() => {
// 使
this.sendAutoStopCommand(snapInfo);
//
delete this.timers[type];
}, snapInfo.delayTime);
// 5.
this.timers[type] = {
timerId: timerId,
snapInfo: snapInfo
};
}
}
},
publishMessage() {
if (!this.connected || !this.publishTopic || !this.message) {
@ -573,89 +523,15 @@ export default {
// 使$set
this.$set(this.status, type, this.status[type] === 0 ? 1 : 0);
this.$set(this.show, type, this.status[type] === 0 ? "暂停" : "运行");
// ========== ==========
const isStartCommand = commandValue === 1; // 1=0=
if (isStartCommand) {
// 1. type
if (this.timers[type]?.timerId) {
clearTimeout(this.timers[type].timerId);
delete this.timers[type];
}
// 2.
const snapInfo = {
imei: this.imei, // imei
publishTopic: this.publishTopic, // Topic
delayTime: Number(this.limitTimes[`${type}Limit`] || 0) * 1000, //
deviceType: type, //
connected: this.connected //
};
// 3.
if (snapInfo.delayTime && snapInfo.delayTime > 0) {
// 4. timerId
const timerId = setTimeout(() => {
// 使
this.sendAutoStopCommand(snapInfo);
//
delete this.timers[type];
}, snapInfo.delayTime);
// 5.
this.timers[type] = {
timerId: timerId,
snapInfo: snapInfo
};
}
}
}
this.deviceType = '';
// ========== ==========
if (commandValue === 1) this.$modal.msgSuccess("设备操作成功!");
this.$modal.msgSuccess("设备操作成功!");
console.log(`指令[${commandField}=${commandValue}]执行${isSuccess ? "成功" : "失败"}`);
},
// 使
sendAutoStopCommand(snapInfo) {
// 1.
if (!snapInfo || !snapInfo.imei || !snapInfo.publishTopic || !snapInfo.deviceType) {
this.addMessage(`【自动停止失败】快照数据缺失`);
return;
}
// 2.
let needStop = true;
if (this.imei === snapInfo.imei) {
needStop = this.status[snapInfo.deviceType] === 1;
}
if (needStop) {
// 3. 使
const stopMessage = JSON.stringify({[snapInfo.deviceType]: 0});
console.info(`自动停:${snapInfo.publishTopic}:${stopMessage}`);
// todo 4. 使Topic
const publishSuccess = mqttUtil.publishMqtt(snapInfo.publishTopic, stopMessage);
if (publishSuccess) {
this.addMessage(`【自动停止-旧大棚${snapInfo.imei}】设备${snapInfo.deviceType},指令: ${stopMessage}`);
// 5.
if (this.imei === snapInfo.imei) {
this.$set(this.status, snapInfo.deviceType, 0);
this.$set(this.show, snapInfo.deviceType, "暂停");
}
} else {
this.addMessage(`【自动停止失败-旧大棚${snapInfo.imei}】设备${snapInfo.deviceType}`);
}
} else {
this.addMessage(`【自动停止跳过】旧大棚${snapInfo.imei}设备${snapInfo.deviceType}已非运行状态`);
}
},
handleOtherContent(msgData,payload) {
//
//
@ -768,13 +644,9 @@ export default {
},
onHide() {
mqttUtil.removeOnMessageCallback();
//
this.clearAllTimers();
},
beforeDestroy() {
mqttUtil.removeOnMessageCallback();
//
this.clearAllTimers();
},
};
</script>

View File

@ -277,6 +277,39 @@ export function getMqttState() {
}
}
// utils/mqttOnline.js
let timer = null;
export function startMqttOnlinePing(intervalMs = 20000) {
if (!mqttState.client || !mqttState.options.clientId) return;
if (timer) return;
const topic = `frontend/${mqttState.options.clientId}/online`;
const ping = () => {
try {
if (!mqttState.client.connected) return;
const payload = JSON.stringify({ ts: Date.now() });
// qos=0 足够retain 不要
mqttState.client.publish(topic, payload, { qos: 0, retain: false });
} catch (e) {}
};
ping();
// 每 20000ms20秒执行一次 ping
timer = setInterval(ping, intervalMs);
}
export function stopMqttOnlinePing() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
// 导出所有方法(全局调用)
export default {
initMqttConfig,