|
|
@@ -0,0 +1,323 @@
|
|
|
+package com.coffee.aliyun;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.iot.model.v20180120.*;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.aliyuncs.profile.IClientProfile;
|
|
|
+import lombok.Data;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+//import com.tuoren.common.utils.StringUtils;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author XX
|
|
|
+ * @Date 2021-06-11 08:10:58
|
|
|
+ * @Version 1.0
|
|
|
+ * @Description XXX
|
|
|
+ */
|
|
|
+@Data
|
|
|
+public class AliyunIotSdk {
|
|
|
+ private static final String ACCESSKEY = "accessKey";
|
|
|
+ private static final String ACCESSSECRET = "accessSecret";
|
|
|
+ private static final String REFIONID = "regionId";
|
|
|
+
|
|
|
+ private String accessKey;
|
|
|
+ private String accessSecret;
|
|
|
+ private String regionId;
|
|
|
+ // client
|
|
|
+ private DefaultAcsClient client;
|
|
|
+ public AliyunIotSdk(String accessKey, String accessSecret, String regionId) {
|
|
|
+ this.accessKey = accessKey;
|
|
|
+ this.accessSecret = accessSecret;
|
|
|
+ this.regionId = regionId;
|
|
|
+ }
|
|
|
+ public AliyunIotSdk(AliyunParams aliyunParams){
|
|
|
+ this.accessKey = aliyunParams.getAccessKey();
|
|
|
+ this.accessSecret = aliyunParams.getAccessKeySecret();
|
|
|
+ this.regionId = aliyunParams.getRegionId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public AliyunIotSdk(PlatformAccount platformAccount) {
|
|
|
+ this.accessKey = platformAccount.getConfiguration().get(ACCESSKEY).toString();
|
|
|
+ this.accessSecret = platformAccount.getConfiguration().get(ACCESSSECRET).toString();
|
|
|
+ this.regionId = platformAccount.getConfiguration().get(REFIONID).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取阿里云客户端
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ protected final DefaultAcsClient getAliyuniotClient(){
|
|
|
+ if (client != null){
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+ IClientProfile profile = DefaultProfile.getProfile(regionId, accessKey, accessSecret);
|
|
|
+ DefaultAcsClient client = new DefaultAcsClient(profile); //初始化SDK客户端。
|
|
|
+ this.client = client;
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询产品列表。
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<QueryProductListResponse.Data.ProductInfo> queryProductList(){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+ List<QueryProductListResponse.Data.ProductInfo> products = new ArrayList<>();
|
|
|
+ Integer pageSize = 100,currentPage = 1;
|
|
|
+ while (true){
|
|
|
+ QueryProductListRequest queryProductListRequest = new QueryProductListRequest();
|
|
|
+ queryProductListRequest.setPageSize(pageSize);
|
|
|
+ queryProductListRequest.setCurrentPage(currentPage);
|
|
|
+ try {
|
|
|
+ QueryProductListResponse queryProductListResponse = client.getAcsResponse(queryProductListRequest);
|
|
|
+ System.out.println(queryProductListResponse.getSuccess());
|
|
|
+ // 获取失败直接跳出循环
|
|
|
+ if (!queryProductListResponse.getSuccess()){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ QueryProductListResponse.Data data = queryProductListResponse.getData();
|
|
|
+ // 添加到列表
|
|
|
+ products.addAll(data.getList());
|
|
|
+ // 判断是否获取了全部
|
|
|
+ if (data.getCurrentPage() < data.getPageCount()){
|
|
|
+ // 当前页小于总页数,说明没有获取完
|
|
|
+ currentPage++;
|
|
|
+ }else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return products;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册设备
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public RegisterDeviceResponse.Data registerDevice(String productKey, String name){
|
|
|
+
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+ List<QueryDeviceResponse.DeviceInfo> list = new ArrayList<>();
|
|
|
+ Integer pageSize = 100,currentPage = 1;
|
|
|
+
|
|
|
+ RegisterDeviceRequest request = new RegisterDeviceRequest();
|
|
|
+ // 产品key,必需
|
|
|
+ request.setProductKey(productKey);
|
|
|
+ // 产品名称,非必需
|
|
|
+ request.setDeviceName(name);
|
|
|
+ try {
|
|
|
+ RegisterDeviceResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println(response.getSuccess());
|
|
|
+ // 获取失败直接跳出循环
|
|
|
+ if (!response.getSuccess()){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ RegisterDeviceResponse.Data data = response.getData();
|
|
|
+ return data;
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除设备
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public int deleteDevice(String iotId){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+ DeleteDeviceRequest request = new DeleteDeviceRequest();
|
|
|
+ // 产品iotId,必需
|
|
|
+ request.setIotId(iotId);
|
|
|
+ try {
|
|
|
+ DeleteDeviceResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println(response.getSuccess());
|
|
|
+ // 获取失败直接跳出循环
|
|
|
+ if (!response.getSuccess()){
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取设备
|
|
|
+ * @param productKey
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<QueryDeviceResponse.DeviceInfo> queryDevice(String productKey){
|
|
|
+
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+ List<QueryDeviceResponse.DeviceInfo> list = new ArrayList<>();
|
|
|
+ Integer pageSize = 100,currentPage = 1;
|
|
|
+ while (true){
|
|
|
+ QueryDeviceRequest request = new QueryDeviceRequest();
|
|
|
+ request.setPageSize(pageSize);
|
|
|
+ request.setCurrentPage(currentPage);
|
|
|
+ request.setProductKey(productKey);
|
|
|
+ try {
|
|
|
+ QueryDeviceResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println(response.getSuccess());
|
|
|
+ // 获取失败直接跳出循环
|
|
|
+ if (!response.getSuccess()){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ List<QueryDeviceResponse.DeviceInfo> data = response.getData();
|
|
|
+ // 添加到列表
|
|
|
+ list.addAll(data);
|
|
|
+ // 判断是否获取了全部
|
|
|
+ if (response.getPage() < response.getPageCount()){
|
|
|
+ // 当前页小于总页数,说明没有获取完
|
|
|
+ currentPage++;
|
|
|
+ }else {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取阿里云消费组
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<QueryConsumerGroupListResponse.ConsumerGroupDTO> queryConsumerGroupList(){
|
|
|
+
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+
|
|
|
+ QueryConsumerGroupListRequest _request = new QueryConsumerGroupListRequest();
|
|
|
+ _request.setPageSize(100);
|
|
|
+ _request.setCurrentPage(1);
|
|
|
+
|
|
|
+ List<QueryConsumerGroupListResponse.ConsumerGroupDTO> list = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ QueryConsumerGroupListResponse _response = client.getAcsResponse(_request);
|
|
|
+ list = _response.getData();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建一个消费组
|
|
|
+ * @param groupName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public CreateConsumerGroupResponse createConsumerGroup(String groupName){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+
|
|
|
+ CreateConsumerGroupRequest _request = new CreateConsumerGroupRequest();
|
|
|
+ _request.setGroupName(groupName);
|
|
|
+
|
|
|
+ CreateConsumerGroupResponse acsResponse = null;
|
|
|
+ try {
|
|
|
+ acsResponse = client.getAcsResponse(_request);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return acsResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建一个云产品流转规则
|
|
|
+ * @param ruleName
|
|
|
+ * @param productKey
|
|
|
+ * @param device_list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public CreateRuleResponse createRule(String ruleName, String productKey, String[] device_list){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+
|
|
|
+ CreateRuleRequest _request = new CreateRuleRequest();
|
|
|
+ _request.setName(ruleName);
|
|
|
+ _request.setSelect("*");
|
|
|
+ _request.setShortTopic("+/thing/event/property/post");
|
|
|
+ _request.setProductKey(productKey);
|
|
|
+// _request.setWhere("deviceName() in ("+ StringUtils.arrayToString(device_list,"'",",") +")");
|
|
|
+
|
|
|
+
|
|
|
+ CreateRuleResponse acsResponse = null;
|
|
|
+ try {
|
|
|
+ acsResponse = client.getAcsResponse(_request);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return acsResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ public CreateRuleActionResponse createRuleAction(Long ruleId, String groupId){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+
|
|
|
+ CreateRuleActionRequest _request = new CreateRuleActionRequest();
|
|
|
+ _request.setRuleId(ruleId);
|
|
|
+ _request.setType("AMQP");
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("groupId",groupId);
|
|
|
+
|
|
|
+ _request.setConfiguration(JSON.toJSONString(map));
|
|
|
+
|
|
|
+
|
|
|
+ CreateRuleActionResponse acsResponse = null;
|
|
|
+ try {
|
|
|
+ acsResponse = client.getAcsResponse(_request);
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return acsResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取设备的详细信息
|
|
|
+ * @param iotId
|
|
|
+ */
|
|
|
+ public QueryDeviceDetailResponse.Data queryDeviceDetail(String iotId){
|
|
|
+ // 获取阿里云SDK客户端
|
|
|
+ DefaultAcsClient client = this.getAliyuniotClient();
|
|
|
+
|
|
|
+ QueryDeviceDetailRequest _request = new QueryDeviceDetailRequest();
|
|
|
+ _request.setIotId(iotId);
|
|
|
+
|
|
|
+ QueryDeviceDetailResponse.Data data = null;
|
|
|
+ try {
|
|
|
+ QueryDeviceDetailResponse acsResponse = client.getAcsResponse(_request);
|
|
|
+ data = acsResponse.getData();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|