-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·213 lines (182 loc) · 6.19 KB
/
Copy pathindex.js
File metadata and controls
executable file
·213 lines (182 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
const pkg = require('./package.json');
function verifyDependencies() {
const dependencies = Object.keys(pkg.dependencies || {});
const missing = [];
for (const dep of dependencies) {
try {
require.resolve(dep);
} catch (error) {
missing.push(dep);
}
}
if (missing.length > 0) {
console.error('Missing required npm packages: ' + missing.join(', '));
console.error('Run `npm install` in this project directory, then start the app again.');
process.exit(1);
}
}
verifyDependencies();
const UDPServer = require('./lib/UDPServer');
const Logger = require('./lib/Logger');
// Display startup banner
console.log('');
console.log('================================================================');
console.log(' UDPLogCollector v' + pkg.version);
console.log('================================================================');
console.log('');
console.log(' Universal UDP receiver and bridge for amateur radio');
console.log(' logging applications (WSJT-X, N1MM Logger+)');
console.log('');
console.log(' Author: ' + pkg.author);
console.log(' License: ' + pkg.license);
console.log('================================================================');
console.log('');
// Parse command line arguments
const args = process.argv.slice(2);
let port = 2237; // Default port
let adifPath = null;
let mqttConfig = {};
let wavelogConfig = {};
let logLevel = 'NONE'; // Default log level (only success messages)
// Helper function to parse command line arguments
function parseArgument(args, i, argName) {
const value = args[i + 1];
if (!value) {
console.error(`Error: ${argName} requires a value`);
process.exit(1);
}
return value;
}
// Parse named arguments
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--help' || arg === '-h') {
console.log(`Usage: node index.js [options]
Options:
--port <number> UDP port to listen on (default: 2237)
--adif <path> Path to ADIF log file (optional)
--mqtt-broker <url> MQTT broker URL (mqtt://host:port or mqtts://host:port)
--mqtt-topic <topic> MQTT topic (default: qso/log)
--mqtt-username <user> MQTT username (optional)
--mqtt-password <pass> MQTT password (optional)
--wavelog-url <url> Wavelog instance URL (e.g., https://log.example.com)
--wavelog-token <token> Wavelog API token
--wavelog-stationid <id> Wavelog station profile ID
--log-level <level> Log level: NONE, ERROR, WARN, INFO, DEBUG, TRACE (default: NONE)
-h, --help Show this help message
Examples:
node index.js
node index.js --port 2237 --adif ./logs/qso.adi
node index.js --mqtt-broker mqtt://broker.hivemq.com:1883 --mqtt-topic ham/qso
node index.js --mqtt-broker mqtts://broker.example.com:8883 --mqtt-username user --mqtt-password pass
node index.js --wavelog-url https://log.example.com --wavelog-token YOUR_API_KEY --wavelog-stationid 1
node index.js --port 2237 --adif ./logs/qso.adi --mqtt-broker mqtt://localhost:1883 --mqtt-topic qso/log
`);
process.exit(0);
}
if (arg === '--port' || arg === '-p') {
const portValue = parseArgument(args, i, '--port');
const parsedPort = parseInt(portValue, 10);
if (isNaN(parsedPort) || parsedPort < 1 || parsedPort > 65535) {
console.error(`Error: Invalid port number "${portValue}"`);
process.exit(1);
}
port = parsedPort;
i++;
continue;
}
if (arg === '--adif' || arg === '-a') {
adifPath = parseArgument(args, i, '--adif');
i++;
continue;
}
if (arg === '--mqtt-broker') {
mqttConfig.broker = parseArgument(args, i, '--mqtt-broker');
i++;
continue;
}
if (arg === '--mqtt-topic') {
mqttConfig.topic = parseArgument(args, i, '--mqtt-topic');
i++;
continue;
}
if (arg === '--mqtt-username') {
mqttConfig.username = parseArgument(args, i, '--mqtt-username');
i++;
continue;
}
if (arg === '--mqtt-password') {
mqttConfig.password = parseArgument(args, i, '--mqtt-password');
i++;
continue;
}
if (arg === '--wavelog-url') {
wavelogConfig.url = parseArgument(args, i, '--wavelog-url');
i++;
continue;
}
if (arg === '--wavelog-token') {
wavelogConfig.token = parseArgument(args, i, '--wavelog-token');
i++;
continue;
}
if (arg === '--wavelog-stationid') {
wavelogConfig.stationId = parseArgument(args, i, '--wavelog-stationid');
i++;
continue;
}
if (arg === '--log-level') {
const levelValue = parseArgument(args, i, '--log-level');
const upperLevel = levelValue.toUpperCase();
const validLevels = ['NONE', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'];
if (!validLevels.includes(upperLevel)) {
console.error(`Error: Invalid log level "${levelValue}". Valid levels: ${validLevels.join(', ')}`);
process.exit(1);
}
logLevel = upperLevel;
i++;
continue;
}
// Unknown argument
console.error(`Error: Unknown option "${arg}"`);
console.log('Use --help for usage information');
process.exit(1);
}
// Validate MQTT config
const mqttEnabled = Object.keys(mqttConfig).length > 0;
if (mqttEnabled && !mqttConfig.broker) {
console.error('Error: --mqtt-broker is required when using MQTT options');
process.exit(1);
}
// Validate Wavelog config
const wavelogEnabled = Object.keys(wavelogConfig).length > 0;
if (wavelogEnabled) {
if (!wavelogConfig.url || !wavelogConfig.token || !wavelogConfig.stationId) {
console.error('Error: --wavelog-url, --wavelog-token, and --wavelog-stationid are all required when using Wavelog');
process.exit(1);
}
}
// Set log level
Logger.setLevel(logLevel);
const appLogger = new Logger('Main');
// Only show log level message if logging is enabled
if (Logger.currentLevel > Logger.LOG_LEVELS.NONE) {
appLogger.info(`Log level set to: ${Logger.getLevelName()}`);
}
// Start server (ADIFHandler is passed to WavelogClient)
const server = new UDPServer({
port,
host: 'localhost',
adifPath,
mqttConfig: mqttEnabled ? mqttConfig : null,
wavelogConfig: wavelogEnabled ? wavelogConfig : null
});
server.start();
// Graceful shutdown
process.on('SIGINT', () => {
appLogger.info('Shutdown signal received');
server.stop();
appLogger.info('Shutdown complete');
process.exit(0);
});