2024-11-11 14:28:21 +01:00
|
|
|
const ping = require('ping');
|
|
|
|
const mqtt = require('mqtt');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
// Lecture du fichier de configuration
|
2024-12-23 22:02:57 +01:00
|
|
|
const config = JSON.parse(fs.readFileSync('\services\\config\\config_network.json', 'utf8'));
|
2024-11-11 14:28:21 +01:00
|
|
|
|
|
|
|
// Extraction des informations de config
|
|
|
|
const { hosts: { buzzers: { IP: buzzerIPs, MQTTconfig: { mqttHost, mqttTopic } } } } = config;
|
|
|
|
|
|
|
|
// Connexion au broker MQTT
|
|
|
|
const client = mqtt.connect(mqttHost);
|
|
|
|
|
|
|
|
client.on('connect', () => {
|
|
|
|
console.log(`Connecté au broker MQTT à ${mqttHost}`);
|
|
|
|
|
|
|
|
// Fonction pour pinger les buzzers et publier l'état
|
|
|
|
const pingAndPublish = async () => {
|
|
|
|
for (const [buzzerName, ip] of Object.entries(buzzerIPs)) {
|
|
|
|
try {
|
|
|
|
const res = await ping.promise.probe(ip);
|
|
|
|
const status = res.alive ? 'online' : 'offline';
|
|
|
|
|
|
|
|
// Publication du statut dans le topic MQTT
|
|
|
|
client.publish(`${mqttTopic}`, JSON.stringify({ buzzer: buzzerName, ip, status }));
|
|
|
|
console.log(`Ping ${buzzerName} (${ip}) - Status: ${status}`);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(`Erreur avec le buzzer ${buzzerName} (${ip}):`, error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pinger toutes les 5 secondes
|
|
|
|
setInterval(pingAndPublish, 3000);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('error', (error) => {
|
|
|
|
console.error('Erreur de connexion au broker MQTT:', error.message);
|
|
|
|
});
|