东丽网口版透析机 socket- server 通讯
chenyc
2026-01-01 ce98f732b9e4f32154d39454213e1abf3dc07f5b
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// httpServer.js - HTTP 接口服务
const http = require('http');
const url = require('url');
const logger = require('./logger');
const dataCache = require('./dataCache');
const rateLimiter = require('./rateLimiter');
const propertyMapper = require('./propertyMapper');
 
class HttpServer {
    constructor(port = 8080, config = {}) {
        this.port = port;
        this.config = {
            enabled: config.enabled !== false,
            host: config.host || '0.0.0.0',
            cors: {
                enabled: config.cors?.enabled !== false,
                allowOrigin: config.cors?.allowOrigin || '*'
            },
            rateLimit: {
                enabled: config.rateLimit?.enabled !== false,
                interval: config.rateLimit?.interval || 5000, // 默认 5 秒
                allDevicesInterval: config.rateLimit?.allDevicesInterval || 60000 // 默认 1 分钟
            },
            propertyMapping: {
                enabled: config.propertyMapping?.enabled !== false,
                includeRawData: config.propertyMapping?.includeRawData === true
            }
        };
        this.server = null;
    }
 
    /**
     * 启动 HTTP 服务
     */
    start() {
        if (!this.config.enabled) {
            logger.info('🔴 HTTP 服务已禁用');
            return;
        }
 
        this.server = http.createServer((req, res) => {
            this.handleRequest(req, res);
        });
 
        this.server.on('error', (err) => {
            logger.error(`HTTP 服务错误: ${err.message}`);
        });
 
        this.server.listen(this.port, this.config.host, () => {
            logger.info(`🌐 HTTP 服务已启动,监听地址: ${this.config.host}:${this.port}`);
        });
    }
 
    /**
     * 处理 HTTP 请求
     */
    handleRequest(req, res) {
        const parsedUrl = url.parse(req.url, true);
        const pathname = parsedUrl.pathname;
        const query = parsedUrl.query;
 
        // 设置通用响应头
        res.setHeader('Content-Type', 'application/json; charset=utf-8');
        
        // 根据配置设置 CORS 头
        if (this.config.cors.enabled) {
            res.setHeader('Access-Control-Allow-Origin', this.config.cors.allowOrigin);
            res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
            res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
        }
 
        // 处理 OPTIONS 请求(CORS 预检)
        if (req.method === 'OPTIONS') {
            res.writeHead(200);
            res.end();
            return;
        }
 
        try {
            logger.info(`📥 收到请求: ${req.method} ${pathname}${req.url.includes('?') ? '?' + req.url.split('?')[1] : ''}`);
 
            switch (pathname) {
                // 获取指定设备的最新数据
                case '/api/device/data':
                    this.handleGetDeviceData(req, res, query);
                    break;
 
                // 获取所有设备的数据
                case '/api/device/all':
                    this.handleGetAllDevices(req, res, query);
                    break;
 
                // 获取设备列表
                case '/api/device/list':
                    this.handleGetDeviceList(req, res);
                    break;
 
                // 获取缓存统计信息
                case '/api/cache/stats':
                    this.handleGetStats(req, res);
                    break;
 
                // 清空缓存
                case '/api/cache/clear':
                    this.handleClearCache(req, res);
                    break;
 
                // 获取限流统计信息
                case '/api/ratelimit/stats':
                    this.handleRateLimitStats(req, res);
                    break;
 
                // 清空限流记录
                case '/api/ratelimit/clear':
                    this.handleRateLimitClear(req, res);
                    break;
 
                // 获取空闲设备列表
                case '/api/device/idle':
                    this.handleGetIdleDevices(req, res, query);
                    break;
 
                // 健康检查
                case '/api/health':
                    this.handleHealth(req, res);
                    break;
 
                // 根路径
                case '/':
                    this.handleRoot(req, res);
                    break;
 
                default:
                    this.sendError(res, 404, '接口不存在');
            }
        } catch (err) {
            logger.error(`请求处理错误: ${err.message}`);
            this.sendError(res, 500, '服务器内部错误');
        }
    }
 
    /**
     * 获取指定设备的最新数据
     * GET /api/device/data?deviceNumber=D001
     * GET /api/device/data?deviceNumber=D001&mapped=true  (启用属性映射)
     */
    handleGetDeviceData(req, res, query) {
        const deviceNumber = query.deviceNumber;
        const enableMapping = query.mapped === 'true'; // 支持查询参数控制映射
 
        if (!deviceNumber) {
            this.sendError(res, 400, '缺少必要参数: deviceNumber');
            return;
        }
 
        // ✅【新增】限流检查 - 根据设备号进行限流
        if (this.config.rateLimit.enabled) {
            const limitCheck = rateLimiter.checkLimit(deviceNumber, this.config.rateLimit.interval);
            if (!limitCheck.allowed) {
                this.sendError(res, 429, `请求过于频繁,请在 ${limitCheck.remainingTime}ms 后再试`);
                return;
            }
        }
 
        const data = dataCache.getDeviceData(deviceNumber);
 
        if (!data) {
            this.sendError(res, 404, `设备 ${deviceNumber} 未找到`);
            return;
        }
 
        // ✅【新增】属性映射处理
        let responseData = {
            deviceNumber: deviceNumber,
            timestamp: data.timestamp || new Date().toISOString(),
            data: data
        };
 
        // 如果启用了映射,或配置中默认启用了映射
        const useMappedFormat = enableMapping || this.config.propertyMapping.enabled;
        if (useMappedFormat) {
            try {
                const mappedData = propertyMapper.transformForHTTP(data, deviceNumber);
                responseData = {
                    deviceNumber: deviceNumber,
                    timestamp: data.timestamp || new Date().toISOString(),
                    properties: mappedData, // [{identifier, name, value}, ...]
                    format: 'mapped'
                };
 
                // 如果配置允许,也包含原始数据
                if (this.config.propertyMapping.includeRawData) {
                    responseData.rawData = data;
                }
            } catch (err) {
                logger.error(`属性映射失败: ${err.message}`);
                // 映射失败时回退到原始格式
                responseData.format = 'raw';
                responseData.mappingError = err.message;
            }
        }
 
        this.sendSuccess(res, responseData);
    }
 
    /**
     * 获取所有设备的数据
     * GET /api/device/all
     * GET /api/device/all?mapped=true  (启用属性映射)
     */
    handleGetAllDevices(req, res, query) {
        const enableMapping = query?.mapped === 'true'; // 支持查询参数控制映射
 
        // ✅【新增】限流检查 - 所有设备使用统一标识符,间隔 1 分钟
        if (this.config.rateLimit.enabled) {
            const limitCheck = rateLimiter.checkLimit('__all_devices__', this.config.rateLimit.allDevicesInterval);
            if (!limitCheck.allowed) {
                this.sendError(res, 429, `请求过于频繁,请在 ${limitCheck.remainingTime}ms 后再试`);
                return;
            }
        }
 
        const allData = dataCache.getAllDeviceData();
        
        if (Object.keys(allData).length === 0) {
            this.sendSuccess(res, {
                message: '暂无设备数据',
                count: 0,
                data: {},
                format: 'raw'
            });
            return;
        }
 
        // ✅【新增】属性映射处理
        const useMappedFormat = enableMapping || this.config.propertyMapping.enabled;
        
        if (useMappedFormat) {
            try {
                const mappedDevices = {};
                for (const [deviceNumber, rawData] of Object.entries(allData)) {
                    mappedDevices[deviceNumber] = {
                        timestamp: rawData.timestamp || new Date().toISOString(),
                        properties: propertyMapper.transformForHTTP(rawData, deviceNumber),
                        format: 'mapped'
                    };
                    
                    if (this.config.propertyMapping.includeRawData) {
                        mappedDevices[deviceNumber].rawData = rawData;
                    }
                }
 
                this.sendSuccess(res, {
                    count: Object.keys(mappedDevices).length,
                    data: mappedDevices,
                    format: 'mapped'
                });
                return;
            } catch (err) {
                logger.error(`批量属性映射失败: ${err.message}`);
                // 映射失败时回退到原始格式
            }
        }
 
        this.sendSuccess(res, {
            count: Object.keys(allData).length,
            data: allData,
            format: 'raw'
        });
    }
 
    /**
     * 获取设备列表(简化版,仅包含基本信息)
     * GET /api/device/list
     */
    handleGetDeviceList(req, res) {
        const deviceList = dataCache.getDeviceList();
 
        this.sendSuccess(res, {
            count: deviceList.length,
            devices: deviceList
        });
    }
 
    /**
     * 获取缓存统计信息
     * GET /api/cache/stats
     */
    handleGetStats(req, res) {
        const stats = dataCache.getStats();
 
        this.sendSuccess(res, {
            timestamp: new Date().toISOString(),
            ...stats
        });
    }
 
    /**
     * 清空所有缓存
     * POST /api/cache/clear
     */
    handleClearCache(req, res) {
        if (req.method !== 'POST' && req.method !== 'GET') {
            this.sendError(res, 405, '方法不允许');
            return;
        }
 
        dataCache.clearAll();
        
        this.sendSuccess(res, {
            message: '缓存已清空'
        });
    }
 
    /**
     * 获取空闲设备列表
     * GET /api/device/idle?timeout=300000
     */
    handleGetIdleDevices(req, res, query) {
        const timeout = parseInt(query.timeout) || 300000; // 默认 5 分钟
        const idleDevices = dataCache.getIdleDevices(timeout);
 
        this.sendSuccess(res, {
            timeout: timeout,
            count: idleDevices.length,
            devices: idleDevices
        });
    }
 
    /**
     * 健康检查
     * GET /api/health
     */
    handleHealth(req, res) {
        this.sendSuccess(res, {
            status: 'ok',
            timestamp: new Date().toISOString()
        });
    }
 
    /**
     * 获取限流统计信息
     * GET /api/ratelimit/stats
     */
    handleRateLimitStats(req, res) {
        const stats = rateLimiter.getStats();
 
        this.sendSuccess(res, {
            enabled: this.config.rateLimit.enabled,
            interval: this.config.rateLimit.interval,
            ...stats
        });
    }
 
    /**
     * 清空限流记录
     * POST /api/ratelimit/clear
     */
    handleRateLimitClear(req, res) {
        if (req.method !== 'POST' && req.method !== 'GET') {
            this.sendError(res, 405, '方法不允许');
            return;
        }
 
        rateLimiter.clearAll();
 
        this.sendSuccess(res, {
            message: '限流记录已清空'
        });
    }
 
    /**
     * 根路径 - 返回 API 文档
     * GET /
     */
    handleRoot(req, res) {
        const apiDocs = {
            service: '透析机数据 HTTP API',
            version: '1.0.0',
            timestamp: new Date().toISOString(),
            rateLimit: {
                enabled: this.config.rateLimit.enabled,
                deviceDataInterval: `${this.config.rateLimit.interval}ms`,
                allDevicesInterval: `${this.config.rateLimit.allDevicesInterval}ms`
            },
            endpoints: {
                '获取指定设备数据': {
                    method: 'GET',
                    path: '/api/device/data',
                    params: { deviceNumber: '设备序号' },
                    example: '/api/device/data?deviceNumber=D001',
                    rateLimit: `${this.config.rateLimit.interval}ms (按设备号)`
                },
                '获取所有设备数据': {
                    method: 'GET',
                    path: '/api/device/all',
                    description: '返回所有设备的完整数据',
                    rateLimit: `${this.config.rateLimit.allDevicesInterval}ms (全局)`
                },
                '获取设备列表': {
                    method: 'GET',
                    path: '/api/device/list',
                    description: '返回设备列表(简化版)'
                },
                '获取缓存统计': {
                    method: 'GET',
                    path: '/api/cache/stats',
                    description: '返回缓存使用情况'
                },
                '获取限流状态': {
                    method: 'GET',
                    path: '/api/ratelimit/stats',
                    description: '返回限流统计信息'
                },
                '清空缓存': {
                    method: 'POST',
                    path: '/api/cache/clear',
                    description: '清空所有缓存数据'
                },
                '清空限流记录': {
                    method: 'POST',
                    path: '/api/ratelimit/clear',
                    description: '清空所有限流记录'
                },
                '获取空闲设备': {
                    method: 'GET',
                    path: '/api/device/idle',
                    params: { timeout: '超时时间(毫秒,默认300000)' },
                    example: '/api/device/idle?timeout=300000'
                },
                '健康检查': {
                    method: 'GET',
                    path: '/api/health'
                }
            }
        };
 
        this.sendSuccess(res, apiDocs);
    }
 
    /**
     * 发送成功响应
     */
    sendSuccess(res, data) {
        res.writeHead(200);
        res.end(JSON.stringify({
            code: 0,
            message: 'success',
            data: data,
            timestamp: new Date().toISOString()
        }));
    }
 
    /**
     * 发送错误响应
     */
    sendError(res, statusCode, message) {
        res.writeHead(statusCode);
        res.end(JSON.stringify({
            code: statusCode,
            message: message,
            timestamp: new Date().toISOString()
        }));
    }
 
    /**
     * 停止 HTTP 服务
     */
    stop() {
        if (this.server) {
            this.server.close();
            logger.info('HTTP 服务已停止');
        }
    }
}
 
module.exports = HttpServer;