From debec4b609ceb56c848a3395a04f261729d0a4fb Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 3 Oct 2024 18:20:52 +0000 Subject: [PATCH] 2eme iteration --- services/buzzer-manager.js | 54 +++++++++++++++++++++++++++------ services/test-buzzer-manager.js | 43 ++++++++++++++++++-------- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/services/buzzer-manager.js b/services/buzzer-manager.js index 0ae22b5..be2acfb 100644 --- a/services/buzzer-manager.js +++ b/services/buzzer-manager.js @@ -57,6 +57,20 @@ function validateBuzzerPayload(payload) { return true; } +// Send updated tilt status +function sendTiltStatus(action, buzzerId) { + const tiltList = Array.from(tiltBuzzers); // Convert Set to Array + + client.publish('brainblast/buzzer/status', JSON.stringify({ + status: "tilt_update", + tilt_buzzers: tiltList, + message: `Buzzer ID ${buzzerId} ${action} to tilt mode`, + timestamp: new Date().toISOString() + })); + + console.log(`[INFO] Tilt status updated: ${tiltList.length} buzzers in tilt mode`); +} + // Handle incoming messages client.on('message', (topic, message) => { let payload; @@ -78,6 +92,7 @@ client.on('message', (topic, message) => { return; } + // Update tilt status based on the command if (status === 'add') { tiltBuzzers.add(buzzer_id); console.log(`[INFO] Buzzer ID ${buzzer_id} added to tilt mode`); @@ -85,6 +100,19 @@ client.on('message', (topic, message) => { tiltBuzzers.delete(buzzer_id); console.log(`[INFO] Buzzer ID ${buzzer_id} removed from tilt mode`); } + + // Confirm that the tilt command has been received + client.publish(`brainblast/buzzer/tilt/confirmation/${buzzer_id}`, JSON.stringify({ + status: "received", + action: status, + buzzer_id: buzzer_id, + message: `Tilt command '${status}' received for buzzer ID ${buzzer_id}`, + timestamp: new Date().toISOString() + })); + + // Send the updated tilt status to all components + sendTiltStatus(status === 'add' ? 'added' : 'removed', buzzer_id); + return; } @@ -99,9 +127,25 @@ client.on('message', (topic, message) => { const buzzerId = payload.buzzer_id; // Unique buzzer ID const color = payload.color; // Associated hex color - // Ignore if the buzzer is in tilt mode + // Always send a confirmation, even if the buzzer is in tilt mode + client.publish(`brainblast/buzzer/confirmation/${buzzerId}`, JSON.stringify({ + status: "received", + buzzer_id: buzzerId, + message: `Buzzer ID ${buzzerId} received (Color: ${color})`, + timestamp: new Date().toISOString() + })); + + // Ignore if the buzzer is in tilt mode, but notify this event if (tiltBuzzers.has(buzzerId)) { console.log(`[INFO] Buzzer ID ${buzzerId} ignored (Tilt mode active)`); + + // Notify that the buzzer is in tilt mode and ignored + client.publish(`brainblast/buzzer/tilt/ignored/${buzzerId}`, JSON.stringify({ + status: "tilt_ignored", + buzzer_id: buzzerId, + message: `Buzzer ID ${buzzerId} is in tilt mode and ignored.`, + timestamp: new Date().toISOString() + })); return; } @@ -114,14 +158,6 @@ client.on('message', (topic, message) => { timestamp: new Date().toISOString() })); - // Confirm buzzer press receipt - client.publish(`brainblast/buzzer/confirmation/${buzzerId}`, JSON.stringify({ - status: "received", - buzzer_id: buzzerId, - message: `Buzzer ID ${buzzerId} activated with color ${color}`, - timestamp: new Date().toISOString() - })); - if (!buzzerActive) { // Block further buzzers and record the first pressed ID buzzerActive = true; diff --git a/services/test-buzzer-manager.js b/services/test-buzzer-manager.js index 23cdf01..e47b1bd 100644 --- a/services/test-buzzer-manager.js +++ b/services/test-buzzer-manager.js @@ -17,10 +17,12 @@ let testResults = { confirmationReceived: false, statusBlocked: false, statusUnblocked: false, - tiltAdded: false, + tiltAddConfirmed: false, + tiltRemoveConfirmed: false, tiltIgnored: false, - tiltRemoved: false, - unlockConfirmation: false + unlockConfirmation: false, + tiltUpdateAdd: false, + tiltUpdateRemove: false }; // Subscribe to topics to capture the responses from the buzzer manager @@ -59,16 +61,32 @@ client.on('message', (topic, message) => { testResults.statusUnblocked = true; } - if (topic === 'brainblast/buzzer/unlock/confirmation' && payload.status === "received") { - testResults.unlockConfirmation = true; + if (topic.startsWith(`brainblast/buzzer/tilt/confirmation/2`) && payload.status === "received" && payload.action === "add") { + testResults.tiltAddConfirmed = true; } - if (topic === 'brainblast/buzzer/activity' && payload.buzzer_id === 2 && payload.status === "blocked") { + if (topic.startsWith(`brainblast/buzzer/tilt/confirmation/2`) && payload.status === "received" && payload.action === "remove") { + testResults.tiltRemoveConfirmed = true; + } + + if (topic === `brainblast/buzzer/tilt/ignored/2` && payload.status === "tilt_ignored") { testResults.tiltIgnored = true; } - if (topic.startsWith('brainblast/buzzer/status') && payload.message.includes('Tilt removed')) { - testResults.tiltRemoved = true; + if (topic === 'brainblast/buzzer/status' && payload.status === "tilt_update") { + // Check for tilt update with added buzzer + if (payload.tilt_buzzers.includes(2) && payload.message.includes("added")) { + testResults.tiltUpdateAdd = true; + } + + // Check for tilt update with removed buzzer + if (!payload.tilt_buzzers.includes(2) && payload.message.includes("removed")) { + testResults.tiltUpdateRemove = true; + } + } + + if (topic === 'brainblast/buzzer/unlock/confirmation' && payload.status === "received") { + testResults.unlockConfirmation = true; } }); @@ -99,7 +117,6 @@ function runTestSequence() { buzzer_id: 2, status: "add" })); - testResults.tiltAdded = true; }, 1500); // 4. Simulate pressing a buzzer in tilt mode (should be ignored) @@ -133,10 +150,12 @@ function runTestSequence() { console.log(`2. Confirmation received for buzzer 1: ${testResults.confirmationReceived ? 'PASSED' : 'FAILED'}`); console.log(`3. Buzzer 1 status set to "blocked": ${testResults.statusBlocked ? 'PASSED' : 'FAILED'}`); console.log(`4. Buzzer status set to "unblocked": ${testResults.statusUnblocked ? 'PASSED' : 'FAILED'}`); - console.log(`5. Tilt mode added for buzzer 2: ${testResults.tiltAdded ? 'PASSED' : 'FAILED'}`); + console.log(`5. Tilt mode add confirmed for buzzer 2: ${testResults.tiltAddConfirmed ? 'PASSED' : 'FAILED'}`); console.log(`6. Tilted buzzer press ignored: ${testResults.tiltIgnored ? 'PASSED' : 'FAILED'}`); - console.log(`7. Tilt mode removed for buzzer 2: ${testResults.tiltRemoved ? 'PASSED' : 'FAILED'}`); - console.log(`8. Unlock confirmation received: ${testResults.unlockConfirmation ? 'PASSED' : 'FAILED'}`); + console.log(`7. Tilt status update sent (add): ${testResults.tiltUpdateAdd ? 'PASSED' : 'FAILED'}`); + console.log(`8. Tilt mode remove confirmed for buzzer 2: ${testResults.tiltRemoveConfirmed ? 'PASSED' : 'FAILED'}`); + console.log(`9. Tilt status update sent (remove): ${testResults.tiltUpdateRemove ? 'PASSED' : 'FAILED'}`); + console.log(`10. Unlock confirmation received: ${testResults.unlockConfirmation ? 'PASSED' : 'FAILED'}`); client.end(); // End the MQTT connection }, 4000); }