德朗6000透析机socket客户端程序通讯
gx
chenyc
2025-12-10 efa42a573642e7de5e3f0621034822c3238e80cb
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
 
const https = require('https');
const { info, warn, error } = require('./logger'); // 引入自定义的日志模块
const path = require('path');
const fs = require('fs');
const url = require('url');
const querystring = require('querystring');
// 读取配置文件
// 使用 pkg 特定的方式访问打包进来的资产
const configPath = path.join(__dirname, 'config.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
 
info(`获取文件 ${configPath}地址,${JSON.stringify(config)}`);
/**解析数据1 */
const parseData=(buf,index1,index2,index3,index4)=> {
    let data5 = (buf[index1] << 24) | (buf[index2] << 16) | (buf[index3] << 8) | buf[index4];
    return data5;
  }
 
  /**
   * 
   * @param {*} data:buff 
   * @returns 数据对象
   */
const funToModel=(data)=>{
    const row=data
    const msgBody={
        A: Number((parseData(row,5,6,7,8)/100).toFixed(2)),
        B: Number((parseData(row,9,10,11,12)/100).toFixed(2)),
        //脱水速率
        C: Number((parseData(row,13,14,15,16)/100).toFixed(2)),
        //血泵流量设定
        xlllsd: parseData(row,17,18,19,20),
        //透析液流量
        L: parseData(row,21,22,23,24),
        //透析液温度
        F: Number((parseData(row,25,26,27,28)/10).toFixed(1)),
        //电导度
        G: Number((parseData(row,29,30,31,32)/100).toFixed(2)),
        //静脉压
        H: parseData(row,33,34,35,36),
        //透析液压
        I: parseData(row,37,38,39,40),
        //跨膜压
        J: parseData(row,41,42,43,44),
        //透析时间
        K: parseData(row,45,46,47,48),
        //肝素泵流量
        E: Number((parseData(row,49,50,51,52)/10).toFixed(1)),
        //累计注射量
        X: Number((parseData(row,53,54,55,56)/100).toFixed(2)),
        //报警编号
        bjbh: parseData(row,57,58,59,60),
        //剩余时间
        sysj: parseData(row,61,62,63,64),
        //血流量
        D: parseData(row,65,66,67,68),
        //动脉压
        o: parseData(row,69,70,71,72),
        //收缩压
        N: parseData(row,73,74,75,76),
        //舒张压
        O: parseData(row,77,78,79,80),
        //脉搏
        P: parseData(row,81,82,83,84),
        suedtime:getFormattedDateTime()
    }
    console.log(msgBody)
    return msgBody
 
}
 
/**数据校验 校验贝恩透析器 */
const calculateChecksum=(buf)=> {
    let checkSum = 0;
    for (let i =0;i<=84;i++) {
        if(i!==2&&i!==3&&i!==4){
            checkSum += buf[i];
        }
    }
    return  checkSum & 0xFF; 
}
 
 
const getFormattedDateTime=()=> {
    const now = new Date();
    
    // 获取各个部分
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,所以需要加1
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
 
    // 组合成所需格式
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
 
const  fetchServerList= async()=>{
    return new Promise((resolve, reject) => {
        // 构建完整的URL,包括查询参数
        const baseUrl = config.http.url;
        const params = querystring.stringify(config.http.params);
        const fullUrl = `${baseUrl}?${params}`;
        // 解析URL以获取hostname、path等信息
        const parsedUrl = new URL(fullUrl);
        // 配置选项
        const options = {
            hostname: parsedUrl.hostname,
            port: 443, // HTTPS 默认端口
            path: parsedUrl.pathname + parsedUrl.search,
            method: config.http.method.toUpperCase(),
            headers: {
                ...config.http.headers,
                'Content-Type': 'application/json',
                'Content-Length': 0 // 对于POST请求,如果不需要发送请求体,设置为0
            }
        };
 
        // 发送请求
        const req = https.request(options, (res) => {
            let data = '';
 
            // 接收数据
            res.on('data', (chunk) => {
                data += chunk;
            });
 
            // 请求完成
            res.on('end', () => {
                try {
                    resolve(JSON.parse(data));
                } catch (err) {
                    console.error(`解析服务器列表失败: ${err.message}`);
                    reject(err);
                }
            });
        }).on("error", (err) => {
            console.error(`获取服务器列表失败: ${err.message}`);
            reject(err);
        });
 
        // 结束请求
        req.end();
    });
}
// 导出日志函数
module.exports = {
    funToModel,
    calculateChecksum,
    getFormattedDateTime,
    fetchServerList
    
};