Skip to content

驱动管理

驱动(Driver)是 CoreC 南向层的协议适配器,负责与工业设备通信。以下端点用于查询驱动运行状态及其最新采集的标签值。

所有端点均需认证。

获取驱动列表

获取所有已注册驱动的运行状态。

请求

http
GET /drivers

响应

200 OK

json
{
  "drivers": [
    {
      "name": "plc1",
      "type": "modbus-tcp",
      "state": "connected",
      "last_read": "2024-09-08T10:30:00.123456789Z",
      "last_error": "",
      "tag_count": 120,
      "read_count": 45000,
      "error_count": 3
    },
    {
      "name": "plc2",
      "type": "s7",
      "state": "connecting",
      "last_read": "0001-01-01T00:00:00Z",
      "last_error": "connection refused",
      "tag_count": 64,
      "read_count": 0,
      "error_count": 1
    }
  ]
}

字段说明 — DriverStatus

字段类型说明
namestring驱动实例名称
typestring驱动协议类型(modbus-tcp / s7 / opcua
statestring连接状态,见下表
last_readstring (RFC 3339)最近一次成功读取时间
last_errorstring最近一次错误信息,无错误时为空字符串
tag_countint该驱动配置的标签总数
read_countuint64累计成功读取次数
error_countuint64累计错误次数

连接状态(state)

说明
disconnected已断开
connecting连接中
connected已连接
error错误状态

示例

bash
curl http://localhost:9090/drivers \
  -H "Authorization: Bearer corec-secret-token"

获取单个驱动状态

按名称获取指定驱动的运行状态。

请求

http
GET /drivers/{name}

路径参数

参数类型说明
namestring驱动实例名称

响应

成功 — 200 OK

返回单个 DriverStatus 对象(不含外层 drivers 数组):

json
{
  "name": "plc1",
  "type": "modbus-tcp",
  "state": "connected",
  "last_read": "2024-09-08T10:30:00.123456789Z",
  "last_error": "",
  "tag_count": 120,
  "read_count": 45000,
  "error_count": 3
}

驱动不存在 — 404 Not Found

json
{
  "error": "driver not found"
}

示例

bash
curl http://localhost:9090/drivers/plc1 \
  -H "Authorization: Bearer corec-secret-token"

获取驱动最新标签值

获取指定驱动下所有标签的最新采集值。数据来自内核 LatestCache,由 RWMutex 保护,读取零阻塞。

请求

http
GET /drivers/{name}/tags

路径参数

参数类型说明
namestring驱动实例名称

空名称行为

name 为空字符串,内核将返回所有驱动的最新标签值(与 GET /tags 行为一致)。

响应

200 OK

返回以标签名为键、DataPoint 为值的映射:

json
{
  "tags": {
    "temperature": {
      "driver": "plc1",
      "device": "192.168.1.10",
      "group": "g1",
      "tag": "temperature",
      "value": 42.5,
      "type": "float64",
      "quality": "good",
      "timestamp": "2024-09-08T10:30:00.123456789Z"
    },
    "pressure": {
      "driver": "plc1",
      "device": "192.168.1.10",
      "group": "g1",
      "tag": "pressure",
      "value": 101.3,
      "type": "float64",
      "quality": "good",
      "timestamp": "2024-09-08T10:30:00.123456789Z"
    }
  }
}

字段说明 — DataPoint

字段类型说明
driverstring采集该值的驱动名称
devicestring设备标识(如 IP 地址)
groupstring采集分组名称
tagstring标签名称
valueany标签值(类型由 type 决定)
typestring数据类型,见下表
qualitystring数据质量,见下表
timestampstring (RFC 3339)采集时间戳
metadataobject附加元数据键值对(可选,存在时才出现)

数据类型(type)

Go 类型说明
boolbool布尔值
int8 / int16 / int32 / int64int有符号整数
uint8 / uint16 / uint32 / uint64uint无符号整数
float32 / float64float浮点数
stringstring字符串
bytes[]byte字节序列

数据质量(quality)

说明
good数据有效
bad数据无效(设备错误、通信失败等)
uncertain数据不确定

示例

bash
curl http://localhost:9090/drivers/plc1/tags \
  -H "Authorization: Bearer corec-secret-token"

Released under the MIT License.