Shifu高级功能:自定义的deviceShifu数据处理程序
2023/3/15 21:22:05
本文主要是介绍Shifu高级功能:自定义的deviceShifu数据处理程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
自定义的deviceShifu数据处理程序
Shifu允许用户自定义deviceShifu,让数字孪生更加高效。
默认情况
默认情况下,从设备到deviceShifu的数据将以其原始格式提供。
自定义处理
在添加了定制的逻辑后,deviceShifu可以对数据进行处理,使其更适合应用:
1.将数据转换成用户需要的格式。
2.过滤掉不需要的数据。
3.进行动态计算和数据分析。
添加自定义的 deviceShifu
在我们开始运行Shifu之前,要先做三件事。
1.在customized_handlers.py
中编写数据处理逻辑,处理方法的一般结构是这样的:
#使用指令/API名称作为方法名称 def humidity(raw_data): new_data = process(raw_data) return new_data
2.将customized_handlers.py
移到pkg/deviceshifu/pythoncustomizedhandlers
中。
3.生成deviceShifu。
示例:湿度检测器
这里使用的示例设备在examples/deviceshifu/customized/humidity_detector
目录下。
这个设备是一个虚拟的湿度检测器,它将使用HTTP
提供JSON
格式的湿度和温度数据,指令/API只有humidity
。
0.建立湿度检测器的虚拟设备
第一步是用humidity-detector.go
和它在examples/deviceshifu/customized/humidity_detector
中的 Dockerfile 建立humidity-detector
的docker镜像。
1.添加定制的数据处理程序
在examples/deviceshifu/customized/humidity_detector/pythoncustomizedhandlers
中,有一个customized_hanlders.py
文件,将该文件复制到pkg/deviceshifu/pythoncustomizedhandlers
中。
2.编辑deviceShifu的Docker文件
将examples/deviceshifu/customized/humidity_detector/sample_deviceshifu_dockerfiles/Dockerfile.deviceshifuHTTP
复制到dockerfiles
。
3.添加数据处理程序的映射
在examples/deviceshifu/customized/humidity_detector/configuration中
的deviceshifu-humidity-detector-configmap.yaml
文件,将您的设备指令,与数据处理程序的funcName,进行映射。
(例如:instructions为 ‘/123’ , funcName为 ‘humidity’)
则需要在customInstructionsPython
下设置123: humidity
并在instructions.instructions
和telemetries.telemetries.device_health.properties.instruction
下设置设备指令Instructions
如下所示:
data: customInstructionsPython: | 123: humidity #123是instructions,humidity是处理程序funcName instructions: | instructions: 123: telemetries: | telemetries: device_health: properties: instruction: 123
4.建立deviceShifu的docker镜像
构建一个新的deviceShifu镜像用来添加定制的数据处理程序。
在shifu
的根目录下,运行以下命令来构建deviceShifu的docker镜像。
make buildx-build-imag-deviceshifu-http-http
5.启动Shifu
这部分与快速启动演示中的内容完全一样。
在Shifu运行后,我们需要将新建立的湿度检测器虚拟设备导入到kind
集群中。
kind load docker-image humidity-detector:v0.0.1
6.检查处理后的数据
来自这个虚拟设备的原始数据应该由customized_hanlders.py
中定义的自定义处理程序来处理。
在 nginx shell 中,我们应该可以 curl API humidity
并检查结果是否被处理,结果如下:
[ { "unit": "℃", "code": "20990922009", "exception": "temperature is too high", "name": "atmosphere temperature", "val": "37" }, { "unit": "%RH", "code": "20990922009", "exception": "humidity is too high", "name": "atmosphere humidity", "val": "88" } ]
本文由边无际授权发布
这篇关于Shifu高级功能:自定义的deviceShifu数据处理程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08云原生周刊:Kubernetes v1.30 一瞥 | 2024.3.25
- 2024-05-08KubeSphere 社区双周报|2024.03.15-03.29
- 2024-05-08云原生周刊:Kubernetes 1.30 的一切新功能 | 2024.4.1
- 2024-05-08云原生周刊:2024 年 K8s 基准报告 | 2024.4.8
- 2024-05-08如何监控容器或K8s中的OpenSearch
- 2024-05-08Kubernetes 升级不弃 Docker:KubeKey 的丝滑之道
- 2024-05-08使用 Prometheus 在 KubeSphere 上监控 KubeEdge 边缘节点(Jetson) CPU、GPU 状态
- 2024-05-08KubeSphere 社区双周报|2024.03.29-04.11
- 2024-05-08云原生周刊:CNCF 2023 年度调查报告 | 2024.4.15
- 2024-05-08Kubernetes Dashboard部署安装recommended.yaml