test light manager
This commit is contained in:
parent
ff1b546f0c
commit
561b8b9820
138
services/light-manager.js
Normal file
138
services/light-manager.js
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
// Import du module MQTT
|
||||||
|
const mqtt = require('mqtt');
|
||||||
|
|
||||||
|
// Configuration du broker MQTT et de WLED
|
||||||
|
const brokerUrl = 'mqtt://localhost'; // Change ce lien si nécessaire
|
||||||
|
const clientId = 'light_manager_wled';
|
||||||
|
const wledTopicBase = 'wled/all'; // Le topic de base pour ton ruban WLED
|
||||||
|
const options = {
|
||||||
|
clientId,
|
||||||
|
clean: true
|
||||||
|
};
|
||||||
|
|
||||||
|
// État des lumières
|
||||||
|
let currentColor = '#FFFFFF'; // Couleur par défaut (blanc)
|
||||||
|
let currentEffect = 'none'; // Pas d'effet par défaut
|
||||||
|
|
||||||
|
// Connexion au broker MQTT
|
||||||
|
const client = mqtt.connect(brokerUrl, options);
|
||||||
|
|
||||||
|
client.on('connect', () => {
|
||||||
|
console.log(`[INFO] Connected to MQTT broker ${brokerUrl} as ${clientId}`);
|
||||||
|
|
||||||
|
// Souscription aux topics de gestion de lumière
|
||||||
|
client.subscribe('brainblast/light/#', (err) => {
|
||||||
|
if (err) console.error('[ERROR] Subscription to light topics failed');
|
||||||
|
else console.log('[INFO] Successfully subscribed to light topics');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fonction pour envoyer un message au ruban WLED
|
||||||
|
function sendToWLED(topicSuffix, message) {
|
||||||
|
const wledTopic = `${wledTopicBase}/${topicSuffix}`;
|
||||||
|
client.publish(wledTopic, message, { qos: 1 }, (err) => {
|
||||||
|
if (err) console.error(`[ERROR] Failed to send message to WLED topic: ${wledTopic}`);
|
||||||
|
else console.log(`[INFO] Sent to WLED (${wledTopic}): ${message}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour appliquer un changement de lumière
|
||||||
|
function applyLightChange(color, effect, intensity) {
|
||||||
|
currentColor = color || currentColor;
|
||||||
|
currentEffect = effect || currentEffect;
|
||||||
|
|
||||||
|
console.log(`[INFO] Applying light change: Color=${currentColor}, Effect=${currentEffect}, Intensity=${intensity}`);
|
||||||
|
|
||||||
|
// Envoyer la couleur au ruban WLED
|
||||||
|
sendToWLED('col', currentColor);
|
||||||
|
|
||||||
|
// Appliquer l'effet si défini
|
||||||
|
if (currentEffect !== 'none') {
|
||||||
|
const effectId = getWLEDEffectId(currentEffect);
|
||||||
|
sendToWLED('api', "FX=" + effectId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Régler l'intensité si spécifiée
|
||||||
|
if (intensity) {
|
||||||
|
sendToWLED('api', intensity.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Envoi de l'état mis à jour
|
||||||
|
sendLightStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour obtenir l'ID d'effet WLED correspondant à un effet donné
|
||||||
|
function getWLEDEffectId(effect) {
|
||||||
|
const effectsMap = {
|
||||||
|
'none': 0,
|
||||||
|
'blink': 1, // Effet de fondu
|
||||||
|
'fade': 12, // Clignotement
|
||||||
|
'rainbow': 9 // Effet arc-en-ciel
|
||||||
|
};
|
||||||
|
return effectsMap[effect] || 0; // Par défaut, aucun effet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour envoyer l'état actuel des lumières sur le topic de réponse
|
||||||
|
function sendLightStatus() {
|
||||||
|
const statusMessage = JSON.stringify({
|
||||||
|
status: "updated",
|
||||||
|
color: currentColor,
|
||||||
|
effect: currentEffect,
|
||||||
|
message: `Current light state: Color=${currentColor}, Effect=${currentEffect}`,
|
||||||
|
timestamp: new Date().toISOString()
|
||||||
|
});
|
||||||
|
|
||||||
|
// Envoi de l'état à tous les clients intéressés
|
||||||
|
client.publish('brainblast/light/status/response', statusMessage);
|
||||||
|
|
||||||
|
console.log('[INFO] Light status sent to brainblast/light/status/response');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gestion des messages entrants
|
||||||
|
client.on('message', (topic, message) => {
|
||||||
|
let payload;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Analyse du message reçu
|
||||||
|
payload = JSON.parse(message.toString());
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[ERROR] Invalid JSON message received on topic ${topic}: ${message.toString()}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Changement de lumière
|
||||||
|
if (topic === 'brainblast/light/change') {
|
||||||
|
const { color, effect, intensity } = payload;
|
||||||
|
|
||||||
|
// Valider la couleur et l'effet
|
||||||
|
if (!/^#[0-9A-F]{6}$/i.test(color)) {
|
||||||
|
console.error('[ERROR] Invalid color format');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Appliquer le changement de lumière
|
||||||
|
applyLightChange(color, effect, intensity);
|
||||||
|
|
||||||
|
} else if (topic === 'brainblast/light/reset') {
|
||||||
|
// Réinitialisation des lumières à la couleur et l'effet par défaut
|
||||||
|
console.log('[INFO] Resetting lights to default state');
|
||||||
|
applyLightChange('#FFFFFF', 'reset', 255);
|
||||||
|
|
||||||
|
} else if (topic === 'brainblast/light/status/request') {
|
||||||
|
// Répondre à la requête de statut
|
||||||
|
console.log('[INFO] Light status request received');
|
||||||
|
sendLightStatus();
|
||||||
|
} else if (topic === 'brainblast/light/status/response') {
|
||||||
|
// Répondre à la requête de statut
|
||||||
|
console.log('[INFO] Light status response received');
|
||||||
|
} else {
|
||||||
|
console.error(`[ERROR] Unrecognized topic: ${topic}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Gestion des erreurs de connexion
|
||||||
|
client.on('error', (err) => {
|
||||||
|
console.error(`[ERROR] Error connecting to broker: ${err}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('[INFO] Light Manager with WLED support and status handling started...');
|
Loading…
Reference in New Issue
Block a user