Shifu advanced features: custom deviceShifu data handler

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.

file

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.

file

Add custom deviceShifu

Before we start running Shifu , there are three things to do first.

1. customized_handlers.pyWrite 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.pyMove pkg/deviceshifu/pythoncustomizedhandlersto .

3. Generate deviceShifu .

Example: Moisture Detector

The example device used here is in examples/deviceshifu/customized/humidity_detectorthe directory.

This device is a virtual humidity detector that will consume humidity and temperature data in HTTPthe provided JSONformat, 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.gowith 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.pyfile, 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.deviceshifuHTTPcopied to dockerfiles.

3. Add the mapping of the data handler

In examples/deviceshifu/customized/humidity_detector/configuration中the deviceshifu-humidity-detector-configmap.yamlfile, map your device instructions with the funcName of the data handler.

(For example: instructions is '/123', funcName is 'humidity')

Then you need customInstructionsPythonto set up 123: humidityand set up the equipment instructions under instructions.instructionsand telemetries.telemetries.device_health.properties.instructionunder 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 shifuthe 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 kindthe 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.pyin .

In the nginx shell, we should be able to curl the API humidityand 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

Guess you like

Origin blog.csdn.net/Edgenesis/article/details/129526757