- Remove hardcoded database credentials from all scripts - Remove hardcoded Legacy API tokens from backend scripts - Remove hardcoded MQTT credentials from mqtt-test.js - Update backend/.env.example with DB_HOST, DB_USER, DB_PASSWORD, DB_NAME - Update backend/.env.example with LEGACY_API_TOKEN and MQTT credentials - Add dotenv config to all scripts requiring credentials - Create comprehensive documentation: - scripts/README.md - Root scripts usage - backend/scripts/README.md - Backend scripts documentation - MQTT_TESTING.md - MQTT testing guide - SECURITY_CREDENTIALS_CLEANUP.md - Security changes summary All scripts now read credentials from backend/.env instead of hardcoded values. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
105 lines
2.9 KiB
JavaScript
105 lines
2.9 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');
|
|
require('dotenv').config({ path: './backend/.env' });
|
|
|
|
// Configuration
|
|
const MQTT_BROKER = process.env.MQTT_BROKER || 'mqtt://mqtt.eluxnetworks.net:1883';
|
|
const MQTT_USER = process.env.MQTT_USER;
|
|
const MQTT_PASSWORD = process.env.MQTT_PASSWORD;
|
|
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);
|
|
});
|