立即试用 商务报价
专业版
文档 > 规则引擎 > 规则节点 > Transformation节点

本页目录

Transformation

Transformation节点用于更改入站的消息字段例如:发起者、消息类型、playload和元数据。

变更发起者

支持TB版本2.0+

image

Thingsboard中的所有入站消息都有“发起者”字段该字段标识提交消息的实体可以是Device, Asset, Customer, Tenant等。

当提交的消息是来自另一个实体的消息处理的情况下使用此节点;例如:设备上传遥测并且遥测应复制到更高级别的资产或客户中。

在这种情况下管理员必须在[Save Timeseries]Save Timeseries节点之前添加此节点。

发起者可以更改为:

  • 发起人的客户
  • 发起人的租户
  • 关系查询所标识的相关实体

在’Relations query’管理员可以选择所需的Directionrelation depth level还可以使用必需的关系类型和实体类型配置Relation filters

image

如果找到多个相关实体则仅第一个实体当作新的发起者其他实体则被丢弃。

如果未找到相关实体/客户/租户则使用Failure链否则Success链。

出站邮件将具有新的发起者ID。


脚本

支持TB版本2.0

image

使用已配置的JavaScript函数更改消息payload、Metadata或消息类型。

JavaScript函数接收3个输入参数:

  • msg - 消息payload
  • metadata - 消息metadata
  • msgType - 消息类型

脚本应返回以下结构

1
2
3
4
5
{   
    msg: new payload,
    metadata: new metadata,
    msgType: new msgType 
}

image

结果对象中的所有字段都是可选的如果未指定则将从原始消息中获取

来自此节点的出站消息将是使用已配置的JavaScript函数构造的新消息。

JavaScript转换功能可以使用Test JavaScript function.


实例

节点收到带有payload的消息:

1
2
3
4
{
    "temperature": 22.4,
    "humidity": 78
}

Original Metadata:

1
{ "sensorType" : "temperature" }

原始Message Type - POST_TELEMETRY_REQUEST

应该执行以下修改:

  • 将消息类型更改为’CUSTOM_UPDATE’
  • 将其他属性version添加到payload中值为v1.1
  • 将元数据中的_sensorType_属性值更改为roomTemp

以下转换函数将执行所有必要的修改:

1
2
3
4
var newType = "CUSTOM_UPDATE";
msg.version = "v1.1";
metadata.sensorType = "roomTemp"
return {msg: msg, metadata: metadata, msgType: newType};

在下一面教程中你可以看到使用该节点的真实示例:

邮件

支持TB版本2.0

image

通过使用从消息元数据派生的值填充电子邮件字段后将消息转换为电子邮件并设置’SEND_EMAIL’为输出消息类型后使用发送邮件节点发送。

可以将所有电子邮件字段配置为元数据中的值。

image

例如入站消息在元数据中具有deviceName字段并且电子邮件正文应包含其值。

在这种情况下可以像下面的示例一样引用deviceName${deviceName}:

1
 Device ${deviceName} has high temperature


如果想发送html或图像则必须在邮件正文类型字段中选择 HTMLDynamic请参阅在发送HTML或图片邮件示例。

如果入站消息元数据包含引用存储在数据库中的attachments字段可以做为电子邮件的附件使用。 注意: 这是文件存储支持的ThingsBoard专业版的功能。


在下一面教程中你可以看到使用该节点的真实示例:

Duplicate To Group Node

ThingsBoard PE功能

仅限专业版支持PE Transformation Nodes功能。
基于ThingsBoard云服务专业版平台实例。

Since TB Version 2.1

image

Duplicates message to all entities belonging to specific Entity Group.

Entities are fetched from Entity Group detected according to the configuration.

Entity Group can be specified directly or can be message originator entity itself.

For each entity from group new message is created with entity as originator and message parameters copied from original message.

Configuration:

image

  • Entity group is message originator - if set, message originator will be considered as Entity Group used to fetch entities. In this case incoming message will be routed via Failure chain if message originator type is not Entity Group.
  • Target entity group - specific target Entity Group used to fetch entities.

New messages will be duplicated to group entities and forwarded via Success chain if target Entity Group is detected successfully and contains at least one entity. Otherwise original message will be forwarded via Failure chain.


Duplicate To Related Node

ThingsBoard PE功能

仅限专业版支持PE Transformation Nodes功能。
基于ThingsBoard云服务专业版平台实例。

Since TB Version 2.1

image

Duplicates message to related entities fetched by relation query.

Related Entities found using configured relation direction and Relation Type.

For each found related entity new message is created with related entity as originator and message parameters copied from original message.

Configuration:

image

  • Relations query - query used to find new entities starting from incoming message originator. In ‘Relations query’ configuration Administrator can select required Direction and relation depth level. Also set of Relation filters can be configured with required Relation type and Entity Types.

New messages will be duplicated to found entities and forwarded via Success chain if at least one entity will be found using Relations Query. Otherwise original message will be forwarded via Failure chain.