Commit 7539135b9beeffa1eb420b4e1b51c4c8b770361c
1 parent
3d76350e
Exists in
master
涂鸦红外接口对接完成添加遥控器的增删改æ
Showing
4 changed files
with
249 additions
and
38 deletions
Show diff stats
springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaInfrared.java
0 → 100644
| ... | ... | @@ -0,0 +1,81 @@ |
| 1 | +package com.sincre.springboot.ApiModel; | |
| 2 | + | |
| 3 | +import com.fasterxml.jackson.annotation.JsonInclude; | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * 红外所需要的类 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +@JsonInclude(JsonInclude.Include.NON_EMPTY) | |
| 10 | +public class TuYaInfrared { | |
| 11 | + | |
| 12 | + private String category_id; | |
| 13 | + | |
| 14 | + public String getCategory_id() { | |
| 15 | + return category_id; | |
| 16 | + } | |
| 17 | + | |
| 18 | + public void setCategory_id(String category_id) { | |
| 19 | + this.category_id = category_id; | |
| 20 | + } | |
| 21 | + | |
| 22 | + public String getCategory_name() { | |
| 23 | + return category_name; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public void setCategory_name(String category_name) { | |
| 27 | + this.category_name = category_name; | |
| 28 | + } | |
| 29 | + | |
| 30 | + public String getBrand_id() { | |
| 31 | + return brand_id; | |
| 32 | + } | |
| 33 | + | |
| 34 | + public void setBrand_id(String brand_id) { | |
| 35 | + this.brand_id = brand_id; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public String getBrand_name() { | |
| 39 | + return brand_name; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public void setBrand_name(String brand_name) { | |
| 43 | + this.brand_name = brand_name; | |
| 44 | + } | |
| 45 | + | |
| 46 | + public String getRemote_index() { | |
| 47 | + return remote_index; | |
| 48 | + } | |
| 49 | + | |
| 50 | + public void setRemote_index(String remote_index) { | |
| 51 | + this.remote_index = remote_index; | |
| 52 | + } | |
| 53 | + | |
| 54 | + | |
| 55 | + private String category_name; | |
| 56 | + | |
| 57 | + private String brand_id; | |
| 58 | + | |
| 59 | + private String brand_name; | |
| 60 | + | |
| 61 | + private String remote_index; | |
| 62 | + private String remote_id; | |
| 63 | + | |
| 64 | + public String getRemote_id() { | |
| 65 | + return remote_id; | |
| 66 | + } | |
| 67 | + | |
| 68 | + public void setRemote_id(String remote_id) { | |
| 69 | + this.remote_id = remote_id; | |
| 70 | + } | |
| 71 | + | |
| 72 | + public String getRemote_name() { | |
| 73 | + return remote_name; | |
| 74 | + } | |
| 75 | + | |
| 76 | + public void setRemote_name(String remote_name) { | |
| 77 | + this.remote_name = remote_name; | |
| 78 | + } | |
| 79 | + | |
| 80 | + private String remote_name; | |
| 81 | +} | ... | ... |
springboot/src/main/java/com/sincre/springboot/common/ValidParam.java
0 → 100644
springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java
| ... | ... | @@ -13,7 +13,9 @@ import io.swagger.annotations.Api; |
| 13 | 13 | import io.swagger.annotations.ApiImplicitParam; |
| 14 | 14 | import io.swagger.annotations.ApiImplicitParams; |
| 15 | 15 | import io.swagger.annotations.ApiOperation; |
| 16 | +import org.apache.commons.lang3.StringUtils; | |
| 16 | 17 | import org.springframework.web.bind.annotation.*; |
| 18 | +import springfox.documentation.spring.web.json.Json; | |
| 17 | 19 | |
| 18 | 20 | import java.util.HashMap; |
| 19 | 21 | import java.util.Map; |
| ... | ... | @@ -337,6 +339,113 @@ public class TuYaYunController { |
| 337 | 339 | public ServerResponse removeDevice(String deviceId){ |
| 338 | 340 | |
| 339 | 341 | String apiUrl = String.format("/v1.0/devices/%s",deviceId); |
| 342 | + Map<String,String> map = headContent(); | |
| 343 | + String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 344 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 345 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 346 | + } | |
| 347 | + | |
| 348 | + @ApiOperation("获取红外设备支持的设 备类型") | |
| 349 | + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) | |
| 350 | + @GetMapping("getHWCategories") | |
| 351 | + public ServerResponse getHWCategories(String infraredId){ | |
| 352 | + | |
| 353 | + String apiUrl = String.format("/v1.0/infrareds/%s/categories",infraredId); | |
| 354 | + | |
| 355 | + Map<String,String> map = headContent(); | |
| 356 | + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 357 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 358 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 359 | + } | |
| 360 | + | |
| 361 | + @ApiOperation("获取红外设备支持的设备品牌") | |
| 362 | + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) | |
| 363 | + @GetMapping("getHWBrands") | |
| 364 | + public ServerResponse getHWBrands(String infraredId,String categoryId){ | |
| 365 | + | |
| 366 | + String apiUrl = String.format("/v1.0/infrareds/%s/categories/%s/brands",infraredId,categoryId); | |
| 367 | + Map<String,String> map = headContent(); | |
| 368 | + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 369 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 370 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 371 | + } | |
| 372 | + @ApiOperation("获取设备品牌支持的遥控器索引") | |
| 373 | + @ApiImplicitParam(name="infraredId",value = "设备ID",required = true) | |
| 374 | + @GetMapping("getHWBrandsIndex") | |
| 375 | + public ServerResponse getHWBrandsIndex(String infraredId,String categoryId,String brandId){ | |
| 376 | + | |
| 377 | + String apiUrl = String.format("/v1.0/infrareds/%s/categories/%s/brands/%s",infraredId,categoryId,brandId); | |
| 378 | + Map<String,String> map = headContent(); | |
| 379 | + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 380 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 381 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 382 | + } | |
| 383 | + | |
| 384 | + @ApiOperation("向红外设备添加普通遥控器") | |
| 385 | + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path") | |
| 386 | + @PostMapping("{infrared_id}/addHWRemote") | |
| 387 | + public ServerResponse addHWRemote(@RequestBody TuYaInfrared tuYaInfrared,@PathVariable String infrared_id){ | |
| 388 | + | |
| 389 | + if(StringUtils.isBlank(infrared_id)||tuYaInfrared==null){ | |
| 390 | + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); | |
| 391 | + } | |
| 392 | + else{ | |
| 393 | + if(StringUtils.isBlank(tuYaInfrared.getBrand_id())||StringUtils.isBlank(tuYaInfrared.getCategory_id())||StringUtils.isBlank(tuYaInfrared.getRemote_index())){ | |
| 394 | + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); | |
| 395 | + } | |
| 396 | + } | |
| 397 | + String apiUrl = String.format("/v1.0/infrareds/%s/normal/add-remote",infrared_id); | |
| 398 | + Map<String,String> map = headContent(); | |
| 399 | + | |
| 400 | + String json = JSON.toJSONString(tuYaInfrared); | |
| 401 | + String result = ApiHelper.doPost(TuYaCloudService.TuYaOpenUrl + apiUrl,map,json); | |
| 402 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 403 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 404 | + } | |
| 405 | + | |
| 406 | + @ApiOperation("删除红外设备下的遥控器") | |
| 407 | + @ApiImplicitParams({ | |
| 408 | + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path"), | |
| 409 | + @ApiImplicitParam(name="remote_id",value = "设备ID",required = true,paramType = "path") | |
| 410 | + }) | |
| 411 | + @DeleteMapping("{infrared_id}/delHWRemote/{remote_id}") | |
| 412 | + public ServerResponse delHWRemote(@PathVariable String infrared_id,@PathVariable String remote_id){ | |
| 413 | + | |
| 414 | + String apiUrl = String.format("/v1.0/infrareds/%s/remotes/%s",infrared_id,remote_id); | |
| 415 | + Map<String,String> map = headContent(); | |
| 416 | + String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 417 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 418 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 419 | + } | |
| 420 | + | |
| 421 | + @ApiOperation("编辑红外设备下的遥控器的名称") | |
| 422 | + @ApiImplicitParams({ | |
| 423 | + @ApiImplicitParam(name="infrared_id",value = "设备ID",required = true,paramType = "path") | |
| 424 | + }) | |
| 425 | + @PutMapping("{infrared_id}/editHWRemote") | |
| 426 | + public ServerResponse editHWRemote(@PathVariable String infrared_id,@RequestBody TuYaInfrared tuYaInfrared){ | |
| 427 | + | |
| 428 | + if(tuYaInfrared==null){ | |
| 429 | + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); | |
| 430 | + } | |
| 431 | + else{ | |
| 432 | + if(StringUtils.isBlank(tuYaInfrared.getRemote_id())||StringUtils.isBlank(tuYaInfrared.getRemote_name())){ | |
| 433 | + return ServerResponse.createByErrorCodeMessage(400,"参数错误"); | |
| 434 | + } | |
| 435 | + } | |
| 436 | + String apiUrl = String.format("/v1.0/infrareds/%s",infrared_id); | |
| 437 | + Map<String,String> map = headContent(); | |
| 438 | + String json = JSON.toJSONString(tuYaInfrared); | |
| 439 | + String result = ApiHelper.doPut(TuYaCloudService.TuYaOpenUrl + apiUrl,map,json); | |
| 440 | + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 441 | + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 442 | + } | |
| 443 | + /** | |
| 444 | + * 请求头的参数 | |
| 445 | + * @return | |
| 446 | + */ | |
| 447 | + private Map<String,String> headContent(){ | |
| 448 | + | |
| 340 | 449 | Long t = System.currentTimeMillis(); |
| 341 | 450 | String access_Token = CacheHelper.getTuYaToken(); |
| 342 | 451 | String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret); |
| ... | ... | @@ -347,9 +456,7 @@ public class TuYaYunController { |
| 347 | 456 | map.put("sign",sign); |
| 348 | 457 | map.put("sign_method",TuYaCloudService.Sign_method); |
| 349 | 458 | map.put("t",t.toString()); |
| 350 | - String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map); | |
| 351 | 459 | |
| 352 | - TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class); | |
| 353 | - return ResultUtils.getInstance().returnResultTuYa(tuYaResResult); | |
| 460 | + return map; | |
| 354 | 461 | } |
| 355 | 462 | } | ... | ... |
springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java
| ... | ... | @@ -5,10 +5,7 @@ import org.apache.http.NameValuePair; |
| 5 | 5 | import org.apache.http.client.ClientProtocolException; |
| 6 | 6 | import org.apache.http.client.config.RequestConfig; |
| 7 | 7 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 8 | -import org.apache.http.client.methods.CloseableHttpResponse; | |
| 9 | -import org.apache.http.client.methods.HttpDelete; | |
| 10 | -import org.apache.http.client.methods.HttpGet; | |
| 11 | -import org.apache.http.client.methods.HttpPost; | |
| 8 | +import org.apache.http.client.methods.*; | |
| 12 | 9 | import org.apache.http.entity.ContentType; |
| 13 | 10 | import org.apache.http.entity.StringEntity; |
| 14 | 11 | import org.apache.http.impl.client.CloseableHttpClient; |
| ... | ... | @@ -36,13 +33,8 @@ public class ApiHelper { |
| 36 | 33 | httpGet.setHeader(entry.getKey(),entry.getValue()); |
| 37 | 34 | } |
| 38 | 35 | |
| 39 | - // 设置配置请求参数 | |
| 40 | - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 | |
| 41 | - .setConnectionRequestTimeout(35000)// 请求超时时间 | |
| 42 | - .setSocketTimeout(60000)// 数据读取超时时间 | |
| 43 | - .build(); | |
| 44 | 36 | // 为httpGet实例设置配置 |
| 45 | - httpGet.setConfig(requestConfig); | |
| 37 | + httpGet.setConfig(setRequestConfig()); | |
| 46 | 38 | // 执行get请求得到返回对象 |
| 47 | 39 | response = httpClient.execute(httpGet); |
| 48 | 40 | // 通过返回对象获取返回数据 |
| ... | ... | @@ -74,6 +66,7 @@ public class ApiHelper { |
| 74 | 66 | } |
| 75 | 67 | |
| 76 | 68 | |
| 69 | + | |
| 77 | 70 | public static String doDelete(String url,Map<String, String> headerParamMap) { |
| 78 | 71 | CloseableHttpClient httpClient = null; |
| 79 | 72 | CloseableHttpResponse response = null; |
| ... | ... | @@ -87,14 +80,8 @@ public class ApiHelper { |
| 87 | 80 | for (Map.Entry<String, String> entry : headerParamMap.entrySet()) { |
| 88 | 81 | httpDelete.setHeader(entry.getKey(),entry.getValue()); |
| 89 | 82 | } |
| 90 | - | |
| 91 | - // 设置配置请求参数 | |
| 92 | - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 | |
| 93 | - .setConnectionRequestTimeout(35000)// 请求超时时间 | |
| 94 | - .setSocketTimeout(60000)// 数据读取超时时间 | |
| 95 | - .build(); | |
| 96 | 83 | // 为httpGet实例设置配置 |
| 97 | - httpDelete.setConfig(requestConfig); | |
| 84 | + httpDelete.setConfig(setRequestConfig()); | |
| 98 | 85 | // 执行get请求得到返回对象 |
| 99 | 86 | response = httpClient.execute(httpDelete); |
| 100 | 87 | // 通过返回对象获取返回数据 |
| ... | ... | @@ -145,13 +132,8 @@ public class ApiHelper { |
| 145 | 132 | for (Map.Entry<String, String> entry : headerParamMap.entrySet()) { |
| 146 | 133 | httpPost.setHeader(entry.getKey(), entry.getValue()); |
| 147 | 134 | } |
| 148 | - // 配置请求参数实例 | |
| 149 | - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间 | |
| 150 | - .setConnectionRequestTimeout(35000)// 设置连接请求超时时间 | |
| 151 | - .setSocketTimeout(60000)// 设置读取数据连接超时时间 | |
| 152 | - .build(); | |
| 153 | 135 | // 为httpPost实例设置配置 |
| 154 | - httpPost.setConfig(requestConfig); | |
| 136 | + httpPost.setConfig(setRequestConfig()); | |
| 155 | 137 | |
| 156 | 138 | // 封装post请求参数 |
| 157 | 139 | if (null != paramMap && paramMap.size() > 0) { |
| ... | ... | @@ -173,7 +155,7 @@ public class ApiHelper { |
| 173 | 155 | e.printStackTrace(); |
| 174 | 156 | } |
| 175 | 157 | } |
| 176 | - result = closeHttpAndResult(httpClient, httpPost); | |
| 158 | + result = closeHttpAndResult(httpClient, httpPost,null); | |
| 177 | 159 | return result; |
| 178 | 160 | } |
| 179 | 161 | /** |
| ... | ... | @@ -185,7 +167,6 @@ public class ApiHelper { |
| 185 | 167 | */ |
| 186 | 168 | public static String doPost(String url, Map<String, String> headerParamMap, String jsonParam) { |
| 187 | 169 | CloseableHttpClient httpClient; |
| 188 | -// CloseableHttpResponse httpResponse = null; | |
| 189 | 170 | String result; |
| 190 | 171 | // 创建httpClient实例 |
| 191 | 172 | httpClient = HttpClients.createDefault(); |
| ... | ... | @@ -196,13 +177,8 @@ public class ApiHelper { |
| 196 | 177 | for (Map.Entry<String, String> entry : headerParamMap.entrySet()) { |
| 197 | 178 | httpPost.setHeader(entry.getKey(),entry.getValue()); |
| 198 | 179 | } |
| 199 | - // 配置请求参数实例 | |
| 200 | - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间 | |
| 201 | - .setConnectionRequestTimeout(35000)// 设置连接请求超时时间 | |
| 202 | - .setSocketTimeout(60000)// 设置读取数据连接超时时间 | |
| 203 | - .build(); | |
| 204 | 180 | // 为httpPost实例设置配置 |
| 205 | - httpPost.setConfig(requestConfig); | |
| 181 | + httpPost.setConfig(setRequestConfig()); | |
| 206 | 182 | // try { |
| 207 | 183 | StringEntity stringEntity = new StringEntity(jsonParam, ContentType.create("application/json","UTF-8")); |
| 208 | 184 | // stringEntity.setContentType("application/json"); |
| ... | ... | @@ -211,16 +187,44 @@ public class ApiHelper { |
| 211 | 187 | // }catch (UnsupportedEncodingException e){ |
| 212 | 188 | // e.printStackTrace(); |
| 213 | 189 | // } |
| 214 | - result = closeHttpAndResult(httpClient,httpPost); | |
| 190 | + result = closeHttpAndResult(httpClient,httpPost,null); | |
| 215 | 191 | return result; |
| 216 | 192 | } |
| 217 | 193 | |
| 218 | - private static String closeHttpAndResult(CloseableHttpClient httpClient,HttpPost httpPost){ | |
| 194 | + public static String doPut(String url, Map<String, String> headerParamMap, String jsonParam) { | |
| 195 | + CloseableHttpClient httpClient; | |
| 196 | + String result; | |
| 197 | + // 创建httpClient实例 | |
| 198 | + httpClient = HttpClients.createDefault(); | |
| 199 | + // 创建httpPost远程连接实例 | |
| 200 | + HttpPut httpPut= new HttpPut(url); | |
| 201 | + // 设置请求头 | |
| 202 | + httpPut.addHeader("Content-Type", "application/json"); | |
| 203 | + for (Map.Entry<String, String> entry : headerParamMap.entrySet()) { | |
| 204 | + httpPut.setHeader(entry.getKey(),entry.getValue()); | |
| 205 | + } | |
| 206 | + // 为httpPut实例设置配置 | |
| 207 | + httpPut.setConfig(setRequestConfig()); | |
| 208 | + | |
| 209 | + StringEntity stringEntity = new StringEntity(jsonParam, ContentType.create("application/json","UTF-8")); | |
| 210 | + | |
| 211 | + httpPut.setEntity(stringEntity); | |
| 212 | + | |
| 213 | + result = closeHttpAndResult(httpClient,null,httpPut); | |
| 214 | + return result; | |
| 215 | + } | |
| 216 | + | |
| 217 | + | |
| 218 | + private static String closeHttpAndResult(CloseableHttpClient httpClient,HttpPost httpPost,HttpPut httpPut){ | |
| 219 | 219 | CloseableHttpResponse httpResponse = null; |
| 220 | 220 | String result = null; |
| 221 | 221 | try { |
| 222 | - // httpClient对象执行post请求,并返回响应参数对象 | |
| 223 | - httpResponse = httpClient.execute(httpPost); | |
| 222 | + // httpClient对象执行post或者是put请求,并返回响应参数对象 | |
| 223 | + if(httpPost != null) { | |
| 224 | + httpResponse = httpClient.execute(httpPost); | |
| 225 | + }else { | |
| 226 | + httpResponse = httpClient.execute(httpPut); | |
| 227 | + } | |
| 224 | 228 | // 从响应对象中获取响应内容 |
| 225 | 229 | HttpEntity entity = httpResponse.getEntity(); |
| 226 | 230 | result = EntityUtils.toString(entity); |
| ... | ... | @@ -248,5 +252,19 @@ public class ApiHelper { |
| 248 | 252 | |
| 249 | 253 | return result; |
| 250 | 254 | } |
| 255 | + | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * 设置配置请求参数 | |
| 259 | + * @return | |
| 260 | + */ | |
| 261 | + private static RequestConfig setRequestConfig(){ | |
| 262 | + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间 | |
| 263 | + .setConnectionRequestTimeout(35000)// 请求超时时间 | |
| 264 | + .setSocketTimeout(60000)// 数据读取超时时间 | |
| 265 | + .build(); | |
| 266 | + | |
| 267 | + return requestConfig; | |
| 268 | + } | |
| 251 | 269 | } |
| 252 | 270 | ... | ... |