生活物聯網平臺提供的場景自動化是基于物模型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"}