Commit b68ce5d06f25fc3aa80944da568ab1c62562da9f

Authored by chenjunyu_1481036421
1 parent 04bf5005

feat:萤石token获取验证

... ... @@ -9,13 +9,19 @@ import org.thingsboard.server.common.data.yunteng.utils.HttpClientUtils;
9 9 import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
10 10
11 11 import java.io.IOException;
  12 +import java.util.Date;
  13 +import java.util.HashMap;
  14 +import java.util.Map;
12 15
13 16 public class EzvizUtils {
14 17 //萤石开放平台API
15   - private static String url = "https://open.ys7.com/api/lapp/";
  18 + private static final String url = "https://open.ys7.com/api/lapp/";
  19 + private static Map<String,Map<String,Object>> token = new HashMap<>();
  20 + private static String accessToken;
  21 + private static Long expireTime;
16 22
17   - public static String getCameraPreviewURL( String appKey, String appSecret,String sn) throws IOException {
18   - String accessToken = getAccessTokenByUrl(appKey,appSecret);
  23 + public static String getCameraPreviewURL(String appKey, String appSecret, String sn) throws IOException {
  24 + getAccessTokenByUrl(appKey,appSecret);
19 25 String cameraPreviewURL = url+"v2/live/address/get?accessToken="+accessToken+
20 26 "&deviceSerial="+sn+"&protocol=4&quality=1";
21 27 //调用接口
... ... @@ -44,14 +50,30 @@ public class EzvizUtils {
44 50 }
45 51
46 52 /** 获取token */
47   - private static String getAccessTokenByUrl(String appKey, String appSecret) {
  53 + private static void getAccessTokenByUrl(String appKey, String appSecret) {
  54 + //查询是否有当前key的token
  55 + Map<String,Object> thisToken = token.get(appKey);
  56 + if(thisToken!=null){
  57 + expireTime = (Long) thisToken.get("expireTime");
  58 + Date now = new Date();
  59 + if(now.getTime()<expireTime){
  60 + accessToken = thisToken.get("accessToken").toString();
  61 + return;
  62 + }
  63 + }
  64 +
48 65 try {
49 66 String tokenUrl = url+"token/get?appKey="+appKey+"&appSecret="+appSecret;
50 67 String result = HttpClientUtils.sendPOST(tokenUrl,"");
51 68 JsonNode json = JacksonUtil.toJsonNode(result);
52 69 if(json.get("code").asText().equals("200")){
53 70 json = json.get("data");
54   - return json.get("accessToken").textValue();
  71 + accessToken = json.get("accessToken").textValue();
  72 + expireTime = json.get("expireTime").longValue();
  73 + Map<String,Object> newToken = new HashMap<>();
  74 + newToken.put("accessToken",accessToken);
  75 + newToken.put("expireTime",expireTime);
  76 + token.put(appKey,newToken);
55 77 }else{
56 78 String errorCode = json.get("code").asText();
57 79 String msg = json.get("msg").asText();
... ...
... ... @@ -93,7 +93,7 @@ public class TkVideoServiceImpl extends AbstractBaseService<TkVideoMapper, TkVid
93 93 }
94 94
95 95 @Override
96   - public String getCameraPreviewURL(boolean isTenantAdmin, String customerUserId, String tenantId, String entityId,
  96 + public String getCameraPreviewURL(boolean isTenantAdmin, String customerUserId, String tenantId, String entityId,
97 97 ProtocolType protocolType) throws IOException {
98 98 if (isTenantAdmin) {
99 99 customerUserId = null;
... ...