Commit faab39dd15640f43849110cc18de025d96decafe

Authored by baishou
1 parent 464aaf3f
Exists in master

物联网接口服务接口代码优化

springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaCommand.java 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +package com.sincre.springboot.ApiModel;
  2 +
  3 +import java.util.List;
  4 +
  5 +/**
  6 + * 命令集
  7 + */
  8 +public class TuYaCommand {
  9 +
  10 + public List<Command> getCommands() {
  11 + return commands;
  12 + }
  13 +
  14 + public void setCommands(List<Command> commands) {
  15 + this.commands = commands;
  16 + }
  17 +
  18 + private List<Command> commands;
  19 +
  20 +}
  21 +class Command{
  22 + private String code;
  23 +
  24 + public String getCode() {
  25 + return code;
  26 + }
  27 +
  28 + public void setCode(String code) {
  29 + this.code = code;
  30 + }
  31 +
  32 + public String getValue() {
  33 + return value;
  34 + }
  35 +
  36 + public void setValue(String value) {
  37 + this.value = value;
  38 + }
  39 +
  40 + private String value;
  41 +
  42 +}
  43 +
... ...
springboot/src/main/java/com/sincre/springboot/ApiModel/TuYaDevicesList.java
1 1 package com.sincre.springboot.ApiModel;
2 2  
3   -import java.awt.*;
  3 +
  4 +import java.util.List;
4 5  
5 6 public class TuYaDevicesList {
6 7 /**
... ...
springboot/src/main/java/com/sincre/springboot/ApiPlatform/YinShiServiceConfig.java
... ... @@ -14,7 +14,7 @@ public class YinShiServiceConfig {
14 14 /**
15 15 * 用于管理员获取accessToken
16 16 */
17   - public static String AccessToken = "at.81rs7cox2ckxiw4v8t4wrt6k07ehcbw2-6ii49gj6ji-1j6mggg-xnrve0akm"; //测试阶段先写死
  17 + public static String AccessToken = "at.ami8hxj50xa4lgrz07d237242w2zv0oi-4g2c500x7p-1wgkprt-fzjld4k67"; //测试阶段先写死,该接口到期时间是6月17号
18 18  
19 19  
20 20  
... ...
springboot/src/main/java/com/sincre/springboot/common/ServerResponse.java
... ... @@ -62,6 +62,7 @@ public class ServerResponse&lt;T&gt; implements Serializable {
62 62 return this.code == ResponseCode.SUCCESS.getCode();
63 63 }
64 64  
  65 + @JsonInclude(JsonInclude.Include.NON_DEFAULT)
65 66 public String getTotal() {
66 67 return total;
67 68 }
... ...
springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java
... ... @@ -23,10 +23,33 @@ import java.util.Map;
23 23 @Api(tags = "涂鸦云服务API调用")
24 24 public class TuYaYunController {
25 25  
  26 + @ApiOperation("根据设备ID来获取设备的信息")
  27 + @ApiImplicitParam(name="deviceId",value = "设备ID",required = true)
  28 + @GetMapping("getDeviceInfoByDeviceID")
  29 + public ServerResponse getDeviceInfoByDeviceID(String deviceId){
  30 +
  31 + String apiUrl = String.format("/v1.0/devices/%s",deviceId);
  32 + Long t = System.currentTimeMillis();
  33 + String access_Token = CacheHelper.getTuYaToken();
  34 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  35 + Map<String,String> map = new HashMap();
  36 + map.put("client_id", TuYaCloudService.ClientId);
  37 + map.put("access_token",access_Token);
  38 + map.put("sign",sign);
  39 + map.put("sign_method",TuYaCloudService.Sign_method);
  40 + map.put("t",t.toString());
  41 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  42 +
  43 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  44 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  45 + }
  46 +
  47 + @ApiOperation("批量获取设备信息")
  48 + @ApiImplicitParam(name="deviceIds",value = "设备ID 列表,多个设备号用逗号隔开",required = true)
26 49 @GetMapping("GetDevicesInfo")
27   - public ServerResponse getTuYaDevicesInfo(){
  50 + public ServerResponse getTuYaDevicesInfo(String deviceIds){
28 51  
29   - String apiUrl = "/v1.0/devices?device_ids=";
  52 + String apiUrl = "/v1.0/devices?device_ids="+deviceIds;
30 53 Long t = System.currentTimeMillis();
31 54 String access_Token = CacheHelper.getTuYaToken();
32 55 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
... ... @@ -38,21 +61,53 @@ public class TuYaYunController {
38 61 map.put("t",t.toString());
39 62 String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
40 63  
41   -// TuYaResResult<TuYaDevicesList> resResult = null;
42   -// try{
43   -// ObjectMapper objectMapper = new ObjectMapper();
44   -// TypeReference<TuYaResResult<TuYaDevicesList>> reference = new TypeReference<TuYaResResult<TuYaDevicesList>>(){};
45   -// resResult = objectMapper.readValue(result,reference);
46   -// } catch(Exception ex){
47   -// System.out.println(ex.getMessage());
48   -// }
49   - System.out.println(result);
  64 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  65 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  66 + }
  67 +
  68 + @ApiOperation("获取设备状态信息")
  69 + @ApiImplicitParam(name="deviceId",value = "设备ID",required = true)
  70 + @GetMapping("getStatusByDeviceID")
  71 + public ServerResponse getStatusByDeviceID(String deviceId){
  72 +
  73 + String apiUrl = String.format("/v1.0/devices/%s/status",deviceId);
  74 + Long t = System.currentTimeMillis();
  75 + String access_Token = CacheHelper.getTuYaToken();
  76 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  77 + Map<String,String> map = new HashMap();
  78 + map.put("client_id", TuYaCloudService.ClientId);
  79 + map.put("access_token",access_Token);
  80 + map.put("sign",sign);
  81 + map.put("sign_method",TuYaCloudService.Sign_method);
  82 + map.put("t",t.toString());
  83 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
50 84  
51 85 TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
52 86 return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
53 87 }
54 88  
55 89  
  90 + @ApiOperation("批量获取设备状态")
  91 + @ApiImplicitParam(name="deviceIds",value = "设备ID 列表,多个设备号用逗号隔开",required = true)
  92 + @GetMapping("getTuYaStatusInfo")
  93 + public ServerResponse getTuYaStatusInfo(String deviceIds){
  94 +
  95 + String apiUrl = "/v1.0/devices/status?device_ids="+deviceIds;
  96 + Long t = System.currentTimeMillis();
  97 + String access_Token = CacheHelper.getTuYaToken();
  98 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  99 + Map<String,String> map = new HashMap();
  100 + map.put("client_id", TuYaCloudService.ClientId);
  101 + map.put("access_token",access_Token);
  102 + map.put("sign",sign);
  103 + map.put("sign_method",TuYaCloudService.Sign_method);
  104 + map.put("t",t.toString());
  105 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  106 +
  107 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  108 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  109 + }
  110 +
56 111 /**
57 112 * 根据schema分页获取开发者App下的用户列表
58 113 * @param AppId 应用标识
... ... @@ -62,6 +117,7 @@ public class TuYaYunController {
62 117 */
63 118 @ApiOperation(value = "获取用户信息列表")
64 119 @ApiImplicitParams({
  120 + @ApiImplicitParam(name="AppId",value = "应用标识",required = true),
65 121 @ApiImplicitParam(name="pageIndex", value = "分页起始页,从1开始",required = true),
66 122 @ApiImplicitParam(name="pageSize", value = "分页大小,默认为10,最大为50")
67 123 })
... ... @@ -87,6 +143,8 @@ public class TuYaYunController {
87 143 }
88 144  
89 145  
  146 + @ApiOperation("注册用户")
  147 + @ApiImplicitParam(name="AppId",value = "应用标识",required = true)
90 148 @PostMapping("{AppId}/AddTuYaAPPUser")
91 149 public ServerResponse addTuYaAPPUser(@PathVariable("AppId") String AppId, @RequestBody UserInfo userInfo){
92 150  
... ... @@ -121,6 +179,8 @@ public class TuYaYunController {
121 179 * @param uid
122 180 * @return
123 181 */
  182 + @ApiOperation("根据用户ID来获取用户账号下的所有设备")
  183 + @ApiImplicitParam(name="uid",value = "用户标识",required = true)
124 184 @GetMapping("{uid}/GetDevicesByUid")
125 185 public ServerResponse getDevicesByUid(@PathVariable("uid") String uid){
126 186  
... ... @@ -146,8 +206,10 @@ public class TuYaYunController {
146 206 * 生成配网的Token
147 207 * @return
148 208 */
  209 + @ApiOperation("生成配网的Token")
  210 + @ApiImplicitParam(name="uid",value = "用户标识",required = true)
149 211 @GetMapping("AddDevicesToken")
150   - public ServerResponse addDevicesToken(@RequestParam String UId){
  212 + public ServerResponse addDevicesToken(@RequestParam String uid){
151 213  
152 214 String apiUrl = String.format("/v1.0/devices/token");
153 215 Long t = System.currentTimeMillis();
... ... @@ -163,7 +225,7 @@ public class TuYaYunController {
163 225  
164 226 ObjectMapper objectMapper = new ObjectMapper();
165 227 DeviceToken deviceToken = new DeviceToken();
166   - deviceToken.setUid(UId);
  228 + deviceToken.setUid(uid);
167 229 deviceToken.setTimeZoneId("Asia/ZheJiang");
168 230  
169 231 String jsonResult = "";
... ... @@ -177,7 +239,8 @@ public class TuYaYunController {
177 239 TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
178 240 return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
179 241 }
180   -
  242 + @ApiOperation("获取配网token下所有设备列表")
  243 + @ApiImplicitParam(name="token",value = "配网的Token",required = true)
181 244 @GetMapping("{token}/GetDevicesByToken")
182 245 public ServerResponse getDevicesByToken(@PathVariable("token") String token){
183 246  
... ... @@ -198,4 +261,95 @@ public class TuYaYunController {
198 261 TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
199 262 return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
200 263 }
  264 +
  265 +
  266 + @ApiOperation("根据category获取function列表(指令集)")
  267 + @ApiImplicitParam(name="category",value = "设备类型",required = true)
  268 + @GetMapping("getFunctionByType")
  269 + public ServerResponse getFunctionByType(String category){
  270 +
  271 + String apiUrl = String.format("/v1.0/functions/%s",category);
  272 + Long t = System.currentTimeMillis();
  273 + String access_Token = CacheHelper.getTuYaToken();
  274 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  275 + Map<String,String> map = new HashMap();
  276 + map.put("client_id", TuYaCloudService.ClientId);
  277 + map.put("access_token",access_Token);
  278 + map.put("sign",sign);
  279 + map.put("sign_method",TuYaCloudService.Sign_method);
  280 + map.put("t",t.toString());
  281 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  282 +
  283 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  284 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  285 + }
  286 +
  287 + @ApiOperation("获取设备支持指令集")
  288 + @ApiImplicitParam(name="deviceId",value = "设备ID",required = true)
  289 + @GetMapping("getDeviceSupportFun")
  290 + public ServerResponse getDeviceSupportFun(String deviceId){
  291 +
  292 + String apiUrl = String.format("/v1.0/devices/%s/functions",deviceId);
  293 + Long t = System.currentTimeMillis();
  294 + String access_Token = CacheHelper.getTuYaToken();
  295 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  296 + Map<String,String> map = new HashMap();
  297 + map.put("client_id", TuYaCloudService.ClientId);
  298 + map.put("access_token",access_Token);
  299 + map.put("sign",sign);
  300 + map.put("sign_method",TuYaCloudService.Sign_method);
  301 + map.put("t",t.toString());
  302 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  303 +
  304 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  305 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  306 + }
  307 +
  308 + @ApiOperation("下发设备控制命令")
  309 + @ApiImplicitParams({
  310 + @ApiImplicitParam(name="deviceId",value = "设备ID",required = true)
  311 +// @ApiImplicitParam(name="tuYaCommands",value = "命令集",required = true)
  312 + })
  313 + @PostMapping("sendDeviceCommandsFun")
  314 + public ServerResponse sendDeviceCommandsFun(String deviceId,@RequestBody TuYaCommand tuYaCommands){
  315 +
  316 + String apiUrl = String.format("/v1.0/devices/%s/commands",deviceId);
  317 + Long t = System.currentTimeMillis();
  318 + String access_Token = CacheHelper.getTuYaToken();
  319 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  320 + Map<String,String> map = new HashMap();
  321 + map.put("client_id", TuYaCloudService.ClientId);
  322 + map.put("access_token",access_Token);
  323 + map.put("sign",sign);
  324 + map.put("sign_method",TuYaCloudService.Sign_method);
  325 + map.put("t",t.toString());
  326 +
  327 + String body = JSON.toJSONString(tuYaCommands);
  328 + String result = ApiHelper.doPost(TuYaCloudService.TuYaOpenUrl + apiUrl,map,body);
  329 +
  330 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  331 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  332 + }
  333 +
  334 + @ApiOperation("根据设备ID移除设备")
  335 + @ApiImplicitParam(name="deviceId",value = "设备ID",required = true)
  336 + @DeleteMapping("removeDevice")
  337 + public ServerResponse removeDevice(String deviceId){
  338 +
  339 + String apiUrl = String.format("/v1.0/devices/%s",deviceId);
  340 + Long t = System.currentTimeMillis();
  341 + String access_Token = CacheHelper.getTuYaToken();
  342 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  343 +
  344 + Map<String,String> map = new HashMap();
  345 + map.put("client_id", TuYaCloudService.ClientId);
  346 + map.put("access_token",access_Token);
  347 + map.put("sign",sign);
  348 + map.put("sign_method",TuYaCloudService.Sign_method);
  349 + map.put("t",t.toString());
  350 + String result = ApiHelper.doDelete(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  351 +
  352 + TuYaResResult tuYaResResult = JSON.parseObject(result,TuYaResResult.class);
  353 + return ResultUtils.getInstance().returnResultTuYa(tuYaResResult);
  354 + }
201 355 }
... ...
springboot/src/main/java/com/sincre/springboot/controller/YinShiController.java
... ... @@ -26,7 +26,7 @@ import java.util.Map;
26 26  
27 27 @RestController
28 28 @RequestMapping("/YinShi")
29   -@Api(value = "YinShiController", tags = "萤石对接接口")
  29 +@Api(value = "YinShiController", tags = "用户管理和用户组权限管理以及设备管理接口")
30 30 public class YinShiController {
31 31  
32 32 @ApiOperation(value = "用于管理员获取accessToken")
... ... @@ -415,6 +415,7 @@ public class YinShiController {
415 415 @GetMapping("getDeviceCapacity")
416 416 public ServerResponse getDeviceCapacity(@RequestParam String deviceSerial) {
417 417  
  418 + System.out.println(YinShiServiceConfig.AccessToken);
418 419 String url = YinShiServiceConfig.HostUrl + "lapp/device/capacity";
419 420 Map<String, Object> map = new HashMap<>();
420 421 map.put("accessToken", YinShiServiceConfig.AccessToken);
... ...
springboot/src/main/java/com/sincre/springboot/utils/ApiHelper.java
... ... @@ -6,6 +6,7 @@ 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 8 import org.apache.http.client.methods.CloseableHttpResponse;
  9 +import org.apache.http.client.methods.HttpDelete;
9 10 import org.apache.http.client.methods.HttpGet;
10 11 import org.apache.http.client.methods.HttpPost;
11 12 import org.apache.http.entity.ContentType;
... ... @@ -72,6 +73,58 @@ public class ApiHelper {
72 73 return result;
73 74 }
74 75  
  76 +
  77 + public static String doDelete(String url,Map<String, String> headerParamMap) {
  78 + CloseableHttpClient httpClient = null;
  79 + CloseableHttpResponse response = null;
  80 + String result = "";
  81 + try {
  82 + // 通过址默认配置创建一个httpClient实例
  83 + httpClient = HttpClients.createDefault();
  84 + // 创建HttpDelete远程连接实例
  85 + HttpDelete httpDelete = new HttpDelete(url);
  86 + // 设置请求头信息,
  87 + for (Map.Entry<String, String> entry : headerParamMap.entrySet()) {
  88 + httpDelete.setHeader(entry.getKey(),entry.getValue());
  89 + }
  90 +
  91 + // 设置配置请求参数
  92 + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
  93 + .setConnectionRequestTimeout(35000)// 请求超时时间
  94 + .setSocketTimeout(60000)// 数据读取超时时间
  95 + .build();
  96 + // 为httpGet实例设置配置
  97 + httpDelete.setConfig(requestConfig);
  98 + // 执行get请求得到返回对象
  99 + response = httpClient.execute(httpDelete);
  100 + // 通过返回对象获取返回数据
  101 + HttpEntity entity = response.getEntity();
  102 + // 通过EntityUtils中的toString方法将结果转换为字符串
  103 + result = EntityUtils.toString(entity);
  104 + } catch (ClientProtocolException e) {
  105 + e.printStackTrace();
  106 + } catch (IOException e) {
  107 + e.printStackTrace();
  108 + } finally {
  109 + // 关闭资源
  110 + if (null != response) {
  111 + try {
  112 + response.close();
  113 + } catch (IOException e) {
  114 + e.printStackTrace();
  115 + }
  116 + }
  117 + if (null != httpClient) {
  118 + try {
  119 + httpClient.close();
  120 + } catch (IOException e) {
  121 + e.printStackTrace();
  122 + }
  123 + }
  124 + }
  125 + return result;
  126 + }
  127 +
75 128 /**
76 129 * 表单格式的post请求
77 130 *
... ...
springboot/src/main/java/com/sincre/springboot/utils/ResultUtils.java
1 1 package com.sincre.springboot.utils;
2 2  
3 3  
  4 +import com.alibaba.fastjson.JSON;
  5 +import com.alibaba.fastjson.JSONArray;
4 6 import com.sincre.springboot.ApiModel.TuYaResResult;
5 7 import com.sincre.springboot.ApiModel.YinShiResResult;
6 8 import com.sincre.springboot.common.ResponseCode;
... ... @@ -40,11 +42,11 @@ public class ResultUtils {
40 42 int code = yinShiResResult.getCode();
41 43  
42 44 if (code == 200) {
43   - String total = null;
  45 + int total = 0;
44 46 if (yinShiResResult.getPage() != null) {
45   - total = yinShiResResult.getPage().getTotal().toString();
  47 + total = yinShiResResult.getPage().getTotal();
46 48 }
47   - return ServerResponse.createBySuccess(total, ResponseCode.SUCCESS.getDesc(), yinShiResResult.getData());
  49 + return ServerResponse.createBySuccess(String.valueOf(total), ResponseCode.SUCCESS.getDesc(), yinShiResResult.getData());
48 50 } else {
49 51 if (code == 10002) {//accessToken过期或异常
50 52 CacheHelper.GetYinShiToken();
... ... @@ -72,7 +74,14 @@ public class ResultUtils {
72 74 boolean success = tuYaResResult.getSuccess();
73 75  
74 76 if (success) {
75   -
  77 + //data 返回的是数组
  78 + if(JSON.isValidArray(tuYaResResult.getResult().toString())){
  79 + int total;
  80 + JSONArray jsonArray = JSON.parseArray(tuYaResResult.getResult().toString());
  81 + total = jsonArray.size();
  82 + return ServerResponse.createBySuccess(String.valueOf(total), ResponseCode.SUCCESS.getDesc(), tuYaResResult.getResult());
  83 + }
  84 + //data 返回的是对象
76 85 return ServerResponse.createBySuccess(ResponseCode.SUCCESS.getDesc(), tuYaResResult.getResult());
77 86 }
78 87  
... ...