# Jb-Communication(Gambro Artis ORU^R31) 用于连接 Gambro/Baxter Artis 透析机 TCP 端口并解析 ORU^R31(XML,UTF-16LE/UNICODE)消息中的 OBX 参数。 ## 目录 - [离线自测(推荐先跑)](#离线自测推荐先跑) - [解析抓到的原始数据流](#解析抓到的原始数据流) - [连接真实设备](#连接真实设备) - [重要协议注意事项](#重要协议注意事项) ## 离线自测(推荐先跑) ```powershell Set-Location D:\gitData\Jb-Communication npm install npm test ``` 预期会打印类似:`[7] 温度 (Temp): 37.500000 cel`。 ## 解析抓到的原始数据流 当你拿到“原始数据流”(抓包/串流日志)时,优先把数据保存成文件,然后用离线工具解析并复用现有的 `parseAndPrintData()`。 二进制抓包(推荐): ```powershell Set-Location D:\gitData\Jb-Communication npm run parse:raw -- .\raw.bin ``` 如果你的文件是 **hex 文本**(例如 `00 3C 00 4F ...`): ```powershell Set-Location D:\gitData\Jb-Communication npm run parse:raw -- .\raw_hex.txt --hex ``` 如果你的文件是 **base64 文本**: ```powershell Set-Location D:\gitData\Jb-Communication npm run parse:raw -- .\raw_b64.txt --b64 ``` 编码强制(默认会自动猜测,一般不需要): ```powershell npm run parse:raw -- .\raw.bin --utf16le npm run parse:raw -- .\raw.xml --utf8 ``` ## 直接用 index.js 离线解码(推荐解析 RTF/hex) 如果你的文件像 `新结果.txt` 这样是“RTF 包了一层 + 里面是 `3C 00 4F 00 ...` 的 hex 字节”,可以直接用 `index.js` 做提取+解码+解析: ```powershell Set-Location D:\gitData\Jb-Communication node .\index.js --decode-file .\新结果.txt --hex --utf16le ``` 说明:如果抓到的数据被截断,导致没有 `` 闭合,脚本会自动尝试从片段提取完整 `...` 并兜底解析。 ## 模拟器发送但脚本没打印? 脚本只会在“收到完整的 XML 帧(包含 `` 或 ``)”后才打印。常见原因是模拟器发的是“文本形式的 hex(字符 `3C 00 4F...`)”,而不是原始字节流。 推荐做法:把 hex 文本转成二进制,然后用模拟器的“发送文件/原始发送”功能发送。 ```powershell Set-Location D:\gitData\Jb-Communication npm run hex:bin -- .\raw_hex.txt .\payload.bin ``` 如果要确认脚本是否真的收到字节/是否形成完整帧,可开启调试: ```powershell node .\index.js --debug ``` ## 模拟透析机服务端(本机调试推荐) 当你想在没有真实设备的情况下调试“TCP 粘包/半包分帧 + OBX 解析”,推荐直接用仓库里的 `sim_server.js` 回放你抓到的真实数据(例如 `结果全部.txt`)。 终端 A:启动模拟服务端(监听本机端口,按帧回放): ```powershell Set-Location D:\gitData\Jb-Communication npm run sim:server -- --host 127.0.0.1 --port 39023 --hex-file .\结果全部.txt --interval 200 ``` 终端 B:启动客户端连接模拟服务端: ```powershell Set-Location D:\gitData\Jb-Communication node .\index.js --host 127.0.0.1 --port 39023 --debug ``` 压测分帧(模拟半包/粘包):把发送模式切到“按字节流拆块写出”。例如每 200 字节写一次: ```powershell npm run sim:server -- --host 127.0.0.1 --port 39023 --hex-file .\结果全部.txt --mode bytes --chunk 200 --interval 5 ``` 如果只想发送一轮后退出,加 `--once`。 ## 连接真实设备 1. 打开 `index.js`,修改 `CONFIG.host` 为透析机 IP。 2. 运行: ```powershell Set-Location D:\gitData\Jb-Communication node .\index.js ``` 如需“收集”每帧解析结果到文件(NDJSON,一行一条帧): ```powershell Set-Location D:\gitData\Jb-Communication node .\index.js --collect ``` 默认输出到:`artis_frames.ndjson`(项目目录)。也可以自定义输出路径: ```powershell node .\index.js --collect --out .\my_frames.ndjson ``` 默认端口: - `3021`:自动推送(治疗状态下通常每 60 秒一条) ## 多设备工程化网关(推荐生产部署) 当你需要“一套程序同时连接多台透析机”,并且按设备编号做最新数据缓存 + 分发到不同平台,请使用 `gateway.js`。 ### 能力 - 设备列表配置:每台设备可配置 `host/port/subscriptions` - 本地最新缓存:以“设备编号(`MSH.4/HD.1`)”为 key,只保留最新一条 - 按订阅分发:目前支持 `mqtt` 与 `aliyunIot` ### 1)准备配置 先复制模板: ```powershell Set-Location D:\gitData\Jb-Communication Copy-Item .\gateway.config.example.json .\gateway.config.json ``` 然后编辑 `gateway.config.json`: - `devices[]`:配置多台透析机 IP 和端口(通常 `3021`) - `devices[].subscriptions`:可选 `mqtt` / `aliyunIot` - `services.mqtt`:MQTT Broker 地址、topic 前缀等 - `services.aliyunIot.tripleApi`:通过设备编号换取三元组的接口 ### 2)启动网关 ```powershell Set-Location D:\gitData\Jb-Communication npm install npm run gateway ``` 指定配置文件启动: ```powershell node .\gateway.js --config .\gateway.config.json ``` ### 2.1)阿里云物模型映射(已内置) `gateway.js` 已按 `阿里物模型.json` 做属性映射,并优先按物模型类型做转换(`int/text/...`): - `n` <- 设备编号(`MSH.4/HD.1`) - `deviceType` <- 配置项 `services.aliyunIot.deviceType`(默认 `Artis`) - `IPAddress` <- 当前设备连接 IP - `zljd` <- OBX `id=0`(治疗阶段) - `sysj` <- OBX `id=1`(剩余时间) - `A` <- OBX `id=34`(设定超滤量,作为超滤总量) - `B` <- OBX `id=2`(已超滤量) - `C` <- OBX `id=3`(超滤率) - `D` <- OBX `id=6`(血流量) - `sdxll` <- OBX `id=32`(设定血流速) - `F` <- OBX `id=7`(温度) - `J` <- OBX `id=12`(TMP) - `H` <- OBX `id=13`(静脉压) - `o` <- OBX `id=14`(动脉压) - `L` <- OBX `id=8`(透析液流速) - `G` <- OBX `id=4`(电导率) 说明:如果某个属性当前帧没有解析到,网关会跳过该属性,不会发送空值。 ### 3)缓存文件 默认会把每台设备最新数据缓存到: - `./cache/latest-device-cache.json` 你可在 `gateway.config.json` 的 `cacheFile` 修改路径。 ## 重要协议注意事项 - 编码:脚本按 `utf16le` 解码(对应文档中的 UNICODE/UTF-16)。 - ACK:根据协议说明(MSH-15/16 = `NE`),客户端收到数据后不要发送 ACK。 - 文档:协议说明见 `artis_protocol_oru_r31.md`。