brew redis
brew install redis // 安装redis
Homebrew安装的软件会默认在/usr/local/Cellar/路径下
redis的配置文件redis.conf存放在/usr/local/etc路径下
brew services start redis // 启动redis服务
redis-server // 查看服务相关信息
ps axu | grep redis // 查看服务进程
redis-cli -h 127.0.0.1 -p 6379 // cli客户端连接
redis-cli shutdown // 关闭客户端连接
brew services stop redis // 停止服务
列出docker容器及IP地址
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
启动通用导出
./app-service-configurable -cp=consul.http://127.0.0.1:8500 --registry --confdir=./res -profile=http-export
启动自定义的导出服务
./app-service -cp=consul.http://127.0.0.1:8500 --registry --confdir=./res
关闭安全
在此示例中,我们没有启动安全元素,因此我们需要设置EDGEX_SECURITY_SECRET_STORE为false以关闭安全性。最后,直接从终端运行服务。
cd cmd
export EDGEX_SECURITY_SECRET_STORE=false
./device-virtual
akunorokia@foxmail.coma11111111
数据走向
01.设备 -> 数据中心 -> 03.数据导出 -> 04.MQTT brocker -> 05.MQTT client
01.启动自定义设备数据模拟 device-random
实例代码
https://github.com/edgexfoundry/edgex-examples/tree/main/device-services/device-random
02. 启动服务需要的操作
export EDGEX_SECURITY_SECRET_STORE=false // 关闭安全
export SERVICE_HOST="192.168.10.68" // 对应configuration.toml的[Service].Host值, 首先读取环境变量, 如果没读到就会读取配置
configuration.toml的[Registry].Host = 'ifconfig.en0'
./device-random -cp=consul.http://127.0.0.1:8500 --registry --confdir=./res
03.启动数据导出
edgex ui界面中设置brocker地址
ui -> 应用服务 -> 管道函数 -> 选中的管道函数 -> 管道函数参数设置 -> Function MQTTExport Parameters
-> BrokerAddress: tcp://{{MQTTBrocker的网络ip}}:1883
influxdb_v2
user: admin->Ma123456
organization: zcw
bucket: zcwBucket
telegrafConfigurationName: myTelegraf
grafana
user: admin->admin
规则引擎
01.创建流
curl -X POST http://localhost:59720/streams -H 'Content-Type: application/json' -d '{"sql": "create stream demo1() WITH (FORMAT="JSON", TYPE="edgex")"}'
查看Random-Boolean-Device设备可使用的命令
curl -X GET http://127.0.0.1:59882/api/v2/device/name/{{设备名称}}
02.测试命令
curl -X PUT
http://localhost:59882/api/v2/device/name/{{设备名称}}/RandomNumber
-H 'Content-Type: application/json'
-d '{"Bool":"true", "EnableRandomization_Bool": "true"}'
03.创建规则1
curl -X POST \
http://localhost:59720/rules \
-H 'Content-Type: application/json' \
-d '{
"id": "rule1",
"sql": "select uint8 from demo1 where uint8 > 20",
"actions": [
{
"rest": {
"url": "http://edgex-core-command:59882/api/v2/device/name/Random-Boolean-Device/WriteBoolValue",
"method": "put",
"dataTemplate": "{\"Bool\":\"true\", \"EnableRandomization_Bool\": \"true\"}",
"sendSingle": true
}
},
{
"log":{}
}
]
}'
04.创建规则2
curl -X POST \
http://localhost:59720/rules \
-H 'Content-Type: application/json' \
-d '{
"id": "tule2",
"sql": "select avg(int8) as avg_int8 from demo1 where int8 != nil group by tumblingwindow(ss, 20) having avg(int8)>0",
"actions": [
{
"rest": {
"url": "http://edgex-core-command:59882/api/v2/device/name/Random-Boolean-Device/WriteBoolValue",
"method": "put",
"dataTemplate": "{\"Bool\":\"false\", \"EnableRandomization_Bool\": \"false\"}",
"sendSingle": true
}
},
{
"log":{}
}
]
}'
05.查询结果
$ docker exec -it edgex-kuiper /bin/sh
/kuiper/bin $ ./kuiper query
kuiper > SELECT uint8, "true" AS randomization FROM demo1 WHERE uint8 > 20
[{"randomization":"true","uint8":207}]
[{"randomization":"true","uint8":36}]
[{"randomization":"true","uint8":209}]
定义设备(元数据) & 注册设备服务 & 预配设备
01.设备配置文件, 在edgex中添加设备元数据
wget https://docs.edgexfoundry.org/2.1/walk-through/EdgeX_CameraMonitorProfile.yml
curl -X POST -F 'file=@EdgeX_CameraMonitorProfile.yml' http://localhost:59881/api/v2/deviceprofile/uploadfile
curl -X GET http://localhost:59881/api/v2/deviceprofile/all
02.注册设备服务, 在edgex中添加设备服务
curl -X 'POST' 'http://localhost:59881/api/v2/deviceservice' -d '[{"apiVersion": "v2","service": {"name": "camera-control-device-service","description": "Manage human and dog counting cameras", "adminState": "UNLOCKED", "labels": ["camera","counter"], "baseAddress": "camera-device-service:59990"}}]'
curl -X GET http://localhost:59881/api/v2/deviceservice/all
03.添加设备
curl -X 'POST' 'http://localhost:59881/api/v2/device' -d '[{"apiVersion": "v2", "device": {"name": "countcamera1","description": "human and dog counting camera #1","adminState": "UNLOCKED","operatingState": "UP","labels": ["camera","counter"],"location": "{lat:45.45,long:47.80}","serviceName": "camera-control-device-service","profileName": "camera-monitor-profile","protocols": {"camera-protocol": {"camera-address": "localhost","port": "1234","unitID": "1"}},"notify": false}}]'
curl -X GET http://localhost:59881/api/v2/device/all | json_pp
curl -X GET http://localhost:59881/api/v2/device/profile/name/camera-monitor-profile | json_pp
04.透过命令微服务管理设备
请注意上述命令请求的端口更改。在演练的这一部分,我们不再调用核心元数据。命令微服务默认在 59882 端口。
curl -X GET http://localhost:59882/api/v2/device/name/countcamera1 | json_pp
设置扫描深度
curl -X PUT -d '{"depth":"9"}' http://localhost:59882/api/v2/device/name/countcamera1/ScanDepth
docker logs edgex-core-command // 命令失败查看日志
05.发送事件和读取数据
数据作为Event对象提交给核心数据(core-data).
Event是在'特定时间'点从设备的传感器读数的集合
Reading对象中的对象Event是设备感知的特定值, 并与deviceResources按名称相关联
事件事件(event)/读数(readings)
curl -X GET localhost:59880/api/v2/event/count/device/name/countcamera1
curl -X GET localhost:59880/api/v2/evencurl -X POST -d '{"apiVersion": "v2","event": {"apiVersion": "v2","deviceName": "countcamera1","profileName": "camera-monitor-profile","sourceName": "HumanCount","id":"d5471d59-2810-419a-8744-18eb8fa03464","origin": 1602168089665565200,"readings": [{"id": "7003cacc-0e00-4676-977c-4e58b9612abc","origin": 1602168089665565200,"deviceName": "countcamera1","resourceName": "HumanCount","profileName": "camera-monitor-profile","valueType": "Int16","value": "5"},{"id": "7003cacc-0e00-4676-977c-4e58b9612abf","origin":1602168089665565200,"deviceName": "countcamera1","resourceName": "CanineCount","profileName": "camera-monitor-profile","valueType": "Int16","value": "3"}]}}' localhost:59880/api/v2/event/camera-monitor-profile/countcamera1/HumanCountt/count/device/name/Random-Integer-Device
06.发送
postman
http://localhost:59880/api/v2/event/camera-monitor-profile/countcamera1/HumanCount
POST
{
"apiVersion": "v2",
"event": {
"apiVersion": "v2",
"deviceName": "countcamera1",
"profileName": "camera-monitor-profile",
"sourceName": "HumanCount",
"id": "d5471d59-2810-419a-8744-18eb8fa03465",
"origin": 1602168089665565200,
"readings": [{
"id": "7003cacc-0e00-4676-977c-4e58b9612abd",
"origin": 1602168089665565200,
"deviceName": "countcamera1",
"resourceName": "HumanCount",
"profileName": "camera-monitor-profile",
"valueType": "Int16",
"value": "5"
},
{
"id": "7003cacc-0e00-4676-977c-4e58b9612abe",
"origin": 1602168089665565200,
"deviceName": "countcamera1",
"resourceName": "CanineCount",
"profileName": "camera-monitor-profile",
"valueType": "Int16",
"value": "3"
}
]
}
}
07.查询事件(event)/读数(readings)
curl -X GET localhost:59880/api/v2/event/device/name/countcamera1 | json_pp
curl -X GET localhost:59880/api/v2/reading/device/name/countcamera1 | json_pp
curl -X GET localhost:59880/api/v2/event/start/1602168089665560000/end/1602168089665570000 | json_pp
go-sdk开发简单的设备服务
源码: https://gitee.com/zcwmt/device-simple
docker镜像: https://hub.docker.com/repository/docker/masterzcw/device-simple/
edgex: https://gitee.com/zcwmt/my-edgex
构建设备服务基础部分
mkdir -p ~/edgexfoundry
cd ~/edgexfoundry
git clone --depth 1 --branch v2.0.0 https://github.com/edgexfoundry/device-sdk-go.git
mkdir -p ~/edgexfoundry/device-simple
cd ~/edgexfoundry
cp -rf ./device-sdk-go/example/* ./device-simple/
cp ./device-sdk-go/Makefile ./device-simple
cp ./device-sdk-go/version.go ./device-simple/
cd ~/edgexfoundry/device-simple/
GO111MODULE=on go mod init github.com/edgexfoundry/device-simple
go mod tidy
~/edgexfoundry/device-simple/Makefile
把example/cmd/device-simple/替换为cmd/device-simple/
创建设备配置文件
关于设备类型、这些设备提供的数据以及如何命令设备的一般特征都在设备配置文件中
~/edgexfoundry/device-simple/cmd/device-simple/res/profiles/random-generator-device.yaml
name: "RandNum-Device"
manufacturer: "Dell Technologies"
model: "1"
labels:
- "random"
- "test"
description: "random number generator to simulate a device"
deviceResources:
name: "RandomNumber"
description: "generated random number"
attributes:
{ type: "random" }
properties:
valueType: "Int32"
readWrite: "R"
创建设备
在设备服务启动期间添加到 EdgeX 的预定义设备信息
~/edgexfoundry/device-simple/cmd/device-simple/res/devices/random-generator-device.toml
[[DeviceList]]
Name = 'RandNum-Device01'
ProfileName = 'RandNum-Device'
Description = 'Random Number Generator Device'
Labels = [ 'random', 'test' ]
[DeviceList.Protocols]
[DeviceList.Protocols.Other]
Address = 'random'
Port = '300'
[[DeviceList.AutoEvents]]
Interval = '10s'
SourceName = 'RandomNumber'
OnChange = false
把配置的资源对接到代码中
~/edgexfoundry/driver/simpledriver.go
if reqs[0].DeviceResourceName == "RandomNumber" {
cv, _ := sdkModels.NewCommandValue(reqs[0].DeviceResourceName, common.ValueTypeInt32, int32(rand.Intn(100)))
res[0] = cv
} else
cd ~/edgexfoundry/device-simple
make build
cd ~/edgexfoundry/device-simple/cmd/device-simple
export EDGEX_SECURITY_SECRET_STORE=false
./device-simple