EdgeX 安装
1. docker-compose安装
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
2.下载edgex docker-compose文件,参考:https://docs.edgexfoundry.org/2.0/getting-started/quick-start/ 命令如下:
curl https://raw.githubusercontent.com/edgexfoundry/edgex-compose/ireland/docker-compose-no-secty.yml -o docker-compose.yml
3. 添加ui界面
在步骤二中是没有安装ui的,我们可以添加入ui界面,
使用给这个 https://github.com/edgexfoundry/edgex-ui-go 编译出镜像(make docker_xxx看MakeFile文件),然后在 Quick Start - EdgeX Foundry Documentation 指定的 docker-compose.yml文件里面把UI给加入到相同的网络就可以了,大致如下
networks:
edgex-network:
driver: bridge
services:
ui:
container_name: edgex-ui-go
hostname: edgex-ui-go
image: edgexfoundry/edgex-ui:0.0.0-dev
networks:
edgex-network: null
ports:
- 192.168.0.38:4000:4000/tcp
read_only: true
app-service-rules:
container_name: edgex-app-rules-engine
depends_on:
- consul
- data
4. 启动命令
docker-compose up -d
docker-compose ps
5. 访问页面
http://192.168.0.86:4000/#/dashboard # ui界面
http://192.168.0.86:59880/api/v2/event/device/name/Random-Integer-Device (core-data)
http://192.168.0.86:59882/api/v2/device/all (core- command)
http://192.168.0.86:59881/api/v2/deviceprofile/all (core-meta)
6. 效果图
6. 停机命令
docker-compose down # 可以添加-v,把卷都删除掉,一个新的环境
device service(端侧)开发与部署
设备接入
edgex在设备接入的时候: When a device registers with the EdgeX services, it provides a Device Profile that describes both the data readings available from that device, and also the commands that control it. 也就是说组要就是一个Device Profile来描述这个设备,包括它的属性和命令
name: "Random-Boolean-Device"
manufacturer: "IOTech"
model: "Device-Virtual-01"
labels:
- "device-virtual-example"
description: "Example of Device-Virtual"
deviceResources:
-
name: "EnableRandomization_Bool"
isHidden: true
description: "used to decide whether to re-generate a random value"
properties:
valueType: "Bool"
readWrite: "W"
defaultValue: "true"
-
name: "Bool"
isHidden: false
description: "Generate random boolean value"
properties:
valueType: "Bool"
readWrite: "RW"
defaultValue: "true"
-
name: "EnableRandomization_BoolArray"
isHidden: true # 感觉有这个的话就会不会自动生成command 命令
description: "used to decide whether to re-generate a random value"
properties:
valueType: "Bool"
readWrite: "W"
defaultValue: "true"
-
name: "BoolArray"
isHidden: false
description: "Generate random boolean array value"
properties:
valueType: "BoolArray"
readWrite: "RW"
defaultValue: "[true]"
# 这些命令会体现在 http://192.168.0.86:4000/#/metadata/device-center/device-list?profileName=Random-Boolean-Device
deviceCommands:
-
name: "WriteBoolValue"
readWrite: "W"
isHidden: false
resourceOperations:
- { deviceResource: "Bool" }
- { deviceResource: "EnableRandomization_Bool", defaultValue: "false" }
-
name: "WriteBoolArrayValue"
readWrite: "W"
isHidden: false
resourceOperations:
- { deviceResource: "BoolArray" }
- { deviceResource: "EnableRandomization_BoolArray", defaultValue: "false" }
开发实践
参考:https://docs.edgexfoundry.org/2.0/getting-started/Ch-GettingStartedSDK-Go/
需要安装libzmq: sudo apt-get install libzmq3-dev
go需要1.16版本
按上面的例子执行到 "Build your Device Service",make build之后,可以在cmd/device-simple下面看到二进制
我们试着自己跑起来:
export EDGEX_SECURITY_SECRET_STORE=false
# 修改:cmd/device-simple/res/configuration.toml的内容,主要是把host修改为实际部署edgex的地址
./device-simple
# 打开ui,http://192.168.0.86:4000/#/metadata/device-service-list,可以看到会注册一个evice-simple
继续根据:https://docs.edgexfoundry.org/2.0/getting-started/Ch-GettingStartedSDK-Go/, 自定义Device Service,其实就是已有的Device Service 里面添加我们自己的device和device 控制器
1. 在simpledrvie.go里面添加:
/ HandleReadCommands triggers a protocol Read operation for the specified device.
func (s *SimpleDriver) HandleReadCommands(deviceName string, protocols map[string]models.ProtocolProperties, reqs []sdkModels.CommandRequest) (res []*sdkModels.CommandValue, err error) {
s.lc.Debugf("SimpleDriver.HandleReadCommands: protocols: %v resource: %v attributes: %v", protocols, reqs[0].DeviceResourceName, reqs[0].Attributes)
if len(reqs) == 1 {
res = make([]*sdkModels.CommandValue, 1)
# 这是添加的
if reqs[0].DeviceResourceName == "RandomNumber" {
cv, _ := sdkModels.NewCommandValue(reqs[0].DeviceResourceName, common.ValueTypeInt32, int32(rand.Intn(100)))
res[0] = cv
} else if reqs[0].DeviceResourceName == "SwitchButton" {
cv, _ := sdkModels.NewCommandValue(reqs[0].DeviceResourceName, common.ValueTypeBool, s.switchButton)
res[0] = cv
2. 创建Device Profile random-generator-device.yaml,放置在cmd/device-simple/res/profiles下面
name: "RandNum-Device"
manufacturer: "Dell Technologies"
model: "1"
labels:
- "random"
- "test"
description: "random number generator to simulate a device"
deviceResources:
-
name: "RandomNumber"
description: "generated random number"
attributes:
{ type: "random" }
properties:
valueType: "Int32"
readWrite: "R"
3. 创建设备random-generator-device.toml 放置在 cmd/device-simple/res/devices
# Pre-define Devices
[[DeviceList]]
Name = 'RandNum-Device01'
ProfileName = 'RandNum-Device'
Description = 'Random Number Generator Device'
Labels = [ 'random', 'test' ]
[DeviceList.Protocols]
[DeviceList.Protocols.Other]
Address = 'random'
Port = '300'
[[DeviceList.AutoEvents]]
Interval = '10s'
SourceName = 'RandomNumber'
OnChange = false
4. configuration your device service
这一步的configuration.toml与原本的那个配置文件没什么差别
5. 效果
运行起来后:http://192.168.0.86:4000/#/metadata/device-center/device-list?svcName=device-simple,可以看到多了我们自己定义的RandNum-Device01
命令执行