更新时间:2024-02-28 GMT+08:00
分享

查询规则列表

功能介绍

应用服务器可调用此接口查询物联网平台中设置的规则列表。

调用方法

请参见如何调用API

URI

GET /v5/iot/{project_id}/rules

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数说明:项目ID。获取方法请参见 获取项目ID

表2 Query参数

参数

是否必选

参数类型

描述

app_id

String

参数说明:资源空间ID。此参数为非必选参数,存在多资源空间的用户需要使用该接口时,可以携带该参数查询指定资源空间下的规则列表,不携带该参数则会查询该用户下所有规则列表。 取值范围:长度不超过36,只允许字母、数字、下划线(_)、连接符(-)的组合。

rule_type

String

参数说明:规则类型。此参数为非必选参数,指定对应的规则类型结果进行返回,不携带该参数则会返回所有类型规则。 取值范围

  • DEVICE_LINKAGE:云端联动规则。

  • DEVICE_SIDE:端侧规则。

limit

Integer

参数说明:分页查询时每页显示的记录数。 取值范围:1-50的整数,默认值为10。

最小值:1

最大值:50

缺省值:10

marker

String

参数说明:上一次分页查询结果中最后一条记录的ID,在上一次分页查询时由物联网平台返回获得。分页查询时物联网平台是按marker也就是记录ID降序查询的,越新的数据记录ID也会越大。若填写marker,则本次只查询记录ID小于marker的数据记录。若不填写,则从记录ID最大也就是最新的一条数据开始查询。如果需要依次查询所有数据,则每次查询时必须填写上一次查询响应中的marker值。 取值范围:长度为24的十六进制字符串,默认值为ffffffffffffffffffffffff。

缺省值:ffffffffffffffffffffffff

offset

Integer

参数说明:表示从marker后偏移offset条记录开始查询。默认为0,取值范围为0-500的整数。当offset为0时,表示从marker后第一条记录开始输出。限制offset最大值是出于API性能考虑,您可以搭配marker使用该参数实现翻页,例如每页50条记录,1-11页内都可以直接使用offset跳转到指定页,但到11页后,由于offset限制为500,您需要使用第11页返回的marker作为下次查询的marker,以实现翻页到12-22页。 取值范围:0-500的整数,默认为0。

最小值:0

最大值:500

缺省值:0

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数说明:用户Token。通过调用IAM服务 获取IAM用户Token接口获取,接口返回的响应消息头中“X-Subject-Token”就是需要获取的用户Token。简要的获取方法样例请参见 Token认证

Instance-Id

String

参数说明:实例ID。物理多租下各实例的唯一标识,建议携带该参数,在使用专业版时必须携带该参数。您可以在IoTDA管理控制台界面,选择左侧导航栏“总览”页签查看当前实例的ID,具体获取方式请参考查看实例详情

响应参数

状态码: 200

表4 响应Body参数

参数

参数类型

描述

marker

String

本次分页查询结果中最后一条记录的ID,可在下一次分页查询时使用。

count

Long

满足查询条件的记录总数。

rules

Array of RuleResponse objects

规则信息列表。

表5 RuleResponse

参数

参数类型

描述

rule_id

String

规则id。

最大长度:128

name

String

规则名称。

最小长度:1

最大长度:128

description

String

规则的描述信息。

最大长度:256

condition_group

ConditionGroup object

规则的条件组,包含简单规则和复杂规则集合。

actions

Array of RuleAction objects

规则的动作列表,单个规则最多支持设置10个动作。

rule_type

String

规则的类型

  • DEVICE_LINKAGE:云端联动规则。

  • DEVICE_SIDE:端侧规则。

status

String

规则的状态,默认值:active。

  • active:激活。

  • inactive:未激活。

app_id

String

资源空间ID。此参数为非必选参数,存在多资源空间的用户需要使用该接口时,建议携带该参数指定创建的规则归属到哪个资源空间下,否则创建的规则将会归属到默认资源空间下。

edge_node_ids

Array of strings

归属边缘侧节点设备ID列表。

last_update_time

String

规则最后更新时间,使用UTC时区,格式:yyyyMMdd'T'HHmmss'Z'。

device_side

DeviceSide object

参数说明:端侧执行下发设备信息,当规则类型为DEVICE_SIDE时,该参数必填。

表6 ConditionGroup

参数

参数类型

描述

conditions

Array of RuleCondition objects

参数说明:规则的条件列表,单个规则最多支持设置10个条件。

logic

String

参数说明:规则条件列表中多个条件之间的逻辑关系,默认值:and。 取值范围

  • and:逻辑且。

  • or:逻辑或。

time_range

TimeRange object

参数说明:规则条件触发的有效时间段。

表7 RuleCondition

参数

参数类型

描述

type

String

参数说明:规则条件的类型。 取值范围

  • DEVICE_DATA:设备属性数据类型条件。

  • SIMPLE_TIMER:简单定时类型条件。

  • DAILY_TIMER:每日定时类型条件。

  • DEVICE_LINKAGE_STATUS:设备状态类型条件。

device_property_condition

DeviceDataCondition object

参数说明:条件中设备数据类型的信息,当type为DEVICE_DATA时,为必选参数。

simple_timer_condition

SimpleTimerType object

参数说明:条件中简单定时类型的信息,当type为SIMPLE_TIMER时,为必选参数。

daily_timer_condition

DailyTimerType object

参数说明:条件中每日定时类型的信息,当type为DAILY_TIMER时,为必选参数。

device_linkage_status_condition

DeviceLinkageStatusCondition object

参数说明:条件中设备状态类型的信息,当规则类型为DEVICE_LINKAGE且type为DEVICE_LINKAGE_STATUS时,为必选参数。

表8 DeviceDataCondition

参数

参数类型

描述

device_id

String

参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备属性触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。

product_id

String

参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备属性触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。

最大长度:128

filters

Array of PropertyFilter objects

数据过滤条件。

表9 PropertyFilter

参数

参数类型

描述

path

String

参数说明:设备属性的路径信息,格式:service_id/DataProperty,例如门磁状态为“DoorWindow/status”。

最大长度:128

operator

String

参数说明:数据比较的操作符。 取值范围:当前支持的操作符有:>,<,>=,<=,=,in:表示在指定值中匹配和between:表示数值区间。

value

String

参数说明:数据比较表达式的右值。与数据比较操作符between联用时,右值表示最小值和最大值,用逗号隔开,如“20,30”表示大于等于20小于30。

最大长度:64

in_values

Array of strings

参数说明:当operator为in时该字段必填,使用该字段传递比较表达式右值,上限为20个。

最大长度:128

strategy

Strategy object

参数说明:规则条件的处理策略,用于确定规则是否判断上次数据是否满足条件。当rule_type为DEVICE_LINKAGE时,该参数值不能为空。端侧执行不支持该字段。

表10 Strategy

参数

参数类型

描述

trigger

String

参数说明:规则条件触发的判断策略,默认为pulse。 取值范围

  • pulse:设备上报的数据满足条件则触发,不判断上一次上报的数据。

  • reverse:设备上一次上报的数据不满足条件,本次上报的数据满足条件则触发。

event_valid_time

Integer

参数说明:设备数据的有效时间,单位为秒,设备数据的产生时间以上报数据中的eventTime为基准。

最小值:-1

表11 SimpleTimerType

参数

参数类型

描述

start_time

String

参数说明:规则触发的开始时间,使用UTC时区,格式:yyyyMMdd'T'HHmmss'Z'。

最大长度:128

repeat_interval

Integer

参数说明:规则触发的重复时间间隔,单位为秒。

最小值:1

最大值:31536000

repeat_count

Integer

参数说明:规则触发的重复次数。

最小值:1

最大值:9999

表12 DailyTimerType

参数

参数类型

描述

time

String

参数说明:规则触发的时间,格式:HH:MM。

最大长度:128

days_of_week

String

参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。

最大长度:128

表13 DeviceLinkageStatusCondition

参数

参数类型

描述

device_id

String

参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备状态触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。

product_id

String

参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备状态触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。

最大长度:128

status_list

Array of strings

参数说明:状态列表,设备状态条件携带该参数。 取值范围

  • ONLINE:设备上线

  • OFFLINE:设备下线

最大长度:128

duration

Integer

持续时长:设备状态持续时长,取值范围: 0-60(分钟)。

最小值:0

最大值:60

缺省值:0

表14 TimeRange

参数

参数类型

描述

start_time

String

参数说明:规则条件触发的开始时间,格式:HH:mm。

end_time

String

参数说明:规则条件触发的结束时间,格式:HH:mm。若结束时间与开始时间一致,则时间为全天。

days_of_week

String

参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。星期列表中的日期为开始时间的日期。

表15 RuleAction

参数

参数类型

描述

type

String

参数说明:规则动作的类型,端侧执行只支持下发设备命令消息类型。 取值范围

  • DEVICE_CMD:下发设备命令消息类型。

  • SMN_FORWARDING:发送SMN消息类型。

  • DEVICE_ALARM:上报设备告警消息类型。当选择该类型时,condition中必须有DEVICE_DATA条件类型。该类型动作只能唯一。

device_command

ActionDeviceCommand object

下发设备命令消息内容。当type为DEVICE_CMD时,必填。

smn_forwarding

ActionSmnForwarding object

发送给SMN消息结构。当规则类型为DEVICE_LINKAGE且type为SMN_FORWARDING时,必填。

device_alarm

ActionDeviceAlarm object

上报设备告警消息内容。当规则类型为DEVICE_LINKAGE且type为DEVICE_ALARM时,必填。

表16 ActionDeviceCommand

参数

参数类型

描述

device_id

String

参数说明:下发命令的设备ID。

  • 当创建设备数据规则时,若device_id为空,则命令下发给触发条件的设备。

  • 当创建定时规则时,不允许为空。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。

cmd

CMD object

参数说明:下发的命令信息。

表17 CMD

参数

参数类型

描述

command_name

String

参数说明:设备命令名称,在设备关联的产品模型中定义。

最大长度:128

command_body

Object

参数说明:设备命令参数,Json格式。 使用LWM2M协议设备命令示例:{"value":"1"},里面是一个个键值对,每个键都是产品模型中命令的参数名(paraName)。 使用MQTT协议设备命令示例:{"header": {"mode": "ACK","from": "/users/testUser","method": "SET_TEMPERATURE_READ_PERIOD","to":"/devices/{device_id}/services/{service_id}"},"body": {"value" : "1"}}。

  • mode:必选,设备收到命令后是否需要回复确认消息,默认为ACK模式。ACK表示需要回复确认消息,NOACK表示不需要回复确认消息,其它值无效。

  • from:可选,命令发送方的地址。App发起请求时格式为/users/{userId} ,应用服务器发起请求时格式为/{serviceName},物联网平台发起请求时格式为/cloud/{serviceName}。

  • to:可选,命令接收方的地址,格式为/devices/{device_id}/services/{service_id}。

  • method:可选,产品模型中定义的命令名称。

  • body:可选,命令的消息体,里面是一个个键值对,每个键都是产品模型中命令的参数名(paraName)。具体格式需要应用和设备约定。

service_id

String

参数说明:设备命令所属的设备服务ID,在设备关联的产品模型中定义。

最大长度:64

buffer_timeout

Integer

参数说明:设备命令的缓存时间,单位为秒,表示物联网平台在把命令下发给设备前缓存命令的有效时间,超过这个时间后命令将不再下发,默认值为172800s(48小时)。 如果buffer_timeout设置为0,则无论物联网平台上设置的命令下发模式是什么,该命令都会立即下发给设备。

最小值:0

最大值:31536000

缺省值:172800

response_timeout

Integer

参数说明:命令响应的有效时间,单位为秒,表示设备接收到命令后,在response_timeout时间内响应有效,超过这个时间未收到命令的响应,则认为命令响应超时,默认值为1800s。

最小值:1

最大值:31536000

缺省值:1800

mode

String

参数说明:设备命令的下发模式,仅当buffer_timeout的值大于0时有效。

  • ACTIVE:主动模式,物联网平台主动将命令下发给设备。

  • PASSIVE:被动模式,物联网平台创建设备命令后,会直接缓存命令。等到设备再次上线或者上报上一条命令的执行结果后才下发命令。

表18 ActionSmnForwarding

参数

参数类型

描述

region_name

String

参数说明:SMN服务对应的region区域。

最大长度:256

project_id

String

参数说明:SMN服务对应的projectId信息。

theme_name

String

参数说明:SMN服务对应的主题名称。

最大长度:256

topic_urn

String

参数说明:SMN服务对应的topic的主题URN。

最大长度:256

message_content

String

参数说明:短信或邮件的内容。

最大长度:1024

message_template_name

String

参数说明:SMN服务对应的模板名称。

message_title

String

参数说明:短信或邮件的主题。最大长度支持UTF-8编码后的521个字节。

表19 ActionDeviceAlarm

参数

参数类型

描述

name

String

参数说明:告警名称。

最大长度:256

alarm_status

String

参数说明:告警状态。 取值范围

  • fault:上报告警。

  • recovery:恢复告警。

severity

String

参数说明:告警级别。 取值范围:warning(警告)、minor(一般)、major(严重)和critical(致命)。

dimension

String

参数说明:告警维度,与告警名称和告警级别组合起来共同标识一条告警,默认不携带该字段为用户维度告警,支持设备维度和资源空间维度告警。 取值范围

  • device:设备维度。

  • app:资源空间维度。

description

String

参数说明:告警的描述信息。

最大长度:256

表20 DeviceSide

参数

参数类型

描述

device_ids

Array of strings

参数说明:端侧执行下发的目标设备ID列表。设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。

最大长度:128

请求示例

列表查询指定资源空间的联动规则。

GET https://{endpoint}/v5/iot/{project_id}/rules?app_id={app_id}&limit={limit}&marker={marker}&offset={offset}

响应示例

状态码: 200

OK

{
  "marker" : "6336a282cda07a01f7ac5d11",
  "count" : 1,
  "rules" : [ {
    "rule_id" : "5eb3628d017d9105d0cf9aec",
    "name" : "openLight",
    "condition_group" : {
      "conditions" : [ {
        "type" : "DEVICE_DATA",
        "device_property_condition" : {
          "device_id" : "07b69d78-c716-4be6-9545-869920738397",
          "filters" : [ {
            "path" : "StreetLight/visibility",
            "operator" : "<",
            "value" : "30",
            "strategy" : {
              "trigger" : "reverse",
              "event_valid_time" : 300
            }
          } ]
        }
      } ],
      "logic" : "and"
    },
    "actions" : [ {
      "type" : "DEVICE_CMD",
      "device_command" : {
        "device_id" : "3a9e52d9-3ebf-4985-89e9-6d2396748a2f",
        "cmd" : {
          "command_name" : "SET_LIGHT_SWITCH",
          "command_body" : {
            "value" : 0
          },
          "service_id" : "Switch",
          "buffer_timeout" : 0,
          "response_timeout" : 1800
        }
      }
    } ],
    "rule_type" : "DEVICE_LINKAGE",
    "status" : "active",
    "app_id" : "9562bf8541e44361b6ae3a7e9fbe1144",
    "last_update_time" : "20221017T023727Z"
  } ]
}

SDK代码示例

SDK代码示例如下。

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.AbstractCredentials;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import com.huaweicloud.sdk.iotda.v5.*;
import com.huaweicloud.sdk.iotda.v5.model.*;


public class ListRulesSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");

        ICredential auth = new BasicCredentials()
                .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                .withAk(ak)
                .withSk(sk);

        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.valueOf("<YOUR REGION>"))
                .build();
        ListRulesRequest request = new ListRulesRequest();
        request.withAppId("<app_id>");
        request.withRuleType("<rule_type>");
        request.withLimit(<limit>);
        request.withMarker("<marker>");
        request.withOffset(<offset>);
        try {
            ListRulesResponse response = client.listRules(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.auth.credentials import DerivedCredentials
from huaweicloudsdkiotda.v5.region.iotda_region import IoTDARegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkiotda.v5 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = __import__('os').getenv("CLOUD_SDK_AK")
    sk = __import__('os').getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \
            .with_derived_predicate(DerivedCredentials.get_default_derived_predicate()) \

    client = IoTDAClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(IoTDARegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ListRulesRequest()
        request.app_id = "<app_id>"
        request.rule_type = "<rule_type>"
        request.limit = <limit>
        request.marker = "<marker>"
        request.offset = <offset>
        response = client.list_rules(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/region"
    core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
        Build()

    client := iotda.NewIoTDAClient(
        iotda.IoTDAClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ListRulesRequest{}
	appIdRequest:= "<app_id>"
	request.AppId = &appIdRequest
	ruleTypeRequest:= "<rule_type>"
	request.RuleType = &ruleTypeRequest
	limitRequest:= int32(<limit>)
	request.Limit = &limitRequest
	markerRequest:= "<marker>"
	request.Marker = &markerRequest
	offsetRequest:= int32(<offset>)
	request.Offset = &offsetRequest
	response, err := client.ListRules(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

OK

400

Bad Request

403

Forbidden

404

Not Found

500

Internal Server Error

错误码

请参见错误码

分享:

    相关文档

    相关产品