2024-02-17 20:01:52 +01:00
|
|
|
import mqtt from 'mqtt';
|
2024-02-17 20:14:14 +01:00
|
|
|
import config from '@/config.js';
|
2024-02-17 20:01:52 +01:00
|
|
|
|
2024-02-17 20:14:14 +01:00
|
|
|
const mqttBrokerUrl = config.mqttBrokerUrl;
|
2024-02-17 20:01:52 +01:00
|
|
|
// Créer une instance de client MQTT
|
2024-02-17 20:14:14 +01:00
|
|
|
const client = mqtt.connect(mqttBrokerUrl);
|
2024-02-17 20:01:52 +01:00
|
|
|
|
|
|
|
// Fonction pour publier un message sur un topic MQTT
|
|
|
|
export function publishMessage(topic, message) {
|
|
|
|
client.publish(topic, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fonction pour s'abonner à un topic MQTT et écouter les messages entrants
|
|
|
|
export function subscribeToTopic(topic, callback) {
|
|
|
|
client.subscribe(topic);
|
|
|
|
client.on('message', (receivedTopic, message) => {
|
|
|
|
callback(receivedTopic.toString(), message.toString());
|
|
|
|
});
|
|
|
|
}
|