规则链的使用
创建规则链
调试模式用于调试日志的输出,测试阶段建议选择
配置规则
-
点击规则链,进入详情页面
-
打开规则链进行规则配置
-
规则链节点类型分为6类,每一类下面又包含多个小类
- 过滤器/筛选器:过滤器节点用于消息过滤和路由
- asset profile switch:根据设备配置文件的名称路由传入消息。规则节点可以连接到其他规则节点
- device profile switch:根据设备配置文件的名称路由传入消息。
- check alarm status:检查警报状态是否与指定状态之一匹配。
- check fields presence: 检查消息和/或元数据中是否存在指定字段。
- check relation:检查消息的发起者和其他实体之间是否存在关系。
- entity type:按消息发起者实体的类型过滤传入消息。
- entity type switch:按消息发起者实体的类型切换传入消息。
- message type:根据一种或多种预定义或自定义消息类型过滤传入消息。
- message type switch:按消息类型值路由传入消息。
- script:使用传入消息评估布尔函数(自定义脚本)
- switch:将传入消息路由到一个或多个输出连接(自定义脚本)
- GPS geofencing filter:通过基于 GPS 的地理围栏过滤传入消息
- 丰富节点:丰富节点用于更新传入消息的元数据(数据预处理)
- Calculate delta:根据之前的时间序列读数和当前读数计算“增量”并将其添加到消息中
- Customer attributes:节点找到消息发起者实体的客户,并将客户属性或最新遥测值添加到消息元数据中。
- Device attributes:节点使用配置的查询查找消息发起者实体的相关设备,并将属性(客户端共享服务器范围)和最新遥测值添加到消息元数据中。
- Originator attributes:将消息发起者属性(客户端共享服务器范围)和最新遥测值添加到消息元数据中。
- Originator fields:节点获取消息发起者实体的字段值并将它们添加到消息元数据中。
- Related attributes:节点使用配置的查询查找消息发起者实体的相关实体,并将属性或最新遥测值添加到消息元数据中。
- Tenant attributes:节点找到消息发起者实体的租户,并将租户属性或最新遥测值添加到消息元数据中
- Originator telemetry:将节点配置中选择的特定时间范围内的消息发起者遥测值添加到消息元数据。
- Tenant details:规则节点 将租户详细信息中的字段添加到消息正文或元数据。
- Customer details:规则节点 将客户详细信息中的字段添加到消息正文或元数据。
- 变换节点:转换节点用于更改传入消息字段,如发起者、消息类型、有效负载和元数据。
- Change originator:当提交的消息应作为来自另一个实体的消息进行处理时,使用此节点。例如,设备提交遥测数据,遥测数据应复制到更高级别的资产或客户。在这种情况下,管理员应在保存时间序列节点之前添加此节点。
- Script Transformation Node:使用配置的 JavaScript 函数更改消息有效负载、元数据或消息类型。
- To Email Node:通过使用从消息元数据派生的值填充电子邮件字段,将消息转换为电子邮件消息
- 动作节点:操作节点根据传入的消息执行各种操作。
- Math Function Node
- Create Alarm Node
- Clear Alarm Node
- 外部节点:使用的外部节点用于与外部系统交互
- AWS
- Kafka
- MQTT
- RabbitMQ
- REST API Call
- 规则节点:用于控制消息处理流程。
- Acknowledge Node:节点会将消息标记为已成功处理(已确认)。如果您不想重新处理失败的消息,这很有用
- Checkpoint Node:将消息的副本发布到选定的规则引擎队列。
- Rule Chain Node:将消息转发到选定的规则链。复用共性链路
- 过滤器/筛选器:过滤器节点用于消息过滤和路由
-
定义一个规则:JSON在最后
- 将设备类型为“HTTP设备配置”下的设备,提取“温度”遥测属性字段,转换数值为华式温度,并保存到属性中,判断温度超过50度或低于10度,产生告警;温度在50~10中间告警恢复;将产生的告警发送到kafka中
- 将设备类型为“HTTP设备配置”下的设备,提取“温度”遥测属性字段,转换数值为华式温度,并保存到属性中,判断温度超过50度或低于10度,产生告警;温度在50~10中间告警恢复;将产生的告警发送到kafka中
创建设备配置
创建设备
数据接收
curl -v -X POST http://192.168.0.16:8080/api/v1/vl4hijxt4ihg4szr2xdl/telemetry --header Content-Type:application/json --data "{temperature:60,mac:\"HTTP设备\"}"
链路追踪
-
打开规则中的Debug模式
-
查看消息的输入与输出
-
观察最终结果-属性
-
观察最终结果-告警
-
观察最终结果-kafka
规则链文件
{
"ruleChain": {
"additionalInfo": {
"description": ""
},
"name": "device profile switch example",
"type": "CORE",
"firstRuleNodeId": null,
"root": false,
"debugMode": false,
"configuration": null,
"externalId": null
},
"metadata": {
"firstNodeIndex": 0,
"nodes": [
{
"additionalInfo": {
"description": "设备配置选择",
"layoutX": 125,
"layoutY": 334
},
"type": "org.thingsboard.rule.engine.filter.TbDeviceTypeSwitchNode",
"name": "to device rule chain",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"version": 0
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 306,
"layoutY": 580
},
"type": "org.thingsboard.rule.engine.transform.TbTransformMsgNode",
"name": "修改温度",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"scriptLang": "JS",
"jsScript": "var hsTemp = msg.temperature * 33.8\nmsg.hsTemp = hsTemp\nreturn {msg: msg, metadata: metadata, msgType: msgType};",
"tbelScript": "return {msg: msg, metadata: metadata, msgType: msgType};"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 877,
"layoutY": 517
},
"type": "org.thingsboard.rule.engine.action.TbCreateAlarmNode",
"name": "温度预警",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"scriptLang": "TBEL",
"alarmDetailsBuildJs": "var details = {};\nif (metadata.prevAlarmDetails) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n delete metadata.prevAlarmDetails;\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"alarmDetailsBuildTbel": "var details = {};\nif (metadata.prevAlarmDetails != null) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n // metadata.remove('prevAlarmDetails');\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"useMessageAlarmData": false,
"overwriteAlarmDetails": false,
"alarmType": "General Alarm",
"severity": "CRITICAL",
"propagate": false,
"relationTypes": [],
"propagateToOwner": false,
"propagateToTenant": false,
"dynamicSeverity": false
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 882,
"layoutY": 355
},
"type": "org.thingsboard.rule.engine.action.TbClearAlarmNode",
"name": "消除",
"debugMode": false,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"alarmType": "General Alarm",
"scriptLang": "TBEL",
"alarmDetailsBuildJs": "var details = {};\nif (metadata.prevAlarmDetails) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n delete metadata.prevAlarmDetails;\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;",
"alarmDetailsBuildTbel": "var details = {};\nif (metadata.prevAlarmDetails != null) {\n details = JSON.parse(metadata.prevAlarmDetails);\n //remove prevAlarmDetails from metadata\n metadata.remove('prevAlarmDetails');\n //now metadata is the same as it comes IN this rule node\n}\n\n\nreturn details;"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 531,
"layoutY": 477
},
"type": "org.thingsboard.rule.engine.filter.TbJsFilterNode",
"name": "temperature",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"scriptLang": "TBEL",
"jsScript": "return msg.temperature > 20;",
"tbelScript": "return msg.temperature > 50 || msg.temperature < 10;"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 1198,
"layoutY": 420
},
"type": "org.thingsboard.rule.engine.kafka.TbKafkaNode",
"name": "告警消息",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"topicPattern": "tb_test_topic",
"keyPattern": " $[metadataKey]",
"bootstrapServers": "192.168.0.11:9092,192.168.0.12:9092,192.168.0.13:9092",
"retries": 0,
"batchSize": 16384,
"linger": 0,
"bufferMemory": 33554432,
"acks": "-1",
"keySerializer": "org.apache.kafka.common.serialization.StringSerializer",
"valueSerializer": "org.apache.kafka.common.serialization.StringSerializer",
"otherProperties": {},
"addMetadataKeyValuesAsKafkaHeaders": true,
"kafkaHeadersCharset": "UTF-8"
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 861,
"layoutY": 615
},
"type": "org.thingsboard.rule.engine.telemetry.TbMsgAttributesNode",
"name": "保存属性",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 1,
"configuration": {
"scope": "SERVER_SCOPE",
"notifyDevice": false,
"sendAttributesUpdatedNotification": false,
"updateAttributesOnlyOnValueChange": true
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 860,
"layoutY": 720
},
"type": "org.thingsboard.rule.engine.telemetry.TbMsgTimeseriesNode",
"name": "保存遥测",
"debugMode": false,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"defaultTTL": 0,
"skipLatestPersistence": false,
"useServerTs": false
},
"externalId": null
},
{
"additionalInfo": {
"description": "",
"layoutX": 529,
"layoutY": 651
},
"type": "org.thingsboard.rule.engine.filter.TbMsgTypeSwitchNode",
"name": "消息类型配置",
"debugMode": true,
"singletonMode": false,
"configurationVersion": 0,
"configuration": {
"version": 0
},
"externalId": null
}
],
"connections": [
{
"fromIndex": 0,
"toIndex": 1,
"type": "HTTP设备配置"
},
{
"fromIndex": 1,
"toIndex": 4,
"type": "Success"
},
{
"fromIndex": 1,
"toIndex": 8,
"type": "Success"
},
{
"fromIndex": 2,
"toIndex": 5,
"type": "Created"
},
{
"fromIndex": 2,
"toIndex": 5,
"type": "Updated"
},
{
"fromIndex": 3,
"toIndex": 5,
"type": "Cleared"
},
{
"fromIndex": 4,
"toIndex": 2,
"type": "True"
},
{
"fromIndex": 4,
"toIndex": 3,
"type": "False"
},
{
"fromIndex": 8,
"toIndex": 6,
"type": "Post attributes"
},
{
"fromIndex": 8,
"toIndex": 7,
"type": "Post telemetry"
}
],
"ruleChainConnections": null
}
}