PLC Gateway API Documentation
Status API
System Status
GET
/api/status
获取系统状态信息,包括启动时间、PLC数量和缓存大小。
响应示例
{
"status": "running",
"start_time": "2023-10-30 14:30:22",
"plc_count": 2,
"cache_size": 11000,
"plc_statuses": {
"PLC1": {
"status": "connected",
"last_connected": "2023-10-30 14:35:10"
},
"PLC2": {
"status": "disconnected",
"last_connected": "Never"
}
}
}
Area Status
获取指定PLC区域的状态信息。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Read) |
响应示例
{
"status": "connected",
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01",
"size": 4000,
"type": "read"
}
Data API
Single Read
从指定区域读取数据。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Read) |
| offset |
起始偏移量(字节) |
| length |
读取长度(字节) |
响应示例
{
"status": "success",
"plc_name": "PLC1",
"area_name": "DB100_Read",
"offset": 0,
"length": 4,
"data": [0, 0, 123, 45],
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01"
}
Single Write
向指定区域写入数据。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Write) |
| offset |
起始偏移量(字节) |
请求体
原始二进制数据
响应示例
{
"status": "success",
"plc_name": "PLC1",
"area_name": "DB100_Write",
"offset": 0,
"length": 4,
"plc_connection_status": "connected",
"last_update": 1698754350.789,
"last_update_formatted": "2023-10-30 14:12:30"
}
Single Read Bool
从指定区域读取数据。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Read) |
| offset |
起始偏移量(字节) |
| length |
读取长度(字节) |
响应示例
{
"status": "success",
"plc_name": "PLC1",
"area_name": "DB100_Read",
"offset": 0,
"length": 2,
"data": [0:False, 1:False],
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01"
}
Single Write Bool
向指定区域写入数据。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Write) |
| offset |
起始偏移量(字节) |
请求体
{0:True}
响应示例
{
"status": "success",
"plc_name": "PLC1",
"area_name": "DB100_Write",
"offset": 0,
"length": 1,
"plc_connection_status": "connected",
"last_update": 1698754350.789,
"last_update_formatted": "2023-10-30 14:12:30"
}
Batch Read
POST
/api/batch_read
批量读取多个区域的数据。
请求体
| 字段 |
类型 |
必需 |
描述 |
| plc_name |
string |
是 |
PLC名称(与配置中一致) |
| area_name |
string |
是 |
区域名称(与配置中一致) |
| offset |
number |
否 |
起始偏移量(字节),默认为0 |
| length |
number |
否 |
读取长度(字节),不提供则读取整个区域 |
请求示例
[
{
"plc_name": "PLC1",
"area_name": "DB100_Read",
"offset": 0,
"length": 4
},
{
"plc_name": "PLC1",
"area_name": "DB202_Params",
"offset": 10,
"length": 2
}
]
响应示例
[
{
"plc_name": "PLC1",
"area_name": "DB100_Read",
"status": "success",
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01",
"offset": 0,
"length": 4,
"data": [0, 0, 123, 45]
},
{
"plc_name": "PLC1",
"area_name": "DB202_Params",
"status": "success",
"plc_connection_status": "connected",
"last_update": 1698754322.123,
"last_update_formatted": "2023-10-30 14:12:02",
"offset": 10,
"length": 2,
"data": [255, 0]
}
]
Batch Write
POST
/api/batch_write
批量写入多个区域的数据。
请求体
| 字段 |
类型 |
必需 |
描述 |
| plc_name |
string |
是 |
PLC名称(与配置中一致) |
| area_name |
string |
是 |
区域名称(与配置中一致) |
| offset |
number |
是 |
起始偏移量(字节) |
| data |
array |
是 |
要写入的数据(字节数组) |
请求示例
[
{
"plc_name": "PLC1",
"area_name": "DB100_Write",
"offset": 0,
"data": [1, 2, 3, 4]
},
{
"plc_name": "PLC1",
"area_name": "DB202_Params",
"offset": 10,
"data": [255, 0]
}
]
响应示例
[
{
"plc_name": "PLC1",
"area_name": "DB100_Write",
"status": "success",
"plc_connection_status": "connected",
"last_update": 1698754350.789,
"last_update_formatted": "2023-10-30 14:12:30",
"offset": 0,
"length": 4
},
{
"plc_name": "PLC1",
"area_name": "DB202_Params",
"status": "success",
"plc_connection_status": "connected",
"last_update": 1698754351.234,
"last_update_formatted": "2023-10-30 14:12:31",
"offset": 10,
"length": 2
}
]
Parsed Data
获取解析后的数据(如果配置了结构)。
路径参数
| 参数 |
描述 |
| plc_name |
PLC名称(如PLC1) |
| area_name |
区域名称(如DB100_Read) |
响应示例(配置了解析结构)
{
"parsed": {
"temperature": 25.5,
"pressure": 100,
"status": true
},
"raw_data": [0, 0, 128, 65, 0, 100],
"status": "connected",
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01"
}
响应示例(未配置解析结构)
{
"raw_data": [0, 0, 128, 65, 0, 100],
"status": "connected",
"plc_connection_status": "connected",
"last_update": 1698754321.456,
"last_update_formatted": "2023-10-30 14:12:01"
}
Configuration API
Get Configuration
GET
/api/config
获取当前配置。
认证要求
需要Basic Auth认证
响应示例
{
"plcs": [
{
"name": "PLC1",
"ip": "192.168.0.10",
"rack": 0,
"slot": 1,
"areas": [
{
"name": "DB100_Read",
"type": "read",
"db_number": 100,
"offset": 0,
"size": 4000,
"structure": [
{"name": "temperature", "type": "real", "offset": 0},
{"name": "pressure", "type": "int", "offset": 4}
]
}
]
}
]
}
Validate Configuration
POST
/api/config/validate
验证配置是否有效。
认证要求
需要Basic Auth认证
请求体
要验证的配置JSON
响应示例(有效)
{
"valid": true
}
响应示例(无效)
{
"valid": false,
"message": "Invalid configuration: 'ip' is a required property"
}
Save Configuration
POST
/api/config
保存配置。
查询参数
| 参数 |
描述 |
| reload |
是否立即重载配置(true/false) |
认证要求
需要Basic Auth认证
请求体
要保存的配置JSON
响应示例
{
"success": true,
"message": "Configuration saved and reload requested"
}