WellNuo/mqtt-test.js
Sergei 671374da9a Improve BLE WiFi error handling and logging
- setWiFi() now throws detailed errors instead of returning false
- Shows specific error messages: "WiFi credentials rejected", timeout etc.
- Added logging throughout BLE WiFi configuration flow
- Fixed WiFi network deduplication (keeps strongest signal)
- Ignore "Operation cancelled" error (normal cleanup behavior)
- BatchSetupProgress shows actual error in hint field

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-26 19:10:45 -08:00

104 lines
2.7 KiB
JavaScript

#!/usr/bin/env node
/**
* WellNuo MQTT Alert Monitor
*
* Usage:
* node mqtt-test.js # Monitor deployment 21 (Ferdinand)
* node mqtt-test.js 42 # Monitor specific deployment
* node mqtt-test.js send "text" # Send test alert
*/
const mqtt = require('mqtt');
// Configuration
const MQTT_BROKER = 'mqtt://mqtt.eluxnetworks.net:1883';
const MQTT_USER = 'anandk';
const MQTT_PASSWORD = 'anandk_8';
const DEFAULT_DEPLOYMENT = 21; // Ferdinand
// Parse args
const args = process.argv.slice(2);
const isSendMode = args[0] === 'send';
const deploymentId = isSendMode ? DEFAULT_DEPLOYMENT : (parseInt(args[0]) || DEFAULT_DEPLOYMENT);
const topic = `/well_${deploymentId}`;
console.log(`🔌 Connecting to ${MQTT_BROKER}...`);
const client = mqtt.connect(MQTT_BROKER, {
username: MQTT_USER,
password: MQTT_PASSWORD,
clientId: `wellnuo-monitor-${Date.now()}`,
});
client.on('connect', () => {
console.log(`✅ Connected to MQTT broker`);
console.log(`📡 Topic: ${topic}`);
if (isSendMode) {
// Send mode: publish test message and exit
const message = args.slice(1).join(' ') || 'Test alert from Node.js';
const payload = JSON.stringify({
Command: 'REPORT',
body: message,
time: Math.floor(Date.now() / 1000),
});
console.log(`📤 Sending: ${payload}`);
client.publish(topic, payload, (err) => {
if (err) {
console.error('❌ Publish error:', err);
} else {
console.log('✅ Message sent successfully');
}
client.end();
});
} else {
// Monitor mode: subscribe and listen
console.log(`👂 Listening for messages... (Ctrl+C to stop)\n`);
client.subscribe(topic, (err) => {
if (err) {
console.error('❌ Subscribe error:', err);
process.exit(1);
}
});
}
});
client.on('message', (receivedTopic, payload) => {
const timestamp = new Date().toISOString();
const message = payload.toString();
console.log(`\n📨 [${timestamp}]`);
console.log(` Topic: ${receivedTopic}`);
try {
const parsed = JSON.parse(message);
console.log(` Command: ${parsed.Command || 'N/A'}`);
console.log(` Body: ${parsed.body || 'N/A'}`);
console.log(` Time: ${parsed.time ? new Date(parsed.time * 1000).toISOString() : 'N/A'}`);
// Special handling for different commands
if (parsed.Command === 'REPORT') {
console.log(` 🚨 ALERT: ${parsed.body}`);
}
} catch (e) {
console.log(` Raw: ${message}`);
}
});
client.on('error', (err) => {
console.error('❌ MQTT Error:', err.message);
});
client.on('close', () => {
console.log('🔌 Connection closed');
});
// Handle Ctrl+C
process.on('SIGINT', () => {
console.log('\n👋 Shutting down...');
client.end();
process.exit(0);
});