|
@@ -12,6 +12,7 @@ export interface MqttMessage {
|
|
|
|
|
|
//const MQTT_BROKER_URL = 'ws://your-mqtt-broker-url'; // Replace with your actual MQTT broker URL
|
|
//const MQTT_BROKER_URL = 'ws://your-mqtt-broker-url'; // Replace with your actual MQTT broker URL
|
|
const MQTT_TOPIC = 'MODULE/TASK/IMGPROC/PROGRESS';
|
|
const MQTT_TOPIC = 'MODULE/TASK/IMGPROC/PROGRESS';
|
|
|
|
+const MQTT_GLOBAL_TOPIC = 'MODULE/TASK/STATUS/GLOBAL';
|
|
console.log('[mqttService] MQTT_BROKER_URL', MQTT_BROKER_URL);
|
|
console.log('[mqttService] MQTT_BROKER_URL', MQTT_BROKER_URL);
|
|
const options = {
|
|
const options = {
|
|
clean: true, // true: 清除会话, false: 保留会话
|
|
clean: true, // true: 清除会话, false: 保留会话
|
|
@@ -55,6 +56,11 @@ const startListening = () => {
|
|
console.error('Failed to subscribe to topic', err);
|
|
console.error('Failed to subscribe to topic', err);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
+ mqttClient.subscribe(MQTT_GLOBAL_TOPIC, (err) => {
|
|
|
|
+ if (err) {
|
|
|
|
+ console.error('Failed to subscribe to global topic', err);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
});
|
|
});
|
|
|
|
|
|
mqttClient.on('error', (error) => {
|
|
mqttClient.on('error', (error) => {
|
|
@@ -65,6 +71,13 @@ const startListening = () => {
|
|
if (topic === MQTT_TOPIC) {
|
|
if (topic === MQTT_TOPIC) {
|
|
const parsedMessage: MqttMessage = JSON.parse(message.toString());
|
|
const parsedMessage: MqttMessage = JSON.parse(message.toString());
|
|
handleMqttMessage(parsedMessage);
|
|
handleMqttMessage(parsedMessage);
|
|
|
|
+ } else if (topic === MQTT_GLOBAL_TOPIC) {
|
|
|
|
+ const parsedMessage = JSON.parse(message.toString());
|
|
|
|
+ if (parsedMessage.AllReady) {
|
|
|
|
+ emitter.emit('AllReady_TRUE');
|
|
|
|
+ } else {
|
|
|
|
+ emitter.emit('AllReady_FALSE');
|
|
|
|
+ }
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
};
|
|
@@ -73,5 +86,4 @@ const stopListening = () => {
|
|
mqttClient.end();
|
|
mqttClient.end();
|
|
console.log('Stopped listening to MQTT broker');
|
|
console.log('Stopped listening to MQTT broker');
|
|
};
|
|
};
|
|
-
|
|
|
|
export { mqttClient, handleMqttMessage, startListening, stopListening };
|
|
export { mqttClient, handleMqttMessage, startListening, stopListening };
|