diff --git a/springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaInfrared.java b/springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaInfrared.java new file mode 100644 index 0000000..d1ecb0c --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaInfrared.java @@ -0,0 +1,81 @@ +package com.sincre.springboot.ApiModel; + +import com.fasterxml.jackson.annotation.JsonInclude; + +/** + * 红外所需要的类 + */ + +@JsonInclude(JsonInclude.Include.NON_EMPTY) +public class TuYaInfrared { + + private String category_id; + + public String getCategory_id() { + return category_id; + } + + public void setCategory_id(String category_id) { + this.category_id = category_id; + } + + public String getCategory_name() { + return category_name; + } + + public void setCategory_name(String category_name) { + this.category_name = category_name; + } + + public String getBrand_id() { + return brand_id; + } + + public void setBrand_id(String brand_id) { + this.brand_id = brand_id; + } + + public String getBrand_name() { + return brand_name; + } + + public void setBrand_name(String brand_name) { + this.brand_name = brand_name; + } + + public String getRemote_index() { + return remote_index; + } + + public void setRemote_index(String remote_index) { + this.remote_index = remote_index; + } + + + private String category_name; + + private String brand_id; + + private String brand_name; + + private String remote_index; + private String remote_id; + + public String getRemote_id() { + return remote_id; + } + + public void setRemote_id(String remote_id) { + this.remote_id = remote_id; + } + + public String getRemote_name() { + return remote_name; + } + + public void setRemote_name(String remote_name) { + this.remote_name = remote_name; + } + + private String remote_name; +} diff --git a/springboot/src/main/java/com/sincre/springboot/common/ValidParam.java b/springboot/src/main/java/com/sincre/springboot/common/ValidParam.java new file mode 100644 index 0000000..064c567 --- /dev/null +++ b/springboot/src/main/java/com/sincre/springboot/common/ValidParam.java @@ -0,0 +1,5 @@ +package com.sincre.springboot.common; + +public class ValidParam { + +} diff --git a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java index e5a4aa3..9367dec 100644 --- a/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java +++ b/springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java @@ -13,7 +13,9 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.*; +import springfox.documentation.spring.web.json.Json; import java.util.HashMap; import java.util.Map; @@ -337,6 +339,113 @@ public class TuYaYunController { public ServerResponse removeDevice(String deviceId){ String apiUrl = String.format("/v1.0/devices/%s",deviceId); + Map map = headContent(); + String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + + @ApiOperation("获取红外设备支持的设 备类型") + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) + @GetMapping("getHWCategories") + public ServerResponse getHWCategories(String infraredId){ + + String apiUrl = String.format("/v1.0/infrareds/%s/categories",infraredId); + + Map map = headContent(); + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + + @ApiOperation("获取红外设备支持的设备品牌") + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) + @GetMapping("getHWBrands") + public ServerResponse getHWBrands(String infraredId,String categoryId){ + + String apiUrl = String.format("/v1.0/infrareds/%s/categories/%s/brands",infraredId,categoryId); + Map map = headContent(); + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + @ApiOperation("获取设备品牌支持的遥控器索引") + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) + @GetMapping("getHWBrandsIndex") + public ServerResponse getHWBrandsIndex(String infraredId,String categoryId,String brandId){ + + String apiUrl = String.format("/v1.0/infrareds/%s/categories/%s/brands/%s",infraredId,categoryId,brandId); + Map map = headContent(); + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + + @ApiOperation("向红外设备添加普通遥控器") + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path") + @PostMapping("{infrared_id}/addHWRemote") + public ServerResponse addHWRemote(@RequestBody TuYaInfrared tuYaInfrared,@PathVariable String infrared_id){ + + if(StringUtils.isBlank(infrared_id)||tuYaInfrared==null){ + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); + } + else{ + if(StringUtils.isBlank(tuYaInfrared.getBrand_id())||StringUtils.isBlank(tuYaInfrared.getCategory_id())||StringUtils.isBlank(tuYaInfrared.getRemote_index())){ + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); + } + } + String apiUrl = String.format("/v1.0/infrareds/%s/normal/add-remote",infrared_id); + Map map = headContent(); + + String json = JSON.toJSONString(tuYaInfrared); + String result = ApiHelper.doPost(TuYaCloudService.TuYaOpenUrl + apiUrl,map,json); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + + @ApiOperation("删除红外设备下的遥控器") + @ApiImplicitParams({ + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path"), + @ApiImplicitParam(name="remote_id",value = "设备ID",required = true,paramType = "path") + }) + @DeleteMapping("{infrared_id}/delHWRemote/{remote_id}") + public ServerResponse delHWRemote(@PathVariable String infrared_id,@PathVariable String remote_id){ + + String apiUrl = String.format("/v1.0/infrareds/%s/remotes/%s",infrared_id,remote_id); + Map map = headContent(); + String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + + @ApiOperation("编辑红外设备下的遥控器的名称") + @ApiImplicitParams({ + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path") + }) + @PutMapping("{infrared_id}/editHWRemote") + public ServerResponse editHWRemote(@PathVariable String infrared_id,@RequestBody TuYaInfrared tuYaInfrared){ + + if(tuYaInfrared==null){ + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); + } + else{ + if(StringUtils.isBlank(tuYaInfrared.getRemote_id())||StringUtils.isBlank(tuYaInfrared.getRemote_name())){ + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); + } + } + String apiUrl = String.format("/v1.0/infrareds/%s",infrared_id); + Map map = headContent(); + String json = JSON.toJSONString(tuYaInfrared); + String result = ApiHelper.doPut(TuYaCloudService.TuYaOpenUrl + apiUrl,map,json); + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + } + /** + * 请求头的参数 + * @return + */ + private Map headContent(){ + Long t = System.currentTimeMillis(); String access_Token = CacheHelper.getTuYaToken(); String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); @@ -347,9 +456,7 @@ public class TuYaYunController { map.put("sign",sign); map.put("sign_method",TuYaCloudService.Sign_method); map.put("t",t.toString()); - String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); - TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); - return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); + return map; } } diff --git a/springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java b/springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java index 1e28d5d..21700e2 100644 --- a/springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java +++ b/springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java @@ -5,10 +5,7 @@ import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpDelete; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.*; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -36,13 +33,8 @@ public class ApiHelper { httpGet.setHeader(entry.getKey(),entry.getValue()); } - // 设置配置请求参数 - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 - .setConnectionRequestTimeout(35000)// 请求超时时间 - .setSocketTimeout(60000)// 数据读取超时时间 - .build(); // 为httpGet实例设置配置 - httpGet.setConfig(requestConfig); + httpGet.setConfig(setRequestConfig()); // 执行get请求得到返回对象 response = httpClient.execute(httpGet); // 通过返回对象获取返回数据 @@ -74,6 +66,7 @@ public class ApiHelper { } + public static String doDelete(String url,Map headerParamMap) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; @@ -87,14 +80,8 @@ public class ApiHelper { for (Map.Entry entry : headerParamMap.entrySet()) { httpDelete.setHeader(entry.getKey(),entry.getValue()); } - - // 设置配置请求参数 - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 - .setConnectionRequestTimeout(35000)// 请求超时时间 - .setSocketTimeout(60000)// 数据读取超时时间 - .build(); // 为httpGet实例设置配置 - httpDelete.setConfig(requestConfig); + httpDelete.setConfig(setRequestConfig()); // 执行get请求得到返回对象 response = httpClient.execute(httpDelete); // 通过返回对象获取返回数据 @@ -145,13 +132,8 @@ public class ApiHelper { for (Map.Entry entry : headerParamMap.entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue()); } - // 配置请求参数实例 - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间 - .setConnectionRequestTimeout(35000)// 设置连接请求超时时间 - .setSocketTimeout(60000)// 设置读取数据连接超时时间 - .build(); // 为httpPost实例设置配置 - httpPost.setConfig(requestConfig); + httpPost.setConfig(setRequestConfig()); // 封装post请求参数 if (null != paramMap && paramMap.size() > 0) { @@ -173,7 +155,7 @@ public class ApiHelper { e.printStackTrace(); } } - result = closeHttpAndResult(httpClient, httpPost); + result = closeHttpAndResult(httpClient, httpPost,null); return result; } /** @@ -185,7 +167,6 @@ public class ApiHelper { */ public static String doPost(String url, Map headerParamMap, String jsonParam) { CloseableHttpClient httpClient; -// CloseableHttpResponse httpResponse = null; String result; // 创建httpClient实例 httpClient = HttpClients.createDefault(); @@ -196,13 +177,8 @@ public class ApiHelper { for (Map.Entry entry : headerParamMap.entrySet()) { httpPost.setHeader(entry.getKey(),entry.getValue()); } - // 配置请求参数实例 - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间 - .setConnectionRequestTimeout(35000)// 设置连接请求超时时间 - .setSocketTimeout(60000)// 设置读取数据连接超时时间 - .build(); // 为httpPost实例设置配置 - httpPost.setConfig(requestConfig); + httpPost.setConfig(setRequestConfig()); // try { StringEntity stringEntity = new StringEntity(jsonParam, ContentType.create("application/json","UTF-8")); // stringEntity.setContentType("application/json"); @@ -211,16 +187,44 @@ public class ApiHelper { // }catch (UnsupportedEncodingException e){ // e.printStackTrace(); // } - result = closeHttpAndResult(httpClient,httpPost); + result = closeHttpAndResult(httpClient,httpPost,null); return result; } - private static String closeHttpAndResult(CloseableHttpClient httpClient,HttpPost httpPost){ + public static String doPut(String url, Map headerParamMap, String jsonParam) { + CloseableHttpClient httpClient; + String result; + // 创建httpClient实例 + httpClient = HttpClients.createDefault(); + // 创建httpPost远程连接实例 + HttpPut httpPut= new HttpPut(url); + // 设置请求头 + httpPut.addHeader("Content-Type", "application/json"); + for (Map.Entry entry : headerParamMap.entrySet()) { + httpPut.setHeader(entry.getKey(),entry.getValue()); + } + // 为httpPut实例设置配置 + httpPut.setConfig(setRequestConfig()); + + StringEntity stringEntity = new StringEntity(jsonParam, ContentType.create("application/json","UTF-8")); + + httpPut.setEntity(stringEntity); + + result = closeHttpAndResult(httpClient,null,httpPut); + return result; + } + + + private static String closeHttpAndResult(CloseableHttpClient httpClient,HttpPost httpPost,HttpPut httpPut){ CloseableHttpResponse httpResponse = null; String result = null; try { - // httpClient对象执行post请求,并返回响应参数对象 - httpResponse = httpClient.execute(httpPost); + // httpClient对象执行post或者是put请求,并返回响应参数对象 + if(httpPost != null) { + httpResponse = httpClient.execute(httpPost); + }else { + httpResponse = httpClient.execute(httpPut); + } // 从响应对象中获取响应内容 HttpEntity entity = httpResponse.getEntity(); result = EntityUtils.toString(entity); @@ -248,5 +252,19 @@ public class ApiHelper { return result; } + + + /** + * 设置配置请求参数 + * @return + */ + private static RequestConfig setRequestConfig(){ + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 + .setConnectionRequestTimeout(35000)// 请求超时时间 + .setSocketTimeout(60000)// 数据读取超时时间 + .build(); + + return requestConfig; + } } -- libgit2 0.21.0