2eme iteration

This commit is contained in:
Laurent 2024-10-03 18:20:52 +00:00
parent fafacb6d3c
commit debec4b609
2 changed files with 76 additions and 21 deletions

View File

@ -57,6 +57,20 @@ function validateBuzzerPayload(payload) {
return true; 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 // Handle incoming messages
client.on('message', (topic, message) => { client.on('message', (topic, message) => {
let payload; let payload;
@ -78,6 +92,7 @@ client.on('message', (topic, message) => {
return; return;
} }
// Update tilt status based on the command
if (status === 'add') { if (status === 'add') {
tiltBuzzers.add(buzzer_id); tiltBuzzers.add(buzzer_id);
console.log(`[INFO] Buzzer ID ${buzzer_id} added to tilt mode`); console.log(`[INFO] Buzzer ID ${buzzer_id} added to tilt mode`);
@ -85,6 +100,19 @@ client.on('message', (topic, message) => {
tiltBuzzers.delete(buzzer_id); tiltBuzzers.delete(buzzer_id);
console.log(`[INFO] Buzzer ID ${buzzer_id} removed from tilt mode`); 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; return;
} }
@ -99,9 +127,25 @@ client.on('message', (topic, message) => {
const buzzerId = payload.buzzer_id; // Unique buzzer ID const buzzerId = payload.buzzer_id; // Unique buzzer ID
const color = payload.color; // Associated hex color 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)) { if (tiltBuzzers.has(buzzerId)) {
console.log(`[INFO] Buzzer ID ${buzzerId} ignored (Tilt mode active)`); 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; return;
} }
@ -114,14 +158,6 @@ client.on('message', (topic, message) => {
timestamp: new Date().toISOString() 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) { if (!buzzerActive) {
// Block further buzzers and record the first pressed ID // Block further buzzers and record the first pressed ID
buzzerActive = true; buzzerActive = true;

View File

@ -17,10 +17,12 @@ let testResults = {
confirmationReceived: false, confirmationReceived: false,
statusBlocked: false, statusBlocked: false,
statusUnblocked: false, statusUnblocked: false,
tiltAdded: false, tiltAddConfirmed: false,
tiltRemoveConfirmed: false,
tiltIgnored: false, tiltIgnored: false,
tiltRemoved: false, unlockConfirmation: false,
unlockConfirmation: false tiltUpdateAdd: false,
tiltUpdateRemove: false
}; };
// Subscribe to topics to capture the responses from the buzzer manager // Subscribe to topics to capture the responses from the buzzer manager
@ -59,16 +61,32 @@ client.on('message', (topic, message) => {
testResults.statusUnblocked = true; testResults.statusUnblocked = true;
} }
if (topic === 'brainblast/buzzer/unlock/confirmation' && payload.status === "received") { if (topic.startsWith(`brainblast/buzzer/tilt/confirmation/2`) && payload.status === "received" && payload.action === "add") {
testResults.unlockConfirmation = true; 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; testResults.tiltIgnored = true;
} }
if (topic.startsWith('brainblast/buzzer/status') && payload.message.includes('Tilt removed')) { if (topic === 'brainblast/buzzer/status' && payload.status === "tilt_update") {
testResults.tiltRemoved = true; // 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, buzzer_id: 2,
status: "add" status: "add"
})); }));
testResults.tiltAdded = true;
}, 1500); }, 1500);
// 4. Simulate pressing a buzzer in tilt mode (should be ignored) // 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(`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(`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(`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(`6. Tilted buzzer press ignored: ${testResults.tiltIgnored ? 'PASSED' : 'FAILED'}`);
console.log(`7. Tilt mode removed for buzzer 2: ${testResults.tiltRemoved ? 'PASSED' : 'FAILED'}`); console.log(`7. Tilt status update sent (add): ${testResults.tiltUpdateAdd ? 'PASSED' : 'FAILED'}`);
console.log(`8. Unlock confirmation received: ${testResults.unlockConfirmation ? '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 client.end(); // End the MQTT connection
}, 4000); }, 4000);
} }