(update) Mise à jour de Score-Manager.js pour ajouter le mecanisme de request des scores (utiles en cas de reload de page).
This commit is contained in:
@@ -114,6 +114,10 @@ function updateTeamTotalScore(teamColor, points) {
|
|||||||
|
|
||||||
// Mettre à jour le score
|
// Mettre à jour le score
|
||||||
jsonData.TEAM[teamColor].TotalScore += points;
|
jsonData.TEAM[teamColor].TotalScore += points;
|
||||||
|
|
||||||
|
// Update global state
|
||||||
|
global.jsonData = jsonData;
|
||||||
|
|
||||||
console.log(`Le score total pour l'équipe ${teamColor} est de ${jsonData.TEAM[teamColor].TotalScore} points !`)
|
console.log(`Le score total pour l'équipe ${teamColor} est de ${jsonData.TEAM[teamColor].TotalScore} points !`)
|
||||||
// Enregistrer les modifications dans le fichier
|
// Enregistrer les modifications dans le fichier
|
||||||
client.publish(mqttScoreTopic, JSON.stringify(jsonData));
|
client.publish(mqttScoreTopic, JSON.stringify(jsonData));
|
||||||
@@ -136,10 +140,10 @@ const configPath = path.join(__dirname, '../config/configuration.json');
|
|||||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||||
|
|
||||||
// Extraction des informations de config
|
// Extraction des informations de config
|
||||||
const { mqttHost, services: { score: { MQTTconfig: { mqttScoreTopic, mqttScoreChangeTopic } } } } = config;
|
const { mqttHost, services: { score: { MQTTconfig: { mqttScoreTopic, mqttScoreChangeTopic, mqttScoreRequestTopic } } } } = config;
|
||||||
console.log("DEBUG: Config loaded from:", configPath);
|
console.log("DEBUG: Config loaded from:", configPath);
|
||||||
console.log("DEBUG: MQTT Host:", mqttHost);
|
console.log("DEBUG: MQTT Host:", mqttHost);
|
||||||
console.log("DEBUG: Topics:", mqttScoreTopic, mqttScoreChangeTopic);
|
console.log("DEBUG: Topics:", mqttScoreTopic, mqttScoreChangeTopic, mqttScoreRequestTopic);
|
||||||
|
|
||||||
// Connexion au broker MQTT
|
// Connexion au broker MQTT
|
||||||
const client = mqtt.connect(mqttHost);
|
const client = mqtt.connect(mqttHost);
|
||||||
@@ -152,10 +156,27 @@ client.on('connect', () => {
|
|||||||
else console.log(`[INFO] Souscription réalisée avec succès au topic ${mqttScoreChangeTopic}]`);
|
else console.log(`[INFO] Souscription réalisée avec succès au topic ${mqttScoreChangeTopic}]`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.subscribe(mqttScoreRequestTopic, (err) => {
|
||||||
|
if (err) console.error('[ERROR] impossible de souscrire au topic de demande de score');
|
||||||
|
else console.log(`[INFO] Souscription réalisée avec succès au topic ${mqttScoreRequestTopic}]`);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Gestion des messages entrants
|
// Gestion des messages entrants
|
||||||
client.on('message', (topic, message) => {
|
client.on('message', (topic, message) => {
|
||||||
|
// Gestion de la demande de score (REFRESH)
|
||||||
|
if (topic === mqttScoreRequestTopic) {
|
||||||
|
console.log(`[INFO] Demande de rafraîchissement des scores reçue sur ${topic}`);
|
||||||
|
if (global.jsonData) {
|
||||||
|
client.publish(mqttScoreTopic, JSON.stringify(global.jsonData));
|
||||||
|
console.log(`[INFO] Scores envoyés sur ${mqttScoreTopic}`);
|
||||||
|
} else {
|
||||||
|
console.warn("[WARN] Aucune donnée de score disponible pour le rafraîchissement.");
|
||||||
|
}
|
||||||
|
return; // Fin du traitement pour ce message
|
||||||
|
}
|
||||||
|
|
||||||
let payload;
|
let payload;
|
||||||
let process;
|
let process;
|
||||||
let Team;
|
let Team;
|
||||||
@@ -232,6 +253,9 @@ function updateTeamScoreAbsolute(teamColor, totalScore, roundScore) {
|
|||||||
|
|
||||||
console.log(`Mise à jour absolue pour ${teamColor} -> Total: ${jsonData.TEAM[teamColor].TotalScore}, Round: ${jsonData.TEAM[teamColor].RoundScore}`);
|
console.log(`Mise à jour absolue pour ${teamColor} -> Total: ${jsonData.TEAM[teamColor].TotalScore}, Round: ${jsonData.TEAM[teamColor].RoundScore}`);
|
||||||
|
|
||||||
|
// Update global state
|
||||||
|
global.jsonData = jsonData;
|
||||||
|
|
||||||
client.publish(mqttScoreTopic, JSON.stringify(jsonData));
|
client.publish(mqttScoreTopic, JSON.stringify(jsonData));
|
||||||
fs.writeFile(filePath, JSON.stringify(jsonData, null, 2), (err) => {
|
fs.writeFile(filePath, JSON.stringify(jsonData, null, 2), (err) => {
|
||||||
if (err) console.error("Erreur d'écriture :", err);
|
if (err) console.error("Erreur d'écriture :", err);
|
||||||
|
|||||||
Reference in New Issue
Block a user