属性上报规则


属性上报规则

当云平台接收设备端的属性上报消息,并更新设备属性后,会触发该类规则。

属性上报规则目前支持以下操作:

articles/2023/20231125161729_1b24ddf088ce67353dc71ee15cbfc7cf.png

articles/2023/20231125161838_8d568e39729e30e9bef7674901e05b96.png

向指定设备下发属性

该操作用于向其它设备下发属性,实现设备联动功能。

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的属性集合。
  • null:表示不下发属性到设备。

示例

例如,当前传感器温度超过30度后,我们希望自动开启风机设备。我们可以在操作配置界面中选择要下发的风机设备,然后写一段属性构造函数如下:

module.exports = function (report_attributes) {

    var temperature = report_attributes.temperature;
    var push_attributes = {
        "fan_relay": temperature > 30 ? true : false
    };
    return push_attributes;
}

向指定设备下发命令

该操作用于向其它设备下发命令,实现设备联动功能。

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的命令消息。
  • null:表示不下发命令到设备。

提示

关于下发命令的消息结构说明,请浏览 设备接收云端下发命令

示例

例如,我们想根据传感器设备上报的温度,来自动执行继电器操作:

  • 当温度升到 35 度或以上,将继电器 #1、#2、#3、#4 开启一次,并在 60 秒后关闭。
  • 当温度降到 27 度或以下,将继电器 #5、#6、#7、#8 开启一次,并在 60 秒后关闭。

由于使用 ThingsEdge DTU 来连接 RS485 继电器,支持 下发开关量控制命令open in new window

规则操作中的云函数可以编写如下:

module.exports = function (report_attributes) {
    /**
     * report_attributes:   上报的属性对象,作为函数参数传入
     * command:             构造下发的命令对象,作为函数返回值,下发到硬件
     */
    var command = {}
    // 获得设备上一次的属性
    const attrs = Cloud.getCurrentAttributes();
    
    if (report_attributes.temperature >= 35 && attrs.temperature < 35) {
        // 如果当前温度大于等于 35 度,并且上一次小于 35 度
        command = {
            "method": "switch_relay",
            "params": {
                "attributes": ["relay1", "relay2", "relay3", "relay4"],
                "state": true,
                "delay_reverse": 60     // 60秒后反转
            },
            "id": 1000
        }
    } else if (report_attributes.temperature <= 27 && attrs.temperature > 27) {
        // 如果当前温度小于等于 27 度,并且上一次大于 27 度
        command = {
            "method": "switch_relay",
            "params": {
                "attributes": ["relay5", "relay6", "relay7", "relay8"], // 自己修改这里
                "state": true,
                "delay_reverse": 60     // 60秒后反转
            },
            "id": 1000
        }
    }

    return command;
}  

向指定设备下发自定义数据

该操作用于向其它设备下发自定义数据,通过目标设备支持的自定义数据流,可下发十六进制、文本、JSON 等格式。

articles/2023/20231125163839_f0c66aa1463689ae9670566181ccb3e1.png

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的自定义数据消息。
  • null:表示不下发数据到目标设备。

向当前设备下发属性

该操作用于向当前设备下发属性,实现设备联动功能。

articles/2023/20231125164319_21cc7e0304cd5b00d22ccb42c77b591c.png

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的属性集合。
  • null:表示不下发属性到设备。

示例

例如,我们需要当设备上报开关量 di1 属性变为 ON 后,自动断开继电器 do1,可以编写如下的云函数:

 module.exports = function (report_attributes) {
    /**
     * report_attributes:   上报的属性对象,作为函数参数传入
     * push_attributes:     构造下发的属性对象,作为函数返回值,下发到硬件
     */
    var push_attributes = {

    };

    // 这里的 === 表示上报属性中必须存在 di1 且为 true
    if (report_attributes.di1 === true) {
        push_attributes.do1 = false;
    }
    return push_attributes;
} 

向当前设备下发命令

该操作用于向当前设备下发命令,实现设备联动功能。

articles/2023/20231125165005_32561e710d12bd4626f7d5670bf309df.png

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的命令消息。
  • null:表示不下发命令到设备。

提示

关于下发命令的消息结构说明,请浏览 设备接收云端下发命令

示例

可参考向 指定设备下发命令 的示例,除了目标设备不同,云函数的用法完全一致。

向当前设备下发自定义数据

该操作用于向当前设备下发自定义数据,通过目标设备支持的自定义数据流,可下发十六进制、文本、JSON 等格式。

articles/2023/20231125165039_9e50f808823a40b651459e96f1e79010.png

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个下发到目标设备的自定义数据消息。
  • null:表示不下发数据到目标设备。

推送到外部 MQTT

该操作将设备上报的属性直接转发到第三方 MQTT 服务器,需要填写 MQTT 主机名、端口、身份验证信息等。

推送到外部 URL

该操作将设备上报的属性直接转发到第三方 URL。

向当前网关上报子设备属性

该操作将设备上报的属性转发到子设备,仅用于当前设备是网关类型的情况下。

参数

  • report_attributes:是设备上报的属性集合,作为参数传入函数。

返回值

  • object 类型:构造一个向网关上报子设备的消息,消息格式请参考 上报子设备属性
  • null:表示不转发消息到子设备。

示例

module.exports = function (report_attributes) {
    /**
     * report_attributes:   上报的属性对象,作为函数参数传入
     */

    var data = {};
    if (report_attributes.devEUI) {
        data[report_attributes.devEUI] = report_attributes;
    }

    return data;
}   

以上云函数示例的详细介绍请浏览 星纵 LoRaWAN UG65 网关使用消息规则转发数据到子设备

自定义云函数

自定义云函数 操作可处理自定义逻辑,并支持一些内置函数,例如:

  • 更新设备云端私有属性
  • 给指定设备下发属性
  • 给指定设备下发命令