edgex

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容