码迷,mamicode.com
首页 > 其他好文 > 详细

EdgeX Foundry初体验(三)--开启随机设备服务及如何控制设备

时间:2020-06-17 10:49:24      阅读:43      评论:0      收藏:0      [点我收藏+]

标签:虚拟   highlight   类型   随机   表示   create   udp   cond   date   

技术图片

=====1. 配置文件的修改 ======

将文件docker-compose.yml中的内容进行修改,开启随机设备服务

 ~]# ll
total 16
-rw-------. 1 root root  1249  anaconda-ks.cfg
-rw-r--r--. 1 root root 11451  docker-compose.yml
 ~]# pwd
/root

去掉random前面的#号

~]#vim 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

。。。。。。。。

去掉#以后,改为下面的内容:

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-random

~]# docker-compose config --services
volume
consul
config-seed
mongo
logging
notifications
metadata
data
command
scheduler
export-client
export-distro
rulesengine
device-virtual
device-random
portainer

 

=====2.,启动新添加的微服务容器======  

启动它:

 ~]# docker-compose up -d device-random
edgex-files is up-to-date
edgex-mongo is up-to-date
edgex-core-consul is up-to-date
Starting edgex-config-seed ... done
edgex-support-logging is up-to-date
edgex-core-metadata is up-to-date
edgex-core-data is up-to-date
edgex-core-command is up-to-date
Starting edgex-device-random ... done

 

 查看是否正常启动:

~]# docker-compose ps -a
           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-random           /device-random --registry  ...   Up       0.0.0.0:49988->49988/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                                                                                                          
root_portainer_1              /portainer -H unix:///var/ ...   Exit 1    

 

 当device-random这个微服务正常启动以后,会自动注册一个设备,名字叫做Random-Integer-Generator01,此设备会将随机数发送到EdgeX,此设备会在下一篇的文中通过UI界面展示出来,暂时不用过多关注,就理解为有个虚拟设备挂到了edgex上。

 验证device-random是否在正常发送数据到edgex,可以通过EdgeX Logging 服务来验证,此服务位于edgex-core-data的event/device中,在浏览器中使用下面的URL来确认:(本机地址为192.168.0.133)

http://192.168.0.133:48080/api/v1/event/device/Random-Integer-Generator01/100

技术图片

 

 

 

这上面的这些数据就是这个random device在发送数据给edgex,可能上面的情况看得不太明显,可以复制数据转换为json文件来查看:

这个random device会发出 

  • RandomValue_Int8、
  • RandomValue_Int16、
  • RandomValue_Int32 

这 3 种属性值,而且三个类型的值不是一起发出,而是每次只发一种。

红色: 设备名称

蓝色:随机数属性

绿色:随机值

 

[
    {
        "id": "5ee0ab3d0e3608000184a581",
        "pushed": 0,
        "device": "Random-Integer-Generator01",
        "created": 1591782205002,
        "modified": 0,
        "origin": 1591782205000,
        "schedule": null,
        "event": null,
        "readings": [
            {
                "id": "5ee0ab3d0e3608000184a582",
                "pushed": 0,
                "created": 1591782205002,
                "origin": 1591782205000,
                "modified": 0,
                "device": "Random-Integer-Generator01",
                "name": "RandomValue_Int32",
                "value": "-1016650009"
            }
        ]
    },
    {
        "id": "5ee0ab3d0e3608000184a583",
        "pushed": 0,
        "device": "Random-Integer-Generator01",
        "created": 1591782205003,
        "modified": 0,
        "origin": 1591782205001,
        "schedule": null,
        "event": null,
        "readings": [
            {
                "id": "5ee0ab3d0e3608000184a584",
                "pushed": 0,
                "created": 1591782205003,
                "origin": 1591782205001,
                "modified": 0,
                "device": "Random-Integer-Generator01",
                "name": "RandomValue_Int16",
                "value": "31326"
            }
        ]
    },
    {
        "id": "5ee0ab3d0e3608000184a585",
        "pushed": 0,
        "device": "Random-Integer-Generator01",
        "created": 1591782205004,
        "modified": 0,
        "origin": 1591782205000,
        "schedule": null,
        "event": null,
        "readings": [
            {
                "id": "5ee0ab3d0e3608000184a586",
                "pushed": 0,
                "created": 1591782205004,
                "origin": 1591782205000,
                "modified": 0,
                "device": "Random-Integer-Generator01",
                "name": "RandomValue_Int8",
                "value": "-49"
            }
        ]
    },
    {
        "id": "5ee0ab420e3608000184a587",
        "pushed": 0,
        "device": "Random-Integer-Generator01",
        "created": 1591782210002,
        "modified": 0,
        "origin": 1591782210001,
        "schedule": null,
        "event": null,
        "readings": [
            {
                "id": "5ee0ab420e3608000184a588",
                "pushed": 0,
                "created": 1591782210002,
                "origin": 1591782210001,
                "modified": 0,
                "device": "Random-Integer-Generator01",
                "name": "RandomValue_Int8",
                "value": "-54"
            }
        ]
    },
    {
        "id": "5ee0ab420e3608000184a589",
        "pushed": 0,
        "device": "Random-Integer-Generator01",
        "created": 1591782210002,
        "modified": 0,
        "origin": 1591782210000,
        "schedule": null,
        "event": null,
        "readings": [
            {
                "id": "5ee0ab420e3608000184a58a",
                "pushed": 0,
                "created": 1591782210002,
                "origin": 1591782210000,
                "modified": 0,
                "device": "Random-Integer-Generator01",
                "name": "RandomValue_Int32",
                "value": "1202123764"
            }
        ]
    },
。。。。。。

 从上面这些行为可以看出  这个device在不断的向edge发送自己的数据。

=====3. 控制设备======

从设备读取数据只是 EdgeX 的一部分功能,我们也可以使用它来控制设备。当设备注册 EdgeX 服务时,它会提供设备配置文件,该设备配置文件描述该设备可用的数据读数,以及可以控制它的命令。 

比如:上面当我们的随机数设备服务注册设备 Random-Integer-Generator01 时,它使用了一个配置文件,该配置文件里定义了用于更改它将生成的随机数的最小值和最大值范围的命令。

 ~]# docker-compose ps -a
           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-random           /device-random --registry  ...   Up       0.0.0.0:49988->49988/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                                                                                                          
root_portainer_1              /portainer -H unix:///var/ ...   Exit 1

(本机IP:192.168.0.133)在浏览器中输入下面的URL以得到配置文件

http://192.168.0.133:48082/api/v1/device/name/Random-Integer-Generator01

技术图片

 同样把配置文件内容用json转为可读模式:

以属性名称为GenerateRandomValue_Int8为例)

红色: 设备名称
绿色: url为可操作Random-Integer-Generator01的控制指令(就是使用这些指令来控制这个设备,假如这个设备有跳跃,跑步,走步等多个动作,一个url表示一个动作或者多个动作)
橙色: 请求url时的动作,Get或者PUT
亮绿色背景色:属性名字
{
    "id": "5ee0ab379f8fc20001df7e1d",
    "name": "Random-Integer-Generator01",
    "adminState": "UNLOCKED",
    "operatingState": "ENABLED",
    "lastConnected": 0,
    "lastReported": 0,
    "labels": [
        "device-random-example"
    ],
    "location": null,
    "commands": [
        {
            "created": 1591782199972,
            "modified": 0,
            "origin": 0,
            "id": "5ee0ab379f8fc20001df7e18",
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e18"
            },
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e18"
            }
        },
        {
            "created": 1591782199973,
            "modified": 0,
            "origin": 0,
            "id": "5ee0ab379f8fc20001df7e19",
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e19"
            },
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e19"
            }
        },
        {
            "created": 1591782199973,
            "modified": 0,
            "origin": 0,
            "id": "5ee0ab379f8fc20001df7e1a",
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e1a"
            },
            "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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e1a"
            }
        }
    ]
}

  使用配置文件中的url请求该设备可以被控制的动作:

"url": "http://edgex-core-command:48082/api/v1/device/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e18"  在请求的时候rul中的edgex-core-command要替换为本机IP

在浏览器中以GET方式请求GenerateRandomValue_Int8的控制指令,可以看到value值是18(默认情况下生成的随机数范围是 -128 到 127)
可以通过PUT方式来修改
GenerateRandomValue_Int8的控制指令值得范围
注意:GET和PUT方式请求的URL内容是一样的,只是请求方式不一样。

技术图片

 通过PUT方式来修改范围,因为在配置文件有这样一个字段parameterNames

"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/5ee0ab379f8fc20001df7e1d/command/5ee0ab379f8fc20001df7e18"
            }

在put方式的body里设置这个参数:

技术图片

 

 在进行get请求的时候,value值就变成了0-10范围内:

技术图片技术图片

 

EdgeX Foundry初体验(三)--开启随机设备服务及如何控制设备

标签:虚拟   highlight   类型   随机   表示   create   udp   cond   date   

原文地址:https://www.cnblogs.com/horizonli/p/13143670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!