Tracking de l'application VNode (moteur du jeu, services, broker.. etc
This commit is contained in:
39
VNode/services/buzzer/buzzer-watcher.js
Normal file
39
VNode/services/buzzer/buzzer-watcher.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const ping = require('ping');
|
||||
const mqtt = require('mqtt');
|
||||
const fs = require('fs');
|
||||
|
||||
// Lecture du fichier de configuration
|
||||
const config = JSON.parse(fs.readFileSync('\services\\config\\config_network.json', 'utf8'));
|
||||
|
||||
// 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);
|
||||
});
|
||||
Reference in New Issue
Block a user