安装docker和docker-compose

参考下面文档完成安装

  • https://linuxize.com/post/how-to-install-and-use-docker-on-ubuntu-18-04/
  • https://linuxize.com/post/how-to-install-and-use-docker-compose-on-ubuntu-18-04/
  • 清华docker镜像 安装完成后使用docker命令查看是否安装成功

docker version

iot@flyway:~$ docker version
Client: Docker Engine - Community
 Version: 19.03.1
 API version: 1.40
 Go version: go1.12.5
 Git commit: 74b1e89
 Built: Thu Jul 25 21:21:05 2019
 OS/Arch: linux/amd64
 Experimental: false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/version: dial unix /var/run/docker.sock: connect: permission denied

docker命令需要sudo权限

sudo docker version

iot@flyway:~$ sudo docker version
[sudo] password for iot:
Client: Docker Engine - Community
 Version: 19.03.1
 API version: 1.40
 Go version: go1.12.5
 Git commit: 74b1e89
 Built: Thu Jul 25 21:21:05 2019
 OS/Arch: linux/amd64
 Experimental: false
Server: Docker Engine - Community
 Engine:
  Version: 19.03.1
  API version: 1.40 (minimum version 1.12)
  Go version: go1.12.5
  Git commit: 74b1e89
  Built: Thu Jul 25 21:19:41 2019
  OS/Arch: linux/amd64
  Experimental: false
 containerd:
  Version: 1.2.6
  GitCommit: 894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version: 1.0.0-rc8
  GitCommit: 425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version: 0.18.0
  GitCommit: fec3683

查看docker-compose

sudo docker-compose version

iot@flyway:~$ sudo docker-compose version
docker-compose version 1.23.1, build b02f1306
docker-py version: 3.5.0
CPython version: 3.6.7
OpenSSL version: OpenSSL 1.1.0f 25 May 2017

运行EdgeX

下载EdgeX foundry compose文件, 脚本都在https://github.com/edgexfoundry/developer-scripts下面,我选用的是 https://github.com/edgexfoundry/developer-scripts/blob/master/releases/delhi/compose-files/docker-compose-delhi-0.7.1.yml, 将它名字修改为docker-compose.yml放到机器上 首先,使用下面命令将docker image从docker hub上下载到本地

docker-compose pull

打印如下

iot@flyway:~$ sudo docker-compose pull
[sudo] password for iot:
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 device-random ... done
Pulling portainer ... done

接下来启动EdgeX Foundry微服务

docker-compose up -d

执行结果如下

iot@flyway:~$ sudo docker-compose up -d
Starting edgex-files ... done
Starting edgex-support-rulesengine ... done
Starting edgex-mongo ... done
Starting iot_portainer_1_61ccf44183fe ... done
Starting edgex-core-consul ... done
Starting edgex-config-seed ... done
Starting edgex-support-logging ... done
Starting edgex-core-metadata ... done
Starting edgex-core-data ... done
Starting edgex-support-notifications ... done
Starting edgex-core-command ... done
Starting edgex-support-scheduler ... done
Starting edgex-export-client ... done
Starting edgex-device-random ... done
Starting edgex-device-virtual ... done
Starting edgex-export-distro ... done

也可以针对单个image做启动

docker-compose up -d volume
docker-compose up -d config-seed
docker-compose up -d mongo
docker-compose up -d logging
docker-compose up -d notifications
docker-compose up -d metadata
docker-compose up -d data
docker-compose up -d command
docker-compose up -d scheduler
docker-compose up -d export-client
docker-compose up -d export-distro
docker-compose up -d rulesengine
docker-compose up -d device-virtual

最后,使用docker ps -a来确认是否所有的容器都已经被下载及启动

连接设备

EdgeX Foundry 提供了服务Random Number Device Service 来用于测试, 它可以根据配置的范围返回一个随机数. 运行这个服务的配置已经包含在一开始下载的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

通过下面命令启动Random Device Service

docker-compose up -d device-random 该设备服务会自动注册一个名为Random-Integer-Generator01的设备, 并发送随机数到EdgeX. 通过下面命令,你可以查询EdgeX的Logging服务来验证信息正在被发送

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

结果如下

[{"id":"5d4167cf0e360800017ec7bc","pushed":0,"device":"Random-Integer-Generator01","created":1564567503005,"modified":0,"origin":1564567503002,"schedule":null,"event":null,"readings":[{"id":"5d4167cf0e360800017ec7bd","pushed":0,"created":1564567503005,"origin":1564567503002,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int8","value":"-67"}]},{"id":"5d4167cf0e360800017ec7be","pushed":0,"device":"Random-Integer-Generator01","created":1564567503006,"modified":0,"origin":1564567503005,"schedule":null,"event":null,"readings":[{"id":"5d4167cf0e360800017ec7bf","pushed":0,"created":1564567503006,"origin":1564567503004,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int16","value":"-5653"}]},{"id":"5d4167cf0e360800017ec7ba","pushed":0,"device":"Random-Integer-Generator01","created":1564567503003,"modified":0,"origin":1564567503002,"schedule":null,"event":null,"readings":[{"id":"5d4167cf0e360800017ec7bb","pushed":0,"created":1564567503003,"origin":1564567503002,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"-136860958"}]},{"id":"5d4167d40e360800017ec7c0","pushed":0,"device":"Random-Integer-Generator01","created":1564567508002,"modified":0,"origin":1564567508000,"schedule":null,"event":null,"readings":[{"id":"5d4167d40e360800017ec7c1","pushed":0,"created":1564567508002,"origin":1564567508000,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int8","value":"-7"}]},{"id":"5d4167d40e360800017ec7c2","pushed":0,"device":"Random-Integer-Generator01","created":1564567508002,"modified":0,"origin":1564567508000,"schedule":null,"event":null,"readings":[{"id":"5d4167d40e360800017ec7c3","pushed":0,"created":1564567508002,"origin":1564567508000,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int32","value":"832330401"}]}]

连接应用

EdgeX 为各种云服务和应用提供了exporters. 为了简化实现,我们会使用一个公共的MQTT broker,它由HiveMQ提供,接下来观察EdgeX将readings自动推送给它. 使用下面名叫,将一个应用注册为export client:

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

你可以用任意MQTT客服端连接该broker去观察发送的数据. HiveMQ提供了一个web-based client,通过该工具注册主题“EdgeXQuickStartGuide”,然后就就可以看到随机数readings了. 注册成功后,打印消息如下 你也可以通过Mosquitto CLI工具来验证readings正在被发送:

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

控制设备

edgex不仅能够从设备读取数据,同时他也能够控制设备。 当edgex服务注册了一个设置之后,它需要提供Device Profile文件来描述从设备中能读到的数据,以及控制它的命令。 当Random Number Device Service注册了设备Random-Integer-Generator01, 它使用profile来定义生成随机数的最小和最大值范围。 你不需要直接用命令来控制设备,可以借助EdgeX Foundry命令来完成,第一步是通过Command服务来查询哪些针对你的设备有哪些命令可以用来控制

curl http://localhost:48082/api/v1/device/name/Random-Integer-Generator01 该命令会返回很多JSON,因为有很多命令可以用于控制你的设备。我们选用其中一个命令如下

 {
  "created":1564567498590,
  "modified":0,
  "origin":0,
  "id":"5d4167ca9f8fc2000190d911",
  "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/5d4167ca9f8fc2000190d916/command/5d4167ca9f8fc2000190d911"
  },
  "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/5d4167ca9f8fc2000190d916/command/5d4167ca9f8fc2000190d911"
  }
 },

这条命令包含getput选项,get调用会返回一个随机数,这个命令也会被自动调用发送数据到其余edgex, 你也可以要它提供的命令手动调用

curl http://localhost:48082/api/v1/device/5d4167ca9f8fc2000190d916/command/5d4167ca9f8fc2000190d911 注意: 上面的命令中,我把edgex-core-command替换成了localhost, 因为EdgeX Foundry服务在docker里运行,它能够识别内部主机名hostname edgex-core-command, 但是如何要从外部调用的话,需要指定主机地址,可以用localhost或者ip 该命令会返回如下的JSON结果

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

readings的默认范围是-128到127,我们可以通过调用put命令来限制最小和最大值,下面命令中,我们将范围限制在0~100

curl -X PUT -d '[ {"Min_Int8": "0", "Max_Int8": "100"} ]' http://localhost:48082/api/v1/device/5d4167ca9f8fc2000190d916/command/5d4167ca9f8fc2000190d911 你也可以在一条命令里指定多个请求,根据command里put的定义在[]完成相应的设置。 现在,再通过get命令调用,它的返回值总保持在0~100内。

iot@flyway:~$ sudo curl http://localhost:48082/api/v1/device/5d4167ca9f8fc2000190d916/command/5d4167ca9f8fc2000190d911
{"id":"","pushed":0,"device":"Random-Integer-Generator01","created":0,"modified":0,"origin":1564992610479,"schedule":null,"event":null,"readings":[{"id":"","pushed":0,"created":0,"origin":1564992610479,"modified":0,"device":"Random-Integer-Generator01","name":"RandomValue_Int8","value":"76"}]}

参考文档

评论