Commit 0e8cec42 by yanHeng

[初始化] wuLink对接

parent f1a6e64a
......@@ -67,6 +67,8 @@ ext {
micrometerRegistryVersion = "1.0.4"
poiTlVersion = "1.5.0"
cxfVersion = "3.1.6"
websocketVersion = "1.5.2"
lib = [
/* jiuqi next platform */
np_authz: "com.jiuqi:np.authz:${npAuthzVersion}",
......@@ -251,7 +253,9 @@ ext {
hibernate_common_annotations: "org.hibernate.common:hibernate-commons-annotations:5.0.4.Final",
net_byte_buddy: "net.bytebuddy:byte-buddy:1.8.15",
thumbnailator: "net.coobird:thumbnailator:0.4.8"
thumbnailator: "net.coobird:thumbnailator:0.4.8",
websocket: "org.java-websocket:Java-WebSocket:${websocketVersion}"
]
}
......@@ -413,6 +417,10 @@ configurations.all {
force lib.net_byte_buddy
force lib.thumbnailator
/* websocket */
force lib.websocket
exclude group: "c3p0", module: "c3p0"
exclude group: "dom4j", module: "dom4j"
exclude group: "com.vaadin.external.google", module: "android-json"
......
......@@ -3,6 +3,7 @@ package com.beecode.inz.common.util;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang.StringUtils;
import java.nio.charset.StandardCharsets;
......@@ -56,10 +57,10 @@ public class GpsWuLingUtil {
* @throws Exception
*/
public static String getTokenByAccount() throws Exception {
// String token = RedisUtils.getInstance().get(getGpsWuLingTokenRedisKey(CLIET_ID, CLIENT_SECRET));
// if(StringUtils.isNotEmpty(token)){
// return token;
// }else {
String token = RedisUtils.getInstance().get(getGpsWuLingTokenRedisKey(CLIET_ID, CLIENT_SECRET));
if(StringUtils.isNotEmpty(token)){
return token;
}else {
String sendParam = "grant_type="+GRANT_TYPE + "&client_id=" + CLIET_ID + "&client_secret=" + CLIENT_SECRET;
String sendPost = HttpSendUtil.sendPost(TOKEN_URL, sendParam, StandardCharsets.UTF_8.name());
......@@ -71,7 +72,7 @@ public class GpsWuLingUtil {
}else {
logger.error("请求GPSWuLink获取token出错,错误信息"+postStr.get("msg"));
}
// }
}
return null;
}
......
......@@ -8,6 +8,7 @@ dependencies {
compile lib.jackson_datatype_jdk8
compile lib.jackson_datatype_jsr310
compile lib.json
compile lib.websocket
compile "com.beecode:bap2.participant:${aminoVersion}"
compile "com.beecode:bcp.org:${aminoVersion}"
compile "com.beecode:bap2.department:${aminoVersion}"
......
package com.xyst.dinas.camera.constant;
import com.beecode.inz.common.util.HttpSendUtil;
import com.beecode.inz.common.util.RedisUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
/**
* Description:IOT平台对接工具类
*
* @author 孟庆鑫
* @version 1.0
* @date: 2021/7/13 15:06
*/
@Component
public class IotPlatformUtils {
public static String getGpsWuLingTokenRedisKey(String clietId, String clientSecret) {
return "gps_wu_link_token_key" + clietId + "_" + clientSecret;
}
//获取token必填参数 如下三个
private static final String GRANT_TYPE = "client_credentials";
private static final String CLIET_ID = "jt808";
private static final String CLIENT_SECRET = "f04dd1e4";
private static final String GPS_WU_LING_DOMAIN = "http://dev.kunlun.cloud/iot/";
/**
* 获取tokenUrl
*/
private static final String TOKEN_URL = GPS_WU_LING_DOMAIN +"oauth/token";
/**
* 根据设备序列号获取id
*/
private static final String FIND_GATEWAY_ID_URL = GPS_WU_LING_DOMAIN +"gateway/api/findGatewayId";
/**
* 根据设备id查询⽹关指标信息
*/
private static final String GATEWAYINDICATORS_URL = GPS_WU_LING_DOMAIN +"gateway/api/gatewayIndicators";
/**
* 历史轨迹URL
*/
private static final String HISTORY_URL = GPS_WU_LING_DOMAIN +"devices/history";
/**
* Description:获取平台accesstoken
*
* @author 孟庆鑫
* @date 2021/7/13 16:45
* @return: 0:请求正常 -1:clientId或clientSecret为空 -2:请求accessToken服务器异常
*/
public HashMap<String, Object> getAuthToken(String clientId, String clientSecret)throws Exception {
HashMap<String, Object> map = new HashMap<>();
if (StringUtils.isBlank(clientId) || StringUtils.isBlank(clientSecret)) {
//clientId和clientSecret不能为空
map.put("status", -1);
map.put("message", "当前项目未注册IOT平台");
return map;
}
String accessTokenData = null;
try {
accessTokenData = RedisUtils.getInstance().get(getGpsWuLingTokenRedisKey(CLIET_ID, CLIENT_SECRET));
} catch (Exception e) {
e.printStackTrace();
}
//优先从redis缓存拿
if (StringUtils.isNotBlank(accessTokenData)) {
map.put("status", 0);
map.put("data", accessTokenData);
return map;
}
String sendParam = "grant_type="+GRANT_TYPE + "&client_id=" + CLIET_ID + "&client_secret=" + CLIENT_SECRET;
String sendPost = HttpSendUtil.sendPost(TOKEN_URL, sendParam, StandardCharsets.UTF_8.name());
JSONObject postStr = new JSONObject(sendPost);
String accessToken = postStr.getString("access_token");
if (accessToken != null) {
RedisUtils.getInstance().set(getGpsWuLingTokenRedisKey(CLIET_ID, CLIENT_SECRET),accessToken,postStr.getInt("expires_in")-3);
map.put("status", 0);
map.put("data", accessToken);
return map;
} else {
//获取accesstoken异常
map.put("status", -2);
map.put("message", "IOT平台返回错误");
return map;
}
}
}
package com.xyst.dinas.camera.web;
import com.beecode.inz.basis.team.pojo.ResponseObj;
import com.xyst.dinas.camera.constant.IotPlatformUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* Description:对接IOT平台controller
* @param
* @author yanHeng
* @version 1.0
* @date: 2021/7/15 9:37
*/
@RestController
@RequestMapping("/iotplatform")
public class IotPlatformController {
//获取token必填参数 如下三个
private static final String GRANT_TYPE = "client_credentials";
private static final String CLIET_ID = "jt808";
private static final String CLIENT_SECRET = "f04dd1e4";
@Autowired
private IotPlatformUtils iotPlatformUtils;
/**Description:从IOT平台获取access_token
* @Author yanHeng
* @Date 2022/4/24 18:25
* @Param []
* @return com.beecode.inz.basis.team.pojo.ResponseObj
**/
@GetMapping("/getAccessToken")
public ResponseObj getAccessToken() throws Exception{
HashMap<String, Object> tokenMap = iotPlatformUtils.getAuthToken(CLIET_ID, CLIENT_SECRET);
if ((int) tokenMap.get("status") == -1 || (int) tokenMap.get("status") == -2) {
return ResponseObj.success("获取失败",tokenMap.get("message").toString());
} else {
String token = tokenMap.get("data").toString();
return ResponseObj.success("获取成功",token);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment