| | |
| | | |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | |
| | | |
| | | |
| | | |
| | |
| | | * @param type 0=服务端,1=乘客,2=司机 |
| | | * @return |
| | | */ |
| | | public String fleetEngineAuth(int type, Integer id){ |
| | | public Map<String, Object> fleetEngineAuth(int type, Integer id){ |
| | | try { |
| | | //谷歌云服务器使用这部分代码 |
| | | // AuthTokenMinter minter = AuthTokenMinter.builder() |
| | |
| | | .build(); |
| | | |
| | | String jwt = ""; |
| | | Long expirationTimestamp = 0L; |
| | | if(0 == type){ |
| | | FleetEngineToken serverToken = minter.getServerToken(); |
| | | jwt = serverToken.jwt(); |
| | | expirationTimestamp = serverToken.expirationTimestamp().getTime(); |
| | | } |
| | | if(1 == type){ |
| | | FleetEngineToken consumerToken = minter.getConsumerToken(TripClaims.create("I-GO-USER" + id)); |
| | | jwt = consumerToken.jwt(); |
| | | expirationTimestamp = consumerToken.expirationTimestamp().getTime(); |
| | | } |
| | | if(2 == type){ |
| | | FleetEngineToken driverToken = minter.getDriverToken(VehicleClaims.create("I-GO-CAR" + id)); |
| | | jwt = driverToken.jwt(); |
| | | expirationTimestamp = driverToken.expirationTimestamp().getTime(); |
| | | } |
| | | return jwt; |
| | | Map<String, Object> map = new HashMap<>(); |
| | | map.put("token", jwt); |
| | | map.put("expirationTimestamp", expirationTimestamp); |
| | | return map; |
| | | } catch (SigningTokenException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public String getToken(){ |
| | | String token_json = redisUtil.getValue("google_token"); |
| | | String google_token = ""; |
| | | if(!StringUtils.hasLength(token_json)){ |
| | | Map<String, Object> map = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", JSON.toJSONString(map)); |
| | | google_token = map.get("token").toString(); |
| | | }else{ |
| | | JSONObject jsonObject1 = JSON.parseObject(token_json); |
| | | Long expirationTimestamp = jsonObject1.getLong("expirationTimestamp"); |
| | | google_token = jsonObject1.getString("token"); |
| | | if((expirationTimestamp - 10000L) <= System.currentTimeMillis()){ |
| | | Map<String, Object> map = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", JSON.toJSONString(map)); |
| | | google_token = map.get("token").toString(); |
| | | } |
| | | } |
| | | return google_token; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | |
| | | * @param id 车辆id |
| | | */ |
| | | public String createVehicles(int maximumCapacity, String licensePlate, String id) throws Exception{ |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles?vehicleId=" + id; |
| | | HttpRequest post = HttpUtil.createPost(url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | post.addHeaders(headers); |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public String updateVehicles(String vehicleState, Integer maximumCapacity, String licensePlate, String id) throws Exception{ |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles/" + id + "?updateMask="; |
| | | List<String> sb = new ArrayList<>(); |
| | | if(StringUtils.hasLength(vehicleState)){ |
| | |
| | | |
| | | HttpRequest put = HttpUtil.createRequest(Method.PUT, url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | put.addHeaders(headers); |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public String getVehicles(String id) throws Exception{ |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/vehicles/" + (null != id ? id : ""); |
| | | HttpRequest get = HttpUtil.createGet(url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | get.addHeaders(headers); |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public String createTrip(String vehicleId, Integer numberOfPassengers, String tripId, String start_lat, String start_lng, String end_lat, String end_lng) throws Exception{ |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips?tripId=" + tripId; |
| | | HttpRequest post = HttpUtil.createPost(url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | post.addHeaders(headers); |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public String updateTrip(String tripStatus, String vehicleId, Integer numberOfPassengers, String tripId, String start_lat, String start_lng, String end_lat, String end_lng) throws Exception { |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips/" + tripId + "?updateMask="; |
| | | List<String> sb = new ArrayList<>(); |
| | | if(StringUtils.hasLength(vehicleId)){ |
| | |
| | | |
| | | HttpRequest put = HttpUtil.createRequest(Method.PUT, url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | put.addHeaders(headers); |
| | | |
| | |
| | | * @return |
| | | */ |
| | | public String getTrip(String tripId) throws Exception { |
| | | String google_token = redisUtil.getValue("google_token"); |
| | | if(!StringUtils.hasLength(google_token)){ |
| | | google_token = fleetEngineAuth(0, null); |
| | | redisUtil.setStrValue("google_token", google_token); |
| | | } |
| | | String url = "https://fleetengine.googleapis.com/v1/providers/" + provider + "/trips/" + tripId; |
| | | HttpRequest get = HttpUtil.createGet(url); |
| | | Map<String, String> headers = new HashMap<>(); |
| | | headers.put("Authorization", "Bearer " + google_token); |
| | | headers.put("Authorization", "Bearer " + getToken()); |
| | | headers.put("Content-Type", "application/json"); |
| | | get.addHeaders(headers); |
| | | |