ThingsBoard 3.4 -- 规则链使用

发布时间:2023年12月27日

规则链的使用

创建规则链

在这里插入图片描述
调试模式用于调试日志的输出,测试阶段建议选择

配置规则

  • 点击规则链,进入详情页面
    ![[Pasted image 20231227102822.png]]

  • 打开规则链进行规则配置
    ![[Pasted image 20231227102902.png]]

  • 规则链节点类型分为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中
      ![[Pasted image 20231227143454.png]]

创建设备配置

![[Pasted image 20231227134024.png]]

创建设备

![[Pasted image 20231227134128.png]]

数据接收

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模式
    ![[Pasted image 20231227143809.png]]

  • 查看消息的输入与输出
    ![[Pasted image 20231227143826.png]]

  • 观察最终结果-属性
    ![[Pasted image 20231227143847.png]]
    ![[Pasted image 20231227144058.png]]

  • 观察最终结果-告警
    在这里插入图片描述
    在这里插入图片描述

  • 观察最终结果-kafka
    ![[Pasted image 20231227144017.png]]
    ![[Pasted image 20231227144037.png]]

规则链文件

{
  "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
  }
}
文章来源:https://blog.csdn.net/weixin_43871079/article/details/135245775
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。