邊緣應用 Link IoT Edge對接
本章主要介紹如何實現 Link IoT Edge平臺的API接口調用。物聯網邊緣計算支持使用邊緣端API管理接入到網關的設備,包括獲取設備及設備列表、設置設備屬性、訂閱設備事件等。
1.Link IoT Edge平臺概述
Link IoT Edge提供了設備接入和本地計算等能力,當Link IoT Edge被集成到第三方的業務系統中時,需要提供標準的協議接口,來方便其它應用程序訪問設備和配置本地計算規則等能力。
MQTT的訂閱/發布消息模型,允許應用程序靈活的訂閱設備數據,當設備有數據上報時,消息會推送至應用程序,簡化了應用程序的實現。
2. Topic定義規范
MQTT協議使用3.1版本,MQTT Broker默認隨LE一同部署,應用程序通過訪問LE的IP地址+8883端口接入到MQTT Broker。 MQTT客戶端連接Broker的用戶名和密碼由用戶在云端控制臺配置并部署到邊緣網關,目前密碼配置露出在邊緣分組配置頁面。
LE的IP地址獲取方法:
當應用程序被LE托管,運行在容器模式時,應用可以通過讀取環境變量 “FCBASE_IPADDR“獲取LE容器當前的IP地址。
當應用程序被LE托管,運行在進程模式時,使用“127.0.0.1”本機地址即可。
當應用程序為第三方系統組件,沒有被LE托管,訪問運行LE的宿主機IP地址即可。
從訪問對象范圍的角度主要分為兩類,第一類是訪問整個邊緣(網關)分組,第二類是訪問單個設備/網關。
2.1 MQTT主題格式
{srcApp}/{messageType}/{destApp}/{method}每級字段長度上限為128字節。
2.1.1 {srcApp}
{srcApp}表示發布消息的APP身份。如果消息是由linkedge發布,則對應的{srcApp}為linkedge,如果消息是由其它APP發布的,則{srcApp}是該APP的關鍵字名稱,如ibms。主題中包含消息發布者的身份有以下優點:
隔離廣播域,避免應答消息被其它模塊接收。
溯源,排查問題時根據主題信息可以定位到請求發起者的身份。
身份驗證,{srcApp}字段可用于填寫系統頒發的AppID,用于權限驗證。
2.1.2 {messageType}
{messageType}表示消息的類型,目前僅支持三種:
request: 表明消息為請求消息。
response:表明消息為響應消息。
notify:表明消息為事件通知消息,此類消息用于廣播推送,所有訂閱該Topic的APP都可以收到推送信息。
2.1.3 {destApp}
{destApp}表示消息目標,用于指定請求的目標APP。如果請求linkedge的服務,則對應的{destApp}為linkedge,如果請求其它APP服務,則為該APP的關鍵字名稱。當{messageType}為notify時,不需要{destApp}字段。
2.1.4 {method}
{method}表示方法名,例如,可用getDeviceList表示獲取設備信息列表。
2.2 分組Topic格式
該類Topic用于訪問整個邊緣(網關)分組的服務,格式如下:
請求Topic:
{srcApp}/request/linkedge/{method}
應答Topic:
linkedge/response/{srcApp}/{method}
事件通知Topic:
linkedge/notify/{event-name}
樣例:
ibms應用程序獲取分組中所有的設備列表Topic:
請求Topic:ibms/request/linkedge/getDeviceList
應答Topic:linkedge/response/ibms/getDeviceList
子設備狀態變更(下線->上線)Topic:
linkedge/notify/deviceStateChanged
2.3 網關Topic
該類Topic訪問單臺網關的服務,格式如下:
請求Topic:
{srcApp}/request/linkedge/{pk}/{dn}/{method}
應答Topic:
linkedge/response/{srcApp}/{pk}/{dn}/{method}
事件通知Topic:
linkedge/notify/{pk}/{dn}/{event-name}
{pk}和{dn}為目標網關的ProductKey和DeviceName。
以ibms應用程序獲取網關的設備列表為例:
ibms應用程序獲取網關的設備列表Topic:
請求Topic:ibms/request/linkedge/{pk}/{dn}/getDeviceList
應答Topic:linkedge/response/ibms/{pk}/{dn}/getDeviceList
某網關下子設備狀態變更(下線->上線)Topic:
linkedge/notify/{pk}/{dn}/deviceStateChanged
2.3 基于MQTT的方法調用
2.3.1 訂閱應答Topic
基于MQTT的方法調用,通常會定義一對“請求”和“應答”Topic。應用程序需要調用服務方法時,需要先訂閱應答Topic,再向請求Topic發送請求,才能正常獲取服務應答信息。為了便于代碼實現,訂閱應答Topic時,設備的PK,DN和方法都可以使用通配符,這樣只需訂閱一次應答Topic,后續請求設備服務無需再動態訂閱應答Topic。下面的樣例以讀取設備屬性為例,應答Topic配置為”thing/response/testApp/+/+/+”,只需訂閱一次,后續訪問任意設備的任何服務,都可以收到應答消息。
2.3.2 Payload通用字段說明
請求Payload中必填的字段為“requestId”,類型為string,用作消息標識,相同APP源發出的請求消息的requestId應該各不相同,可用UUID、隨機數或自增長數值等手段。如果請求消息中沒有填寫”requestId”,該請求將被丟棄。
請求Payload的可選字段的“timeout”,類型為number,單位為秒,用于標識請求的超時時間,當LE處理請求超過該時間仍未完成,LE會回送TIMEOUT錯誤信息。默認值為4s,最少1s,最大120s。
請求Payload的可選字段的“timestamp”,類型為number,單位為毫秒,用于標識請求的發起時間。
請求Payload的可選字段的“version”,類型為string,用于標識請求的版本號,目前用作保留字段。
/* 1.先訂閱應答Topic,用戶接收設備應答數據 */ #./mosquitto_sub --psk-identity linkedge --psk 12345678 -p 8883 -t thing/response/testApp/+/+/+ -v & /* 2.發送設備方法調用get請求 */ #./mosquitto_pub --psk-identity linkedge --psk 12345678 -p 8883 -t testApp/request/thing/a1Mtx4p2fHG/LightSensor/get -m "{\"params\":[\"MeasuredIlluminance\"],\"requestId\":\"ff8db62b-4d75-46c8-bf6a-1600d326a88b\"}" /* 3.mosquitto_sub輸出設備應答信息 */ thing/response/testApp/a1Mtx4p2fHG/LightSensor/get {"code":0,"message":"success","params":{"MeasuredIlluminance":1000},"requestId":"ff8db62b-4d75-46c8-bf6a-1600d326a88b"}
3. 云端依賴
3.1 配置MQTT用戶名和密碼
使用MQTT協議訪問LE網關,用戶首先要在云端邊緣分組頁面,配置分組訪問的用戶名和密碼。用戶程序中的MQTT客戶端使用該用戶名和密碼訪問LE。
3.2 選擇分組代理網關
應用程序需要訪問邊緣分組的信息時,需要有一臺網關(分組代理網關)負責查詢、收集、匯總分組中所有的網關信息,并返回一條應答消息給應用程序。這時涉及如何選擇分組中代理網關的問題,有兩個策略:
用戶在頁面選擇“代理網關”。
分組中網關根據一定策略協商出一臺代理網關,如選擇網關中IP地址最小的作為代理網關。
跟智慧園區產品線同學確認,傾向于讓用戶指定“代理網關”,原因是“現場會在和外部應用一起在服務器上部署一個LE,最好指定這個LE。”
為了更好的用戶體驗,當分組中只有一臺網關時,該網關默認成為分組代理網關,響應所有分組請求消息。
3.1 邊緣網關Topic示例:
該類Topic用于訪問整個邊緣(網關)分組的服務,格式如下:
請求Topic:
{srcApp}/request/linkedge/{method}
應答Topic:
linkedge/response/{srcApp}/{method}
事件通知Topic:
linkedge/notify/{event-name}
ibms應用程序獲取分組中所有的設備列表Topic:
請求Topic:ibms/request/linkedge/getDeviceList
應答Topic:linkedge/response/ibms/getDeviceList
子設備狀態變更(下線->上線)Topic:linkedge/notify/deviceStateChanged
3.2 分組Topic格式示例:
該類Topic用于訪問整個邊緣(網關)分組的服務,格式如下:
請求Topic:
{srcApp}/request/linkedge/{method}
應答Topic:
linkedge/response/{srcApp}/{method}
事件通知Topic:
linkedge/notify/{event-name}
ibms應用程序獲取分組中所有的設備列表Topic:
請求Topic:ibms/request/linkedge/getDeviceList
應答Topic:linkedge/response/ibms/getDeviceList
子設備狀態變更(下線->上線)Topic:linkedge/notify/deviceStateChanged
4. 接口詳細說明
4.1 設備讀寫
設備讀寫主要包含設備數據的訂閱和設備方法的調用兩部分。
4.1.1 設備數據訂閱
通過訂閱設備數據上報的Topic,應用程序可以監聽設備上報的屬性值和事件信息。
設備屬性上報Topic:
thing/notify/${pk}/${dn}/property
設備事件上報Topic:
thing/notify/${pk}/${dn}/${tsl.event.identifer}
數據發布Payload透傳驅動上報數據:
# 示例1-設備屬性上報:
Topic:thing/notify/${pk}/${dn}/property
Payload:
{
"key1":{
"value":"value1",
"time":152444872****
},
"key2":{
"value":"value2",
"time":152444872****
}
}
# 示例2-設備事件上報:
Topic:thing/notify/${pk}/${dn}/${tsl.event.identifer}
Payload:
{
"params": {
"value" : {
"key1":"value1",
"key2":"value2"
},
"time" : 152444872****
}
}
4.1.2 設備方法調用
設置設備屬性,調用設備set方法Topic:
{srcApp}/request/thing/${pk}/${dn}/set
獲取設備屬性值,調用設備get方法Topic:
{srcApp}/request/thing/${pk}/${dn}/get
調用設備自定義方法Topic:
{srcApp}/request/thing/${pk}/${dn}/${tsl.service.identifer}
設備配置Payload在驅動請求參數基礎上增加了requestId字段,用于標識每次配置請求:
#示例-設置設備屬性
Topic:testApp/request/thing/${productKey}/${deviceName}/set
Payload:
{
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9776", #[Required]
"timeout":5,
"params":{ #設備方法調用的參數[Required]
"key1":"value1",
"key2":"value2"
}
}
#示例-獲取設備屬性
Topic:testApp/request/thing/${productKey}/${deviceName}/get
Payload:
{
"requestId":"ff8db62b-4d75-46c8-bf6a-1600d326a88b", #[Required]
"params":[ #設備方法調用的參數[Required]
"key"
]
}
設置設備屬性應答Topic:
thing/response/{srcApp}/${pk}/${dn}/set
獲取設備屬性應答Topic:
thing/response/{srcApp}/${pk}/${dn}/get
調用設備自定義方法應答Topic:
thing/response/{srcApp}/${pk}/${dn}/${tsl.service.identifer}
配置應答Payload在驅動應答消息基礎上增加了requestId字段,用于匹配對應的配置請求:
#示例-設置設備屬性應答數據
Topic:thing/response/{srcApp}/${pk}/${dn}/set
Payload:
{
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9776",
"code":0,
"message":"",
"params":{}
}
#示例-獲取設備屬性應答數據
Topic:thing/response/{srcApp}/${pk}/${dn}/get
Payload:
{
"requestId":"ff8db62b-4d75-46c8-bf6a-1600d326a88b",
"code":0,
"message":"success",
"params":{
"key":"value"
}
}
4.2 設備狀態查詢
4.2.1 獲取設備列表
獲取分組設備列表Topic:
{srcApp}/request/linkedge/getDeviceList
獲取網關設備列表Topic:
{srcApp}/request/linkedge/{pk}/{dn}/getDeviceList
請求Payload:
{
"requestId": "0bc458b5-47f6-472f-bd88-3624696e9777", # [Required]
"timestamp": 157121985****, #ms [Optional]
"timeout":3, # 最少1s,最多120s [Optional: default 4s]
"version": "1.0" # [Optional]
}
獲取分組設備列表應答Topic:
linkedge/response/{srcApp}/getDeviceList
應答Payload
{ "params":[ #數組,包含所有網關下的子設備 { "gwProductKey":"a1PQ9828qvr", #網關的productkey "gwDeviceName":"yinlong_gw_dev1",#網關的devicename "code":0, "message":"success", "deviceList":[ #設備列表 { "productKey":"a1KRepmC2XJ", #設備的productkey "deviceName":"modbus_tcp_dev_1", #設備的devicename "localOnline":"false", #設備與網關的連接狀態 "cloudOnline":"false", #設備與云端的連接狀態 "activationState":"notActivated",#設備在云端是否被激活 "nickName":"xxx", #設備別名 "tags":[ #設備包含的標簽 ] }, { "productKey":"a1ZJTVsqj2y", "deviceName":"lightDevNoSpecial", "localOnline":"true", "cloudOnline":"true", "activationState":"activated", "nickName":"xxx", #設備別名 "tags":[ ] }, { "productKey":"a1ZJTVsqj2y", "deviceName":"LightDev2", "localOnline":"false", "cloudOnline":"false", "activationState":"notActivated", "nickName":"xxx", #設備別名 "tags":[ #設備包含的標簽 { "key":"type", #設備標簽的key "value":"light" #設備標簽的value }, { "key":"owner", "value":"yinlong" }, { "key":"location", "value":"master bedroom" }, { "key":"color", "value":"red" } ] } ] } ], "requestId":"0bc458b5-47f6-472f-bd88-3624696e9777", "version":"1.0", "timestamp":157225013****, "code":0, "message":"success" }
獲取網關設備列表應答Topic:
linkedge/response/``{srcApp}``/{pk}/{dn}/getDeviceList
應答Payload:
{ "requestId":"0bc458b5-47f6-472f-bd88-3624696e9777", "timestamp": 157121985****, "version":"1.0", "code":0, "message":"success", "params":[ #設備列表 { "productKey":"a1Mtx4p2fHG", #設備的productkey "deviceName":"device1", #設備的devicename "localOnline":"true", #設備和網關的連接狀態 "cloudOnline":"true", #設備和云端的連接狀態 "activationState": "activated/notActivated/activationFailed", #設備在云端的激活狀態 "tag":[ { "key":"coordinate", "value":"0:0" } ] } ] }
4.2.2 獲取設備TSL配置信息
獲取設備TSL請求Topic:
{srcApp}/request/thing/{pk}/{dn}/getTsl
如果topic中的pk、dn為網關的,參數中指定設備pk,需要遍歷所有網關,查詢返回可能會有多個網關下pk的tsl,可能存在不一致的情況,如何選擇是個問題。
如果Topic中僅指定設備pk可能有多個網關回復, Topic中pk、dn為設備的,這樣可以保證僅有一個網關回復,而且就是你想操作的設備的tsl,更加合理。
請求Payload:
{
"requestId": "0bc458b5-47f6-472f-bd88-3624696e9778",
"version": "1.0",
"timeout":3 # 最少1s,最多120s
}
獲取設備TSL應答Topic:
thing/response/{srcApp}/{pk}/{dn}/getTsl
應答Payload:
{
"params":{
"productKey":"a1KRepmC2XJ",
"deviceName":"modbus_tcp_dev_1",
"code":0,
"message":"success",
"tsl":"{"events":[{"desc":"屬性上報","identifier":"post","method":"thing.event.property.post","name":"post","outputData":[{"dataType":{"specs":{"max":"2147483647","min":"-2147483648","unit":"Mpa"},"type":"double"},"identifier":"pressure1","name":"pressure1"}],"required":true,"type":"info"}],"profile":{"productKey":"a1KRepmC2XJ"},"properties":[{"accessMode":"rw","dataType":{"specs":{"max":"2147483647","min":"-2147483648","unit":"Mpa"},"type":"double"},"identifier":"pressure1","name":"pressure1","required":true}],"schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json","services":[{"callType":"async","desc":"屬性設置","identifier":"set","inputData":[{"dataType":{"specs":{"max":"2147483647","min":"-2147483648","unit":"Mpa"},"type":"double"},"identifier":"pressure1","name":"pressure1"}],"method":"thing.service.property.set","name":"set","outputData":[],"required":true},{"callType":"async","desc":"屬性獲取","identifier":"get","inputData":["pressure1"],"method":"thing.service.property.get","name":"get","outputData":[{"dataType":{"specs":{"max":"2147483647","min":"-2147483648","unit":"Mpa"},"type":"double"},"identifier":"pressure1","name":"pressure1"}],"required":true}]}", #物模型
"tslConfig":"{"profile":{"productKey":"a1KRepmC2XJ"},"properties":[{"identifier":"pressure1","operateType":"holdingRegister","originalDataType":{"specs":{"registerCount":1,"reverseRegister":0,"swap16":0},"type":"int16"},"pollingTime":1000,"registerAddress":"0x01","scaling":1,"trigger":1,"writeFunctionCode":16}]}" #擴展物模型
},
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777",
"version":"1.0",
"timestamp":157224857****,
"code":0,
"message":"success"
}
4.3 IFTTT規則配置
4.3.1 獲取IFTTT規則列表
獲取分組IFTTT規則列表Topic:
{srcApp}/request/linkedge/getSceneList
獲取網關IFTTT規則列表Topic:
{srcApp}/request/linkedge/{pk}/{dn}/getSceneList
請求Payload:
{
"requestId": "0bc458b5-47f6-472f-bd88-3624696e9778",
"timestamp": 157121985****, #ms [Optional]
"timeout":3, # 最少1s,最多120s [Optional: default 4s]
"version": "1.0" # [Optional]
}
獲取分組IFTTT規則應答Topic:
linkedge/response/{srcApp}/getSceneList
應答Payload:
{
"params":[ #規則列表
{
"gwProductKey":"a1PQ9828qvr", #網關productkey
"gwDeviceName":"alpine_test_gateway",#網關的devicenmae
"code":0,
"message":"success",
"data":[ #包含網關中所有規則的描述
]
},
{
"gwProductKey":"a1PQ9828qvr",
"gwDeviceName":"yinlong_gw_dev1",
"code":0,
"message":"success",
"data":[
{
"times":5,
"failureTimes":5,
"lastState":{
"state":"failure",
"cause":"walk action set: The name iot.device.ida1ZJTVsqj2y_alpine_light_dev1 was not provided by any .service files",
"time":"2019-10-28T19:35:00+08:00",
"duration":1
},
"id":"a08d6c08bc1a440693ba9c9c8166a968",
"name":"ifttt",
"running":true,
"time":"2019-10-28T19:30:47+08:00",
"uptime":259072,
"actions":{
"times":5,
"failureTimes":5,
"lastState":{
"state":"failure",
"cause":"The name iot.device.ida1ZJTVsqj2y_alpine_light_dev1 was not provided by any .service files",
"time":"2019-10-28T19:35:00+08:00",
"duration":1
}
},
"conditions":{
"times":5,
"failureTimes":0,
"lastState":{
"state":"success",
"cause":"",
"time":"2019-10-28T19:35:00+08:00",
"duration":0
}
}
}
]
}
],
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777",
"version":"1.0",
"timestamp":157226250****,
"code":0,
"message":"success"
}
獲取網關IFTTT規則應答Topic:
linkedge/response/{srcApp}/{pk}/{dn}/getSceneList
應答Payload:
{
"params":[ #網關下的規則列表
{
"times":0,
"failureTimes":0,
"lastState":null,
"id":"a08d6c08bc1a440693ba9c9c8166a968",
"name":"ifttt",
"running":true,
"time":"2019-10-28T17:35:35+08:00",
"uptime":5967,
"actions":{
"times":0,
"failureTimes":0,
"lastState":null
},
"conditions":{
"times":0,
"failureTimes":0,
"lastState":null
}
}
],
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777",
"version":"1.0",
"timestamp":157225534****,
"code":0,
"message":"success"
}
4.3.2 控制IFTTT規則啟停
同一個IFTTT規則,部署到不同的網關,ID是相同的。因此IFTTT規則啟停,僅支持面向網關的定向操作,不支持分組維度。
使能網關IFTTT規則Topic:
{srcApp}/request/linkedge/{pk}/{dn}/enableScene
關閉網關IFTTT規則Topic:
{srcApp}/request/linkedge/{pk}/{dn}/disableScene
請求Payload:其中params數組中至少包含一個,至多包含32個對象。
{
"requestId": "0bc458b5-47f6-472f-bd88-3624696e9778",
"version": "1.0",
"params": [ #場景規則id數組[Required]
{
"id": "id1"
}
]
}
使能網關IFTTT規則配置應答Topic:
linkedge/response/{srcApp}/{pk}/{dn}/enableScene
關閉網關IFTTT規則配置應答Topic:
linkedge/response/{srcApp}/{pk}/{dn}/disableScene
應答Payload:
{
"params":[
{
"id":"123000000",
"code":1,
"message":"no such scene"
},
{
"id":"a08d6c08bc1a440693ba9c9c8166a968",
"code":0,
"message":"success"
}
],
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777",
"version":"1.0",
"timestamp":157227690****,
"code":0,
"message":"success"
}
4.4 設備列表和狀態變更
4.4.1 設備列表變更
當網關互聯中某一網關所擁有的設備列表出現變化時,互聯網絡將廣播一條消息通知給訂閱者。
變更Topic:
linkedge/notify/deviceListChanged
內容
{
"timestamp":157225090****,
"version":"1.0",
"params":[
{
"gwProductKey":"a1XqBnyTZLD",
"gwDeviceName":"ModbusTestGW"
}
]
}
4.4.2 設備在線狀態變更
當網關互聯中某子設備的在線狀態出現變化時(包括連接到本地和連接到云端),網關互聯網絡將廣播一條消息給訂閱者。
通知Topic:
linkedge/notify/deviceStateChanged
內容
內容格式大體與4.2.1獲取設備列表相同。
{
"timestamp": 1572250903307,
"version": "1.0",
"params": [{
"gwProductKey": "a1XqBnyTZLD", #網關的productkey
"gwDeviceName": "ModbusTestGW", #網關的devicename
"deviceList": [{ #網關下的設備列表
"productKey": "a1OSqKKiZLL", #設備的productkey
"deviceName": "DN035", #設備的devicename
"localOnline": "true/false", #設備的本地在線狀態
"cloudOnline": "true/false", #設備的云端在線狀態
"activationState": "activated/notActivated/activationFailed",#設備在云端的激活狀態
}]
}]
}
4.5 網關信息查詢
4.5.1 獲取網關列表
獲取分組網關列表Topic:
{srcApp}/request/linkedge/getGatewayList
請求Payload
{
"requestId": "0bc458b5-47f6-472f-bd88-3624696e9777", # [Required]
"timestamp": 157121985****, #ms [Optional]
"timeout":3, # 最少1s,最多120s [Optional: default 4s]
"version": "1.0" # [Optional]
}
獲取分組網關列表應答Topic:
linkedge/response/{srcApp}/getGatewayList
響應Payload
{
"params":[
{
"gwProductKey":"a1phR16zqNZ",
"gwDeviceName":"gateway_01",
"code":0, #此網關接口的服務調用的返回碼
"message":"success",
"gatewayInfo":{ #網關信息
"productKey":"a1phR16zqNZ", #網關的productkey
"deviceName":"gateway_01", #網關的devicename
"nickName":"", #網關的別名
"tags":[ #網關的標簽
]
}
},
{
"gwProductKey":"a1phR16zqNZ",
"gwDeviceName":"gateway_02",
"code":101572,
"message":"getGateway timeout"
},
{
"gwProductKey":"a1phR16zqNZ",
"gwDeviceName":"gateway_03",
"code":101572,
"message":"ERROR_GWC_REQUEST_TIMEOUT"
}
],
"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777",
"version":"1.0",
"timestamp":158202075****,
"code":10****, #分組接口的調用返回碼
"message":"ERROR_GWC_REQUEST_TIMEOUT"
}
5.錯誤碼
應答Payload中的”code”字段代表錯誤碼,成功時返回0,異常時返回下述錯誤碼,錯誤信息保存到”message”字段:
0: 成功
101571: 參數校驗不合法
101572: 請求處理超時
101573: LinkIoTEdge處理失敗
101574:設備驅動處理失敗
101575: MQTT消息代理處理失敗
101576: 設備不存在
101578: 請求的參數個數超出上限
101579: 訪問資源不存在
101580: 請求的參數為空
6.自測用例
該部分測試用例中的測試數據,對于MQTT客戶端開發具有很好的借鑒價值,結合具體例子的請求和應答便于開發者加深對協議規范的理解。
6.1 部署配置樣例
本地部署方法:./casctl deploy gwc_deploy.conf
gwc_deploy.conf配置文件樣例:
{
"version":"1.0",
"instanceId":"iid222",
"instanceGroupId":"linkedge",
"instanceGroupPsk":"123456abcdef12345678123456ABCDEF",
"userTlsConfig":[
{
"userId":"customer1",
"psk":"123456"
},
{
"userId":"customer2",
"psk":"12345678"
}
],
"nodesConfig":[
{
"ipAddress":"172.17.0.2",
"productKey":"a1PQ9828qvr",
"deviceName":"yinlong_gw_dev1",
"isGroupAgent":"1" #Mark this gw as groupAgent
},
{
"ipAddress":"172.17.0.3",
"productKey":"a1PQ9828qvr",
"deviceName":"alpine_test_gateway"
}
]
}
6.2 自測用例
訂閱所有設備屬性、事件上報
/* 訂閱設備屬性上報Topic,接收設備屬性上報數據 */
#./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t thing/notify/+/+/property -v &
thing/notify/a1Mtx4p2fHG/LightSensor/property {"MeasuredIlluminance":{"value":550,"time":156662262****}}
thing/notify/a1Mtx4p2fHG/LightSensor/property {"MeasuredIlluminance":{"value":600,"time":156662265****}}
thing/notify/a1Mtx4p2fHG/LightSensor/property {"MeasuredIlluminance":{"value":650,"time":156662268****}}
訪問外部/本地網關設備方法
/* 1.先訂閱應答Topic,用戶接收設備應答數據 */
#./mosquitto_sub --psk-identity linkedge --psk 12345678 -p 8883 -t thing/response/testApp/+/+/+ -v &
/* 2.發送設備方法調用get請求 */
#./mosquitto_pub --psk-identity linkedge --psk 12345678 -p 8883 -t testApp/request/thing/a1Mtx4p2fHG/LightSensor/get -m "{\"params\":[\"MeasuredIlluminance\"],\"requestId\":\"ff8db62b-4d75-46c8-bf6a-1600d326a88b\"}"
/* 3.mosquitto_sub輸出設備應答信息 */
thing/response/testApp/a1Mtx4p2fHG/LightSensor/get {"code":0,"message":"success","params":{"MeasuredIlluminance":1000},"requestId":"ff8db62b-4d75-46c8-bf6a-1600d326a88b"}
消息路由
配置全部設備數據到FC,配置某個ProductKey下所有設備數據到FC。
獲取網關設備列表
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/getDeviceList -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/a1PQ9828qvr/yinlong_gw_dev1/getDeviceList -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\", \"version\":\"1.0\"}"
linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/getDeviceList {"data":[{"productKey":"a1ZJTVsqj2y","deviceName":"LightDev","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[{"key":"type","value":"light"},{"key":"owner","value":"yinlong"},{"key":"location","value":"livingroom"},{"key":"color","value":"white"}]},{"productKey":"a1arJcX0Koy","deviceName":"Sensor_device1","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[{"key":"location","value":"master bedroom"}]},{"productKey":"a1KRepmC2XJ","deviceName":"modbus_tcp_dev_1","localOnline":"false","cloudOnline":"false","activationState":"notActivated","tags":[]},{"productKey":"a1Mtx4p2fHG","deviceName":"LightSensor","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]},{"productKey":"a1ZJTVsqj2y","deviceName":"LightDev2","localOnline":"false","cloudOnline":"false","activationState":"notActivated","tags":[{"key":"type","value":"light"},{"key":"owner","value":"yinlong"},{"key":"location","value":"master bedroom"},{"key":"color","value":"red"}]},{"productKey":"a1PQ9828qvr","deviceName":"yinlong_gw_dev1","localOnline":"false","cloudOnline":"false","activationState":"notActivated","tags":[]},{"productKey":"a1ZJTVsqj2y","deviceName":"lightDevNoSpecial","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777"}
getTsl 測試用例
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t thing/response/testApp/+/+/getTsl -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/thing/a1arJcX0Koy/alpine_light_sensor/getTsl -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\",\"timeout\":5}"
thing/response/testApp/a1arJcX0Koy/alpine_light_sensor/getTsl {"params":{"productKey":"a1arJcX0Koy","deviceName":"alpine_light_sensor","code":0,"message":"success","tsl":"{\"events\":[{\"desc\":\"屬性上報\",\"identifier\":\"post\",\"method\":\"thing.event.property.post\",\"name\":\"post\",\"outputData\":[{\"dataType\":{\"specs\":{\"max\":\"65535\",\"min\":\"0\",\"step\":\"0.01\",\"unit\":\"Lux\",\"unitName\":\"照度\"},\"type\":\"double\"},\"identifier\":\"MeasuredIlluminance\",\"name\":\"光照度檢測值\"}],\"required\":true,\"type\":\"info\"}],\"profile\":{\"productKey\":\"a1arJcX0Koy\"},\"properties\":[{\"accessMode\":\"r\",\"dataType\":{\"specs\":{\"max\":\"65535\",\"min\":\"0\",\"step\":\"0.01\",\"unit\":\"Lux\",\"unitName\":\"照度\"},\"type\":\"double\"},\"identifier\":\"MeasuredIlluminance\",\"name\":\"光照度檢測值\",\"required\":true}],\"schema\":\"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json\",\"services\":[{\"callType\":\"async\",\"desc\":\"屬性設置\",\"identifier\":\"set\",\"inputData\":[],\"method\":\"thing.service.property.set\",\"name\":\"set\",\"outputData\":[],\"required\":true},{\"callType\":\"async\",\"desc\":\"屬性獲取\",\"identifier\":\"get\",\"inputData\":[\"MeasuredIlluminance\"],\"method\":\"thing.service.property.get\",\"name\":\"get\",\"outputData\":[{\"dataType\":{\"specs\":{\"max\":\"65535\",\"min\":\"0\",\"step\":\"0.01\",\"unit\":\"Lux\",\"unitName\":\"照度\"},\"type\":\"double\"},\"identifier\":\"MeasuredIlluminance\",\"name\":\"光照度檢測值\"}],\"required\":true}]}","productKey":"a1arJcX0Koy","deviceName":"alpine_light_sensor","code":0,"message":"success","tslConfig":"{\"profile\":{\"productKey\":\"a1arJcX0Koy\"}}"},"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":1572249171823,"code":0,"message":"success"}
獲取分組設備列表
前置條件:修改gwc_deploy.conf,選擇一臺網關為groupAgent:設置代理網關標志 “isGroupAgent”:”1”
./casctl dep gwc_deploy.conf
測試命令
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/getDeviceList -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/getDeviceList -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\"}"
linkedge/response/testApp/getDeviceList {"params":[{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"yinlong_gw_dev1","code":0,"message":"success","deviceList":[{"productKey":"a1ZJTVsqj2y","deviceName":"LightDev","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[{"key":"type","value":"light"},{"key":"owner","value":"yinlong"},{"key":"location","value":"livingroom"},{"key":"color","value":"white"}]},{"productKey":"a1arJcX0Koy","deviceName":"Sensor_device1","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[{"key":"location","value":"master bedroom"}]},{"productKey":"a1ZJTVsqj2y","deviceName":"LightDev2","localOnline":"false","cloudOnline":"false","activationState":"notActivated","tags":[{"key":"type","value":"light"},{"key":"owner","value":"yinlong"},{"key":"location","value":"master bedroom"},{"key":"color","value":"red"}]},{"productKey":"a1Mtx4p2fHG","deviceName":"LightSensor","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]},{"productKey":"a1KRepmC2XJ","deviceName":"modbus_tcp_dev_1","localOnline":"false","cloudOnline":"false","activationState":"notActivated","tags":[]},{"productKey":"a1ZJTVsqj2y","deviceName":"lightDevNoSpecial","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]}]},{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"alpine_test_gateway","code":0,"message":"success","deviceList":[{"productKey":"a1fsUodstaA","deviceName":"oil_temperature_dev1","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]},{"productKey":"a1arJcX0Koy","deviceName":"alpine_light_sensor","localOnline":"true","cloudOnline":"true","activationState":"activated","tags":[]}]}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157218682****,"code":0,"message":"success"}
超時情況:linkedge/response/testApp/getDeviceList {"params":[{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"yinlong_gw_dev1","code":101572,"message":"ERROR_GWC_REQUEST_TIMEOUT"},{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"alpine_test_gateway","code":101572,"message":"ERROR_GWC_REQUEST_TIMEOUT"}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157218540****,"code":101572,"message":"ERROR_GWC_REQUEST_TIMEOUT"}
getSceneList
獲取單個網關的規則列表
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/getSceneList -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/a1PQ9828qvr/yinlong_gw_dev1/getSceneList -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\"}"
linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/getSceneList {"params":{"code":0,"message":"success","data":[{"times":0,"failureTimes":0,"lastState":null,"id":"a08d6c08bc1a440693ba9c9c8166a968","name":"ifttt","running":false,"time":"","uptime":0,"actions":{"times":0,"failureTimes":0,"lastState":null},"conditions":{"times":0,"failureTimes":0,"lastState":null}}]},"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157222996****,"code":0,"message":"success"}
dbus-send --bus=unix:path=/tmp/var/run/mbusd/mbusd_socket --dest=iot.gateway.scene --print-reply /iot/gateway/scene iot.gateway.scene.listSceneStats
獲取分組的規則列表
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/getSceneList -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/getSceneList -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\"}"
linkedge/response/testApp/getSceneList {"params":[{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"alpine_test_gateway","code":0,"message":"success","data":[]},{"gwProductKey":"a1PQ9828qvr","gwDeviceName":"yinlong_gw_dev1","code":0,"message":"success","data":[{"times":5,"failureTimes":5,"lastState":{"state":"failure","cause":"walk action set: The name iot.device.ida1ZJTVsqj2y_alpine_light_dev1 was not provided by any .service files","time":"2019-10-28T19:35:00+08:00","duration":1},"id":"a08d6c08bc1a440693ba9c9c8166a968","name":"ifttt","running":true,"time":"2019-10-28T19:30:47+08:00","uptime":273231,"actions":{"times":5,"failureTimes":5,"lastState":{"state":"failure","cause":"The name iot.device.ida1ZJTVsqj2y_alpine_light_dev1 was not provided by any .service files","time":"2019-10-28T19:35:00+08:00","duration":1}},"conditions":{"times":5,"failureTimes":0,"lastState":{"state":"success","cause":"","time":"2019-10-28T19:35:00+08:00","duration":0}}}]}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157226252****,"code":0,"message":"success"}
enableScene
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/+/+/enableScene -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/a1PQ9828qvr/yinlong_gw_dev1/enableScene -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\",\"params\":[{\"id\":\"a08d6c08bc1a440693ba9c9c8166a968\"},{\"id\":\"123000000\"}]}"
linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/enableScene {"params":[{"id":"123000000","code":1,"message":"no such scene"},{"id":"a08d6c08bc1a440693ba9c9c8166a968","code":0,"message":"success"}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157227690****,"code":0,"message":"success"}
disableScene
./mosquitto_sub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t linkedge/response/testApp/+/+/disableScene -v &
./mosquitto_pub --psk-identity linkedge --psk 123456aBcdef12345678123456ABCDEf -p 8883 -t testApp/request/linkedge/a1PQ9828qvr/yinlong_gw_dev1/disableScene -m "{\"requestId\": \"0bc458b5-47f6-472f-bd88-3624696e9777\",\"params\":[{\"id\":\"a08d6c08bc1a440693ba9c9c8166a968\"}]}"
linkedge/response/testApp/a1PQ9828qvr/yinlong_gw_dev1/disableScene {"params":[{"id":"a08d6c08bc1a440693ba9c9c8166a968","code":0,"message":"success"}],"requestId":"0bc458b5-47f6-472f-bd88-3624696e9777","version":"1.0","timestamp":157227715****,"code":0,"message":"success"}
7. 設備端開發
7.1 C版本SDK
本章為您介紹C版本的SDK使用方法及相關API。Link IoT Edge提供C版本的SDK,名稱為linkedge-thing-access-sdk-c。C版本開源的SDK源碼請參見開源C庫。
get_properties_callback
/*
* 獲取屬性(對應設備產品物模型屬性定義)的回調函數,需驅動開發者實現獲取屬性業務邏輯。
*
* Link IoT Edge需要獲取某個設備的屬性時,SDK會調用該接口間接獲取到數據并封裝成固定格式后回傳給Link IoT Edge。
* 開發者需要根據設備id和屬性名找到設備,將獲取到的屬性值按照@device_data_t格式填充。
*
* @dev_handle: Link IoT Edge需要獲取屬性的具體某個設備。
* @properties: 屬性值鍵值結構,驅動開發者需要將根據屬性名稱獲取到的屬性值更新到properties中。
* @properties_count: 屬性個數。
* @usr_data: 注冊設備時,用戶傳遞的私有數據。
* 所有屬性均獲取成功則返回LE_SUCCESS,其他則返回錯誤碼(參考le_error.h錯誤碼宏定義)。
* */
typedef int (*get_properties_callback)(device_handle_t dev_handle,
leda_device_data_t properties[],
int properties_count,
void *usr_data);
set_properties_callback
/*
* 設置屬性(對應設備產品物模型屬性定義)的回調函數,需驅動開發者實現設置屬性業務邏輯。
*
* Link IoT Edge需要設置某個設備的屬性時,SDK會調用該接口將具體的屬性值傳遞給應用程序,開發者需要在本回調
* 函數里將屬性設置到設備。
*
* @dev_handle: Link IoT Edge需要設置屬性的具體某個設備。
* @properties: Link IoT Edge需要設置的設備的屬性名稱和值。
* @properties_count: 屬性個數。
* @usr_data: 注冊設備時,用戶傳遞的私有數據。
*
* 若獲取成功則返回LE_SUCCESS,失敗則返回錯誤碼(參考le_error.h錯誤碼宏定義)。
* */
typedef int (*set_properties_callback)(device_handle_t dev_handle,
const leda_device_data_t properties[],
int properties_count,
void *usr_data);
call_service_callback
/*
* 服務(對應設備產品物模型服務定義)調用的回調函數,需要驅動開發者實現服務對應業務邏輯。
*
* Link IoT Edge需要調用某個設備的服務時,SDK會調用該接口將具體的服務參數傳遞給應用程序,開發者需要在本回調
* 函數里調用具體的服務,并將服務返回值按照@device_data_t格式填充到output_data。
*
* @dev_handle: Link IoT Edge需要調用服務的具體某個設備。
* @service_name: Link IoT Edge需要調用的設備的具體某個服務名,名稱與設備產品物模型一致。
* @data: Link IoT Edge需要調用的設備的具體某個服務參數,參數與設備產品物模型保持一致。
* @data_count: Link IoT Edge需要調用的設備的具體某個服務參數個數。
* @output_data: 開發者需要將服務調用的返回值,按照設備產品物模型規定的服務格式返回到output中。
* @usr_data: 注冊設備時,用戶傳遞的私有數據。
*
* 若獲取成功則返回LE_SUCCESS,失敗則返回錯誤碼(參考le_error.h錯誤碼宏定義)。
* */
typedef int (*call_service_callback)(device_handle_t dev_handle,
const char *service_name,
const leda_device_data_t data[],
int data_count,
leda_device_data_t output_data[],
void *usr_data);
leda_report_properties
/*
* 上報屬性,在設備所屬產品物模型中規定了設備的屬性上報能力。
*
* 上報屬性,可以上報一個,也可以多個一起上報。
*
* dev_handle: 設備在Link IoT Edge本地唯一標識。
* properties: @leda_device_data_t,屬性數組。
* properties_count: 本次上報屬性個數。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
*/
int leda_report_properties(device_handle_t dev_handle, const leda_device_data_t properties[], int properties_count);
leda_report_event
/*
* 上報事件,設備具有的事件上報能力在設備產品物模型有規定。
*
*
* dev_handle: 設備在Link IoT Edge本地唯一標識。
* event_name: 事件名稱。
* data: @leda_device_data_t,事件參數數組。
* data_count: 事件參數數組長度。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
*/
int leda_report_event(device_handle_t dev_handle, const char *event_name, const leda_device_data_t data[], int data_count);
leda_offline
/*
* 下線設備,假如設備工作在不正常的狀態或設備退出前,可以先下線設備,這樣Link IoT Edge就不會繼續下發消息到設備側。
*
* dev_handle: 設備在Link IoT Edge本地唯一標識。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
*/
int leda_offline(device_handle_t dev_handle);
leda_online
/*
* 上線設備,設備只有上線后,才能被Link IoT Edge識別。
*
* dev_handle: 設備在Link IoT Edge本地唯一標識。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
int leda_online(device_handle_t dev_handle);
leda_register_and_online_by_device_name
/*
* 通過已在阿里云物聯網平臺創建的設備device_name,注冊并上線設備,申請設備唯一標識符。
*
* 若需要注冊多個設備,則多次調用該接口即可。
*
* product_key: 在阿里云物聯網平臺創建的產品ProductKey。
* device_name: 在阿里云物聯網平臺創建的設備名稱DeviceName。
* device_cb: 請求調用設備回調函數結構體,詳細描述見@leda_device_callback。
* usr_data: 設備注冊時傳入私有數據,在回調中會傳給設備。
*
* 阻塞接口,返回值設備在Link IoT Edge本地唯一標識,>= 0表示有效,< 0 表示無效。
*
*/
device_handle_t leda_register_and_online_by_device_name(const char *product_key, const char *device_name, leda_device_callback_t *device_cb, void *usr_data);
leda_register_and_online_by_local_name
/*
* 通過本地自定義設備名稱,注冊并上線設備,申請設備唯一標識符。
*
* 若需要注冊多個設備,則多次調用該接口即可。
*
* product_key: 在阿里云物聯網平臺創建的產品ProductKey。
* local_name: 由設備特征值組成的唯一描述信息,必須保證同一個product_key時,每個設備名稱不同。
* device_cb: 請求調用設備回調函數結構體,詳細描述見@leda_device_callback。
* usr_data: 設備注冊時傳入私有數據,在回調中會傳給設備。
*
* 阻塞接口,返回值設備在Link IoT Edge本地唯一標識,>= 0表示有效,< 0 表示無效。
*
* 注:在同一產品ProductKey條件設備注冊,不允許本接口和leda_register_and_online_by_device_name接口同時使用。
* 即每一個產品ProductKey設備注冊必須使用同一接口,否則設備注冊會發生不可控行為。
*/
device_handle_t leda_register_and_online_by_local_name(const char *product_key, const char *local_name, leda_device_callback_t *device_cb, void *usr_data);
leda_init
/*
* 驅動模塊初始化,模塊內部會創建工作線程池,異步執行阿里云物聯網平臺下發的設備操作請求,工作線程數目通過worker_thread_nums配置。
*
* worker_thread_nums: 線程池工作線程數,該數值根據注冊設備數量進行設置。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
int leda_init(int worker_thread_nums);
leda_exit
/*
* 驅動模塊退出。
*
* 模塊退出前,釋放資源。
*
* 阻塞接口。
*/
void leda_exit(void);
leda_get_driver_info_size
/*
* 獲取驅動信息長度。
*
* 阻塞接口,成功返回驅動信息長度,失敗返回0。
*/
int leda_get_driver_info_size(void);
leda_get_driver_info
/*
* 獲取驅動信息(在物聯網平臺配置的驅動配置)。
*
* driver_info: 驅動信息,需要提前申請好內存傳入。
* size: 驅動信息長度,leda_get_driver_info_size,如果傳入driver_info比實際配置內容長度短,會返回LE_ERROR_INVAILD_PARAM。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
* 配置格式:
{
"json":{
"ip":"127.0.0.1",
"port":54321
},
"kv":[
{
"key":"ip",
"value":"127.0.0.1",
"note":"ip地址"
},
{
"key":"port",
"value":"54321",
"note":"port端口"
}
],
"fileList":[
{
"path":"device_config.json"
}
]
}
*/
int leda_get_driver_info(char *driver_info, int size);
leda_get_device_info_size
/*
* 獲取設備信息長度。
*
* 阻塞接口,成功返回設備信息長度,失敗返回0。
*/
int leda_get_device_info_size(void);
leda_get_device_info
/*
* 獲取設備信息(在物聯網平臺配置的設備配置)。
*
* device_info: 設備信息,需要提前申請好內存傳入。
* size: 設備信息長度,該長度通過leda_get_device_info_size接口獲取,如果傳入device_info比實際配置內容長度短,會返回LE_ERROR_INVAILD_PARAM。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
* 配置格式:
[
{
"custom":{
"port":12345,
"ip":"127.0.0.1"
},
"deviceName":"device1",
"productKey":"a1ccxxeypky"
}
]
*/
int leda_get_device_info(char *device_info, int size);
leda_get_config_size
/*
* 獲取驅動配置長度。
*
* 阻塞接口,成功返回驅動配置長度,失敗返回0。
*/
int leda_get_config_size(void);
leda_get_config
/*
* 獲取驅動所有配置。
*
* config: 驅動配置,需要提前申請好內存傳入。
* size: 驅動配置長度,該長度通過leda_get_config_size接口獲取,如果傳入config比實際配置內容長度短,會返回LE_ERROR_INVAILD_PARAM。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*
* 配置格式:
{
"config":{
"json":{
"ip":"127.0.0.1",
"port":54321
},
"kv":[
{
"key":"ip",
"value":"127.0.0.1",
"note":"ip地址"
},
{
"key":"port",
"value":"54321",
"note":"port端口"
}
],
"fileList":[
{
"path":"device_config.json"
}
]
},
"deviceList":[
{
"custom":"{"port":12345,"ip":"127.0.0.1"}",
"deviceName":"device1",
"productKey":"a1ccxxeypky"
}
]
}
*/
int leda_get_config(char *config, int size);
config_changed_callback
/*
* 驅動配置變更回調接口。
*
* config: 配置信息。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
typedef int (*config_changed_callback)(const char *config);
leda_register_config_changed_callback
/*
* 訂閱驅動配置變更監聽回調。
*
* config_cb: 配置變更通知回調接口。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
int leda_register_config_changed_callback(config_changed_callback config_cb);
leda_get_tsl_size
/*
* 獲取指定產品ProductKey對應物模型內容長度。
*
* product_key: 產品ProductKey。
*
* 阻塞接口,成功返回product_key對應物模型內容長度,失敗返回0。
*/
int leda_get_tsl_size(const char *product_key);
leda_get_tsl
/*
* 獲取指定產品ProductKey對應物模型內容。
*
* product_key: 產品ProductKey。
* tsl: 物模型內容,需要提前申請好內存傳入。
* size: 物模型內容長度,該長度通過leda_get_tsl_size接口獲取,如果傳入tsl比實際物模型內容長度短,會返回LE_ERROR_INVAILD_PARAM。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
int leda_get_tsl(const char *product_key, char *tsl, int size);
leda_get_tsl_ext_info_size
/*
* 獲取指定產品ProductKey對應物模型擴展信息內容長度。
*
* product_key: 產品ProductKey。
*
* 阻塞接口,成功返回product_key對應物模型擴展信息內容長度,失敗返回0。
*/
int leda_get_tsl_ext_info_size(const char *product_key);
leda_get_tsl_ext_info
/*
* 獲取指定產品ProductKey對應物模型擴展信息內容。
*
* product_key: 產品ProductKey。
* tsl_ext_info: 物模型擴展信息,需要提前申請好內存傳入。
* size: 物模型擴展信息長度,該長度通過leda_get_tsl_ext_info_size接口獲取,如果傳入tsl_ext_info比實際物模型擴展信息內容長度短,會返回LE_ERROR_INVAILD_PARAM。
*
* 阻塞接口,成功返回LE_SUCCESS,失敗返回錯誤碼。
*/
int leda_get_tsl_ext_info(const char *product_key, char *tsl_ext_info, int size);
leda_get_device_handle
/*
* 獲取設備句柄。
*
* product_key: 產品ProductKey。
* device_name: 設備名稱DeviceName。
*
* 阻塞接口,成功返回device_handle_t,失敗返回小于0數值。
*/
device_handle_t leda_get_device_handle(const char *product_key, const char *device_name);
7.2 Java版本SDK
設備接入SDK(Java版本)支持開發者使用Java語言開發設備接入驅動(以下簡稱驅動)。客戶網關環境在安裝有Link IoT Edge軟件的條件下只要滿足Java運行環境即可運行Java驅動。
Java版本開源的SDK源碼請參見開源Java庫。
LedaConfig
類名全稱:
com.aliyun.linkedge.sdk.LedaConfig
類聲明:
public class LedaConfig
Java方法說明:| 限定符和類型 | 方法和說明 || :—- | :—- || static String | getDriverConfig()獲取驅動配置。 || static String | getDeviceConfig()獲取設備配置。 || static String | getTsl(String productKey)獲取productKey(產品唯一標識符)對應的物模型(TSL)。 || static String | getTslConfig(String productKey)獲取productKey對應的物模型擴展配置。 |
LedaDevice
類名全稱:
com.aliyun.linkedge.sdk.LedaDevice
類聲明:
public class LedaDevice
構造函數:
LedaDevice(String productKey, String deviceName)
Java方法說明:| 限定符和類型 | 方法和說明 || :—- | :—- || LedaData | getProperties(List propertyNameList)根據指定的屬性名稱,獲取屬性值。使用該方法時,需要實現重載(Overload)。 || int | setProperties(HashMap properties)設置屬性名稱和屬性值。使用該方法時,需要實現重載。 || LedaData | callService(String methodName, HashMap params)執行自定義方法。使用該方法時,需要實現重載。 || int | online()上線設備。 || int | offline()下線設備。 || int | reportProperties(HashMap properties)上報設備屬性。 || int | reportEvents(String eventName, HashMap outputData)上報設備事件。 |
數據類
類名全稱:
com.aliyun.linkedge.sdk.LedaDevice
類聲明:
public class LedaData
構造函數:
LedaData(int code, HashMap<String, Object> data)
Java方法說明:| 限定符和類型 | 方法和說明 || :—- | :—- || void | setCode(int code)設置狀態碼。 || int | getCode()獲取狀態碼。 || void | setData(HashMap data)設置數據內容。 || HashMap | getData()獲取數據內容。 |
錯誤碼
類名全稱:
com.aliyun.linkedge.sdk.exception.LedaErrorCode
類聲明:
public class LedaErrorCode { public static final int LE_SUCCESS = 0; public static final int LE_ERROR_UNKNOWN = 100000; public static final int LE_ERROR_INVALID_PARAM = 100001; public static final int LE_ERROR_TIMEOUT = 100006; public static final int LE_ERROR_PARAM_RANGE_OVERFLOW = 100007; public static final int LE_ERROR_SERVICE_UNREACHABLE = 100008; public static final int LEDA_ERROR_DEVICE_UNREGISTER = 109000; public static final int LEDA_ERROR_DEVICE_OFFLINE = 109001; public static final int LEDA_ERROR_PROPERTY_NOT_EXIST = 109002; public static final int LEDA_ERROR_PROPERTY_READ_ONLY = 109003; public static final int LEDA_ERROR_PROPERTY_WRITE_ONLY = 109004; public static final int LEDA_ERROR_SERVICE_NOT_EXIST = 109005; public static final int LEDA_ERROR_SERVICE_INPUT_PARAM = 109006; public static final int LEDA_ERROR_INVALID_JSON = 109007; public static final int LEDA_ERROR_INVALID_TYPE = 109008; private static final String LE_SUCCESS_MSG = "Success"; /* 請求成功*/ private static final String LE_ERROR_INVALID_PARAM_MSG = "Invalid params"; /* 傳入參數為NULL或無效*/ private static final String LE_ERROR_TIMEOUT_MSG = "Tiemout"; /* 超時*/ private static final String LE_ERROR_PARAM_RANGE_OVERFLOW_MSG = "Param range overflow"; /* 參數范圍越界*/ private static final String LE_ERROR_SERVICE_UNREACHABLE_MSG = "Service unreachable"; /* 服務不可達*/ private static final String LEDA_ERROR_DEVICE_UNREGISTER_MSG = "Device has't register"; /* 設備未注冊*/ private static final String LEDA_ERROR_DEVICE_OFFLINE_MSG = "Device has offline"; /* 設備已下線*/ private static final String LEDA_ERROR_PROPERTY_NOT_EXIST_MSG = "Property no exist"; /* 屬性不存在*/ private static final String LEDA_ERROR_PROPERTY_READ_ONLY_MSG = "Property only support read"; /* 屬性只讀*/ private static final String LEDA_ERROR_PROPERTY_WRITE_ONLY_MSG = "Property only support write"; /* 屬性只寫*/ private static final String LEDA_ERROR_SERVICE_NOT_EXIST_MSG = "Service no exist"; /* 服務不存在*/ private static final String LEDA_ERROR_SERVICE_INPUT_PARAM_MSG = "Service param invalid"; /* 服務的輸入參數不正確錯誤碼*/ private static final String LEDA_ERROR_INVALID_JSON_MSG = "Json format invalid"; /* JSON格式錯誤*/ private static final String LEDA_ERROR_INVALID_TYPE_MSG = "Param type invalid"; /* 參數類型錯誤*/ }
Java方法說明:| 限定符和類型 | 方法和說明 || :—- | :—- || String | getMessage(int code)獲取錯誤碼對應的消息。 |