EdgeX Foundry使用教程

EdgeX Foundry使用教程

  • 1. 运行前提
    • 1.1 安装Docker
    • 1.2 安装Docker Compose
      • 1.2.1 下载Docker Compose
      • 1.2.2 修改文件的可执行权限
      • 1.2.3 通过查看Docker Compose的版本验证是否成功安装
  • 2. 运行EdgeX
    • 2.1 下载yml文件
    • 2.2 下载镜像
    • 2.3 启动微服务
    • 2.4 验证容器已启动
  • 3. 连接设备
    • 3.1 开启虚拟设备微服务
    • 3.2 启动虚拟设备微服务
    • 3.3 查看设备发送的数据
  • 4. 连接应用
    • 4.1 注册客户端服务
    • 4.2 注册客户端服务
  • 5. 设备控制
    • 5.1 查看可用指令
    • 5.2 手动获取设备发送数据
    • 5.3 手动控制设备产生的数据范围

根据 官网的操作指导,下面记录下在Ubuntu上运行EdgeX(Ubuntu)的操作流程(官网指导可能会有更新,下面的仅供参考)。

1. 运行前提

1.1 安装Docker

可参考ubuntu环境在线安装Docker

1.2 安装Docker Compose

根据官网指导,安装Docker Compose

1.2.1 下载Docker Compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

1.2.2 修改文件的可执行权限

$ sudo chmod +x /usr/local/bin/docker-compose

1.2.3 通过查看Docker Compose的版本验证是否成功安装

$ docker-compose --version

2. 运行EdgeX

2.1 下载yml文件

$ wget https://raw.githubusercontent.com/edgexfoundry/developer-scripts/master/compose-files/docker-compose.yml

2.2 下载镜像

使用原来的镜像源可能速度比较慢,建议使用国内镜像源:
Docker 官方中国区:https://registry.docker-cn.com
网易:http://hub-mirror.c.163.com
中国科学技术大学:https://docker.mirrors.ustc.edu.cn
先修改/etc/docker/daemon.json:

$ vi /etc/docker/daemon.json

添加如下内容:

{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

重新加载守护进程,然后重启docker:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

可查看Registry Mirrors修改是否生效:

$ sudo docker info

切换完镜像源后下载镜像:

$ sudo docker-compose pull

下载的时候经常会在某个状态卡住,多次尝试未果后选择科学上网瞬间搞定了>o<:

Pulling volume         ... done
Pulling consul         ... done
Pulling config-seed    ... done
Pulling mongo          ... done
Pulling logging        ... done
Pulling notifications  ... done
Pulling metadata       ... done
Pulling data           ... done
Pulling command        ... done
Pulling scheduler      ... done
Pulling export-client  ... done
Pulling export-distro  ... done
Pulling rulesengine    ... done
Pulling device-virtual ... done
Pulling portainer      ... done

镜像安装可参考github上提供的其他方法

2.3 启动微服务

$ sudo docker-compose up -d

因为网络问题反复执行了好几次才没报错:

edgex-files is up-to-date
edgex-support-rulesengine is up-to-date
Starting edgex_portainer_1 ... 
edgex-core-consul is up-to-date
Starting edgex-config-seed ... 
Starting edgex-config-seed ... done
edgex-support-logging is up-to-date
edgex-core-data is up-to-date
edgex-core-metadata is up-to-date
edgex-support-notifications is up-to-date
edgex-export-client is up-to-date
edgex-core-command is up-to-date
edgex-support-scheduler is up-to-date
Starting edgex_portainer_1 ... done
edgex-device-virtual is up-to-date

2.4 验证容器已启动

$ sudo docker-compose ps
返回如下结果:

     Name                          Command               State                                                                  Ports                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
edgex-config-seed             /bin/sh -c /edgex/cmd/conf ...   Exit 0                                                                                                                                     
edgex-core-command            /core-command --consul --p ...   Up       0.0.0.0:48082->48082/tcp                                                                                                          
edgex-core-consul             docker-entrypoint.sh agent ...   Up       8300/tcp, 8301/tcp, 8301/udp, 8302/tcp, 8302/udp, 0.0.0.0:8400->8400/tcp, 0.0.0.0:8500->8500/tcp, 0.0.0.0:8600->8600/tcp, 8600/udp
edgex-core-data               /core-data --consul --prof ...   Up       0.0.0.0:48080->48080/tcp, 0.0.0.0:5563->5563/tcp                                                                                  
edgex-core-metadata           /core-metadata --consul -- ...   Up       0.0.0.0:48081->48081/tcp, 48082/tcp                                                                                               
edgex-device-virtual          /bin/sh -c java -jar -Djav ...   Up       0.0.0.0:49990->49990/tcp                                                                                                          
edgex-export-client           /export-client --consul -- ...   Up       0.0.0.0:48071->48071/tcp                                                                                                          
edgex-export-distro           /export-distro --consul -- ...   Up       0.0.0.0:48070->48070/tcp, 0.0.0.0:5566->5566/tcp                                                                                  
edgex-files                   /bin/sh -c /usr/bin/tail - ...   Up                                                                                                                                         
edgex-mongo                   docker-entrypoint.sh /bin/ ...   Up       0.0.0.0:27017->27017/tcp                                                                                                          
edgex-support-logging         /support-logging --consul  ...   Up       0.0.0.0:48061->48061/tcp                                                                                                          
edgex-support-notifications   /support-notifications --c ...   Up       0.0.0.0:48060->48060/tcp                                                                                                          
edgex-support-rulesengine     /bin/sh -c java -jar -Djav ...   Up       0.0.0.0:48075->48075/tcp                                                                                                          
edgex-support-scheduler       /support-scheduler --consu ...   Up       0.0.0.0:48085->48085/tcp                                                                                                          
edgex_portainer_1             /portainer -H unix:///var/ ...   Up       0.0.0.0:9000->9000/tcp   

3. 连接设备

3.1 开启虚拟设备微服务

EdgeX Foundry提供了虚拟设备以便调试,默认关闭,可修改下载的docker-compose.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

3.2 启动虚拟设备微服务

$ sudo docker-compose up -d device-random

由于之前没有下载过虚拟设备的镜像,执行命令后会重新拉取该镜像:

Pulling device-random (edgexfoundry/docker-device-random-go:0.7.1)...
0.7.1: Pulling from edgexfoundry/docker-device-random-go
76ae2d597c16: Pull complete
Digest: sha256:fa3196f3075f579e105f313f0f9e1a3c6c40b31ce83967cdfa597a1208df41a3
Status: Downloaded newer image for edgexfoundry/docker-device-random-go:0.7.1
edgex-files is up-to-date
edgex-core-consul is up-to-date
edgex-mongo is up-to-date
Starting edgex-config-seed ... done
edgex-support-logging is up-to-date
Starting edgex-core-metadata ... 
Starting edgex-core-metadata ... done
edgex-core-command is up-to-date
Creating edgex-device-random ... done

3.3 查看设备发送的数据

通过EdgeX提供的日志服务查看启动的Random-Integer-Generator01发送的数据:

$ curl http://localhost:48080/api/v1/event/device/Random-Integer-Generator01/10

启动虚拟设备微服务前,返回为[];启动后,能得到类似如下的发送信息:

[{"id":"5ce6b2020e360800014fbb98","pushed":0,"device":"Random-Integer-Generator01","created":1558622722001,"modified":0,"origin":1558622722001,"schedule":null,"event":null,"readings":[{"id":"5ce6b2020e360800014fbb99","pushed":0,"created":1558622722001,"origin":1558622722000,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int16","value":"9162"}]}

4. 连接应用

EdgeX的北向可连接不同的云服务与应用,可通过MQTT broker验证EdgeX的北向数据。

4.1 注册客户端服务

$ curl -X POST -d '{
"name":"QuickStartExport",
"addressable":{
"name":"HiveMQBroker",
"protocol":"tcp",
"address":"broker.hivemq.com",
"port":1883,
"publisher":"EdgeXExportPublisher",
"topic":"EdgeXQuickStartGuide"
},
"format":"JSON",
"filter":{
"deviceIdentifiers":["Random-Integer-Generator01"]
},
"enable":true,
"destination":"MQTT_TOPIC"
}' http://localhost:48071/api/v1/registration

4.2 注册客户端服务

安装了MQTT client的话,可以直接通过命令行订阅设备发布的消息:

$ mosquitto_sub -h broker.hivemq.com -p 1883 -t EdgeXQuickStartGuide

可周期性接收到如下类似的信息:

{"id":"5ce6ba050e36080001cfa68a","pushed":0,"device":"Random-Integer-Generator01","created":1558624773002,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68b","pushed":0,"created":1558624773002,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int8","value":"104"}]}
{"id":"5ce6ba050e36080001cfa68c","pushed":0,"device":"Random-Integer-Generator01","created":1558624773002,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68d","pushed":0,"created":1558624773002,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"411637861"}]}
{"id":"5ce6ba050e36080001cfa68e","pushed":0,"device":"Random-Integer-Generator01","created":1558624773003,"modified":0,"origin":1558624773001,"schedule":null,"event":null,"readings":[{"id":"5ce6ba050e36080001cfa68f","pushed":0,"created":1558624773003,"origin":1558624773001,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int16","value":"2984"}]}

5. 设备控制

使用EdgeX不能直接给设备下发指令,需要统一通过指令微服务(Command Service)下发

5.1 查看可用指令

$ curl http://localhost:48082/api/v1/device/name/Random-Integer-Generator01

执行后会返回如下的JSON数据:

{
	"id": "5ce6b1fd9f8fc20001eef84e",
	"name": "Random-Integer-Generator01",
	"adminState": "UNLOCKED",
	"operatingState": "ENABLED",
	"lastConnected": 0,
	"lastReported": 0,
	"labels": ["device-random-example"],
	"location": null,
	"commands": [{
		"created": 1558622717068,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef849",
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef849"
		},
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef849"
		}
	}, {
		"created": 1558622717072,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef84a",
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84a"
		},
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84a"
		}
	}, {
		"created": 1558622717073,
		"modified": 0,
		"origin": 0,
		"id": "5ce6b1fd9f8fc20001eef84b",
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b"
		},
		"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/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b"
		}
	}]
}

5.2 手动获取设备发送数据

容器外,需要使用宿主机地址去访问URL,URL后半部分内容与之前的json内容保持一致:

$ curl http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

执行后会返回如下的JSON数据:

{
	"id": "",
	"pushed": 0,
	"device": "Random-Integer-Generator01",
	"created": 0,
	"modified": 0,
	"origin": 1558626326372,
	"schedule": null,
	"event": null,
	"readings": [{
		"id": "",
		"pushed": 0,
		"created": 0,
		"origin": 1558626326372,
		"modified": 0,
		"device": "Random-Integer-Generator01",
		"name": "RandomValue_Int32",
		"value": "1225254283"
	}]
}

5.3 手动控制设备产生的数据范围

虚拟设备默认数据范围是 Min_Int32 ~ Max_Int32,现在使用指令微服务中的put方法修改为0 ~ 100:

$ curl -X PUT -d '[ {"Min_Int32": "0", "Max_Int32": "100"} ]' http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

手动查看设备上报的结果

$ curl http://localhost:48082/api/v1/device/5ce6b1fd9f8fc20001eef84e/command/5ce6b1fd9f8fc20001eef84b

可见随机值在0 ~ 100之间:

{"id":"","pushed":0,"device":"Random-Integer-Generator01","created":0,"modified":0,"origin":1558627626973,"schedule":null,"event":null,"readings":[{"id":"","pushed":0,"created":0,"origin":1558627626973,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"82"}]}

至此,利用EdgeX提供的虚拟设备将设备数据发送到云端以及使用指令服务控制设备的操作就完成了。

你可能感兴趣的:(EdgeX,物联网,边缘计算)