自定义映射

This commit is contained in:
zj
2026-03-23 15:57:58 +08:00
parent 3ecba44f9e
commit e97d9c427a
8 changed files with 365 additions and 338 deletions

192
README.md
View File

@@ -42,11 +42,24 @@ upc_resent/
"python_path": "/usr/bin/python3",
"base_dir": "/opt/upc_resent"
},
"command_sets": {
"set1": {
"name": "标准8路控制",
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"open2": "000100000008010F006500010001",
"close2": "000100000008010F006500010000"
}
}
},
"devices": [
{
"id": "dev1",
"name": "设备1-TCP",
"protocol": "tcp", // 协议类型: tcp 或 udp
"listen_protocol": "tcp", // 监听协议: tcp 或 udp
"device_protocol": "tcp", // 设备协议: tcp 或 udp
"command_set": "set1", // 使用的指令集
"upc_ip": "192.168.8.73",
"upc_port": 502,
"listen_port": 10079,
@@ -55,7 +68,9 @@ upc_resent/
{
"id": "dev2",
"name": "设备2-UDP",
"protocol": "udp", // 使用UDP协议
"listen_protocol": "udp",
"device_protocol": "udp",
"command_set": "set1",
"upc_ip": "192.168.8.200",
"upc_port": 502,
"listen_port": 10080,
@@ -112,26 +127,93 @@ crontab crontab.txt
|-----|------|-----|
| `id` | 设备唯一标识(用于文件名) | `"dev1"` |
| `name` | 设备名称(用于日志显示) | `"设备1-TCP"` |
| `protocol` | 协议类型`tcp``udp` | `"tcp"` / `"udp"` |
| `listen_protocol` | 客户端连接协议:`tcp``udp` | `"tcp"` / `"udp"` |
| `device_protocol` | 设备通信协议:`tcp``udp` | `"tcp"` / `"udp"` |
| `command_set` | 使用的指令集ID | `"set1"` |
| `upc_ip` | 控制设备的IP地址 | `"192.168.8.73"` |
| `upc_port` | 控制设备的端口Modbus默认502 | `502` |
| `listen_port` | 本机监听端口(每个设备需不同) | `10079` |
| `enabled` | 是否启用该设备 | `true` / `false` |
#### 指令定义 (commands)
**协议组合说明**
- `listen_protocol`: 主机监听客户端连接的协议(客户端 -> 主机)
- `device_protocol`: 主机与设备通信的协议(主机 -> 设备)
- 支持任意组合TCP->TCP、UDP->UDP、TCP->UDP、UDP->TCP
**配置示例**
```json
// 场景1: 客户端TCP -> 主机TCP -> 设备TCP
{
"id": "dev1",
"name": "设备1-纯TCP",
"listen_protocol": "tcp",
"device_protocol": "tcp",
"upc_ip": "192.168.8.73",
"upc_port": 502,
"listen_port": 10079
}
// 场景2: 客户端TCP -> 主机UDP -> 设备UDP协议转换
{
"id": "dev2",
"name": "设备2-TCP转UDP",
"listen_protocol": "tcp",
"device_protocol": "udp",
"upc_ip": "192.168.8.200",
"upc_port": 502,
"listen_port": 10080
}
// 场景3: 客户端UDP -> 主机TCP -> 设备TCP协议转换
{
"id": "dev3",
"name": "设备3-UDP转TCP",
"listen_protocol": "udp",
"device_protocol": "tcp",
"upc_ip": "192.168.8.201",
"upc_port": 502,
"listen_port": 10081
}
```
#### 指令集定义 (command_sets)
支持为不同设备配置不同的指令集:
```json
{
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"openall4": "000100000008010F00640004000F",
"closeall4": "000100000008010F006400040000"
"command_sets": {
"set1": {
"name": "标准8路控制",
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"open2": "000100000008010F006500010001",
"close2": "000100000008010F006500010000"
}
},
"set2": {
"name": "4路精简控制",
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"openall": "000100000008010F00640004000F",
"closeall": "000100000008010F006400040000"
}
}
}
}
```
格式:指令名 -> 16进制字符串
设备通过 `command_set` 字段指定使用的指令集:
```json
{
"id": "dev1",
"command_set": "set1"
}
```
#### 指令映射 (mappings)
@@ -157,7 +239,7 @@ crontab crontab.txt
## 如何适配自己的设备
### 场景1添加 TCP 设备
### 场景1添加 TCP 设备客户端TCP -> 主机TCP -> 设备TCP
```json
{
@@ -165,7 +247,8 @@ crontab crontab.txt
{
"id": "dev_tcp",
"name": "TCP设备",
"protocol": "tcp",
"listen_protocol": "tcp",
"device_protocol": "tcp",
"upc_ip": "192.168.1.100",
"upc_port": 502,
"listen_port": 10081,
@@ -175,7 +258,7 @@ crontab crontab.txt
}
```
### 场景2添加 UDP 设备
### 场景2添加 UDP 设备客户端UDP -> 主机UDP -> 设备UDP
```json
{
@@ -183,7 +266,8 @@ crontab crontab.txt
{
"id": "dev_udp",
"name": "UDP设备",
"protocol": "udp",
"listen_protocol": "udp",
"device_protocol": "udp",
"upc_ip": "192.168.1.200",
"upc_port": 502,
"listen_port": 10082,
@@ -193,18 +277,86 @@ crontab crontab.txt
}
```
### 场景3添加新的控制指令
### 场景3协议转换客户端TCP -> 主机UDP -> 设备UDP
```json
{
"commands": {
"open9": "000100000008010F006C00010001",
"close9": "000100000008010F006C00010000"
}
"devices": [
{
"id": "dev_tcp2udp",
"name": "TCP转UDP设备",
"listen_protocol": "tcp",
"device_protocol": "udp",
"upc_ip": "192.168.1.201",
"upc_port": 502,
"listen_port": 10083,
"enabled": true
}
]
}
```
### 场景4禁用某个设备
### 场景4协议转换客户端UDP -> 主机TCP -> 设备TCP
```json
{
"devices": [
{
"id": "dev_udp2tcp",
"name": "UDP转TCP设备",
"listen_protocol": "udp",
"device_protocol": "tcp",
"upc_ip": "192.168.1.202",
"upc_port": 502,
"listen_port": 10084,
"enabled": true
}
]
}
```
### 场景5不同设备使用不同指令集
```json
{
"command_sets": {
"set1": {
"name": "8路控制",
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"open8": "000100000008010F006B00010001",
"close8": "000100000008010F006B00010000"
}
},
"set2": {
"name": "4路控制",
"commands": {
"open1": "000100000008010F006400010001",
"close1": "000100000008010F006400010000",
"openall": "000100000008010F00640004000F",
"closeall": "000100000008010F006400040000"
}
}
},
"devices": [
{
"id": "dev1",
"name": "8路设备",
"command_set": "set1",
"upc_ip": "192.168.1.100"
},
{
"id": "dev2",
"name": "4路设备",
"command_set": "set2",
"upc_ip": "192.168.1.200"
}
]
}
```
### 场景6禁用某个设备
```json
{