|
@@ -9,13 +9,19 @@ import org.thingsboard.server.common.data.yunteng.utils.HttpClientUtils; |
|
@@ -9,13 +9,19 @@ import org.thingsboard.server.common.data.yunteng.utils.HttpClientUtils; |
9
|
import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
|
9
|
import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
|
10
|
|
10
|
|
11
|
import java.io.IOException;
|
11
|
import java.io.IOException;
|
|
|
12
|
+import java.util.Date;
|
|
|
13
|
+import java.util.HashMap;
|
|
|
14
|
+import java.util.Map;
|
12
|
|
15
|
|
13
|
public class EzvizUtils {
|
16
|
public class EzvizUtils {
|
14
|
//萤石开放平台API
|
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
|
String cameraPreviewURL = url+"v2/live/address/get?accessToken="+accessToken+
|
25
|
String cameraPreviewURL = url+"v2/live/address/get?accessToken="+accessToken+
|
20
|
"&deviceSerial="+sn+"&protocol=4&quality=1";
|
26
|
"&deviceSerial="+sn+"&protocol=4&quality=1";
|
21
|
//调用接口
|
27
|
//调用接口
|
|
@@ -44,14 +50,30 @@ public class EzvizUtils { |
|
@@ -44,14 +50,30 @@ public class EzvizUtils { |
44
|
}
|
50
|
}
|
45
|
|
51
|
|
46
|
/** 获取token */
|
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
|
try {
|
65
|
try {
|
49
|
String tokenUrl = url+"token/get?appKey="+appKey+"&appSecret="+appSecret;
|
66
|
String tokenUrl = url+"token/get?appKey="+appKey+"&appSecret="+appSecret;
|
50
|
String result = HttpClientUtils.sendPOST(tokenUrl,"");
|
67
|
String result = HttpClientUtils.sendPOST(tokenUrl,"");
|
51
|
JsonNode json = JacksonUtil.toJsonNode(result);
|
68
|
JsonNode json = JacksonUtil.toJsonNode(result);
|
52
|
if(json.get("code").asText().equals("200")){
|
69
|
if(json.get("code").asText().equals("200")){
|
53
|
json = json.get("data");
|
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
|
}else{
|
77
|
}else{
|
56
|
String errorCode = json.get("code").asText();
|
78
|
String errorCode = json.get("code").asText();
|
57
|
String msg = json.get("msg").asText();
|
79
|
String msg = json.get("msg").asText();
|