Création du BuzzerWatcher et mise à jour des fichiers associés
This commit is contained in:
parent
7e2e417191
commit
8aeafd56d6
39
services/BuzzerWatcher.js
Normal file
39
services/BuzzerWatcher.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.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);
|
||||||
|
});
|
17
services/config/config.json
Normal file
17
services/config/config.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"hosts": {
|
||||||
|
"buzzers":{
|
||||||
|
"IP":{
|
||||||
|
"redBuzzerIP": "8.8.8.6",
|
||||||
|
"blueBuzzerIP": "8.8.8.8",
|
||||||
|
"greenBuzzerIP": "8.8.8.8",
|
||||||
|
"yellowBuzzerIP": "8.8.8.8"
|
||||||
|
},
|
||||||
|
"MQTTconfig":{
|
||||||
|
"mqttHost": "mqtt://192.168.1.78",
|
||||||
|
"mqttTopic": "buzzer/watcher"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user