chenyc
2025-12-09 545c24c6a711d71b65f3d4e8122fee3837fb1edc
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
/**
 * XML数据解析模块
 * 负责XML数据的解析、状态转换检测
 */
 
const ParameterParser = require('./parser');
 
class FreseniusXMLParser {
  constructor() {
    this.buffer = '';
    this.recordCount = 0;
    this.lastTSPRGR = null;              // 记录上一个 TSPRGR 值
    this.prewashStartTime = null;        // 预冲开始时间
    this.treatmentStartTime = null;      // 治疗开始时间
    this.treatmentEndTime = null;        // 治疗结束时间
    this.events = [];                    // 事件日志
  }
 
  /**
   * 解析数据流
   * @param {Buffer} data - 接收到的数据
   */
  parseData(data) {
    this.buffer += data.toString();
    const records = [];
 
    const regex = /<Rec[^>]*>[\s\S]*?<\/Rec>/g;
    let match;
 
    while ((match = regex.exec(this.buffer)) !== null) {
      records.push(this.analyzeRecord(match[0]));
    }
 
    const lastRecEnd = this.buffer.lastIndexOf('</Rec>');
    if (lastRecEnd !== -1) {
      this.buffer = this.buffer.substring(lastRecEnd + 6);
    }
 
    return records;
  }
 
  /**
   * 分析单条记录
   * @param {string} xmlStr - XML字符串
   */
  analyzeRecord(xmlStr) {
    this.recordCount++;
    const params = ParameterParser.extractAllParameters(xmlStr);
    const currentTSPRGR = params['TSPRGR'];
    
    // 检查状态转换事件
    const stateTransition = this.checkStateTransition(currentTSPRGR);
    
    const record = {
      recordNo: this.recordCount,
      timestamp: new Date().toLocaleString('zh-CN'),
      crc: ParameterParser.extractAttribute(xmlStr, 'CRC'),
      version: ParameterParser.extractElement(xmlStr, 'Ver'),
      dateTime: ParameterParser.extractElement(xmlStr, 'DtTm'),
      type: ParameterParser.extractElement(xmlStr, 'Type'),
      parameters: params,
      keyData: ParameterParser.extractKeyData(xmlStr),
      tsprgr: currentTSPRGR,
      stateEvent: stateTransition
    };
    
    // 更新最后的 TSPRGR 值
    if (currentTSPRGR) {
      this.lastTSPRGR = currentTSPRGR;
    }
    
    return record;
  }
 
  /**
   * 检查状态转换并生成事件
   * @param {string} currentTSPRGR - 当前TSPRGR值
   */
  checkStateTransition(currentTSPRGR) {
    const now = new Date().toLocaleString('zh-CN');
    let event = null;
 
    if (!currentTSPRGR) return null;
 
    // TSPRGR = 1: 预冲开始
    if (currentTSPRGR === '1' && this.lastTSPRGR !== '1') {
      this.prewashStartTime = now;
      event = {
        type: 'PREWASH_START',
        description: '预冲开始',
        time: now,
        severity: 'info'
      };
      this.events.push(event);
    }
 
    // TSPRGR = 2: 治疗运行
    if (currentTSPRGR === '2' && this.lastTSPRGR !== '2') {
      this.treatmentStartTime = now;
      event = {
        type: 'TREATMENT_START',
        description: '治疗开始 (参数采集已激活)',
        time: now,
        severity: 'info'
      };
      this.events.push(event);
    }
 
    // TSPRGR 从 2 到 3: 治疗结束
    if (currentTSPRGR === '3' && this.lastTSPRGR === '2') {
      this.treatmentEndTime = now;
      event = {
        type: 'TREATMENT_END',
        description: '治疗结束',
        time: now,
        severity: 'warning'
      };
      this.events.push(event);
    }
 
    return event;
  }
 
  /**
   * 获取事件日志
   */
  getEvents() {
    return this.events;
  }
 
  /**
   * 获取记录计数
   */
  getRecordCount() {
    return this.recordCount;
  }
}
 
module.exports = FreseniusXMLParser;