Calling commands - 调用设备命令

原创
2022/04/16 14:59
阅读数 83

目录:https://my.oschina.net/zgldh/blog/5514606
原文:https://docs.edgexfoundry.org/2.1/walk-through/Ch-WalkthroughCommands/

Calling commands 调用设备命令

Recall that the device profile (the camera-monitor-profile in this walkthrough) included a number of commands to get/set (read or write) information from any device of that type. Also recall that the device (the countcamera1 in this walkthrough) was associated to the device profile (again, the camera-monitor-profile) when the device was provisioned.
回想一下,设备配置档案(本演练中的 camera-monitor-profile )里包含许多用于从该类型的任何设备获取/设置(读取或写入)信息的命令。还记得在配置设备时,该设备(本演练中的 countcamera1)与设备配置文件(即camera-monitor-profile)是有相关的。

See core command API for more details.
查看核心命令 API以了解更多细节。

With the setup complete, you can ask the core command micro service for the list of commands associated to the device (the countcamera1). The command micro service exposes the commands in a common, normalized way that enables simplified communications with the devices for
设备配置完成后,您可以向核心命令微服务询问与设备(countcamera1)关联的命令列表。该核心命令微服务对外提供了通用的、标准化的方式命令,从而简化了如下方面与设备的通信过程

  • other micro services within EdgeX Foundry (for example, an edge analytics or rules engine micro service)
    EdgeX Foundry 中的其他微服务(例如,边缘计算分析或规则引擎微服务)
  • other applications that may exist on the same host with EdgeX Foundry (for example, a management agent that needs to shutoff a sensor)
    与 EdgeX Foundry 可能存在于同一主机上的其他应用程序(例如,一个可以开关传感器的管理服务)
  • any external system that needs to command those devices (for example, a cloud-based application that determined the need to modify the settings on a collection of devices)
    任何需要向设备发送命令的外部系统(例如,一个云上应用程序需要批量修改多个设备的配置项)

Walkthrough - Commands 演练 - 命令

Use either the Postman or Curl tab below to walkthrough getting the list of commands.
使用 Postman 或 Curl 来获取命令列表。

Postman
Make a GET request to http://localhost:59882/api/v2/device/name/countcamera1.
发起一个GET请求到http://localhost:59882/api/v2/device/name/countcamera1

Note 提示
Please note the change in port for the command request above. We are no longer calling on core metadata in this part of the walkthrough. The command micro service is at port 59882 by default.
请注意修改上面命令的端口号。因为我们不是向核心元数据服务发起请求,而是向核心命令服务发送。该服务默认端口号是59882。

Curl
Make a curl GET request as shown below.
使用如下 Curl 命令,发起一个 GET 请求。

curl -X GET localhost:59882/api/v2/device/name/countcamera1 | json_pp

Note 提示
Please note the change in port for the command request above. We are no longer calling on core metadata in this part of the walkthrough. The command micro service is at port 59882 by default.
请注意修改上面命令的端口号。因为我们不是向核心元数据服务发起请求,而是向核心命令服务发送。该服务默认端口号是59882。

Explore all of the URLs returned as part of this response! These are the URLs that clients (internal or external to EdgeX) can call to trigger the various get/set (read and write) offerings on the Device. However, do take note that the host for the URLs is edgex-core-command. This is the name of the host for core command inside Docker. To exercise the URL outside of Docker, you would have to use the name of the system host (localhost if executing on the same box).
你看到返回信息里面有很多 URL 地址,客户端(EdgeX 内部或外部)可以访问这些 URL 地址,来执行设备上的各种获取/设置(读取和写入)动作。但是,请注意 URL 的主机是 edgex-core-command。这是 Docker 中核心命令的主机名。要在 Docker 之外访问这些 URL,您必须使用宿主机的名称(如果在同一机器上执行,则为 localhost)。

Check the Events 获取事件数据

While we're at it, check that no data has yet been shipped to core data from the camera device. Since the device service and device in this demonstration are wholly manually driven by you, no sensor data should yet have been collected. You can test this theory by asking for the count of events in core data.
在我们进行此操作时,请检查是否有任何数据从摄像机传输到核心数据服务里。由于本演示中的设备服务和设备完全由您手动操作,因此肯定尚未收集任何传感器数据。您可以通过请求核心数据服务中的事件计数来验证这一点。

Walkthrough - Events 演练 - 事件

Use either the Postman or Curl tab below to walkthrough getting the list of events.
使用 Postman 或 Curl 来获取事件列表。

Postman
Make a GET request to http://localhost:59880/api/v2/event/count/device/name/countcamera1.
发起一个 GET 请求到 http://localhost:59880/api/v2/event/count/device/name/countcamera1

Curl
Make a curl GET request as shown below.
使用如下命令,发起一个 Curl 的 GET 请求。

curl -X GET localhost:59880/api/v2/event/count/device/name/countcamera1

The response returned should indicate no events for the camera in core data.
得到的响应中表示了目前没有收到任何摄像机的事件。

{"apiVersion":"v2","statusCode":200,"Count":0}

Execute a Command 执行一个命令

While there is no real device or device service in this walkthrough, EdgeX doesn't know that. Therefore, with all the configuration and setup you have performed, you can ask EdgeX to set the scan depth or set the snapshot duration to the camera, and EdgeX will dutifully try to perform the task. Of course, since no device service or device exists, as expected EdgeX will ultimately responds with an error. However, through the log files, you can see a command made of the core command micro service, attempts to call on the appropriate command of the fictitious device service that manages our fictitious camera.
虽然本演练中没有真正的设备或设备服务,但 EdgeX 并不知道这一点。因此,通过您之前执行的所有配置步骤,您可以要求 EdgeX 设置摄像机的扫描深度或拍照间隔时间,EdgeX 将努力去尝试执行命令。当然,由于不存在任何设备服务或设备,因此 EdgeX 最终会返回错误。但是,通过日志文件,我们可以看到核心命令微服务发出的命令,试图调用管理我们虚拟的摄像头的虚拟设备服务的相应命令。

For example sake, let's launch a command to set the scan depth of countcamera1 (the name of the single human/dog counting camera device in EdgeX right now). The first task to launch a request to set the scan depth is to get the URL for the command to set or write a new scan depth on the device. Return to the results of the request to get a list of the commands by the device name above.
例如,让我们发起一个命令来设置countcamera1(人狗计数摄像机在 EdgeX 里的名称)的扫描深度。首先我们要得到该命令的 URL,然后才能构造一个命令来配置 set扫描深度。请查看本页面上方Walkthrough - Commands 演练 - 命令以获取命令列表。

Locate and copy the URL and path for the set depth command. Below is a picture containing a slice of the JSON returned by the GET request above and desired set Command URL highlighted - yours will vary based on IDs.
找到并复制设置 set 扫描深度命令的 URL 和路径。如下图,其中包含上面 GET 请求返回的 JSON 片段,并突出显示了所需的设置 set 命令的 URL - 您实际看到的 ID 可能有所不同。

Walkthrough - Actuation Command 驱使该命令

Use either the Postman or Curl tab below to walkthrough actuating the device.
使用 Postman 或 Curl 来演练如何驱使该设备。

Postman
Make a PUT request to http://localhost:59882/api/v2/device/name/countcamera1/ScanDepth with the following body.
发起一个PUT请求到http://localhost:59882/api/v2/device/name/countcamera1/ScanDepth 请求体如下:

{"depth":"9"}

Curl
Make a curl PUT request as shown below.
使用如下命令,发起一个 Curl 的 PUT 请求。

curl -X PUT -d '{"depth":"9"}' localhost:59882/api/v2/device/name/countcamera1/ScanDepth

Warning 警告
Notice that the URL above is a combination of both the command URL and path you found from your command list.
注意上面的 URL 地址是由命令服务地址和你找到的命令路径组成的。

Check Command Service Log 检查命令服务日志

Again, because no device service (or device) actually exists, core command will respond with a Failed to send a http request error. However, checking the logging output will prove that the core command micro service did receive the request and attempted to call on the non-existent device service (at the address provided for the device service - defined earlier in this walkthrough) to issue the actuating command. To see the core command service log issue the following Docker command :
上面提到过,由于实际上不存在设备服务和设备,核心命令服务将返回 Failed to send a http request 错误。但是,通过检查日志可以证明核心命令微服务确实收到了请求,并尝试调用不存在的设备服务(访问的正是之前章节定义过的设备服务的地址)以发出执行命令。要查看核心命令服务日志,请运行以下 Docker 命令:

docker logs edgex-core-command

The last lines of the log entries should highlight the attempt to contact the non-existent device.
日志的最后几行表明了系统尝试去访问不存在的设备。
(译者注:地址camera-device-service:59990

level=ERROR ts=2021-09-16T20:50:09.965368572Z app=core-command source=http.go:47 X-Correlation-ID=49cc97f5-1e84-4a46-9eb5-543ae8bd5284 msg="failed to send a http request -> Put \"camera-device-service:59990/api/v2/device/name/countcamera1/ScanDepth?\": unsupported protocol scheme \"camera-device-service\""
...

上一篇:Provision a device - 生成一个设备
下一篇:Sending events and reading data - 发送事件然后读取数据

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部