java實現 AWS IOT 創建事物、創建證書、關聯Thing和Certificate。
友情提示:你要官方文檔不?只要你開金口,我立馬給你送過來。瘋狂點擊aws iot官方文檔
哇哈哈哈哈哈哈哈,不bb了進入正題。
一、 創建事物
CreateThingRequest thingRequest = new CreateThingRequest();
thingRequest.setThingName("XXXXXX");
thingRequest.setThingTypeName("CPE");
CreateThingResult thingResult = iotClient.createThing(thingRequest);
if (null == thingResult) {
System.out.println("createThing 進行iot創建事物未收到返回結果");
throw new RuntimeException("createThing failed");
}
二、 創建證書
CreateKeysAndCertificateRequest createKeysAndCertificateRequest = new CreateKeysAndCertificateRequest();
createKeysAndCertificateRequest.setSetAsActive(false);
CreateKeysAndCertificateResult certificateResult = iotClient.createKeysAndCertificate(createKeysAndCertificateRequest);
if (null == certificateResult) {
System.out.println("createCertificate 進行iot創建證書未收到返回結果");
throw new RuntimeException("createCertificate failed");
}
三、 關聯事物和證書
thingName是創thing時指定的,certificateArn是創建證書返回的,標識唯一。
注意:證書和事物均已存在方可關聯。
/**
* certificateAttachThing
*
* @param certificateArn
* @param thingName
* @return
*/
public boolean certificateAttachThing(String certificateArn, String thingName) {
try {
//獲取iot服務客戶端
if (null == iotClient) {
log.error("certificateAttachThing 獲取iot服務出現異常");
return false;
}
// 附加事物
// thingName是創thing時指定的。
// certificateArn是創建證書返回的,標識唯一。
AttachThingPrincipalRequest thingPrincipalRequest = new AttachThingPrincipalRequest();
thingPrincipalRequest.setThingName(thingName);
thingPrincipalRequest.setPrincipal(certificateArn);
AttachThingPrincipalResult thingPrincipalResult = iotClient.attachThingPrincipal(thingPrincipalRequest);
if (null == thingPrincipalResult) {
log.error("certificateAttachThing 進行證書附加事物未收到返回結果");
return false;
}
//附加成功
return true;
} catch (Exception e) {
log.error("certificateAttachThing", e);
}
return false;
}
如何查看是否關聯成功
1、在這裏可以看到你所有Thing
2、點擊一個Thing進入,會看到以下頁面。
3、關聯成功
該Thing關聯證書的詳情
Thing和Certificate未關聯的是以下圖片