Commit 0a28faf7808b7995c13fff64e718eb61a960f20a

Authored by baishou
1 parent 241d25d8
Exists in master

获取Token代码整合,加入刷新Token的方法

springboot/src/main/java/com/sincre/springboot/common/Encript.java
... ... @@ -37,7 +37,6 @@ public class Encript {
37 37 sha256_HMAC.init(secret_key);
38 38 byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
39 39 hash = byteArrayToHexString(bytes);
40   - System.out.println(hash);
41 40 } catch (Exception e) {
42 41 System.out.println("Error HmacSHA256 ===========" + e.getMessage());
43 42 }
... ...
springboot/src/main/java/com/sincre/springboot/controller/TuYaYunController.java
... ... @@ -2,12 +2,14 @@ package com.sincre.springboot.controller;
2 2  
3 3 import com.fasterxml.jackson.core.type.TypeReference;
4 4 import com.fasterxml.jackson.databind.ObjectMapper;
5   -import com.sincre.springboot.ApiModel.*;
  5 +import com.sincre.springboot.ApiModel.DeviceToken;
  6 +import com.sincre.springboot.ApiModel.TuYaDevicesList;
  7 +import com.sincre.springboot.ApiModel.TuYaResResult;
  8 +import com.sincre.springboot.ApiModel.UserInfo;
6 9 import com.sincre.springboot.ApiPlatform.TuYaCloudService;
7   -import com.sincre.springboot.common.EhcacheUtil;
8 10 import com.sincre.springboot.utils.ApiHelper;
  11 +import com.sincre.springboot.utils.CacheHelper;
9 12 import io.swagger.annotations.Api;
10   -import org.apache.commons.lang3.StringUtils;
11 13 import org.springframework.web.bind.annotation.*;
12 14  
13 15 import java.util.HashMap;
... ... @@ -18,42 +20,12 @@ import java.util.Map;
18 20 @Api(value = "涂鸦云服务API调用")
19 21 public class TuYaYunController {
20 22  
21   - @GetMapping("GetToken")
22   - public TuYaResResult getTuYaToken(){
23   -
24   - String apiUrl = "/v1.0/token?grant_type=1";
25   - Long t = System.currentTimeMillis();
26   - //Token获取前的签名
27   - String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret);
28   - Map<String,String> map = new HashMap();
29   - map.put("client_id", TuYaCloudService.ClientId);
30   - map.put("sign",sign);
31   - map.put("sign_method",TuYaCloudService.Sign_method);
32   - map.put("t",t.toString());
33   - String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
34   -
35   - TuYaResResult<TuYaResTokenObj> resResult = new TuYaResResult<>();
36   - try{
37   - ObjectMapper objectMapper = new ObjectMapper();
38   - resResult = objectMapper.readValue(result,new TypeReference<TuYaResResult<TuYaResTokenObj>>(){});
39   -
40   - String access_Token = resResult.getResult().getAccess_token(); //存储第一次获取的Token
41   - EhcacheUtil ehcacheUtil = new EhcacheUtil();
42   - ehcacheUtil.setex("TuYa_Token",access_Token,60);
43   - } catch(Exception ex){
44   - System.out.println(ex.getMessage());
45   - }
46   -
47   - return resResult;
48   - }
49   -
50 23 @GetMapping("GetDevicesInfo")
51 24 public TuYaResResult getTuYaDevicesInfo(){
52 25  
53   - EhcacheUtil ehcacheUtil = new EhcacheUtil();
54 26 String apiUrl = "/v1.0/devices?device_ids=";
55 27 Long t = System.currentTimeMillis();
56   - String access_Token = ehcacheUtil.get("TuYa_Token");
  28 + String access_Token = CacheHelper.getTuYaToken();
57 29 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
58 30 Map<String,String> map = new HashMap();
59 31 map.put("client_id", TuYaCloudService.ClientId);
... ... @@ -68,7 +40,6 @@ public class TuYaYunController {
68 40 ObjectMapper objectMapper = new ObjectMapper();
69 41 TypeReference<TuYaResResult<TuYaDevicesList>> reference = new TypeReference<TuYaResResult<TuYaDevicesList>>(){};
70 42 resResult = objectMapper.readValue(result,reference);
71   - System.out.println(resResult.getResult().total);
72 43 } catch(Exception ex){
73 44 System.out.println(ex.getMessage());
74 45 }
... ... @@ -88,12 +59,9 @@ public class TuYaYunController {
88 59 @GetMapping("{AppId}/GetTuYaAPPUser")
89 60 public String getTuYaAPPUser(@PathVariable("AppId") String AppId, @RequestParam int pageIndex, @RequestParam int pageSize){
90 61  
91   - EhcacheUtil ehcacheUtil = new EhcacheUtil();
92   -
93 62 String apiUrl = String.format("/v1.0/apps/%s/users?page_no=%d&page_size=%d",AppId,pageIndex,pageSize);//iotshunzhi
94 63 Long t = System.currentTimeMillis();
95   - String access_Token = ehcacheUtil.get("TuYa_Token");
96   - System.out.println(access_Token);
  64 + String access_Token = CacheHelper.getTuYaToken();
97 65 //Token获取后的签名
98 66 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
99 67 Map<String,String> headMap = new HashMap();
... ... @@ -112,11 +80,9 @@ public class TuYaYunController {
112 80 @PostMapping("{AppId}/AddTuYaAPPUser")
113 81 public String addTuYaAPPUser(@PathVariable("AppId") String AppId,@RequestBody UserInfo userInfo){
114 82  
115   - EhcacheUtil ehcacheUtil = new EhcacheUtil();
116   -
117 83 String apiUrl = String.format("/v1.0/apps/%s/user",AppId);//应用标识iotshunzhi
118 84 Long t = System.currentTimeMillis();
119   - String access_Token = ehcacheUtil.get("TuYa_Token");
  85 + String access_Token = CacheHelper.getTuYaToken();
120 86 //Token获取后的签名
121 87 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
122 88 Map<String,String> headMap = new HashMap();
... ... @@ -131,7 +97,6 @@ public class TuYaYunController {
131 97 String jsonResult = "";
132 98 try {
133 99 jsonResult = objectMapper.writeValueAsString(userInfo);
134   - System.out.println(jsonResult);
135 100 }catch (com.fasterxml.jackson.core.JsonProcessingException ex){
136 101 ex.printStackTrace();
137 102 }
... ... @@ -144,20 +109,41 @@ public class TuYaYunController {
144 109 }
145 110  
146 111 /**
  112 + * 根据用户ID来获取用户账号下的所有设备
  113 + * @param uid
  114 + * @return
  115 + */
  116 + @GetMapping("{uid}/GetDevicesByUid")
  117 + public String getDevicesByUid(@PathVariable("uid") String uid){
  118 +
  119 + String apiUrl = String.format("/v1.0/users/%s/devices",uid);
  120 + Long t = System.currentTimeMillis();
  121 + String access_Token = CacheHelper.getTuYaToken();
  122 + //Token获取后的签名
  123 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  124 + Map<String,String> map = new HashMap();
  125 + map.put("client_id", TuYaCloudService.ClientId);
  126 + map.put("access_token",access_Token);
  127 + map.put("sign",sign);
  128 + map.put("sign_method",TuYaCloudService.Sign_method);
  129 + map.put("t",t.toString());
  130 +
  131 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  132 +
  133 + return result;
  134 + }
  135 + /**
147 136 * 生成配网的Token
148 137 * @return
149 138 */
150 139 @GetMapping("AddDevicesToken")
151 140 public String addDevicesToken(@RequestParam String UId){
152 141  
153   - EhcacheUtil ehcacheUtil = new EhcacheUtil();
154   -
155 142 String apiUrl = String.format("/v1.0/devices/token");
156 143 Long t = System.currentTimeMillis();
157   - String access_Token = ehcacheUtil.get("TuYa_Token");
  144 + String access_Token = CacheHelper.getTuYaToken();
158 145 //Token获取后的签名
159 146 String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
160   - System.out.println("签名"+sign);
161 147 Map<String,String> map = new HashMap();
162 148 map.put("client_id", TuYaCloudService.ClientId);
163 149 map.put("access_token",access_Token);
... ... @@ -165,10 +151,6 @@ public class TuYaYunController {
165 151 map.put("sign_method",TuYaCloudService.Sign_method);
166 152 map.put("t",t.toString());
167 153  
168   - if(StringUtils.isNotBlank(UId)){
169   - UId = "ay1557735327941UvZAG";
170   - }
171   -
172 154 ObjectMapper objectMapper = new ObjectMapper();
173 155 DeviceToken deviceToken = new DeviceToken();
174 156 deviceToken.setUid(UId);
... ... @@ -177,7 +159,6 @@ public class TuYaYunController {
177 159 String jsonResult = "";
178 160 try {
179 161 jsonResult = objectMapper.writeValueAsString(deviceToken);
180   - System.out.println(jsonResult);
181 162 }catch (com.fasterxml.jackson.core.JsonProcessingException ex){
182 163 ex.printStackTrace();
183 164 }
... ... @@ -185,4 +166,24 @@ public class TuYaYunController {
185 166  
186 167 return result;
187 168 }
  169 +
  170 + @GetMapping("{token}/GetDevicesByToken")
  171 + public String getDevicesByToken(@PathVariable("token") String token){
  172 +
  173 + String apiUrl = String.format("/v1.0/devices/tokens/%s",token);
  174 + Long t = System.currentTimeMillis();
  175 + String access_Token = CacheHelper.getTuYaToken();
  176 + //Token获取后的签名
  177 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+access_Token+t,TuYaCloudService.Secret);
  178 + Map<String,String> map = new HashMap();
  179 + map.put("client_id", TuYaCloudService.ClientId);
  180 + map.put("access_token",access_Token);
  181 + map.put("sign",sign);
  182 + map.put("sign_method",TuYaCloudService.Sign_method);
  183 + map.put("t",t.toString());
  184 +
  185 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  186 +
  187 + return result;
  188 + }
188 189 }
... ...
springboot/src/main/java/com/sincre/springboot/utils/CacheHelper.java 0 → 100644
... ... @@ -0,0 +1,94 @@
  1 +package com.sincre.springboot.utils;
  2 +
  3 +import com.fasterxml.jackson.core.type.TypeReference;
  4 +import com.fasterxml.jackson.databind.ObjectMapper;
  5 +import com.sincre.springboot.ApiModel.TuYaResResult;
  6 +import com.sincre.springboot.ApiModel.TuYaResTokenObj;
  7 +import com.sincre.springboot.ApiPlatform.TuYaCloudService;
  8 +import com.sincre.springboot.common.EhcacheUtil;
  9 +import org.apache.commons.lang3.StringUtils;
  10 +
  11 +import java.util.HashMap;
  12 +import java.util.Map;
  13 +
  14 +public class CacheHelper {
  15 +
  16 + static EhcacheUtil ehcacheUtil = new EhcacheUtil();
  17 + static String TuYaTokenKey = "TuYa_Token";
  18 + static String TuYaRefreshTokenKey = "TuYa_Re_Token";
  19 + private static void setTuYaToken(){
  20 +
  21 + String apiUrl = "/v1.0/token?grant_type=1";
  22 + Long t = System.currentTimeMillis();
  23 + //Token获取前的签名
  24 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret);
  25 + Map<String,String> map = new HashMap();
  26 + map.put("client_id", TuYaCloudService.ClientId);
  27 + map.put("sign",sign);
  28 + map.put("sign_method",TuYaCloudService.Sign_method);
  29 + map.put("t",t.toString());
  30 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  31 +
  32 + TuYaResResult<TuYaResTokenObj> resResult ;
  33 + try{
  34 + ObjectMapper objectMapper = new ObjectMapper();
  35 + resResult = objectMapper.readValue(result,new TypeReference<TuYaResResult<TuYaResTokenObj>>(){});
  36 +
  37 + String access_Token = resResult.getResult().getAccess_token(); //存储第一次获取的Token
  38 + String refresh_Token = resResult.getResult().getRefresh_token();
  39 + // 第一次缓存数据的时候,不刷新,后期刷新
  40 + if(StringUtils.isNotBlank(ehcacheUtil.get(TuYaTokenKey))){
  41 + TuYaRefresh(resResult.getResult());
  42 + }else {
  43 + ehcacheUtil.setex(TuYaTokenKey, access_Token, resResult.getResult().getExpire_time()); // 储存二个小时
  44 + ehcacheUtil.setex(TuYaRefreshTokenKey,refresh_Token,60 * 60); // 储存一个小时
  45 + System.out.println("获取Token:"+ access_Token);
  46 + System.out.println("刷新Token:"+ refresh_Token);
  47 +
  48 + }
  49 + } catch(Exception ex){
  50 + System.out.println(ex.getMessage());
  51 + }
  52 + }
  53 +
  54 + /**
  55 + * 获取涂鸦Api 访问的Token
  56 + * @return
  57 + */
  58 + public static String getTuYaToken(){
  59 +
  60 + if(StringUtils.isBlank(ehcacheUtil.get(TuYaRefreshTokenKey))){
  61 + setTuYaToken();
  62 + }
  63 +
  64 + return ehcacheUtil.get(TuYaTokenKey);
  65 + }
  66 +
  67 + private static void TuYaRefresh(TuYaResTokenObj tuYaResTokenObj){
  68 +
  69 + String apiUrl = "/v1.0/token/"+tuYaResTokenObj.getRefresh_token();
  70 + Long t = System.currentTimeMillis();
  71 +
  72 + String sign = TuYaCloudService.createSign(TuYaCloudService.ClientId+t,TuYaCloudService.Secret);
  73 + Map<String,String> map = new HashMap();
  74 + map.put("client_id", TuYaCloudService.ClientId);
  75 + map.put("sign",sign);
  76 + map.put("sign_method",TuYaCloudService.Sign_method);
  77 + map.put("t",t.toString());
  78 + String result = ApiHelper.doGet(TuYaCloudService.TuYaOpenUrl + apiUrl,map);
  79 +
  80 + TuYaResResult<TuYaResTokenObj> resResult;
  81 + try{
  82 + ObjectMapper objectMapper = new ObjectMapper();
  83 + TypeReference<TuYaResResult<TuYaResTokenObj>> reference = new TypeReference<TuYaResResult<TuYaResTokenObj>>(){};
  84 + resResult = objectMapper.readValue(result,reference);
  85 + String re_access_Token = resResult.getResult().getAccess_token(); //重新刷新后获取的Token
  86 + String refresh_Token = resResult.getResult().getRefresh_token();
  87 + System.out.println("刷新后的Token:"+re_access_Token);
  88 + ehcacheUtil.setex(TuYaTokenKey,re_access_Token,resResult.getResult().getExpire_time()); // 储存两个小时
  89 + ehcacheUtil.setex(TuYaRefreshTokenKey,refresh_Token,60 * 60); //储存一个小时
  90 + } catch(Exception ex){
  91 + System.out.println(ex.getMessage());
  92 + }
  93 + }
  94 +}
... ...