Skip to content

快速开始

我们将通过一个简单的 demo 来演示 driver-box 的设备接入流程, 在此之前请确保你已具备以下几项前置条件:

  • Golang 开发环境,版本要求 >= 1.18。
  • 相应的 IDE 工具,如 VSCode、Goland 等。
  • git 工具。

本次演示的形式是直接基于 driver-box 工程源码展开

初始化工程

下载 driver-box 源码至本地,并导入 IDE。

Terminal window
git clone https://gitee.com/iBUILDING-X/driver-box.git

启动程序

在工程所在目录下执行以下命令下载依赖:

Terminal window
go mod tidy
go mod vendor

并通过 IDE 启动 main.go,正常情况下会在终端输出以下内容:

start driver-box success.

倘若启动失败,可能因为内置的演示示例所依赖的端口号(8888、8889)被占用,需要您更换一下。

上报设备数据

在 driver-box 项目的 /res/driver 目录下,我们已内置了一个 http_server 协议的示例,可以此演示设备接入的效果。

{
"deviceModels": [
{
"name": "swtich",
"description": "开关",
"devicePoints": [
{
"description": "开关",
"name": "onOff",
"readWrite": "R",
"reportMode": "change",
"valueType": "int"
}
],
"devices": [
{
"id": "swtich-1",
"description": "1号开关",
"ttl": "5m",
"connectionKey": "8888"
},
{
"id": "swtich-2",
"description": "2号开关",
"ttl": "5m",
"connectionKey": "8889"
}
]
}
],
"connections": {
"8888": {
"host": "127.0.0.1",
"port": 8888
},
"8889": {
"host": "",
"port": 8889
}
},
"protocolName": "http_server"
}

config.json 定义了一个开关模型,仅有一个 onOff 的点位。 且该模型下关联了两个设备:swtich-1 和 swtich-2。

对于 TCP 类协议,还需配套一个 converter.lua 文件,用于将请求数据转换为 driver-box 所需的数据格式。

此时,可通过以下命令模拟开关的状态上报:

Terminal window
curl -X POST -H "Content-Type: application/json" -d '{"id":"swtich-2","onOff":1}' http://127.0.0.1:8888/report
curl -X POST -H "Content-Type: application/json" -d '{"id":"swtich-1","onOff":0}' http://127.0.0.1:8888/report

查看设备影子

打开浏览器,访问:http://localhost:8081/api/v1/shadow/all 可直接查看当前接入设备的点位状态。

{
"success": true,
"errorCode": 200,
"errorMsg": "",
"data": [
{
"id": "swtich-1",
"points": [
{
"name": "onOff",
"value": 0,
"updated_at": "2024-05-21 17:57:14"
}
],
"online": true,
"ttl": "5m0s",
"disconnect_times": 0,
"updated_at": "2024-05-21 17:57:14"
},
{
"id": "swtich-2",
"points": [
{
"name": "onOff",
"value": 1,
"updated_at": "2024-05-21 17:57:08"
}
],
"online": true,
"ttl": "5m0s",
"disconnect_times": 0,
"updated_at": "2024-05-21 17:57:08"
}
]
}