Custom deviceShifu data handler
Shifu allows users to customize deviceShifu to make digital twins more efficient.
default
By default, data from a device to deviceShifu will be provided in its original format.
custom processing
After adding custom logic, deviceShifu can process the data to make it more suitable for the application:
1. Convert the data into the format required by the user. 2. Filter out unwanted data. 3. Perform dynamic calculation and data analysis.
Add custom deviceShifu
Before we start running Shifu , there are three things to do first.
1. customized_handlers.py
Write data processing logic in , the general structure of the processing method is as follows:
#使用指令/API名称作为方法名称
def humidity(raw_data):
new_data = process(raw_data)
return new_data
2. customized_handlers.py
Move pkg/deviceshifu/pythoncustomizedhandlers
to .
3. Generate deviceShifu .
Example: Moisture Detector
The example device used here is in examples/deviceshifu/customized/humidity_detector
the directory.
This device is a virtual humidity detector that will consume humidity and temperature data in HTTP
the provided JSON
format, command/API only humidity
.
0. Create a virtual device for the humidity detector
The first step is to use the docker image built humidity-detector.go
with it examples/deviceshifu/customized/humidity_detector
's Dockerfile humidity-detector
.
1. Add a custom data handler
In examples/deviceshifu/customized/humidity_detector/pythoncustomizedhandlers
, there is a customized_hanlders.py
file, copy that file into pkg/deviceshifu/pythoncustomizedhandlers
.
2. Edit the Docker file of deviceShifu
will be examples/deviceshifu/customized/humidity_detector/sample_deviceshifu_dockerfiles/Dockerfile.deviceshifuHTTP
copied to dockerfiles
.
3. Add the mapping of the data handler
In examples/deviceshifu/customized/humidity_detector/configuration中
the deviceshifu-humidity-detector-configmap.yaml
file, map your device instructions with the funcName of the data handler.
(For example: instructions is '/123', funcName is 'humidity')
Then you need customInstructionsPython
to set up 123: humidity
and set up the equipment instructions under instructions.instructions
and telemetries.telemetries.device_health.properties.instruction
under Instructions
As follows:
data:
customInstructionsPython: |
123: humidity
#123是instructions,humidity是处理程序funcName
instructions: |
instructions:
123:
telemetries: |
telemetries:
device_health:
properties:
instruction:
123
4. Create a docker image of deviceShifu
Build a new deviceShifu image to add custom data handlers.
In shifu
the root directory of , run the following command to build the docker image of deviceShifu .
make buildx-build-imag-deviceshifu-http-http
5. Roaring Shifu
This section is exactly the same as in the quickstart demo .
After Shifu is running, we need to import the newly created humidity detector virtual device into kind
the cluster.
kind load docker-image humidity-detector:v0.0.1
6. Check the processed data
Raw data from this virtual device should be handled by custom handlers defined customized_hanlders.py
in .
In the nginx shell, we should be able to curl the API humidity
and check if the result was processed, the result is as follows:
[
{
"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"
}
]
This article is published by Boundless Authorization