动态驱动管理接口 (Dynamic Driver API)
约 525 字大约 2 分钟
2026-05-20
概述 (Overview)
CommunicationService 提供了系统内通讯驱动的动态生命周期管理以及通过统一网关执行的强类型反射式读写交互。外部应用可通过 RESTful 接口进行设备点位的热插拔和统一数据集成。
使用方法 (Usage)
以单例模式注册在依赖注入 (DI) 容器中,作为 Minimal APIs 或 Controllers 的核心处理器。
参数说明 (Parameters)
1. 驱动注册 (CreateDriverRequest)
| 参数名 | 类型 | 说明 | 必填 |
|---|---|---|---|
| DriverId | string | 驱动的全局唯一标识 ID | 是 |
| DriverType | string | 协议类型(S7, Modbus, OpcUa, Mqtt, OpcUaPubSub) | 是 |
| ConnectionString | string | 物理连接字符串 | 是 |
2. 强类型读取 (Read Tag)
| 参数名 | 类型 | 说明 | 必填 |
|---|---|---|---|
| address | string | 通信物理地址 | 是 |
| type | string | 目标数据类型指示(如 int, float, string) | 否 |
3. 强类型写入 (WriteAddressRequest)
| 参数名 | 类型 | 说明 | 必填 |
|---|---|---|---|
| Address | string | 通信物理地址 | 是 |
| Value | object | 写入的数值(JSON 格式) | 是 |
| TypeHint | string | 强类型映射指示(用于精确序列化字节流) | 否 |
返回值 (Returns)
数据读取结构 (DataValue<object>)
| 属性名 | 类型 | 说明 |
|---|---|---|
| Value | object? | 读取出的强类型物理数据 |
| Status | string | 数据状态戳 (Good, Bad, Uncertain, Timeout) |
| Quality | string | OPC UA 兼容 16 位质量码 |
| SourceTimestamp | DateTime | 数据源/PLC 生成时间戳 |
| ServerTimestamp | DateTime | 网关内部收到数据的时间戳 |
使用示例 (Examples)
示例 1:动态添加并连接 S7 驱动
POST /api/drivers
{
"driverId": "S7_CPU_01",
"driverType": "S7",
"connectionString": "CpuType=S71200;Ip=192.168.0.100;Rack=0;Slot=1"
}示例 2:反射强类型读取
GET /api/drivers/S7_CPU_01/read?address=DB1.DBD0&type=float
{
"value": 45.28,
"status": 0,
"quality": 0,
"sourceTimestamp": "2026-05-18T00:46:12.345Z",
"serverTimestamp": "2026-05-18T00:46:12.350Z"
}示例 3:反射强类型写入
POST /api/drivers/S7_CPU_01/write
{
"address": "DB1.DBX4.0",
"value": true,
"typeHint": "bool"
}示例 4:订阅与轮询缓存获取
注意:CommunicationService 中的订阅会自动在回调中更新注入的 IUniconCacheProvider 缓存介质。
POST /api/drivers/S7_CPU_01/subscribe
{
"address": "DB1.DBD8"
}GET /api/drivers/S7_CPU_01/subscriptions(从 CacheProvider 中提取最近一笔值)
[
{
"driverId": "S7_CPU_01",
"address": "DB1.DBD8",
"value": 102.5,
"quality": "Good",
"timestamp": "2026-05-18T00:49:15.823Z"
}
]