Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use EdgeX Kuiper Rule engine to Control Internet of things Devices

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to use the EdgeX Kuiper rules engine to control the Internet of things. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Scene

The following two rules will be created and run.

The rule for monitoring the Random-UnsignedInteger-Device device, if the uint8 value is greater than 20, sends a command to the Random-Boolean-Device device and turns on the random generation of Boolean values.

The rule for monitoring Random-Integer-Device devices, if the average int8 is greater than 0 every 20 seconds, sends a command to the Random-Boolean-Device device service to turn off random generation of Boolean values.

This scenario does not contain any real business logic, but is only intended to demonstrate the functionality of the EdgeX Kuiper rules engine. You can make reasonable business rules according to our demonstration.

Start using

Be sure to follow the documentation EdgeX Kuiper rules engine getting started tutorial to ensure that the tutorial runs successfully.

Create an EdgeX stream

Before you create a rule, you should create a stream that can consume stream data from the EdgeX application service. If you have completed the getting started tutorial on EdgeX Kuiper rules engine, you do not need this step.

Curl-X POST\ http://$kuiper_docker:48075/streams\-H 'Content-Type: application/json'\-d'{"sql": "create stream demo () WITH (FORMAT=\" JSON\ ", TYPE=\" edgex\ ")"}'

Since both rules send control commands to the device Random-UnsignedInteger-Device, you can get a list of available commands for the device by running the command curl http://localhost:48082/api/v1/device/name/Random-Boolean-Device | jq. It will print similar output, as shown below.

{"id": "9b051411-ca20-4556-bd3e-7f52475764ff", "name": "Random-Boolean-Device", "adminState": "UNLOCKED", "operatingState": "ENABLED", "labels": ["device-virtual-example"], "commands": [{"created": 1589052044139, "modified": 1589052044139, "id": "28d88bb3-e280-46f7-949f-37cc411757f5", "name": "Bool" "get": {"path": "/ api/v1/device/ {deviceId} / Bool", "responses": [{"code": "200"," expectedValues ": [" Bool "]}, {" code ":" 503" "description": "service unavailable"}], "url": "http://edgex-core-command:48082/api/v1/device/bcd18c02-b187-4f29-8265-8312dc5d794d/command/d6d3007d-c4ce-472f-a117-820b5410e498"}," put ": {" path ":" / api/v1/device/ {deviceId} / Bool " "responses": [{"code": "200"}, {" code ":" 503", "description": "service unavailable"}] "url": "http://edgex-core-command:48082/api/v1/device/bcd18c02-b187-4f29-8265-8312dc5d794d/command/d6d3007d-c4ce-472f-a117-820b5410e498"," parameterNames ": [" Bool "," EnableRandomization_Bool "]}}]}

From the output, you can see that there are two commands, and the second command is used to update the configuration of the device. This device has two parameters:

Bool: sets the return value when other services want to get device data. This parameter is used only if EnableRandomization_Bool is set to false.

EnableRandomization_Bool: whether random generation of Bool is enabled. If this value is set to true, the first parameter is ignored.

Therefore, the sample control command will be similar to the following command:

Curl-X PUT\ http://edgex-core-command:48082/api/v1/device/c1459444-79bd-46c8-8b37-d6e1418f2a3a/command/fe202437-236d-41c5-845e-3e6013b928cd\-H 'Content-Type: application/json'\-d'{"Bool": "true", "EnableRandomization_Bool": "true"} 'create rule first rule

The first rule is the rule for monitoring Random-UnsignedInteger-Device devices. If the uint8 value is greater than "20", a command is sent to the Random-Boolean-Device device and random generation of Boolean values is turned on. The following is the rule definition, please note:

This action is triggered when the value of uint8 is greater than 20:00. Because the value of uint8 is not used to send control commands to Random-Boolean-Device, the uint8 value is not used in the dataTemplate property of the rest operation.

Curl-X POST\ http://$kuiper_server:48075/rules\-H 'Content-Type: application/json'\-d' {"id": "rule1", "sql": "SELECT uint8 FROM demo WHERE uint8 > 20" "actions": [{"rest": {"url": "http://edgex-core-command:48082/api/v1/device/bcd18c02-b187-4f29-8265-8312dc5d794d/command/d6d3007d-c4ce-472f-a117-820b5410e498"," method ": put", "retryInterval":-1, "dataTemplate": "{\" Bool\ ":\" true\ " \ "EnableRandomization_Bool\":\ "true\"} "," sendSingle ": true}}, {" log ": {}}]} 'second rule

The second rule monitors Random-Integer-Device devices and sends a command to the Random-Boolean-Device device service to turn off random generation of Boolean values if the average int8 is greater than 0 every 20 seconds.

The average of the uint8 is calculated every 20 seconds, and if the average is greater than 0, a control command is sent to the Random-Boolean-Device service.

Curl-X POST\ http://$kuiper_server:48075/rules\-H 'Content-Type: application/json'\-d'{"id": "rule2", "sql": "SELECT avg (int8) AS avg_int8 FROM demo WHERE int8! = nil GROUP BY TUMBLINGWINDOW (ss, 20) HAVING avg (int8) > 0" "actions": [{"rest": {"url": "http://edgex-core-command:48082/api/v1/device/bcd18c02-b187-4f29-8265-8312dc5d794d/command/d6d3007d-c4ce-472f-a117-820b5410e498"," method ": put", "retryInterval":-1, "dataTemplate": "{\" Bool\ ":\" false\ " \ "EnableRandomization_Bool\":\ "false\"} "," sendSingle ": true}}, {" log ": {}}]}'

Now that you have created two rules, you can view the edgex-kuiper log to get the results of the rule execution.

# how does docker logs edgex-kuiper extract data from analysis results?

Since the analysis results also need to be sent to the command rest service, how to extract data from the analysis results? An example of filtering data through SQL is as follows:

SELECT int8, "true" AS randomization FROM demo WHERE uint8 > 20

The output of SQL is as follows:

[{"int8":-75, "randomization": "true"}]

When reading value fields from field int8 and reading EnableRandomization_Bool from field randomization, it is assumed that the service requires the following data formats:

Curl-X PUT\ http://edgex-core-command:48082/api/v1/device/${deviceId}/command/xyz\-H 'Content-Type: application/json'\-d' {"value":-75, "EnableRandomization_Bool": "true"}'

Kuiper uses the Go template to extract data from the analysis results, and the dataTemplate content is as follows:

"dataTemplate": "{\" value\ ": {{.int8}},\" EnableRandomization_Bool\ ":\" {{.automation}}\ "}"

In some cases, you may need to iterate over the returned array values, or use the if condition to set different values, and then refer to this link to write more complex data template expressions.

The above is the editor to share with you how to use the EdgeX Kuiper rules engine to control Internet of things devices. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report