From 1daaf55ceac01b00be25aecc7efb57cf47a34155 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期三, 10 十二月 2025 18:34:32 +0800
Subject: [PATCH] gx

---
 hmConfig.json |    4 
 部署帮助文档.md     |  575 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 index-linux   |    0 
 server.js     |    8 
 4 files changed, 582 insertions(+), 5 deletions(-)

diff --git a/hmConfig.json b/hmConfig.json
new file mode 100644
index 0000000..eecf4c4
--- /dev/null
+++ b/hmConfig.json
@@ -0,0 +1,4 @@
+{
+  "port": 11000,
+  "iotRegion": "cn-shanghai"
+}
diff --git a/hm-index-linux b/index-linux
similarity index 100%
rename from hm-index-linux
rename to index-linux
Binary files differ
diff --git a/server.js b/server.js
index 626027a..c96cbf5 100644
--- a/server.js
+++ b/server.js
@@ -4,13 +4,11 @@
 const morgan = require('morgan');
 const logger = require('./logger');
 const { getTripleByMac, publishToIot } = require('./aliyun-iot');
-
-// 改为手动设置环境变量(或从命令行传入)
-process.env.PORT = process.env.PORT || 13000;
-process.env.IOT_REGION = 'cn-shanghai';
+const hmConfig = require('./hmConfig.json');
 
 const app = express();
-const PORT = process.env.PORT || 13000;
+const PORT = hmConfig.port || 13000;
+process.env.IOT_REGION =  hmConfig.iotRegion || 'cn-shanghai';
 
 // 日志中间件
 app.use(morgan('combined', {
diff --git "a/\351\203\250\347\275\262\345\270\256\345\212\251\346\226\207\346\241\243.md" "b/\351\203\250\347\275\262\345\270\256\345\212\251\346\226\207\346\241\243.md"
new file mode 100644
index 0000000..bef0a30
--- /dev/null
+++ "b/\351\203\250\347\275\262\345\270\256\345\212\251\346\226\207\346\241\243.md"
@@ -0,0 +1,575 @@
+# 透析机数据服务 - 可执行文件部署指南
+
+## 项目概述
+
+**项目名称**: huamo-data-service  
+**版本**: 1.0.0  
+**描述**: 透析机数据转发服务,接收设备数据并上传至阿里云 IoT  
+**部署方式**: 使用预编译的独立可执行文件部署
+
+## 可用的执行文件
+
+| 文件名 | 操作系统 | 架构 | 使用场景 |
+|--------|--------|------|---------|
+| `index-win.exe` | Windows | x64 | Windows 服务器 |
+| `index-linux` | Linux | x64 | Linux 服务器 |
+| `index-macos` | macOS | x64 | macOS 本地开发 |
+
+## 环境要求
+
+### Windows 部署
+- **操作系统**: Windows 7 及以上
+- **架构**: x64
+- **权限**: 管理员权限(用于开放防火墙端口)
+- **磁盘空间**: 50 MB
+
+### Linux 部署
+- **操作系统**: Linux 2.6 及以上内核
+- **架构**: x64
+- **权限**: 普通用户即可(如果需要使用 1024 以下端口需要 root)
+- **磁盘空间**: 50 MB
+
+### macOS 部署
+- **操作系统**: macOS 10.13 及以上
+- **架构**: Intel x64
+- **磁盘空间**: 50 MB
+
+## 快速开始
+
+### Windows 部署
+
+#### 1. 准备文件
+
+将以下文件复制到部署目录:
+```
+deployment/
+├── index-win.exe          # 可执行文件
+└── hmConfig.json          # 配置文件
+```
+
+#### 2. 配置文件
+
+编辑 `hmConfig.json`:
+```json
+{
+  "port": 13000,
+  "iotRegion": "cn-shanghai"
+}
+```
+
+#### 3. 运行程序
+
+**方式一:直接双击运行**
+```
+双击 index-win.exe
+```
+
+**方式二:命令行运行**
+```powershell
+# PowerShell
+.\index-win.exe
+
+# CMD
+index-win.exe
+```
+
+**方式三:后台运行(推荐)**
+```powershell
+# 使用 Start-Process 在后台运行
+Start-Process -FilePath "C:\path\to\index-win.exe" -WindowStyle Hidden
+
+# 或使用任务调度器(见下文)
+```
+
+#### 4. 验证服务状态
+
+```powershell
+# 检查端口是否监听
+netstat -ano | findstr :13000
+
+# 或者访问健康检查接口
+curl http://localhost:13000/health
+```
+
+---
+
+### Linux 部署
+
+#### 1. 准备文件
+
+将以下文件复制到部署目录:
+```bash
+mkdir -p /opt/huamo-service
+cd /opt/huamo-service
+
+# 复制可执行文件和配置文件
+cp index-linux .
+cp hmConfig.json .
+
+# 添加执行权限
+chmod +x index-linux
+```
+
+#### 2. 配置文件
+
+编辑 `hmConfig.json`:
+```json
+{
+  "port": 13000,
+  "iotRegion": "cn-shanghai"
+}
+```
+
+#### 3. 运行程序
+
+**方式一:直接运行**
+```bash
+./index-linux
+```
+
+**方式二:后台运行(推荐)**
+```bash
+# 使用 nohup
+nohup ./index-linux > service.log 2>&1 &
+
+# 或使用 screen
+screen -S huamo-service
+./index-linux
+# 按 Ctrl+A+D 离开 screen
+```
+
+**方式三:使用 Systemd(推荐)**
+
+创建服务文件 `/etc/systemd/system/huamo-service.service`:
+
+```ini
+[Unit]
+Description=Huamo Data Service
+After=network.target
+
+[Service]
+Type=simple
+User=nobody
+WorkingDirectory=/opt/huamo-service
+ExecStart=/opt/huamo-service/index-linux
+Restart=on-failure
+RestartSec=10
+
+[Install]
+WantedBy=multi-user.target
+```
+
+启动服务:
+```bash
+# 重新加载 systemd
+sudo systemctl daemon-reload
+
+# 启动服务
+sudo systemctl start huamo-service
+
+# 设置开机自启
+sudo systemctl enable huamo-service
+
+# 查看服务状态
+sudo systemctl status huamo-service
+
+# 查看服务日志
+sudo journalctl -u huamo-service -f
+```
+
+#### 4. 验证服务状态
+
+```bash
+# 检查端口是否监听
+netstat -tlnp | grep 13000
+# 或
+ss -tlnp | grep 13000
+
+# 访问健康检查接口
+curl http://localhost:13000/health
+```
+
+---
+
+## 配置说明
+
+### hmConfig.json 配置文件
+
+```json
+{
+  "port": 13000,
+  "iotRegion": "cn-shanghai"
+}
+```
+
+**配置项说明**:
+
+| 配置项 | 类型 | 默认值 | 说明 |
+|--------|------|--------|------|
+| port | number | 13000 | 服务监听端口 |
+| iotRegion | string | cn-shanghai | 阿里云 IoT 区域 |
+
+**常见区域代码**:
+- `cn-shanghai`: 华东2(上海)
+- `cn-beijing`: 华北2(北京)
+- `cn-hangzhou`: 华东1(杭州)
+- `cn-shenzhen`: 华南1(深圳)
+
+---
+
+## API 接口
+
+### 接收设备数据
+
+**请求方式**: POST  
+**接口路径**: `/api/message`  
+**请求头**: `Content-Type: application/json`
+
+**请求体示例**:
+```json
+{
+  "MAC": "00:11:22:33:44:55",
+  "IP": "192.168.1.100",
+  "DataBase": {
+    "temperature": 37.5,
+    "pressure": 120
+  }
+}
+```
+
+**响应成功(200)**:
+```json
+{
+  "success": true,
+  "message": "数据接收并转发成功",
+  "deviceId": "productKey/deviceName"
+}
+```
+
+**响应失败(400)**:
+```json
+{
+  "success": false,
+  "message": "缺少必要字段: MAC, ip, data"
+}
+```
+
+### 健康检查
+
+**请求方式**: GET  
+**接口路径**: `/health`
+
+**响应示例**:
+```json
+{
+  "status": "OK",
+  "clients": 5,
+  "cacheSize": 10,
+  "timestamp": "2025-12-10T12:34:56.789Z"
+}
+```
+
+---
+
+## Windows 任务调度器自动启动
+
+### 设置开机自启动
+
+1. **打开任务调度器**
+   - 按 `Win+R` 输入 `taskschd.msc`
+
+2. **创建基本任务**
+   - 右键点击"任务计划程序库" → "创建基本任务"
+   - 任务名称:`Huamo Data Service`
+   - 描述:`透析机数据服务`
+
+3. **设置触发器**
+   - 开始任务:`启动时`
+
+4. **设置操作**
+   - 操作:`启动程序`
+   - 程序或脚本:`C:\path\to\index-win.exe`
+   - 起始于:`C:\path\to\`
+
+5. **设置条件**
+   - 勾选"如果计算机在后台电源使用时唤醒计算机"
+   - 勾选"忽略条件以强制启动任务"
+
+6. **完成**
+   - 点击"完成"
+
+### 验证任务
+
+```powershell
+# 查看任务
+Get-ScheduledTask -TaskName "Huamo Data Service"
+
+# 运行任务
+Start-ScheduledTask -TaskName "Huamo Data Service"
+
+# 停止任务
+Stop-ScheduledTask -TaskName "Huamo Data Service"
+```
+
+---
+
+## Linux 自动启动
+
+### 使用 Systemd(推荐)
+
+见上面 Systemd 部分
+
+### 使用 Crontab
+
+编辑 crontab:
+```bash
+crontab -e
+```
+
+添加以下行:
+```cron
+@reboot /opt/huamo-service/index-linux > /opt/huamo-service/huamo.log 2>&1
+```
+
+---
+
+## 日志和调试
+
+### Windows 日志查看
+
+```powershell
+# 查看事件查看器(应用程序事件日志)
+eventvwr.msc
+```
+
+### Linux 日志查看
+
+```bash
+# 查看 systemd 日志
+journalctl -u huamo-service -n 50 -f
+
+# 查看实时日志
+tail -f /opt/huamo-service/huamo.log
+
+# 查看最近错误
+grep -i error /opt/huamo-service/huamo.log
+```
+
+---
+
+## 故障排查
+
+### 问题:程序无法启动
+
+**Windows**:
+```powershell
+# 使用详细模式运行查看错误
+.\index-win.exe 2>&1 | tee output.log
+```
+
+**Linux**:
+```bash
+# 直接运行查看错误信息
+./index-linux
+```
+
+### 问题:端口已被占用
+
+**Windows**:
+```powershell
+# 查找占用端口的进程
+netstat -ano | findstr :13000
+
+# 杀死进程(PID 为进程号)
+taskkill /PID <PID> /F
+```
+
+**Linux**:
+```bash
+# 查找占用端口的进程
+lsof -i :13000
+# 或
+ss -tlnp | grep 13000
+
+# 杀死进程
+kill -9 <PID>
+```
+
+### 问题:无法连接阿里云 IoT
+
+1. 检查配置文件中的 iotRegion 是否正确
+2. 检查网络连接是否正常
+3. 检查阿里云 IoT 的三元组信息是否正确
+4. 确认防火墙未阻止出站连接
+
+### 问题:性能问题或内存占用过高
+
+**Windows**:
+```powershell
+# 查看进程资源占用
+Get-Process | findstr index-win
+```
+
+**Linux**:
+```bash
+# 查看进程资源占用
+ps aux | grep index-linux
+
+# 实时监控
+top -p <PID>
+```
+
+---
+
+## 性能优化
+
+### 推荐配置
+
+| 配置项 | 推荐值 | 说明 |
+|--------|--------|------|
+| port | 13000 | 避免使用 80、443 等特殊端口 |
+| iotRegion | 根据实际位置 | 选择离服务器最近的区域 |
+| 内存 | 512 MB+ | 根据设备数量调整 |
+
+### 部署拓扑建议
+
+**小型部署(1-10 个设备)**:
+```
+┌─────────────┐
+│ 单台服务器  │ (index-win.exe 或 index-linux)
+└─────────────┘
+```
+
+**中型部署(10-100 个设备)**:
+```
+     ┌────────────────────┐
+     │   负载均衡器        │
+     └────────┬───────────┘
+              │
+    ┌─────────┼─────────┐
+    │         │         │
+┌───▼──┐ ┌───▼──┐ ┌───▼──┐
+│服务器1│ │服务器2│ │服务器3│
+└──────┘ └──────┘ └──────┘
+```
+
+---
+
+## 备份和恢复
+
+### 备份配置
+
+```bash
+# Windows
+xcopy hmConfig.json backup\hmConfig.json.bak /Y
+
+# Linux
+cp hmConfig.json backup/hmConfig.json.bak
+```
+
+### 迁移到新服务器
+
+1. 复制 `index-win.exe` 或 `index-linux`
+2. 复制 `hmConfig.json`
+3. 修改配置中的端口或区域
+4. 启动服务
+
+---
+
+## 监控建议
+
+### Windows 监控
+
+使用 Windows 性能监控器:
+```powershell
+perfmon.msc
+```
+
+### Linux 监控
+
+使用系统监控工具:
+```bash
+# 使用 Prometheus + Grafana
+# 或者简单的 CPU/内存监控
+watch -n 1 'ps aux | grep index-linux'
+```
+
+---
+
+## 升级说明
+
+### 升级步骤
+
+1. **停止当前服务**
+   - Windows: `taskkill /IM index-win.exe /F`
+   - Linux: `sudo systemctl stop huamo-service`
+
+2. **备份配置**
+   ```bash
+   cp hmConfig.json hmConfig.json.backup
+   ```
+
+3. **替换可执行文件**
+   - 使用新版本的 `index-win.exe` 或 `index-linux` 替换旧文件
+
+4. **启动新版本**
+   - Windows: `.\index-win.exe`
+   - Linux: `sudo systemctl start huamo-service`
+
+5. **验证服务**
+   ```bash
+   curl http://localhost:13000/health
+   ```
+
+---
+
+## 支持和反馈
+
+- **作者**: cyc
+- **License**: MIT
+- **问题报告**: 提交日志和错误信息
+
+---
+
+## 快速参考
+
+### Windows 常用命令
+
+```powershell
+# 启动服务
+.\index-win.exe
+
+# 后台启动
+Start-Process -FilePath ".\index-win.exe" -WindowStyle Hidden
+
+# 查看端口
+netstat -ano | findstr :13000
+
+# 健康检查
+curl http://localhost:13000/health
+```
+
+### Linux 常用命令
+
+```bash
+# 启动服务
+./index-linux
+
+# 后台启动
+nohup ./index-linux > huamo.log 2>&1 &
+
+# 查看端口
+netstat -tlnp | grep 13000
+
+# 健康检查
+curl http://localhost:13000/health
+
+# 查看日志
+journalctl -u huamo-service -f
+```
+
+---
+
+## 版本历史
+
+- v1.0.0 - 初始可执行文件版本发布

--
Gitblit v1.8.0