Skip to content

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
smthing committed May 29, 2024
1 parent f351d39 commit 25e1450
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pages/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default defineConfig({
label: 'Export',
autogenerate: {directory: 'export'},
},
{
label: '开发指南',
autogenerate: {directory: 'developer'},
},
],
}),
],
Expand Down
169 changes: 169 additions & 0 deletions pages/src/content/docs/developer/about.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
---
title: 准备事项
sidebar:
order: 1
---
import { Tabs, TabItem } from '@astrojs/starlight/components';

driver-box 是一款物联网框架,也可以理解为是一款物联网平台。

作为框架(亦或“平台”),通常侧重于提供基础能力,以及为用户开展二次开发提供规范指引。
所以,driver-box 更适合作为三方包,引入至您所要打造的边缘应用中。

甚至您可以根据自身实际情况,选择性引用 driver-box 的部分能力,以此编译出更精简、更轻量的网关程序。

下面将为大家演示如何通过 driver-box 快速搭建一个网关程序。

:::note
以下示例代码可从 [https://github.com/ibuilding-X/demo](https://github.com/ibuilding-X/demo) 获取
:::

## 第一步:创建golang工程

```shell
mkdir demo
cd demo
go mod init demo
go get github.com/ibuilding-x/driver-box@b07be980
```

## 第二步:设备接入配置
:::tip
此步骤主要用于效果演示,通过 modbus 的虚拟设备功能模拟2个开关。
:::

<Tabs>
<TabItem label="config.json" icon="seti:json">
```json
{
"deviceModels": [
{
"name": "switch",
"description": "开关",
"devicePoints": [
{
"name": "onOff",
"description": "开关状态",
"reportMode": "change",
"valueType": "int",
"readWrite": "RW",
"primaryTable": "HOLDING_REGISTER",
"startAddress": "40001d",
"rawType": "uint16"
}
],
"devices": [
{
"id": "switch-1",
"description": "开关-1",
"connectionKey": "dev/ttyUSB01",
"properties": {
"unitID": "1"
}
},
{
"id": "switch-2",
"description": "开关-2",
"connectionKey": "dev/ttyUSB01",
"properties": {
"unitID": "2"
}
}
]
}
],
"connections": {
"dev/ttyUSB01": {
"address": "dev/ttyUSB01",
"batchReadLen": 50,
"enable": true,
"minInterval": 500,
"mode": "rtu",
"parity": 0,
"stop_bits": 0,
"timeout": 5000,
"virtual": true
}
},
"protocolName": "modbus"
}
```
</TabItem>
<TabItem label="converter.lua" icon="seti:lua">
```lua {10-16}
-- 初始化指定从机
function initSlave(slaveId, holdingRegister, coil, discreteInput, inputRegister)
slaves[slaveId] = {
[HOLDING_REGISTER] = initRegisters(holdingRegister),
[COIL] = initRegisters(coil),
[DISCRETE_INPUT] = initRegisters(discreteInput),
[INPUT_REGISTER] = initRegisters(inputRegister)
}

-- 调用 mockWrite 方法初始化模拟数据
-- Begin:以下需要开发者根据实际情况作修改
for i = 1, 2 do
print("初始化第"..i.."个开关")
mockWrite(slaveId, HOLDING_REGISTER, 40001, { 1 })
end
-- End:以上需要开发者根据实际情况作修改

return slaves[slaveId]
end
```
</TabItem>
</Tabs>


## 第三步:编写网关程序

```go title=main.go
package main

import (
"fmt"
"github.com/ibuilding-x/driver-box/driverbox"
"github.com/ibuilding-x/driver-box/driverbox/export"
"github.com/ibuilding-x/driver-box/driverbox/helper"
"github.com/ibuilding-x/driver-box/driverbox/plugin"
"os"
"time"
)

func main() {
//关闭driver-box日志
os.Setenv("LOG_LEVEL", "error")

//启动driver-box服务
driverbox.Start([]export.Export{})

go func() {
// 创建一个 Ticker,每隔 1 秒执行一次任务
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// 获取设备影子开关状态
v, _ := helper.DeviceShadow.GetDevicePoint("switch-1", "onOff")
fmt.Printf("开关状态:%v", v)
fmt.Println()

//切换开关状态
if v == int64(0) {
_ = driverbox.WritePoint("switch-1", plugin.PointData{
PointName: "onOff",
Value: 1,
})
} else {
_ = driverbox.WritePoint("switch-1", plugin.PointData{
PointName: "onOff",
Value: 0,
})
}
}
}
}()
select {}
}
```
5 changes: 5 additions & 0 deletions pages/src/content/docs/developer/export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Export开发
sidebar:
order: 3
---
5 changes: 5 additions & 0 deletions pages/src/content/docs/developer/plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: 通讯插件开发
sidebar:
order: 2
---
13 changes: 13 additions & 0 deletions pages/src/content/docs/guides/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ driver-box 没有提供配套的 UI 界面,但开放了大量实用 RestAPI。
driver-box 适用于多种场景,包括智能家居、智慧楼宇、智慧工厂、智慧门店。它促进了设备数据的采集与场景融合,实现了万物皆可连、万物皆可互联、万物皆可智联。

## 名词解释
### 插件(Plugin)
在 driver-box 中,「**插件**」这个词专指:通讯插件,例如:Http插件、Modbus插件、Bacnet插件。

插件是 driver-box 提供了一项开放性能力,如若内置的插件不满足需求,用户可参考《[通讯插件开发](/driver-box/developer/plugin/)》实现一款自定义插件并集成至 driver-box。

### Export
Export,一时找不到合适的中文名词来表示这个单词在 driver-box 中的用途。

它也是 driver-box 提供的一项开放性能力,用于处理 driver-box 向上层传递的设备数据和事件。

以此,我们可以实现类似场景联动、边缘计算、数据上云等一系列高级能力。而这些能力的组织与融合,便形成了完整的边缘引擎产品。

[《Export 开发》](/driver-box/developer/export/)

### 资产库
资产库是 driver-box 框架提供的一种资源管理能力。通过持续沉淀和复用资产库中的已有资源,逐渐提升项目工程实施效率。
Expand Down

0 comments on commit 25e1450

Please sign in to comment.