生活物联网平台提供的场景自动化是基于物模型TSL(Thing Specification Language)设计的。按照一定的逻辑规律,将设备的物模型、设备动作等按照场景联动,达到自动化执行的效果。
概述
平台针对自动化场景定义一组API协议,API使用Oauth2.0协议进行鉴权。API请参见场景服务。
下面介绍几种创建场景常用的模式。
场景模式 | 规则描述 | 备注 |
TCA(Trigger&Condition&Action) |
| IFTTT(If This Than That)规则,是一个JSON描述,包含type、trigger、condition、action四个部分。即设置一个触发器,当满足条件时,自动触发去执行某动作。 |
CA(Condition&Action) |
设备同一属性不能重复设置,条件和动作不能设置相同的设备属性 | 以CA模式创建场景还分为以下两种情况:
|
Condition介绍
condition通常支持时间点、设备属性、时间段,下面介绍这几种的样例代码。
时间点
指定在某个时间点允许执行对应的任务。
{ "uri": "condition/timer", "params": { "cron": "", //定时表达式,参考http://crontab.org/ "cronType": "", //表达式类型,支持"linux"、"quartz_cron";"linux":表示crontab类型,5位;"quartz_cron":表示quartz类型,7位,支持年。当前只能最小设置分钟级别,第一位秒级,必须是 0 "timezoneID": "" //时区 } }
时间段
指定在一周内的某天的某个时间段中允许执行对应的任务。
{ "uri": "condition/timeRange", "params": { "format": "", //时间格式为"HH:mm" "beginDate": "", //开始时间,例如 "08:30" 指condition生效期从一天的早上8点30开始 "endDate": "", //结束时间,例如 "23:00" 指condition的生效期到晚上23点00结束 "repeat": "", //重复日期,例如 "1,2,3",一周内的周1周2周3允许执行,支持1~7表示周一到周日,用‘,’分割 "timezoneID": "" //时区 } }
设备属性
当设备属性变更到对应值,触发对应的任务。
{ "uri": "condition/device/property", "params": { "productKey": "", //产品productKey "deviceName": "", //设备deviceName "propertyName": "", //属性唯一标识符(产品下唯一),TSL模型中properties的identifier "compareType": "", //比较类型,支持 ">"、"<"、"==" "compareValue": "" //比较的属性值,这里值的类型用TSL模型中properties的type,如果type是int、float等compareValue是数字,type是text时 compareValue是String,以此类推 } }
Action介绍
Action支持以下几种。
设备属性
满足条件时设置设备属性。
{ "uri": "action/device/setProperty", "params": { "iotId": "", //设备ID "propertyValue": "", //比较的属性值 "propertyName": "" //属性唯一标识符(产品下唯一),TSL模型中properties的identifier } }
设备服务
满足条件时执行设备服务,设备服务需要全部属性都设置才可以创建。
{ "uri": "action/device/invokeService", "params": { "iotId": "", //设备ID "serviceName": "", //服务名称,TSL模型中services的identifier "serviceArgs": {} //服务参数集,key为TSL模型中services的inputData某一项的identifier,value为inputData某一项的值 } }
场景
满足条件时执行场景。
{ "uri": "action/scene/trigger", "params": { "sceneId": "" //场景Id } }
发送通知
满足条件时向当前用户发送通知。
{ "uri": "action/mq/send", "params": { "msgTag": "IlopBusiness_CustomMsg", //通知消息的一个固定规则tag "customData": { "message": "" //通知内容,最多60个字符 } } }
创建自动化(示例一)
以设备开关作为condition,发送通知作为action为例,介绍自动化的开发流程如下。
获取设备的属性。
获取设备属性的接口为获取设备上支持TCA配置的功能属性列表,示例入参如下。
"params" : { "iotId" : "dhS41YnxxxxYx000101", //设备ID "flowType" : 1 // 1代表condition,2 代表action,云端会根据flowType过滤掉不支持的属性、服务 }
选择主灯开关属性作为condition(设置属性值为1),对应的返回结果如下。其中,propertyName是选择属性的identifier。通知的内容设置为“演示自动化创建”。
"data" : { "abilityDsl" : { //abilityDsl是设备的TSL数据 "properties" : [ //properties是设备TSL中的属性列表 { "required" : true, "accessMode" : "rw", "identifier" : "LightSwitch", "dataType" : { "specs" : { //specs是可选的值和值的名称 "0" : "关闭", "1" : "开启" }, "type" : "bool" //type是属性值的类型 }, "name" : "主灯开关" } ], "services" : [ { "method" : "thing.service.property.set", "callType" : "async", "desc" : "属性设置", "outputData" : [ ], "identifier" : "set", "required" : true, "inputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "name" : "set" }, { "method" : "thing.service.property.get", "callType" : "async", "desc" : "属性获取", "outputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "identifier" : "get", "required" : true, "inputData" : [ "LightSwitch" ], "name" : "get" } ], "schema" : "https:\/\/iotx-tsl.oss-ap-southeast-1.aliyuncs.com\/schema.json", "profile" : { "productKey" : "a1xxxxG" }, "events" : [ { "method" : "thing.event.property.post", "outputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "identifier" : "post", "type" : "info", "required" : true, "name" : "post", "desc" : "属性上报" }, { "type" : "error", "required" : true, "method" : "thing.event.Error.post", "identifier" : "Error", "name" : "故障上报", "outputData" : [ { "dataType" : { "specs" : { "0" : "正常" }, "type" : "enum" }, "name" : "故障代码", "identifier" : "ErrorCode" } ] } ] }, "simplifyAbilityDTOs" : [ { "type" : 1, // simplifyAbilityDTOs属性列表的值,1表示属性,2表示服务,3表示事件 "identifier" : "LightSwitch", //propertyName是设备属性的identifier,此处设置的值为LightSwitch "categoryType" : "Light", "name" : "主灯开关" } ] }
创建自动化。
创建自动化的接口为创建场景, 示例如下。
"params" : { "enable" : true, "icon" : "http://gaic.alicdn.com/ztms/cloud-intelligence-icons/icons/scene_img_xxxx_1.png", "mode" : "all", "catalogId" : "1", "caConditions" : [ { "uri" : "condition/device/property", "params" : { "propertyName" : "LightSwitch", "productKey" : "a1xxxx7G", "compareValue" : 1, "compareType" : "==", "deviceName" : "VDxxxxx15RH" } } ], "sceneType" : "CA", "actions" : [ { "uri" : "action/mq/send", "params" : { "customData" : { "message" : "演示自动化创建" //设置通知的内容 }, "msgTag" : "IlopBusiness_CustomMsg" } } ], "name" : "演示自动化创建", "iconColor" : "#A86AFB" }
场景自动化(示例二)
下面以时间点(中国内地地区周一~周五13:35 重复执行)作为condition,设备服务作为action为例,介绍自动化的开发流程如下。
设置时间点。
{ "uri" : "condition/timer", "params" : { "timezoneID" : "Asia/Shanghai", "cron" : "35 13 * * 1,2,3,4,5", "cronType" : "linux" } }
获取设备的属性。
获取设备属性的接口为获取设备上支持TCA配置的功能属性列表,示例入参如下。
"params" : { "iotId" : "z2v7EHHxxxxoWs000100", "flowType" : 2 // 2 代表action,云端会根据flowType过滤掉不支持的属性、服务 }
对应的返回结果如下。
"data" : { "abilityDsl" : { //abilityDsl是设备的TSL数据 "properties" : [ //properties是设备TSL中的属性列表 { "required" : true, "accessMode" : "rw", "identifier" : "LightSwitch", "dataType" : { "specs" : { //specs是可选的值和值的名称 "0" : "关闭", "1" : "开启" }, "type" : "bool" //type是属性值的类型 }, "name" : "主灯开关" }, { "required" : true, "accessMode" : "rw", "identifier" : "WIFI_Band", "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "频段" }, { "required" : true, "accessMode" : "rw", "identifier" : "WiFI_RSSI", "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "-1", "step" : "1" }, "type" : "int" }, "name" : "信号强度" }, { "required" : true, "accessMode" : "rw", "identifier" : "WIFI_AP_BSSID", "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "热点BSSID" }, { "required" : true, "accessMode" : "rw", "identifier" : "WIFI_Channel", "dataType" : { "specs" : { "unitName" : "无", "min" : "1", "max" : "255", "step" : "1" }, "type" : "int" }, "name" : "信道" }, { "required" : true, "accessMode" : "rw", "identifier" : "WiFI_SNR", "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "127", "step" : "1" }, "type" : "int" }, "name" : "信噪比" } ], "services" : [ { "method" : "thing.service.property.set", "callType" : "async", "desc" : "属性设置", "outputData" : [ ], "identifier" : "set", "required" : true, "inputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "频段", "identifier" : "WIFI_Band" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "-1", "step" : "1" }, "type" : "int" }, "name" : "信号强度", "identifier" : "WiFI_RSSI" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "热点BSSID", "identifier" : "WIFI_AP_BSSID" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "1", "max" : "255", "step" : "1" }, "type" : "int" }, "name" : "信道", "identifier" : "WIFI_Channel" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "127", "step" : "1" }, "type" : "int" }, "name" : "信噪比", "identifier" : "WiFI_SNR" } ], "name" : "set" }, { "method" : "thing.service.property.get", "callType" : "async", "desc" : "属性获取", "outputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "频段", "identifier" : "WIFI_Band" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "-1", "step" : "1" }, "type" : "int" }, "name" : "信号强度", "identifier" : "WiFI_RSSI" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "热点BSSID", "identifier" : "WIFI_AP_BSSID" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "1", "max" : "255", "step" : "1" }, "type" : "int" }, "name" : "信道", "identifier" : "WIFI_Channel" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "127", "step" : "1" }, "type" : "int" }, "name" : "信噪比", "identifier" : "WiFI_SNR" } ], "identifier" : "get", "required" : true, "inputData" : [ "LightSwitch", "WIFI_Band", "WiFI_RSSI", "WIFI_AP_BSSID", "WIFI_Channel", "WiFI_SNR" ], "name" : "get" }, { "method" : "thing.service.ToggleLightSwitch", "callType" : "async", "outputData" : [ ], "identifier" : "ToggleLightSwitch", "required" : false, "inputData" : [ { "dataType" : { "specs" : { "min" : "1", "max" : "100", "unit" : "count", "step" : "1" }, "type" : "int" }, "name" : "测试1", "identifier" : "test1" }, { "dataType" : { "specs" : { "min" : "1", "max" : "10", "unit" : "mm\/s", "step" : "0.1" }, "type" : "float" }, "name" : "测试2", "identifier" : "test2" } ], "name" : "翻转主灯开关" }, { "method" : "thing.service.SetLightSwitchTimer", "callType" : "async", "outputData" : [ ], "identifier" : "SetLightSwitchTimer", "required" : false, "inputData" : [ { "dataType" : { "specs" : { "unitName" : "分", "min" : "0", "max" : "1440", "unit" : "min", "step" : "0.01" }, "type" : "double" }, "name" : "计时器", "identifier" : "Timer" }, { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "name" : "设置主灯开关倒计时" }, { "method" : "thing.service.ControlDevice", "callType" : "sync", "outputData" : [ { "dataType" : { "specs" : { "min" : "0", "max" : "99999", "unitName" : "无" }, "type" : "int" }, "name" : "返回码", "identifier" : "Code" }, { "dataType" : { "specs" : { "length" : "128" }, "type" : "text" }, "name" : "返回消息", "identifier" : "Message" } ], "identifier" : "ControlDevice", "required" : false, "inputData" : [ { "dataType" : { "specs" : { "length" : "64" }, "type" : "text" }, "name" : "主机ID", "identifier" : "HostId" }, { "dataType" : { "specs" : { "length" : "64" }, "type" : "text" }, "name" : "围栏ID", "identifier" : "EfenceId" }, { "dataType" : { "specs" : { "length" : "64" }, "type" : "text" }, "name" : "动作", "identifier" : "Action" } ], "name" : "控制设备" } ], "schema" : "https:\/\/iotx-tsl.oss-ap-southeast-1.aliyuncs.com\/schema.json", "profile" : { "productKey" : "a1xxxxxska" }, "events" : [ { "method" : "thing.event.property.post", "outputData" : [ { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "频段", "identifier" : "WIFI_Band" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "-1", "step" : "1" }, "type" : "int" }, "name" : "信号强度", "identifier" : "WiFI_RSSI" }, { "dataType" : { "specs" : { "length" : "255" }, "type" : "text" }, "name" : "热点BSSID", "identifier" : "WIFI_AP_BSSID" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "1", "max" : "255", "step" : "1" }, "type" : "int" }, "name" : "信道", "identifier" : "WIFI_Channel" }, { "dataType" : { "specs" : { "unitName" : "无", "min" : "-127", "max" : "127", "step" : "1" }, "type" : "int" }, "name" : "信噪比", "identifier" : "WiFI_SNR" } ], "identifier" : "post", "type" : "info", "required" : true, "name" : "post", "desc" : "属性上报" }, { "type" : "error", "required" : true, "method" : "thing.event.Error.post", "identifier" : "Error", "name" : "故障上报", "outputData" : [ { "dataType" : { "specs" : { "0" : "恢复正常" }, "type" : "enum" }, "name" : "故障代码", "identifier" : "ErrorCode" } ] } ] }, "simplifyAbilityDTOs" : [ { "type" : 1, "identifier" : "LightSwitch", "categoryType" : "Light", "name" : "主灯开关" }, { "type" : 1, "identifier" : "WiFI_RSSI", "categoryType" : "Light", "name" : "信号强度" }, { "type" : 1, "identifier" : "WIFI_Channel", "categoryType" : "Light", "name" : "信道" }, { "type" : 1, "identifier" : "WiFI_SNR", "categoryType" : "Light", "name" : "信噪比" }, { "type" : 2, "identifier" : "ToggleLightSwitch", "categoryType" : "Light", "name" : "翻转主灯开关" }, { "type" : 2, "identifier" : "SetLightSwitchTimer", "categoryType" : "Light", "name" : "设置主灯开关倒计时" }, { "type" : 2, //simplifyAbilityDTOs属性列表的值,1表示属性,2表示服务,3表示事件 "identifier" : "ControlDevice", //propertyName是设备属性的identifier,此处设置的值为ControlDevice "categoryType" : "Light", "name" : "控制设备" } ] }
abilityDsl是设备的TSL数据,services是设备TSL的服务列表,services中inputData是服务需要设置的properties列表。还需要配置以下TSL信息。
// 以下是TSL中的一个服务 { "method" : "thing.service.SetLightSwitchTimer", "callType" : "async", "outputData" : [ ], "identifier" : "SetLightSwitchTimer", "required" : false, "inputData" : [ //inputData是服务需要设置的properties列表 { "dataType" : { "specs" : { "unitName" : "分", "min" : "0", "max" : "1440", "unit" : "min", "step" : "0.01" }, "type" : "double" }, "name" : "计时器", "identifier" : "Timer" }, { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "name" : "设置主灯开关倒计时" }
返回结果如下所示。
{ "method" : "thing.service.SetLightSwitchTimer", "callType" : "async", "outputData" : [ ], "identifier" : "SetLightSwitchTimer", "required" : false, "inputData" : [ { "dataType" : { "specs" : { "unitName" : "分", "min" : "0", "max" : "1440", "unit" : "min", "step" : "0.01" }, "type" : "double" }, "name" : "计时器", "identifier" : "Timer" }, { "dataType" : { "specs" : { "0" : "关闭", "1" : "开启" }, "type" : "bool" }, "name" : "主灯开关", "identifier" : "LightSwitch" } ], "name" : "设置主灯开关倒计时" //服务名称为“设置主灯开关倒计时”作为action }
选择服务作为action,还需要给services包含的所有属性“计时器”、“主灯开关”都设置值后才可以生效,计时器设置值为0.08,主灯开关设置值为1,代码如下。
{ "uri" : "action/device/invokeService", "params" : { "iotId" : "z2v7EHHfExxxxWs000100", "serviceName" : "SetLightSwitchTimer", // TSL中服务的SetLightSwitchTimer "serviceArgs" : { // {属性identifier: 属性值} "Timer" : 0.08, //计时器设置值为0.08 "LightSwitch" : 1 //主灯开关设置值为1 } } }
创建自动化。
创建自动化的接口为创建场景,示例代码如下。
"params" : { "enable" : true, "icon" : "http://gaic.alicdn.com/ztms/cloud-intelligence-icons/icons/scene_xxxx_15.png", "mode" : "all", "catalogId" : "1", "caConditions" : [ { "uri" : "condition/timer", "params" : { "timezoneID" : "Asia/Shanghai", "cron" : "35 13 * * 1,2,3,4,5", "cronType" : "linux" } } ], "sceneType" : "CA", "actions" : [ { "uri" : "action/device/invokeService", "params" : { "iotId" : "z2v7EHHfxxxxoWs000100", "serviceName" : "SetLxxxxxTimer", // TSL中服务的SetLightSwitchTimer "serviceArgs" : { "Timer" : 0.08, "LightSwitch" : 1 } } } ], "name" : "小庆灯我的小灯泡xiaoqingdeng-设置主灯开关倒计时", "iconColor" : "#FF454F" }
创建手动触发场景
自动化是用户创建后会按照响应的触发条件自动触发动作的任务;而手动触发场景则需要用户自己手动执行才会触发执行任务。因此,手动触发场景只有一系列的Action。
创建手动触发场景
下面以执行风扇电源开关(打开)为例,介绍如何创建手动触发场景。
使用创建场景接口来创建场景,示例代码如下。
"params":{ "enable":true, "icon":"http://gaic.alicdn.com/ztms/cloud-intelligence-icons/icons/scene_img_xxxx_13.png", "iconColor":"#738DE1", "name":"家家风扇01-电源开关 - 开启", "actions":[ { "uri":"action/device/setProperty", "params":{ "propertyName":"PowerSwitch", "iotId":"bx6lzXLxxxxLNOO000101", "propertyValue":1 } } ], "catalogId":"0" }
说明手动触发场景与自动化的创建很类似,区别在于没有caConditions,且不传sceneType mode这两个非必填参数。
执行手动触发场景
执行场景的接口为执行场景,示例代码如下。
params:{"sceneId":"7823be10xxxx8790f36bdc629e"}