本文为Edgex系列第三篇文章,本篇将对接EdgeX内部自带的虚拟设备。
一、虚拟设备介绍
虚拟设备服务(Virtual Device Service GO)可以模拟不同类型的设备来为核心数据微服务生成事件和读数。另外,用户可以通过命令服务向设备发送命令。虚拟设备服务允许您在没有任何真实设备的情况下执行功能或性能测试。此版本的虚拟设备服务基于Device SDK GO实现,使用 ql(嵌入式 SQL 数据库引擎)来模拟虚拟资源。
架构图如下:
时序图如下:
二、启动虚拟设备服务
在上一讲中,我们使用了make gen ds-virtual no-secty ui
来生成docker-compose文件,ds-virtual
就包含了虚拟设备服务,docker-compose配置如下:
device-virtual: | |
---|---|
container_name: edgex-device-virtual | |
depends_on: | |
- consul | |
- data | |
- metadata | |
environment: | |
CLIENTS_CORE_COMMAND_HOST: edgex-core-command | |
CLIENTS_CORE_DATA_HOST: edgex-core-data | |
CLIENTS_CORE_METADATA_HOST: edgex-core-metadata | |
CLIENTS_SUPPORT_NOTIFICATIONS_HOST: edgex-support-notifications | |
CLIENTS_SUPPORT_SCHEDULER_HOST: edgex-support-scheduler | |
DATABASES_PRIMARY_HOST: edgex-redis | |
EDGEX_SECURITY_SECRET_STORE: "false" | |
MESSAGEQUEUE_HOST: edgex-redis | |
REGISTRY_HOST: edgex-core-consul | |
SERVICE_HOST: edgex-device-virtual | |
hostname: edgex-device-virtual | |
image: edgexfoundry/device-virtual:2.0.0 | |
networks: | |
edgex-network: {} | |
ports: | |
- 59900:59900/tcp | |
read_only: true | |
security_opt: | |
- no-new-privileges:true | |
user: 2002:2001 |
三、使用虚拟设备服务
使用GET命令获取设备数据:curl -X GET localhost:59882/api/v2/device/name/Random-Integer-Device/Int8
返回:
"apiVersion": "v2",
"statusCode": 200,
"event": {
"apiVersion": "v2",
"id": "2e7a428b-8f8e-41dd-b35e-a84feebf8ffc",
"deviceName": "Random-Integer-Device",
"profileName": "Random-Integer-Device",
"sourceName": "Int8",
"origin": 1649322864714120763,
"readings": [{
"id": "5cdebafa-baec-4c3a-8530-9b50c8b8b266",
"origin": 1649322864714120763,
"deviceName": "Random-Integer-Device",
"resourceName": "Int8",
"profileName": "Random-Integer-Device",
"valueType": "Int8",
"binaryValue": null,
"mediaType": "",
"value": "15"
}]
}
}
复制代码
使用PUT命令修改设备数据: curl -X PUT -d '{"Int8": "123", "EnableRandomization_Int8": "false"}' localhost:59882/api/v2/device/name/Random-Integer-Device/Int8
返回:
{"apiVersion":"v2","statusCode":200}
复制代码
再次使用GET命令获取设备数据:curl -X GET localhost:59882/api/v2/device/name/Random-Integer-Device/Int8
返回:
{
"apiVersion": "v2",
"statusCode": 200,
"event": {
"apiVersion": "v2",
"id": "72f11746-706d-44cf-806a-ae546f705edb",
"deviceName": "Random-Integer-Device",
"profileName": "Random-Integer-Device",
"sourceName": "Int8",
"origin": 1649323276353984286,
"readings": [{
"id": "a83fd76f-4e14-4f58-b53f-f0b620533fe7",
"origin": 1649323276353984286,
"deviceName": "Random-Integer-Device",
"resourceName": "Int8",
"profileName": "Random-Integer-Device",
"valueType": "Int8",
"binaryValue": null,
"mediaType": "",
"value": "123"
}]
}
}
复制代码
可以看到我们put指令已经生效,返回了123。
注:我们也可以在UI上直接操作设备的commands。