创建规则
功能介绍
应用服务器可调用此接口在物联网平台创建一条规则。
调用方法
请参见如何调用API。
URI
POST /v5/iot/{project_id}/rules
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
参数说明:项目ID。获取方法请参见 获取项目ID。 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
X-Auth-Token |
否 |
String |
参数说明:用户Token。通过调用IAM服务 获取IAM用户Token接口获取,接口返回的响应消息头中“X-Subject-Token”就是需要获取的用户Token。简要的获取方法样例请参见 Token认证。 |
Instance-Id |
否 |
String |
参数说明:实例ID。物理多租下各实例的唯一标识,建议携带该参数,在使用专业版时必须携带该参数。您可以在IoTDA管理控制台界面,选择左侧导航栏“总览”页签查看当前实例的ID,具体获取方式请参考查看实例详情 。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
name |
是 |
String |
参数说明:规则名称。 |
description |
否 |
String |
参数说明:规则的描述信息。 |
condition_group |
是 |
ConditionGroup object |
参数说明:规则的条件组,包含简单规则和复杂规则集合。 |
actions |
是 |
Array of RuleAction objects |
参数说明:规则的动作列表,单个规则最多支持设置10个动作。 |
rule_type |
是 |
String |
参数说明:规则的类型。 取值范围:
|
status |
否 |
String |
参数说明:规则的状态,默认值:active。 取值范围:
|
app_id |
否 |
String |
参数说明:资源空间ID。此参数为非必选参数,存在多资源空间的用户需要使用该接口时,建议携带该参数指定创建的规则归属到哪个资源空间下,否则创建的规则将会归属到默认资源空间下。 取值范围:长度不超过36,只允许字母、数字、下划线(_)、连接符(-)的组合。 |
device_side |
否 |
DeviceSide object |
参数说明:端侧执行下发设备信息,当规则类型为DEVICE_SIDE时,该参数必填。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
conditions |
否 |
Array of RuleCondition objects |
参数说明:规则的条件列表,单个规则最多支持设置10个条件。 |
logic |
否 |
String |
参数说明:规则条件列表中多个条件之间的逻辑关系,默认值:and。 取值范围:
|
time_range |
否 |
TimeRange object |
参数说明:规则条件触发的有效时间段。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
type |
是 |
String |
参数说明:规则条件的类型。 取值范围:
|
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时,为必选参数。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
device_id |
否 |
String |
参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备属性触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。 |
product_id |
否 |
String |
参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备属性触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。 |
filters |
否 |
Array of PropertyFilter objects |
数据过滤条件。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
path |
是 |
String |
参数说明:设备属性的路径信息,格式:service_id/DataProperty,例如门磁状态为“DoorWindow/status”。 |
operator |
是 |
String |
参数说明:数据比较的操作符。 取值范围:当前支持的操作符有:>,<,>=,<=,=,in:表示在指定值中匹配和between:表示数值区间。 |
value |
否 |
String |
参数说明:数据比较表达式的右值。与数据比较操作符between联用时,右值表示最小值和最大值,用逗号隔开,如“20,30”表示大于等于20小于30。 |
in_values |
否 |
Array of strings |
参数说明:当operator为in时该字段必填,使用该字段传递比较表达式右值,上限为20个。 |
strategy |
否 |
Strategy object |
参数说明:规则条件的处理策略,用于确定规则是否判断上次数据是否满足条件。当rule_type为DEVICE_LINKAGE时,该参数值不能为空。端侧执行不支持该字段。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
trigger |
否 |
String |
参数说明:规则条件触发的判断策略,默认为pulse。 取值范围:
|
event_valid_time |
否 |
Integer |
参数说明:设备数据的有效时间,单位为秒,设备数据的产生时间以上报数据中的eventTime为基准。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
start_time |
是 |
String |
参数说明:规则触发的开始时间,使用UTC时区,格式:yyyyMMdd'T'HHmmss'Z'。 |
repeat_interval |
是 |
Integer |
参数说明:规则触发的重复时间间隔,单位为秒。 |
repeat_count |
是 |
Integer |
参数说明:规则触发的重复次数。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
time |
是 |
String |
参数说明:规则触发的时间,格式:HH:MM。 |
days_of_week |
否 |
String |
参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
device_id |
否 |
String |
参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备状态触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。 |
product_id |
否 |
String |
参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备状态触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。 |
status_list |
否 |
Array of strings |
参数说明:状态列表,设备状态条件携带该参数。 取值范围:
|
duration |
否 |
Integer |
持续时长:设备状态持续时长,取值范围: 0-60(分钟)。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
start_time |
是 |
String |
参数说明:规则条件触发的开始时间,格式:HH:mm。 |
end_time |
是 |
String |
参数说明:规则条件触发的结束时间,格式:HH:mm。若结束时间与开始时间一致,则时间为全天。 |
days_of_week |
否 |
String |
参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。星期列表中的日期为开始时间的日期。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
type |
是 |
String |
参数说明:规则动作的类型,端侧执行只支持下发设备命令消息类型。 取值范围:
|
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时,必填。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
device_id |
否 |
String |
参数说明:下发命令的设备ID。
|
cmd |
是 |
CMD object |
参数说明:下发的命令信息。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
command_name |
是 |
String |
参数说明:设备命令名称,在设备关联的产品模型中定义。 |
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"}}。
|
service_id |
是 |
String |
参数说明:设备命令所属的设备服务ID,在设备关联的产品模型中定义。 |
buffer_timeout |
否 |
Integer |
参数说明:设备命令的缓存时间,单位为秒,表示物联网平台在把命令下发给设备前缓存命令的有效时间,超过这个时间后命令将不再下发,默认值为172800s(48小时)。 如果buffer_timeout设置为0,则无论物联网平台上设置的命令下发模式是什么,该命令都会立即下发给设备。 |
response_timeout |
否 |
Integer |
参数说明:命令响应的有效时间,单位为秒,表示设备接收到命令后,在response_timeout时间内响应有效,超过这个时间未收到命令的响应,则认为命令响应超时,默认值为1800s。 |
mode |
否 |
String |
参数说明:设备命令的下发模式,仅当buffer_timeout的值大于0时有效。
|
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
region_name |
是 |
String |
参数说明:SMN服务对应的region区域。 |
project_id |
是 |
String |
参数说明:SMN服务对应的projectId信息。 |
theme_name |
是 |
String |
参数说明:SMN服务对应的主题名称。 |
topic_urn |
是 |
String |
参数说明:SMN服务对应的topic的主题URN。 |
message_content |
否 |
String |
参数说明:短信或邮件的内容。 |
message_template_name |
否 |
String |
参数说明:SMN服务对应的模板名称。 |
message_title |
是 |
String |
参数说明:短信或邮件的主题。最大长度支持UTF-8编码后的521个字节。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
name |
是 |
String |
参数说明:告警名称。 |
alarm_status |
是 |
String |
参数说明:告警状态。 取值范围:
|
severity |
是 |
String |
参数说明:告警级别。 取值范围:warning(警告)、minor(一般)、major(严重)和critical(致命)。 |
dimension |
否 |
String |
参数说明:告警维度,与告警名称和告警级别组合起来共同标识一条告警,默认不携带该字段为用户维度告警,支持设备维度和资源空间维度告警。 取值范围:
|
description |
否 |
String |
参数说明:告警的描述信息。 |
响应参数
状态码: 201
参数 |
参数类型 |
描述 |
---|---|---|
rule_id |
String |
规则id。 |
name |
String |
规则名称。 |
description |
String |
规则的描述信息。 |
condition_group |
ConditionGroup object |
规则的条件组,包含简单规则和复杂规则集合。 |
actions |
Array of RuleAction objects |
规则的动作列表,单个规则最多支持设置10个动作。 |
rule_type |
String |
规则的类型
|
status |
String |
规则的状态,默认值:active。
|
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时,该参数必填。 |
参数 |
参数类型 |
描述 |
---|---|---|
conditions |
Array of RuleCondition objects |
参数说明:规则的条件列表,单个规则最多支持设置10个条件。 |
logic |
String |
参数说明:规则条件列表中多个条件之间的逻辑关系,默认值:and。 取值范围:
|
time_range |
TimeRange object |
参数说明:规则条件触发的有效时间段。 |
参数 |
参数类型 |
描述 |
---|---|---|
type |
String |
参数说明:规则条件的类型。 取值范围:
|
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时,为必选参数。 |
参数 |
参数类型 |
描述 |
---|---|---|
device_id |
String |
参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备属性触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。 |
product_id |
String |
参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备属性触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。 |
filters |
Array of PropertyFilter objects |
数据过滤条件。 |
参数 |
参数类型 |
描述 |
---|---|---|
path |
String |
参数说明:设备属性的路径信息,格式:service_id/DataProperty,例如门磁状态为“DoorWindow/status”。 |
operator |
String |
参数说明:数据比较的操作符。 取值范围:当前支持的操作符有:>,<,>=,<=,=,in:表示在指定值中匹配和between:表示数值区间。 |
value |
String |
参数说明:数据比较表达式的右值。与数据比较操作符between联用时,右值表示最小值和最大值,用逗号隔开,如“20,30”表示大于等于20小于30。 |
in_values |
Array of strings |
参数说明:当operator为in时该字段必填,使用该字段传递比较表达式右值,上限为20个。 |
strategy |
Strategy object |
参数说明:规则条件的处理策略,用于确定规则是否判断上次数据是否满足条件。当rule_type为DEVICE_LINKAGE时,该参数值不能为空。端侧执行不支持该字段。 |
参数 |
参数类型 |
描述 |
---|---|---|
trigger |
String |
参数说明:规则条件触发的判断策略,默认为pulse。 取值范围:
|
event_valid_time |
Integer |
参数说明:设备数据的有效时间,单位为秒,设备数据的产生时间以上报数据中的eventTime为基准。 |
参数 |
参数类型 |
描述 |
---|---|---|
start_time |
String |
参数说明:规则触发的开始时间,使用UTC时区,格式:yyyyMMdd'T'HHmmss'Z'。 |
repeat_interval |
Integer |
参数说明:规则触发的重复时间间隔,单位为秒。 |
repeat_count |
Integer |
参数说明:规则触发的重复次数。 |
参数 |
参数类型 |
描述 |
---|---|---|
time |
String |
参数说明:规则触发的时间,格式:HH:MM。 |
days_of_week |
String |
参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。 |
参数 |
参数类型 |
描述 |
---|---|---|
device_id |
String |
参数说明:设备ID,用于唯一标识一个设备,在注册设备时由物联网平台分配获得。存在该参数时设备状态触发根据指定设备触发,该参数值和product_id不能同时为空。如果该参数和product_id同时存在时,以该参数值对应的设备进行条件过滤。 取值范围:长度不超过128,只允许字母、数字、下划线(_)、连接符(-)的组合。 |
product_id |
String |
参数说明:设备关联的产品ID,用于唯一标识一个产品模型,创建产品后获得。方法请参见 创建产品。存在该参数且device_id为空时设备状态触发匹配该产品下所有设备触发,该参数值和device_id不能同时为空。 |
status_list |
Array of strings |
参数说明:状态列表,设备状态条件携带该参数。 取值范围:
|
duration |
Integer |
持续时长:设备状态持续时长,取值范围: 0-60(分钟)。 |
参数 |
参数类型 |
描述 |
---|---|---|
start_time |
String |
参数说明:规则条件触发的开始时间,格式:HH:mm。 |
end_time |
String |
参数说明:规则条件触发的结束时间,格式:HH:mm。若结束时间与开始时间一致,则时间为全天。 |
days_of_week |
String |
参数说明:星期列表,以逗号分隔。1代表周日,2代表周一,依次类推,默认为每天。星期列表中的日期为开始时间的日期。 |
参数 |
参数类型 |
描述 |
---|---|---|
type |
String |
参数说明:规则动作的类型,端侧执行只支持下发设备命令消息类型。 取值范围:
|
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时,必填。 |
参数 |
参数类型 |
描述 |
---|---|---|
device_id |
String |
参数说明:下发命令的设备ID。
|
cmd |
CMD object |
参数说明:下发的命令信息。 |
参数 |
参数类型 |
描述 |
---|---|---|
command_name |
String |
参数说明:设备命令名称,在设备关联的产品模型中定义。 |
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"}}。
|
service_id |
String |
参数说明:设备命令所属的设备服务ID,在设备关联的产品模型中定义。 |
buffer_timeout |
Integer |
参数说明:设备命令的缓存时间,单位为秒,表示物联网平台在把命令下发给设备前缓存命令的有效时间,超过这个时间后命令将不再下发,默认值为172800s(48小时)。 如果buffer_timeout设置为0,则无论物联网平台上设置的命令下发模式是什么,该命令都会立即下发给设备。 |
response_timeout |
Integer |
参数说明:命令响应的有效时间,单位为秒,表示设备接收到命令后,在response_timeout时间内响应有效,超过这个时间未收到命令的响应,则认为命令响应超时,默认值为1800s。 |
mode |
String |
参数说明:设备命令的下发模式,仅当buffer_timeout的值大于0时有效。
|
参数 |
参数类型 |
描述 |
---|---|---|
region_name |
String |
参数说明:SMN服务对应的region区域。 |
project_id |
String |
参数说明:SMN服务对应的projectId信息。 |
theme_name |
String |
参数说明:SMN服务对应的主题名称。 |
topic_urn |
String |
参数说明:SMN服务对应的topic的主题URN。 |
message_content |
String |
参数说明:短信或邮件的内容。 |
message_template_name |
String |
参数说明:SMN服务对应的模板名称。 |
message_title |
String |
参数说明:短信或邮件的主题。最大长度支持UTF-8编码后的521个字节。 |
参数 |
参数类型 |
描述 |
---|---|---|
name |
String |
参数说明:告警名称。 |
alarm_status |
String |
参数说明:告警状态。 取值范围:
|
severity |
String |
参数说明:告警级别。 取值范围:warning(警告)、minor(一般)、major(严重)和critical(致命)。 |
dimension |
String |
参数说明:告警维度,与告警名称和告警级别组合起来共同标识一条告警,默认不携带该字段为用户维度告警,支持设备维度和资源空间维度告警。 取值范围:
|
description |
String |
参数说明:告警的描述信息。 |
请求示例
-
创建云端规则,设备下线触发告警。
POST https://{endpoint}/v5/iot/{project_id}/rules { "name" : "openLight", "description" : "string", "condition_group" : { "time_range" : { "days_of_week" : "2,3,4,5,6", "start_time" : "18:00", "end_time" : "23:00" }, "logic" : "or", "conditions" : [ { "device_linkage_status_condition" : { "device_id" : "07b69d78-c716-4be6-9545-869920738397", "status_list" : [ "OFFLINE" ], "duration" : 0 }, "type" : "DEVICE_LINKAGE_STATUS" } ] }, "actions" : [ { "device_alarm" : { "severity" : "warning", "alarm_status" : "fault", "name" : "设备下线", "description" : "设备下线" }, "type" : "DEVICE_ALARM" } ], "rule_type" : "DEVICE_LINKAGE", "status" : "active", "app_id" : "string" }
-
创建云端规则,属性上报触发命令下发。
POST https://{endpoint}/v5/iot/{project_id}/rules { "name" : "openLight", "description" : "string", "condition_group" : { "time_range" : { "days_of_week" : "2,3,4,5,6", "start_time" : "18:00", "end_time" : "23:00" }, "logic" : "or", "conditions" : [ { "device_property_condition" : { "device_id" : "07b69d78-c716-4be6-9545-869920738397", "filters" : [ { "path" : "StreetLight/visibility", "strategy" : { "trigger" : "reverse", "event_valid_time" : 300 }, "value" : "30", "operator" : "<" } ] }, "type" : "DEVICE_DATA" } ] }, "actions" : [ { "device_command" : { "device_id" : "3a9e52d9-3ebf-4985-89e9-6d2396748a2f", "cmd" : { "buffer_timeout" : 0, "mode" : "ACTIVE", "service_id" : "Switch", "command_name" : "SET_LIGHT_SWITCH", "command_body" : { "value" : 0 } } }, "type" : "DEVICE_CMD" } ], "rule_type" : "DEVICE_LINKAGE", "status" : "active", "app_id" : "string" }
-
创建端侧规则,属性上报触发命令下发。
POST https://{endpoint}/v5/iot/{project_id}/rules { "name" : "openLight", "description" : "string", "condition_group" : { "time_range" : { "days_of_week" : "2,3,4,5,6", "start_time" : "18:00", "end_time" : "23:00" }, "logic" : "or", "conditions" : [ { "device_property_condition" : { "device_id" : "3a9e52d9-3ebf-4985-89e9-6d2396748a2f", "filters" : [ { "path" : "StreetLight/visibility", "strategy" : { "trigger" : "reverse", "event_valid_time" : 300 }, "value" : "30", "operator" : "<" } ] }, "type" : "DEVICE_DATA" } ] }, "actions" : [ { "device_command" : { "device_id" : "3a9e52d9-3ebf-4985-89e9-6d2396748a2f", "cmd" : { "buffer_timeout" : 0, "mode" : "ACTIVE", "service_id" : "Switch", "command_name" : "SET_LIGHT_SWITCH", "command_body" : { "value" : 0 } } }, "type" : "DEVICE_CMD" } ], "rule_type" : "DEVICE_SIDE", "device_side" : { "device_ids" : [ "3a9e52d9-3ebf-4985-89e9-6d2396748a2f" ] }, "status" : "active", "app_id" : "string" }
响应示例
状态码: 201
Created
{ "rule_id" : "5eb3628d017d9105d0cf9aec", "rule_type" : "DEVICE_LINKAGE", "name" : "openLight", "status" : "active", "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 } } ] } } ] }, "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 } } } ] }
SDK代码示例
SDK代码示例如下。
-
创建云端规则,设备下线触发告警。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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.core.region.Region; import com.huaweicloud.sdk.iotda.v5.*; import com.huaweicloud.sdk.iotda.v5.model.*; import java.util.List; import java.util.ArrayList; public class CreateRuleSolution { 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"); // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 String iotdaEndpoint = "<YOUR ENDPOINT>"; String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate"; .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios .withAk(ak) .withSk(sk); IoTDAClient client = IoTDAClient.newBuilder() .withCredential(auth) // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)" .withRegion(new Region("cn-north-4", iotdaEndpoint)) .build(); CreateRuleRequest request = new CreateRuleRequest(); Rule body = new Rule(); ActionDeviceAlarm deviceAlarmActions = new ActionDeviceAlarm(); deviceAlarmActions.withName("设备下线") .withAlarmStatus("fault") .withSeverity("warning") .withDescription("设备下线"); List<RuleAction> listbodyActions = new ArrayList<>(); listbodyActions.add( new RuleAction() .withType("DEVICE_ALARM") .withDeviceAlarm(deviceAlarmActions) ); TimeRange timeRangeConditionGroup = new TimeRange(); timeRangeConditionGroup.withStartTime("18:00") .withEndTime("23:00") .withDaysOfWeek("2,3,4,5,6"); List<String> listDeviceLinkageStatusConditionStatusList = new ArrayList<>(); listDeviceLinkageStatusConditionStatusList.add("OFFLINE"); DeviceLinkageStatusCondition deviceLinkageStatusConditionConditions = new DeviceLinkageStatusCondition(); deviceLinkageStatusConditionConditions.withDeviceId("07b69d78-c716-4be6-9545-869920738397") .withStatusList(listDeviceLinkageStatusConditionStatusList) .withDuration(0); List<RuleCondition> listConditionGroupConditions = new ArrayList<>(); listConditionGroupConditions.add( new RuleCondition() .withType("DEVICE_LINKAGE_STATUS") .withDeviceLinkageStatusCondition(deviceLinkageStatusConditionConditions) ); ConditionGroup conditionGroupbody = new ConditionGroup(); conditionGroupbody.withConditions(listConditionGroupConditions) .withLogic("or") .withTimeRange(timeRangeConditionGroup); body.withAppId("string"); body.withStatus("active"); body.withRuleType("DEVICE_LINKAGE"); body.withActions(listbodyActions); body.withConditionGroup(conditionGroupbody); body.withDescription("string"); body.withName("openLight"); request.withBody(body); try { CreateRuleResponse response = client.createRule(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()); } } }
-
创建云端规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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.core.region.Region; import com.huaweicloud.sdk.iotda.v5.*; import com.huaweicloud.sdk.iotda.v5.model.*; import java.util.List; import java.util.ArrayList; public class CreateRuleSolution { 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"); // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 String iotdaEndpoint = "<YOUR ENDPOINT>"; String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate"; .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios .withAk(ak) .withSk(sk); IoTDAClient client = IoTDAClient.newBuilder() .withCredential(auth) // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)" .withRegion(new Region("cn-north-4", iotdaEndpoint)) .build(); CreateRuleRequest request = new CreateRuleRequest(); Rule body = new Rule(); Cmd cmdDeviceCommand = new Cmd(); cmdDeviceCommand.withCommandName("SET_LIGHT_SWITCH") .withCommandBody("{\"value\":0}") .withServiceId("Switch") .withBufferTimeout(0) .withMode("ACTIVE"); ActionDeviceCommand deviceCommandActions = new ActionDeviceCommand(); deviceCommandActions.withDeviceId("3a9e52d9-3ebf-4985-89e9-6d2396748a2f") .withCmd(cmdDeviceCommand); List<RuleAction> listbodyActions = new ArrayList<>(); listbodyActions.add( new RuleAction() .withType("DEVICE_CMD") .withDeviceCommand(deviceCommandActions) ); TimeRange timeRangeConditionGroup = new TimeRange(); timeRangeConditionGroup.withStartTime("18:00") .withEndTime("23:00") .withDaysOfWeek("2,3,4,5,6"); Strategy strategyFilters = new Strategy(); strategyFilters.withTrigger("reverse") .withEventValidTime(300); List<PropertyFilter> listDevicePropertyConditionFilters = new ArrayList<>(); listDevicePropertyConditionFilters.add( new PropertyFilter() .withPath("StreetLight/visibility") .withOperator("<") .withValue("30") .withStrategy(strategyFilters) ); DeviceDataCondition devicePropertyConditionConditions = new DeviceDataCondition(); devicePropertyConditionConditions.withDeviceId("07b69d78-c716-4be6-9545-869920738397") .withFilters(listDevicePropertyConditionFilters); List<RuleCondition> listConditionGroupConditions = new ArrayList<>(); listConditionGroupConditions.add( new RuleCondition() .withType("DEVICE_DATA") .withDevicePropertyCondition(devicePropertyConditionConditions) ); ConditionGroup conditionGroupbody = new ConditionGroup(); conditionGroupbody.withConditions(listConditionGroupConditions) .withLogic("or") .withTimeRange(timeRangeConditionGroup); body.withAppId("string"); body.withStatus("active"); body.withRuleType("DEVICE_LINKAGE"); body.withActions(listbodyActions); body.withConditionGroup(conditionGroupbody); body.withDescription("string"); body.withName("openLight"); request.withBody(body); try { CreateRuleResponse response = client.createRule(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()); } } }
-
创建端侧规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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.core.region.Region; import com.huaweicloud.sdk.iotda.v5.*; import com.huaweicloud.sdk.iotda.v5.model.*; import java.util.List; import java.util.ArrayList; public class CreateRuleSolution { 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"); // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 String iotdaEndpoint = "<YOUR ENDPOINT>"; String projectId = "{project_id}"; ICredential auth = new BasicCredentials() .withProjectId(projectId) // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate"; .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios .withAk(ak) .withSk(sk); IoTDAClient client = IoTDAClient.newBuilder() .withCredential(auth) // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)" .withRegion(new Region("cn-north-4", iotdaEndpoint)) .build(); CreateRuleRequest request = new CreateRuleRequest(); Rule body = new Rule(); List<String> listDeviceSideDeviceIds = new ArrayList<>(); listDeviceSideDeviceIds.add("3a9e52d9-3ebf-4985-89e9-6d2396748a2f"); DeviceSide deviceSidebody = new DeviceSide(); deviceSidebody.withDeviceIds(listDeviceSideDeviceIds); Cmd cmdDeviceCommand = new Cmd(); cmdDeviceCommand.withCommandName("SET_LIGHT_SWITCH") .withCommandBody("{\"value\":0}") .withServiceId("Switch") .withBufferTimeout(0) .withMode("ACTIVE"); ActionDeviceCommand deviceCommandActions = new ActionDeviceCommand(); deviceCommandActions.withDeviceId("3a9e52d9-3ebf-4985-89e9-6d2396748a2f") .withCmd(cmdDeviceCommand); List<RuleAction> listbodyActions = new ArrayList<>(); listbodyActions.add( new RuleAction() .withType("DEVICE_CMD") .withDeviceCommand(deviceCommandActions) ); TimeRange timeRangeConditionGroup = new TimeRange(); timeRangeConditionGroup.withStartTime("18:00") .withEndTime("23:00") .withDaysOfWeek("2,3,4,5,6"); Strategy strategyFilters = new Strategy(); strategyFilters.withTrigger("reverse") .withEventValidTime(300); List<PropertyFilter> listDevicePropertyConditionFilters = new ArrayList<>(); listDevicePropertyConditionFilters.add( new PropertyFilter() .withPath("StreetLight/visibility") .withOperator("<") .withValue("30") .withStrategy(strategyFilters) ); DeviceDataCondition devicePropertyConditionConditions = new DeviceDataCondition(); devicePropertyConditionConditions.withDeviceId("3a9e52d9-3ebf-4985-89e9-6d2396748a2f") .withFilters(listDevicePropertyConditionFilters); List<RuleCondition> listConditionGroupConditions = new ArrayList<>(); listConditionGroupConditions.add( new RuleCondition() .withType("DEVICE_DATA") .withDevicePropertyCondition(devicePropertyConditionConditions) ); ConditionGroup conditionGroupbody = new ConditionGroup(); conditionGroupbody.withConditions(listConditionGroupConditions) .withLogic("or") .withTimeRange(timeRangeConditionGroup); body.withDeviceSide(deviceSidebody); body.withAppId("string"); body.withStatus("active"); body.withRuleType("DEVICE_SIDE"); body.withActions(listbodyActions); body.withConditionGroup(conditionGroupbody); body.withDescription("string"); body.withName("openLight"); request.withBody(body); try { CreateRuleResponse response = client.createRule(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()); } } }
-
创建云端规则,设备下线触发告警。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.auth.credentials import DerivedCredentials from huaweicloudsdkcore.region.region import Region as coreRegion 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 = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 iotdaEndpoint = "<YOUR ENDPOINT>"; projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate()) client = IoTDAClient.new_builder() \ .with_credentials(credentials) \ # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4) .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \ .build() try: request = CreateRuleRequest() deviceAlarmActions = ActionDeviceAlarm( name="设备下线", alarm_status="fault", severity="warning", description="设备下线" ) listActionsbody = [ RuleAction( type="DEVICE_ALARM", device_alarm=deviceAlarmActions ) ] timeRangeConditionGroup = TimeRange( start_time="18:00", end_time="23:00", days_of_week="2,3,4,5,6" ) listStatusListDeviceLinkageStatusCondition = [ "OFFLINE" ] deviceLinkageStatusConditionConditions = DeviceLinkageStatusCondition( device_id="07b69d78-c716-4be6-9545-869920738397", status_list=listStatusListDeviceLinkageStatusCondition, duration=0 ) listConditionsConditionGroup = [ RuleCondition( type="DEVICE_LINKAGE_STATUS", device_linkage_status_condition=deviceLinkageStatusConditionConditions ) ] conditionGroupbody = ConditionGroup( conditions=listConditionsConditionGroup, logic="or", time_range=timeRangeConditionGroup ) request.body = Rule( app_id="string", status="active", rule_type="DEVICE_LINKAGE", actions=listActionsbody, condition_group=conditionGroupbody, description="string", name="openLight" ) response = client.create_rule(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
创建云端规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.auth.credentials import DerivedCredentials from huaweicloudsdkcore.region.region import Region as coreRegion 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 = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 iotdaEndpoint = "<YOUR ENDPOINT>"; projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate()) client = IoTDAClient.new_builder() \ .with_credentials(credentials) \ # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4) .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \ .build() try: request = CreateRuleRequest() cmdDeviceCommand = Cmd( command_name="SET_LIGHT_SWITCH", command_body="{\"value\":0}", service_id="Switch", buffer_timeout=0, mode="ACTIVE" ) deviceCommandActions = ActionDeviceCommand( device_id="3a9e52d9-3ebf-4985-89e9-6d2396748a2f", cmd=cmdDeviceCommand ) listActionsbody = [ RuleAction( type="DEVICE_CMD", device_command=deviceCommandActions ) ] timeRangeConditionGroup = TimeRange( start_time="18:00", end_time="23:00", days_of_week="2,3,4,5,6" ) strategyFilters = Strategy( trigger="reverse", event_valid_time=300 ) listFiltersDevicePropertyCondition = [ PropertyFilter( path="StreetLight/visibility", operator="<", value="30", strategy=strategyFilters ) ] devicePropertyConditionConditions = DeviceDataCondition( device_id="07b69d78-c716-4be6-9545-869920738397", filters=listFiltersDevicePropertyCondition ) listConditionsConditionGroup = [ RuleCondition( type="DEVICE_DATA", device_property_condition=devicePropertyConditionConditions ) ] conditionGroupbody = ConditionGroup( conditions=listConditionsConditionGroup, logic="or", time_range=timeRangeConditionGroup ) request.body = Rule( app_id="string", status="active", rule_type="DEVICE_LINKAGE", actions=listActionsbody, condition_group=conditionGroupbody, description="string", name="openLight" ) response = client.create_rule(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
创建端侧规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
# coding: utf-8 import os from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkcore.auth.credentials import DerivedCredentials from huaweicloudsdkcore.region.region import Region as coreRegion 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 = os.environ["CLOUD_SDK_AK"] sk = os.environ["CLOUD_SDK_SK"] // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。 iotdaEndpoint = "<YOUR ENDPOINT>"; projectId = "{project_id}" credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate()) client = IoTDAClient.new_builder() \ .with_credentials(credentials) \ # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4) .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \ .build() try: request = CreateRuleRequest() listDeviceIdsDeviceSide = [ "3a9e52d9-3ebf-4985-89e9-6d2396748a2f" ] deviceSidebody = DeviceSide( device_ids=listDeviceIdsDeviceSide ) cmdDeviceCommand = Cmd( command_name="SET_LIGHT_SWITCH", command_body="{\"value\":0}", service_id="Switch", buffer_timeout=0, mode="ACTIVE" ) deviceCommandActions = ActionDeviceCommand( device_id="3a9e52d9-3ebf-4985-89e9-6d2396748a2f", cmd=cmdDeviceCommand ) listActionsbody = [ RuleAction( type="DEVICE_CMD", device_command=deviceCommandActions ) ] timeRangeConditionGroup = TimeRange( start_time="18:00", end_time="23:00", days_of_week="2,3,4,5,6" ) strategyFilters = Strategy( trigger="reverse", event_valid_time=300 ) listFiltersDevicePropertyCondition = [ PropertyFilter( path="StreetLight/visibility", operator="<", value="30", strategy=strategyFilters ) ] devicePropertyConditionConditions = DeviceDataCondition( device_id="3a9e52d9-3ebf-4985-89e9-6d2396748a2f", filters=listFiltersDevicePropertyCondition ) listConditionsConditionGroup = [ RuleCondition( type="DEVICE_DATA", device_property_condition=devicePropertyConditionConditions ) ] conditionGroupbody = ConditionGroup( conditions=listConditionsConditionGroup, logic="or", time_range=timeRangeConditionGroup ) request.body = Rule( device_side=deviceSidebody, app_id="string", status="active", rule_type="DEVICE_SIDE", actions=listActionsbody, condition_group=conditionGroupbody, description="string", name="openLight" ) response = client.create_rule(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
-
创建云端规则,设备下线触发告警。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
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/core/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") // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址 endpoint := "<YOUR ENDPOINT>" projectId := "{project_id}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate" WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios Build() client := iotda.NewIoTDAClient( iotda.IoTDAClientBuilder(). // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象 WithRegion(region.NewRegion("cn-north-4", endpoint)). WithCredential(auth). Build()) request := &model.CreateRuleRequest{} descriptionDeviceAlarm:= "设备下线" deviceAlarmActions := &model.ActionDeviceAlarm{ Name: "设备下线", AlarmStatus: "fault", Severity: "warning", Description: &descriptionDeviceAlarm, } var listActionsbody = []model.RuleAction{ { Type: "DEVICE_ALARM", DeviceAlarm: deviceAlarmActions, }, } daysOfWeekTimeRange:= "2,3,4,5,6" timeRangeConditionGroup := &model.TimeRange{ StartTime: "18:00", EndTime: "23:00", DaysOfWeek: &daysOfWeekTimeRange, } var listStatusListDeviceLinkageStatusCondition = []string{ "OFFLINE", } deviceIdDeviceLinkageStatusCondition:= "07b69d78-c716-4be6-9545-869920738397" durationDeviceLinkageStatusCondition:= int32(0) deviceLinkageStatusConditionConditions := &model.DeviceLinkageStatusCondition{ DeviceId: &deviceIdDeviceLinkageStatusCondition, StatusList: &listStatusListDeviceLinkageStatusCondition, Duration: &durationDeviceLinkageStatusCondition, } var listConditionsConditionGroup = []model.RuleCondition{ { Type: "DEVICE_LINKAGE_STATUS", DeviceLinkageStatusCondition: deviceLinkageStatusConditionConditions, }, } logicConditionGroup:= "or" conditionGroupbody := &model.ConditionGroup{ Conditions: &listConditionsConditionGroup, Logic: &logicConditionGroup, TimeRange: timeRangeConditionGroup, } appIdRule:= "string" statusRule:= "active" descriptionRule:= "string" request.Body = &model.Rule{ AppId: &appIdRule, Status: &statusRule, RuleType: "DEVICE_LINKAGE", Actions: listActionsbody, ConditionGroup: conditionGroupbody, Description: &descriptionRule, Name: "openLight", } response, err := client.CreateRule(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
创建云端规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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/core/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") // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址 endpoint := "<YOUR ENDPOINT>" projectId := "{project_id}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate" WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios Build() client := iotda.NewIoTDAClient( iotda.IoTDAClientBuilder(). // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象 WithRegion(region.NewRegion("cn-north-4", endpoint)). WithCredential(auth). Build()) request := &model.CreateRuleRequest{} var commandBodyCmd interface{} = "{\"value\":0}" bufferTimeoutCmd:= int32(0) modeCmd:= "ACTIVE" cmdDeviceCommand := &model.Cmd{ CommandName: "SET_LIGHT_SWITCH", CommandBody: &commandBodyCmd, ServiceId: "Switch", BufferTimeout: &bufferTimeoutCmd, Mode: &modeCmd, } deviceIdDeviceCommand:= "3a9e52d9-3ebf-4985-89e9-6d2396748a2f" deviceCommandActions := &model.ActionDeviceCommand{ DeviceId: &deviceIdDeviceCommand, Cmd: cmdDeviceCommand, } var listActionsbody = []model.RuleAction{ { Type: "DEVICE_CMD", DeviceCommand: deviceCommandActions, }, } daysOfWeekTimeRange:= "2,3,4,5,6" timeRangeConditionGroup := &model.TimeRange{ StartTime: "18:00", EndTime: "23:00", DaysOfWeek: &daysOfWeekTimeRange, } triggerStrategy:= "reverse" eventValidTimeStrategy:= int32(300) strategyFilters := &model.Strategy{ Trigger: &triggerStrategy, EventValidTime: &eventValidTimeStrategy, } valueFilters:= "30" var listFiltersDevicePropertyCondition = []model.PropertyFilter{ { Path: "StreetLight/visibility", Operator: "<", Value: &valueFilters, Strategy: strategyFilters, }, } deviceIdDevicePropertyCondition:= "07b69d78-c716-4be6-9545-869920738397" devicePropertyConditionConditions := &model.DeviceDataCondition{ DeviceId: &deviceIdDevicePropertyCondition, Filters: &listFiltersDevicePropertyCondition, } var listConditionsConditionGroup = []model.RuleCondition{ { Type: "DEVICE_DATA", DevicePropertyCondition: devicePropertyConditionConditions, }, } logicConditionGroup:= "or" conditionGroupbody := &model.ConditionGroup{ Conditions: &listConditionsConditionGroup, Logic: &logicConditionGroup, TimeRange: timeRangeConditionGroup, } appIdRule:= "string" statusRule:= "active" descriptionRule:= "string" request.Body = &model.Rule{ AppId: &appIdRule, Status: &statusRule, RuleType: "DEVICE_LINKAGE", Actions: listActionsbody, ConditionGroup: conditionGroupbody, Description: &descriptionRule, Name: "openLight", } response, err := client.CreateRule(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
-
创建端侧规则,属性上报触发命令下发。
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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/core/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") // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址 endpoint := "<YOUR ENDPOINT>" projectId := "{project_id}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithProjectId(projectId). // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate" WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios Build() client := iotda.NewIoTDAClient( iotda.IoTDAClientBuilder(). // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象 WithRegion(region.NewRegion("cn-north-4", endpoint)). WithCredential(auth). Build()) request := &model.CreateRuleRequest{} var listDeviceIdsDeviceSide = []string{ "3a9e52d9-3ebf-4985-89e9-6d2396748a2f", } deviceSidebody := &model.DeviceSide{ DeviceIds: &listDeviceIdsDeviceSide, } var commandBodyCmd interface{} = "{\"value\":0}" bufferTimeoutCmd:= int32(0) modeCmd:= "ACTIVE" cmdDeviceCommand := &model.Cmd{ CommandName: "SET_LIGHT_SWITCH", CommandBody: &commandBodyCmd, ServiceId: "Switch", BufferTimeout: &bufferTimeoutCmd, Mode: &modeCmd, } deviceIdDeviceCommand:= "3a9e52d9-3ebf-4985-89e9-6d2396748a2f" deviceCommandActions := &model.ActionDeviceCommand{ DeviceId: &deviceIdDeviceCommand, Cmd: cmdDeviceCommand, } var listActionsbody = []model.RuleAction{ { Type: "DEVICE_CMD", DeviceCommand: deviceCommandActions, }, } daysOfWeekTimeRange:= "2,3,4,5,6" timeRangeConditionGroup := &model.TimeRange{ StartTime: "18:00", EndTime: "23:00", DaysOfWeek: &daysOfWeekTimeRange, } triggerStrategy:= "reverse" eventValidTimeStrategy:= int32(300) strategyFilters := &model.Strategy{ Trigger: &triggerStrategy, EventValidTime: &eventValidTimeStrategy, } valueFilters:= "30" var listFiltersDevicePropertyCondition = []model.PropertyFilter{ { Path: "StreetLight/visibility", Operator: "<", Value: &valueFilters, Strategy: strategyFilters, }, } deviceIdDevicePropertyCondition:= "3a9e52d9-3ebf-4985-89e9-6d2396748a2f" devicePropertyConditionConditions := &model.DeviceDataCondition{ DeviceId: &deviceIdDevicePropertyCondition, Filters: &listFiltersDevicePropertyCondition, } var listConditionsConditionGroup = []model.RuleCondition{ { Type: "DEVICE_DATA", DevicePropertyCondition: devicePropertyConditionConditions, }, } logicConditionGroup:= "or" conditionGroupbody := &model.ConditionGroup{ Conditions: &listConditionsConditionGroup, Logic: &logicConditionGroup, TimeRange: timeRangeConditionGroup, } appIdRule:= "string" statusRule:= "active" descriptionRule:= "string" request.Body = &model.Rule{ DeviceSide: deviceSidebody, AppId: &appIdRule, Status: &statusRule, RuleType: "DEVICE_SIDE", Actions: listActionsbody, ConditionGroup: conditionGroupbody, Description: &descriptionRule, Name: "openLight", } response, err := client.CreateRule(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。
状态码
状态码 |
描述 |
---|---|
201 |
Created |
400 |
Bad Request |
401 |
Unauthorized |
403 |
Forbidden |
404 |
Not Found |
500 |
Internal Server Error |
错误码
请参见错误码。