作者:俏巴
概述
目前阿里云的绝大部分服务都提供了管理资源的Rest API,针对相应的API也做了SDK的封装,且版本也在不断的迭代更新。管理API SDK的统一GitHub地址链接。关于IoT SDK的使用,之前官方也给出了示例参考,但是该示例使用的SDK版本为5.0.0,且这个版本的SDK中没有DeleteProductRequest类的实现,下面演示使用目前最新版本:6.5.0关于产品的创建及删除操作,新建Topic间的消息路由关系、查询向指定Topic订阅消息的目标Topic操作,其它操作类似。
pom依赖
<!--核心依赖-->
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.aliyun<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>aliyun-java-sdk-core<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>3.5.0<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span></span>
<!--管理API SDK调用问题-->
<span class="xml"><span class="hljs-tag"><<span class="hljs-name">dependency</span>></span>
<span class="hljs-tag"><<span class="hljs-name">groupId</span>></span>com.aliyun<span class="hljs-tag"></<span class="hljs-name">groupId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">artifactId</span>></span>aliyun-java-sdk-iot<span class="hljs-tag"></<span class="hljs-name">artifactId</span>></span>
<span class="hljs-tag"><<span class="hljs-name">version</span>></span>6.5.0<span class="hljs-tag"></<span class="hljs-name">version</span>></span>
<span class="hljs-tag"></<span class="hljs-name">dependency</span>></span></span></code></pre>
Java示例代码
import com.aliyuncs.AcsResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.RpcAcsRequest;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.CreateProductRequest;
import com.aliyuncs.iot.model.v20180120.CreateProductResponse;
import com.aliyuncs.iot.model.v20180120.DeleteProductRequest;
import com.aliyuncs.iot.model.v20180120.DeleteProductResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class IoTDemo {
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> main(<span class="hljs-built_in">String</span>[] args) {
<span class="hljs-built_in">String</span> productName = <span class="hljs-string">"iottestdemo123456"</span>;
<span class="hljs-built_in">String</span> productKey = <span class="hljs-string">"******"</span>;
Integer nodeType = <span class="hljs-number">0</span>;
// createProductTest(productName,nodeType);
deleteProductTest(productKey);
System.out.println(<span class="hljs-string">"删除成功"</span>);
}
<span class="hljs-comment">/**
* 创建产品
* @param productName 产品名称
* @param nodeType 产品描述
* @return 产品的PK
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">String</span> createProductTest(<span class="hljs-built_in">String</span> productName, Integer nodeType) {
CreateProductRequest request = <span class="hljs-keyword">new</span> CreateProductRequest();
request.setProductName(productName);
request.setNodeType(nodeType);<span class="hljs-comment">//节点的类型,0标识设备,1标识网关</span>
CreateProductResponse response = (CreateProductResponse)executeTest1(request);
<span class="hljs-keyword">if</span> (response != <span class="hljs-literal">null</span> && response.getSuccess() != <span class="hljs-literal">false</span>) {
System.out.println(<span class="hljs-string">"创建产品成功!productKey:"</span> + response.getProductKey());
<span class="hljs-keyword">return</span> response.getProductKey();
} <span class="hljs-keyword">else</span> {
System.out.println(<span class="hljs-string">"创建产品失败!requestId:"</span> + response.getRequestId() + <span class="hljs-string">"原因:"</span> + response.getErrorMessage());
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}
<span class="hljs-comment">/**
* 删除产品
* @param productKey 产品key
* @return 产品的PK,可以在管理门户获取
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-built_in">Boolean</span> deleteProductTest(<span class="hljs-built_in">String</span> productKey) {
DeleteProductRequest request = <span class="hljs-keyword">new</span> DeleteProductRequest();
request.setProductKey(productKey);
DeleteProductResponse response = (DeleteProductResponse)executeTest1(request);
<span class="hljs-keyword">if</span> (response != <span class="hljs-literal">null</span> && response.getSuccess() != <span class="hljs-literal">false</span>) {
System.out.println(<span class="hljs-string">"删除产品成功!productKey:"</span> + response.getSuccess());
<span class="hljs-keyword">return</span> response.getSuccess();
} <span class="hljs-keyword">else</span> {
System.out.println(<span class="hljs-string">"创建产品失败!requestId:"</span> + response.getRequestId() + <span class="hljs-string">"原因:"</span> + response.getErrorMessage());
}
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
}
public <span class="hljs-keyword">static</span> AcsResponse executeTest1(RpcAcsRequest request) {
DefaultAcsClient client = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">String</span> regionId = <span class="hljs-string">"cn-shanghai"</span>;
<span class="hljs-built_in">String</span> accessKeyID = <span class="hljs-string">"********"</span>;
<span class="hljs-built_in">String</span> accessKeySecret = <span class="hljs-string">"********"</span>;
<span class="hljs-built_in">String</span> productCode = <span class="hljs-string">"Iot"</span>;
<span class="hljs-built_in">String</span> domain = <span class="hljs-string">"iot.cn-shanghai.aliyuncs.com"</span>;
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
<span class="hljs-keyword">try</span> {
DefaultProfile.addEndpoint(regionId, regionId, productCode, domain);
} <span class="hljs-keyword">catch</span> (ClientException e) {
e.printStackTrace();
}
<span class="hljs-comment">// 初始化client</span>
client = <span class="hljs-keyword">new</span> DefaultAcsClient(profile);
System.out.println(<span class="hljs-string">"client 初始化完成!"</span>);
AcsResponse response = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">try</span> {
response = client.getAcsResponse(request);
} <span class="hljs-keyword">catch</span> (Exception e) {
System.out.println(<span class="hljs-string">"执行失败:e:"</span> + e.getMessage());
}
<span class="hljs-keyword">return</span> response;
}
<span class="hljs-comment">/**
* 新建Topic间的消息路由关系
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> CreateTopicRouteTable() {
CreateTopicRouteTableRequest request = <span class="hljs-keyword">new</span> CreateTopicRouteTableRequest();
request.setSrcTopic(<span class="hljs-string">"/a1D8NwXCe56/device1/user/TopicToTopicDemo"</span>); <span class="hljs-comment">// 源Topic,即被订阅的Topic。</span>
List<<span class="hljs-built_in">String</span>> dstTopics = <span class="hljs-keyword">new</span> ArrayList<<span class="hljs-built_in">String</span>>();
dstTopics.add(<span class="hljs-string">"/a1D8NwXCe56/ceshi/user/TopicToTopicDemo"</span>); <span class="hljs-comment">// 目标Topic列表,即从SrcTopic订阅消息的Topic列表。</span>
request.setDstTopics(dstTopics);
CreateTopicRouteTableResponse response = (CreateTopicRouteTableResponse)executeTest1(request);
<span class="hljs-keyword">if</span> (response != <span class="hljs-literal">null</span> && response.getSuccess() != <span class="hljs-literal">false</span>) {
System.out.println(<span class="hljs-string">"CreateTopicRouteTable 成功:"</span> + response.getSuccess());
} <span class="hljs-keyword">else</span> {
System.out.println(<span class="hljs-string">"CreateTopicRouteTable, requestId:"</span> + response.getRequestId() + <span class="hljs-string">"原因:"</span> + response.getErrorMessage());
}
}
<span class="hljs-comment">/**
* 查询向指定Topic订阅消息的目标Topic
*/</span>
public <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> QueryTopicRouteTable() {
QueryTopicRouteTableRequest request = <span class="hljs-keyword">new</span> QueryTopicRouteTableRequest();
request.setActionName(<span class="hljs-string">"QueryTopicRouteTable"</span>);
request.setTopic(<span class="hljs-string">"/a1D8NwXCe56/device1/user/TopicToTopicDemo"</span>); <span class="hljs-comment">// 要查询的源Topic,即发布消息的Topic。</span>
QueryTopicRouteTableResponse response = (QueryTopicRouteTableResponse)executeTest1(request);
<span class="hljs-keyword">if</span> (response != <span class="hljs-literal">null</span> && response.getSuccess() != <span class="hljs-literal">false</span>) {
System.out.println(<span class="hljs-string">"QueryTopicRouteTable 成功:"</span> + response.getSuccess());
System.out.println(response.getDstTopics());
} <span class="hljs-keyword">else</span> {
System.out.println(<span class="hljs-string">"CreateTopicRouteTable, requestId:"</span> + response.getRequestId() + <span class="hljs-string">"原因:"</span> + response.getErrorMessage());
}
}
}
注意
当前阶段国内只有上海地区支持IoT服务,用户只需要根据需求替换AK及IoT的信息即可。删除产品的前提是产品中没有设备,管理门户目前删除产品需要短信验证码,直接使用SDK调用不需要验证码。
参考链接