chenyc
2026-04-21 8632fbd73fdb15f22fae9cd36b9ed3e0635360f1
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env node
 
const fs = require('fs');
const path = require('path');
const { TcpService } = require('./tcp-service');
const { MqttService } = require('./mqtt-service');
const { AliyunService } = require('./aliyun-service');
const { MetricAggregator } = require('./metric-aggregator');
const { getDefaultConfigPath } = require('./runtime-paths');
 
const LOG_LEVELS = {
  debug: 10,
  info: 20,
  warn: 30,
  error: 40,
};
 
function formatLocalTimestamp(date = new Date()) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hour = String(date.getHours()).padStart(2, '0');
  const minute = String(date.getMinutes()).padStart(2, '0');
  const second = String(date.getSeconds()).padStart(2, '0');
  const millisecond = String(date.getMilliseconds()).padStart(3, '0');
 
  return `${year}-${month}-${day} ${hour}:${minute}:${second}.${millisecond}`;
}
 
function normalizeLogLevel(level) {
  const normalized = String(level || 'info').trim().toLowerCase();
  return LOG_LEVELS[normalized] ? normalized : 'info';
}
 
function createLogger(loggingConfig = {}) {
  const settings = {
    enabled: loggingConfig.enabled !== false,
    console: loggingConfig.console !== false,
    dir: loggingConfig.dir || path.join(process.cwd(), 'logs'),
    filePrefix: loggingConfig.filePrefix || 'jhm-service',
    level: normalizeLogLevel(loggingConfig.level),
  };
  let currentDate = '';
  let currentFilePath = '';
 
  function getDatePart(date = new Date()) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}${month}${day}`;
  }
 
  function ensureLogFilePath() {
    if (!settings.enabled) {
      return null;
    }
 
    const today = getDatePart();
 
    if (currentFilePath && currentDate === today) {
      return currentFilePath;
    }
 
    fs.mkdirSync(settings.dir, { recursive: true });
    currentDate = today;
    currentFilePath = path.join(settings.dir, `${settings.filePrefix}-${today}.log`);
 
    return currentFilePath;
  }
 
  function shouldWrite(level) {
    return LOG_LEVELS[level] >= LOG_LEVELS[settings.level];
  }
 
  function format(level, message) {
    return `[${formatLocalTimestamp()}] [${level.toUpperCase()}] ${message}`;
  }
 
  function write(level, message) {
    const normalizedLevel = normalizeLogLevel(level);
 
    if (!shouldWrite(normalizedLevel)) {
      return;
    }
 
    const line = format(normalizedLevel, message);
 
    if (settings.console) {
      if (normalizedLevel === 'error') {
        console.error(line);
      } else if (normalizedLevel === 'warn') {
        console.warn(line);
      } else {
        console.log(line);
      }
    }
 
    const logFilePath = ensureLogFilePath();
 
    if (logFilePath) {
      try {
        fs.appendFileSync(logFilePath, `${line}\n`, 'utf8');
      } catch (error) {
        console.error(`[LOGGER] Failed to append log file: ${error.message}`);
      }
    }
  }
 
  return {
    debug(message) {
      write('debug', message);
    },
    info(message) {
      write('info', message);
    },
    warn(message) {
      write('warn', message);
    },
    error(message) {
      write('error', message);
    },
    getLogFilePath() {
      ensureLogFilePath();
      return currentFilePath;
    },
    async close() {
      currentDate = '';
    },
  };
}
 
function parseArgs(argv) {
  const options = {
    configPath: getDefaultConfigPath(),
  };
 
  for (let index = 2; index < argv.length; index += 1) {
    const arg = argv[index];
    const value = argv[index + 1];
 
    if (arg === '--config' && value) {
      options.configPath = path.resolve(value);
      index += 1;
    } else if (arg === '--help' || arg === '-h') {
      options.help = true;
    }
  }
 
  return options;
}
 
function printHelp() {
  console.log(`JHM TCP Socket Gateway
 
Usage:
  node app.js
  node app.js --config ./config.json
  jhm-service.exe --config .\\runtime\\config.json
  ./jhm-service --config ./runtime/config.json
 
Options:
  --config <path>    set config file path
  --help, -h         show help
`);
}
 
function loadConfig(configPath) {
  if (!fs.existsSync(configPath)) {
    throw new Error(`config file not found: ${configPath}`);
  }
 
  const content = fs.readFileSync(configPath, 'utf8');
  const config = JSON.parse(content);
 
  validateConfig(config);
 
  return config;
}
 
function getSendChannels(config) {
  const channels = Array.isArray(config.send && config.send.channels)
    ? config.send.channels
    : ['mqtt'];
 
  return Array.from(new Set(channels
    .map((item) => String(item || '').trim().toLowerCase())
    .filter(Boolean)));
}
 
function getSendOptions(config) {
  const send = config.send || {};
  const mode = ['immediate', 'batch'].includes(String(send.mode || '').trim().toLowerCase())
    ? String(send.mode).trim().toLowerCase()
    : 'batch';
 
  return {
    mode,
    flushIntervalMs: Number(send.flushIntervalMs) > 0 ? Number(send.flushIntervalMs) : 60000,
    alignToMinute: send.alignToMinute !== false,
    includeDeviceIdField: send.includeDeviceIdField !== false,
    deviceIdField: send.deviceIdField || 'n',
    publishOnShutdown: send.publishOnShutdown !== false,
  };
}
 
function getBloodPressureOptions(config) {
  const bloodPressure = config.protocol && config.protocol.bloodPressure;
 
  return {
    publishTime: bloodPressure ? bloodPressure.publishTime !== false : true,
  };
}
 
function validateConfig(config) {
  if (!config.tcp || !Array.isArray(config.devices)) {
    throw new Error('config.json must include tcp and devices');
  }
 
  if (!config.tcp.host || !config.tcp.port) {
    throw new Error('config.json must include tcp.host and tcp.port');
  }
 
  const channels = getSendChannels(config);
 
  if (channels.length === 0) {
    throw new Error('config.json send.channels must enable at least one channel');
  }
 
  if (channels.includes('mqtt')) {
    if (!config.mqtt) {
      throw new Error('config.json enabled mqtt but missing mqtt config');
    }
 
    const hasBrokerUrl = Boolean(config.mqtt.brokerUrl);
    const hasHostMode = Boolean(config.mqtt.protocol && config.mqtt.host && config.mqtt.port);
 
    if (!hasBrokerUrl && !hasHostMode) {
      throw new Error('config.json mqtt requires brokerUrl or protocol/host/port');
    }
 
    if (!config.mqtt.topicTemplate && !config.mqtt.defaultTopicPrefix) {
      throw new Error('config.json mqtt requires topicTemplate or defaultTopicPrefix');
    }
  }
 
  if (channels.includes('aliyun')) {
    if (!config.aliyun) {
      throw new Error('config.json enabled aliyun but missing aliyun config');
    }
 
    if (!config.aliyun.tupleApiBaseUrl && !config.aliyun.tupleApiUrl) {
      throw new Error('config.json aliyun requires tupleApiBaseUrl or tupleApiUrl');
    }
  }
 
  if (!config.protocol || !config.protocol.alModelPath) {
    throw new Error('config.json protocol.alModelPath is required');
  }
 
  const sendOptions = getSendOptions(config);
 
  if (sendOptions.flushIntervalMs <= 0) {
    throw new Error('config.json send.flushIntervalMs must be > 0');
  }
 
  if (config.logging && config.logging.level) {
    const supportedLevels = ['debug', 'info', 'warn', 'error'];
 
    if (!supportedLevels.includes(String(config.logging.level).toLowerCase())) {
      throw new Error(`config.json logging.level only supports ${supportedLevels.join(', ')}`);
    }
  }
}
 
function resolveConfigPath(configFilePath, targetPath) {
  if (path.isAbsolute(targetPath)) {
    return targetPath;
  }
 
  return path.join(path.dirname(configFilePath), targetPath);
}
 
async function main() {
  const options = parseArgs(process.argv);
 
  if (options.help) {
    printHelp();
    return;
  }
 
  const bootstrapLogger = createLogger({
    enabled: false,
    console: true,
  });
  bootstrapLogger.info(`[APP] Startup arguments parsed config=${options.configPath}`);
 
  const config = loadConfig(options.configPath);
  const logDir = resolveConfigPath(options.configPath, (config.logging && config.logging.dir) || './logs');
  const logger = createLogger({
    ...(config.logging || {}),
    dir: logDir,
  });
  const sendChannels = getSendChannels(config);
  const sendOptions = getSendOptions(config);
  const bloodPressureOptions = getBloodPressureOptions(config);
  const alModelPath = resolveConfigPath(options.configPath, config.protocol.alModelPath);
 
  if (config.logging && config.logging.enabled === false) {
    logger.info('[APP] File logging is disabled; console output only');
  } else {
    logger.info(`[APP] Local logging enabled dir=${logDir} file=${logger.getLogFilePath()} level=${normalizeLogLevel(config.logging && config.logging.level)}`);
  }
 
  logger.info(`[APP] Config loaded tcp=${config.tcp.host}:${config.tcp.port} devices=${config.devices.length} channels=${sendChannels.join(',')} sendMode=${sendOptions.mode} flushIntervalMs=${sendOptions.flushIntervalMs} alModel=${alModelPath}`);
 
  const mqttService = sendChannels.includes('mqtt') ? new MqttService(config.mqtt, logger) : null;
  const aliyunService = sendChannels.includes('aliyun') ? new AliyunService(config.aliyun, logger) : null;
 
  const aggregator = new MetricAggregator({
    logger,
    ...sendOptions,
    onFlush: async (device, payload, meta) => {
      logger.info(`[APP] Batch dispatch deviceId=${device.deviceId} channels=${sendChannels.join(',')} reason=${meta.reason} payload=${JSON.stringify(payload)}`);
 
      let ok = true;
 
      if (mqttService) {
        try {
          mqttService.publish(device, payload);
        } catch (error) {
          ok = false;
          logger.error(`[APP] MQTT batch dispatch failed deviceId=${device.deviceId}: ${error.message}`);
        }
      }
 
      if (aliyunService) {
        try {
          const result = await aliyunService.publish(device, payload);
 
          if (result && result.skipped) {
            ok = false;
            logger.warn(`[APP] Aliyun batch dispatch skipped deviceId=${device.deviceId} reason=${result.reason}`);
          } else if (result && result.ok === false) {
            ok = false;
            logger.error(`[APP] Aliyun batch dispatch failed deviceId=${device.deviceId}: ${result.reason}`);
          }
        } catch (error) {
          ok = false;
          logger.error(`[APP] Aliyun batch dispatch exception deviceId=${device.deviceId}: ${error.message}`);
        }
      }
 
      return ok;
    },
  });
 
  const tcpService = new TcpService({
    tcpConfig: config.tcp,
    devices: config.devices,
    alModelPath,
    publishBloodPressureTime: bloodPressureOptions.publishTime,
    logger,
    onMetric(device, metric) {
      logger.info(`[APP] Metric cached deviceId=${device.deviceId} mode=${sendOptions.mode} metric=${JSON.stringify(metric)}`);
      aggregator.ingest(device, metric).catch((error) => {
        logger.error(`[APP] Metric cache failed deviceId=${device.deviceId}: ${error.message}`);
      });
    },
  });
 
  if (mqttService) {
    logger.info('[APP] Starting MQTT channel');
    mqttService.start();
  }
 
  if (aliyunService) {
    logger.info('[APP] Starting Aliyun channel');
    aliyunService.start();
  }
 
  aggregator.start();
  await tcpService.start();
  logger.info('[APP] All services started');
 
  let shuttingDown = false;
 
  async function shutdown(signal) {
    if (shuttingDown) {
      return;
    }
 
    shuttingDown = true;
    logger.warn(`[APP] Received ${signal}, shutting down`);
 
    await tcpService.stop();
    await aggregator.stop();
 
    if (mqttService) {
      await mqttService.stop();
    }
 
    if (aliyunService) {
      await aliyunService.stop();
    }
 
    logger.info('[APP] Service shutdown completed');
    await logger.close();
    process.exit(0);
  }
 
  process.on('SIGINT', () => {
    shutdown('SIGINT');
  });
 
  process.on('SIGTERM', () => {
    shutdown('SIGTERM');
  });
 
  process.on('uncaughtException', (error) => {
    logger.error(`[APP] Uncaught exception: ${error.stack || error.message}`);
  });
 
  process.on('unhandledRejection', (reason) => {
    logger.error(`[APP] Unhandled promise rejection: ${reason}`);
  });
}
 
if (require.main === module) {
  main().catch((error) => {
    console.error(error.message);
    process.exit(1);
  });
}
 
module.exports = {
  createLogger,
  formatLocalTimestamp,
  getBloodPressureOptions,
  getSendChannels,
  getSendOptions,
  loadConfig,
  main,
  normalizeLogLevel,
  parseArgs,
  printHelp,
  resolveConfigPath,
  validateConfig,
};