目录
1、安装基础软件
2、运行 EdgeX
2.1 下载EdgeX compose文件
2.2文件修改好后,运行下面的命令:
3连接设备测试
4 服务连接
4.1安装HiveMQ
4.2注册应用到EdgeX
5 设备控制
5.1 Device Profile
5.2 执行命令
6 总结
以windows 10为例,使用windows 10 自带Hyper-V。
运行EdgeX 最快的方式就是使用预先构建好的Docker images。首先要把Docker安装好。
Docker 分为 Docker CE(社区版) 和Docker EE(企业版)。我们这里直安装Docker CE
安装手册见:https://docs.docker.com/docker-for-windows/install/
下载的windows 版 Docker CE 自带Docker Compose,就不需要单独安装了。
安装Docker的手册:https://www.runoob.com/docker/windows-docker-install.html
Windows 还需要下载安装CURL :https://curl.haxx.se/windows/
文件名称改为docker-compose.yml。
下载地址:https://raw.githubusercontent.com/edgexfoundry/developer-scripts/master/compose-files/docker-compose-delhi-0.7.1.yml。
文件中 有部分被注释掉的配置打开如下:
device-random:
image: edgexfoundry/docker-device-random-go:0.7.1
ports:
- "49988:49988"
container_name: edgex-device-random
hostname: edgex-device-random
networks:
- edgex-network
volumes:
- db-data:/data/db
- log-data:/edgex/logs
- consul-config:/consul/config
- consul-data:/consul/data
depends_on:
- data
- command
这个是设备服务,后面做测试用的,其他的设备服务也可以下载,如 device-mqtt,device-modbus,device-bluetooth,但是暂时用不到。
一定要cd到在docker-compose.yml所在的文件夹内执命令。
(1)下载EdgeX Foundry所有的docker镜像,由于镜像是国外的可能会出错,重试几次。
(2)通过docker-compose启动所有的服务,-d: 表示后台运行容器,并返回容器ID;
docker-compose up –d
(3)检查是否启动成功,查看容器的状态。
docker-compose ps
EdgeX Foundry 提供了一个用于测试 的设备服务Random Number Device Service,它可以返回配置范围内的一个随机数。就是上面我放开注释的那个设备配置。这个设备服务会注册一个设备Random-Integer-Generator01,它会不停的发送随机数到EdgeX。
curl http://localhost:48080/api/v1/event/device/Random-Integer-Generator01/1
EdgeX 为云服务和应用提供了很多的消息推送方式。我们使用 HiveMQ 作为MQTT的基础消息组件,然后查看EdgeX是否能够将Random-Integer-Generator01设备的消息推送到HiveMQ中。
安装手册见:https://www.hivemq.com/downloads/docker/。这里直接使用Docker安装部署。下面的命令会自动下载并运行hivemq4的镜像。
docker run -p 8080:8080 -p 1883:1883 hivemq/hivemq4
curl -X POST -d '{
"name":"QuickStartExport",
"addressable":{
"name":"HiveMQBroker",
"protocol":"tcp",
"address":"localhost",
"port":1883,
"publisher":"EdgeXExportPublisher",
"topic":"EdgeXQuickStartGuide"
},
"format":"JSON",
"filter":{
"deviceIdentifiers":["Random-Integer-Generator01"]
},
"enable":true,
"destination":"MQTT_TOPIC"
}' http://localhost:48071/api/v1/registration
然后通过HiveMQ 客户端,查看到消息是否已经推送成功
从设备中读取数据只是Edgex的部分功能,它还可以用来控制设备。EdgeX提供了一个Device Profile 功能,它用来注册设备。Device Profile包含两部分内容,一部分是如何从设备读取数据,另一部分就是控制设备的命令。如下图所示:
Device Profile 包含deviceResources和deviceCommands两部分的定义。
我们在上面的示例中注册的设备Random-Integer-Generator01,就是使用了Device Profile来定义的。 下载链接:https://github.com/edgexfoundry/device-random/blob/master/cmd/res/device.random.yaml。Profile定义了生成随机数最大值和最小值的命令。这样就可以通过EdgeX Foundry提供的命令服务来控制设备,而不是直接在设备上执行命令。第一步要做的就是找到设备支持的命令。
下面截取了Device Profile中 deviceCommands的部分内容:
deviceCommands:
-
name: "GenerateRandomValue_Int8"
get:
- { operation: "get", object: "RandomValue_Int8", parameter: "RandomValue_Int8" }
set:
- { operation: "set", object: "Min_Int8", parameter: "Min_Int8" }
- { operation: "set", object: "Max_Int8", parameter: "Max_Int8" }
-
name: "GenerateRandomValue_Int16"
get:
- { operation: "get", object: "RandomValue_Int16", parameter: "RandomValue_Int16" }
set:
- { operation: "set", object: "Min_Int16", parameter: "Min_Int16" }
- { operation: "set", object: "Max_Int16", parameter: "Max_Int16" }
可以通过设备服务提供的查询接口来获取设备命令列表,查询到的是一个JSON串。可以看到查询到的设备命令就是Device Profile配置的命令。
Curl http://localhost:48082/api/v1/device/name/Random-Integer-Generator01
{
"id": "5cdba2599f8fc2000115ef01",
"name": "Random-Integer-Generator01",
"adminState": "UNLOCKED",
"operatingState": "ENABLED",
"lastConnected": 0,
"lastReported": 0,
"labels": [
"device-random-example"
],
"location": null,
"commands": [
{
"created": 1557897817507,
"modified": 0,
"origin": 0,
"id": "5cdba2599f8fc2000115eefc",
"name": "GenerateRandomValue_Int8",
"get": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int8",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": [
"RandomValue_Int8"
]
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefc"
},
"put": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int8",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": []
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"parameterNames": [
"Min_Int8",
"Max_Int8"
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefc"
}
},
{
"created": 1557897817508,
"modified": 0,
"origin": 0,
"id": "5cdba2599f8fc2000115eefd",
"name": "GenerateRandomValue_Int16",
"get": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int16",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": [
"RandomValue_Int16"
]
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefd"
},
"put": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int16",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": []
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"parameterNames": [
"Min_Int8",
"Max_Int8"
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefd"
}
},
{
"created": 1557897817518,
"modified": 0,
"origin": 0,
"id": "5cdba2599f8fc2000115eefe",
"name": "GenerateRandomValue_Int32",
"get": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int32",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": [
"RandomValue_Int32"
]
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefe"
},
"put": {
"path": "/api/v1/device/{deviceId}/GenerateRandomValue_Int32",
"responses": [
{
"code": "200",
"description": null,
"expectedValues": []
},
{
"code": "503",
"description": "service unavailable",
"expectedValues": []
}
],
"parameterNames": [
"Min_Int32",
"Max_Int32"
],
"url": "http://edgex-core-command:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefe"
}
}
]
}
每个命令都 包含get 和put操作。以上面的第一个命令为例:
curl http://localhost:48082/api/v1/device/5cdba2599f8fc2000115ef01/command/5cdba2599f8fc2000115eefc
get 会返回一个随机数,这个请求会自动的发送命令到设备,并将采集的数据通过Rest接口返回给Edgex。这里把 edgex-core-command 替换成了localhost,因为edgex-core-command是Docker容器中定义的内部域名,我们要从Docker外部调用就要使用localhost。
返回结果如下:
{
"id": "",
"pushed": 0,
"device": "Random-Integer-Generator01",
"created": 0,
"modified": 0,
"origin": 1557922255644,
"schedule": null,
"event": null,
"readings": [
{
"id": "",
"pushed": 0,
"created": 0,
"origin": 1557922255644,
"modified": 0,
"device": "Random-Integer-Generator01",
"name": "RandomValue_Int8",
"value": "114"
}
]
}
随机数默认的取值范围是 -128到127。可以通过put命令重新设定 取值范围,
curl -X PUT -d '[
{"Min_Int8": "0", "Max_Int8": "100"}
]' http://localhost:48082/api/v1/device/5c0e8a259f8fc20001a5d230/command/5c0e8a259f8fc20001a5d22b
这时再去执行get命令的时候,返回值就会在 0 到100之间。
按照上面的流程操作完,你就有了一个完整的EdgeX开发环境,你可以从设备采集数据发送到云端,也可以通过EdgeX控制你的设备。