Skip to content

资源库(Library)

Library 是 driver-box 的资源库系统,提供设备模型、设备驱动、协议驱动和镜像模板的统一管理,实现配置驱动的灵活扩展。

driver-box 通过四大资源库实现配置驱动的架构:

资源库配置路径功能说明
Model Libraryres/library/model/设备模型定义和管理
Driver Libraryres/library/driver/Lua 脚本驱动,实现灵活的数据转换
Protocol Libraryres/library/protocol/协议层驱动定义
Mirror Libraryres/library/mirror_tpl/镜像模板配置

Model Library 负责设备模型的定义和管理。

{
"name": "sensor",
"modelId": "sensor_001",
"description": "温湿度传感器",
"attributes": {
"manufacturer": "xxx",
"version": "1.0"
},
"devicePoints": [
{
"name": "temperature",
"description": "温度",
"valueType": "float",
"readWrite": "r",
"reportMode": "change",
"scale": 0.1,
"decimals": 2,
"unit": ""
}
]
}
字段类型必填说明
namestring必填模型名称,全局唯一
modelIdstring必填云端模型 ID
descriptionstring可选模型描述
attributesobject可选模型扩展属性
devicePointsarray必填设备点位列表
字段类型必填说明
namestring必填点位名称
descriptionstring可选点位描述
valueTypestring必填值类型: int、float、bool、string
readWritestring必填读写类型: R(只读)、W(只写)、RW(读写)
reportModestring必填上报模式: realTime(实时)、change(变化)、period(周期)
scalefloat可选缩放系数,用于值转换
decimalsint可选小数位数,仅 float 类型有效
unitstring可选点位单位

Driver Library 通过 Lua 脚本实现设备层的数据编解码,提供灵活的数据转换能力。

编码函数在写操作时被调用,用于将点位值转换为设备可接收的格式。

-- encode.lua
function encode(deviceId, mode, points)
-- deviceId: 设备 ID
-- mode: 操作模式 (ReadMode/WriteMode)
-- points: 点位数组
local result = {}
for _, point in ipairs(points) do
-- 点位值加工
local value = point.value * point.scale
-- 类型转换
if point.valueType == "int" then
value = math.floor(value)
end
table.insert(result, {
name = point.name,
value = value
})
end
return result
end

解码函数在读操作时被调用,用于将设备返回的原始数据转换为标准格式。

-- decode.lua
function decode(deviceId, points)
-- deviceId: 设备 ID
-- points: 设备返回的原始点位数组
local result = {}
local events = {}
for _, point in ipairs(points) do
-- 值转换
local value = point.value / point.scale
-- 小数处理
if point.decimals then
value = tonumber(string.format("%." .. point.decimals .. "f", value))
end
table.insert(result, {
name = point.name,
value = value
})
-- 事件生成
if point.value > threshold then
table.insert(events, {
code = "alarm",
value = {
level = "high",
value = point.value
}
})
end
end
return result, events
end
  • 动态加载: 首次使用时从文件系统加载
  • 缓存机制: 加载后的 Lua 虚拟机缓存,避免重复加载
  • 错误隔离: 脚本执行错误不影响其他脚本
  • 热更新: 支持运行时重新加载脚本
场景说明示例
单位转换转换设备单位到标准单位0.1°C → 实际温度
位操作解析位寄存器中的多个点位Bit0-Bit7 → 8 个开关状态
复杂计算根据多个点位值计算衍生值温度 + 湿度 → 体感温度
事件触发基于点位值变化触发事件温度 > 30°C → 高温告警

Protocol Library 定义协议层的驱动配置,为不同协议提供统一的配置接口。

{
"name": "modbus_tcp",
"description": "Modbus TCP 协议驱动",
"version": "1.0",
"capabilities": [
"read_holding_registers",
"write_single_register",
"write_multiple_registers"
],
"configSchema": {
"host": { "type": "string", "required": true },
"port": { "type": "number", "required": true, "default": 502 },
"timeout": { "type": "number", "required": false, "default": 3000 }
}
}

Mirror Library 提供镜像模板配置,用于设备镜像功能的模板管理。

{
"name": "mirror_sensor",
"description": "传感器镜像模板",
"sourceDevice": {
"plugin": "modbus",
"deviceId": "sensor_001"
},
"targetDevice": {
"plugin": "modbus_server",
"deviceId": "mirror_sensor"
},
"pointMappings": [
{
"sourcePoint": "temperature",
"targetPoint": "mirror_temperature",
"transform": "value * 0.1"
}
]
}

资源库采用懒加载策略:

// 首次使用时加载
driver, err := library.Driver().Load("sensor_driver")
// 缓存复用
driver, _ := library.Driver().Get("sensor_driver")

所有加载的资源都缓存在内存中:

// 清空驱动库缓存
library.Driver().UnloadDeviceDrivers()
// 清空协议库缓存
library.Protocol().UnloadDeviceDrivers()

资源加载时进行完整性验证:

  • 文件存在性检查
  • 脚本语法验证 (Lua)
  • 配置结构验证 (JSON)
  • 依赖关系检查
res/library/
├── model/
│ ├── sensor/
│ │ ├── temperature_sensor.json
│ │ └── humidity_sensor.json
│ └── actuator/
│ └── relay.json
├── driver/
│ ├── common/
│ │ └── base_converter.lua
│ └── sensor/
│ └── sensor_driver.lua
├── protocol/
│ └── modbus.json
└── mirror_tpl/
└── sensor_mirror.json