|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.alibaba.fastjson.TypeReference; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.HKConstants; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.BaseCarsPageResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.BaseCarsResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.request.CarsAlarmListRequest; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.request.CarsDeviceRequest; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.request.CarsGpsRequest; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.response.CarsDeviceDetaisResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.response.CarsDeviceResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.response.CarsGpsResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.cars.response.*; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.BaseListPageResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.BaseResponse; | 
|---|
|  |  |  | import com.doumee.core.haikang.model.param.respose.FindHomeAlarmInfoPageResponse; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.doumee.core.utils.DateUtil; | 
|---|
|  |  |  | import com.google.common.collect.Maps; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.http.client.config.RequestConfig; | 
|---|
|  |  |  | import org.apache.http.client.methods.CloseableHttpResponse; | 
|---|
|  |  |  | import org.apache.http.client.methods.HttpGet; | 
|---|
|  |  |  | import org.apache.http.impl.client.CloseableHttpClient; | 
|---|
|  |  |  | 
|---|
|  |  |  | private static final String DEFAULT_CHARSET  = "UTF-8"; | 
|---|
|  |  |  | private static final String REGION_ID = "cn-hangzhou"; | 
|---|
|  |  |  | private static final String VERSION = "2.1.0"; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static  List<CarsDeviceDetaisResponse> HK_CARS_LIST=  null; | 
|---|
|  |  |  | public static   String BASE_URL = "https://open.hikvisionauto.com:14021/v2/"; | 
|---|
|  |  |  | private static TreeMap<String, String> getBaseParams()   { | 
|---|
|  |  |  | Map<String, String> params = Maps.newHashMap(); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String sign(String accessSecret, TreeMap<String, String> params, HttpMethod method) throws Exception { | 
|---|
|  |  |  | String stringToSign = getStringToSign(params, method); | 
|---|
|  |  |  | System.out.println("StringToSign = [" + stringToSign + "]"); | 
|---|
|  |  |  | javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1"); | 
|---|
|  |  |  | mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes(DEFAULT_CHARSET), "HmacSHA1")); | 
|---|
|  |  |  | byte[] signData = mac.doFinal(stringToSign.getBytes(DEFAULT_CHARSET)); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | getAllCarsDetais(); | 
|---|
|  |  |  | //      getAllCarsDetais(); | 
|---|
|  |  |  | CarsAlarmListRequest param = new CarsAlarmListRequest(); | 
|---|
|  |  |  | param.setStartTime(DateUtil.getPlusTime2(DateUtil.getStartOfDay(new Date()))); | 
|---|
|  |  |  | param.setEndTime(DateUtil.getPlusTime2(DateUtil.getEndOfDay(new Date()))); | 
|---|
|  |  |  | param.setPageNo(1); | 
|---|
|  |  |  | param.setPageSize(100); | 
|---|
|  |  |  | param.setDeviceCode("15954209015"); | 
|---|
|  |  |  | BaseCarsPageResponse<CarsAlarmListResponse>  data = getAlarmList(param); | 
|---|
|  |  |  | System.out.println(JSONObject.toJSONString(data)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static List<CarsDeviceDetaisResponse> getAllCarsDetais() { | 
|---|
|  |  |  | 
|---|
|  |  |  | List<String> cars = new ArrayList<>(); | 
|---|
|  |  |  | List<String> codes = new ArrayList<>(); | 
|---|
|  |  |  | for(CarsDeviceResponse model :data.getResults()){ | 
|---|
|  |  |  | //            System.out.println("=================车牌号:"+model.getPlateNum()); | 
|---|
|  |  |  | System.out.println(model.getTerminalID()+"=================车牌号:"+model.getPlateNum()); | 
|---|
|  |  |  | cars.add(model.getPlateNum()); | 
|---|
|  |  |  | codes.add(model.getTerminalID()); | 
|---|
|  |  |  | CarsDeviceDetaisResponse t = new CarsDeviceDetaisResponse(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for(CarsDeviceDetaisResponse m  : list){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println("=================车牌号:"+m.getPlateNum()+" 状态:【"+m.getStatus()+"】"+" 位置:【"+m.getLongitude()+","+m.getLatitude()+"】"); | 
|---|
|  |  |  | System.out.println("=================车牌号:"+m.getPlateNum()+" 状态:【"+m.getTerminalID()+"】"+" 位置:【"+m.getLongitude()+","+m.getLatitude()+"】"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static List<CarsAlarmResultListResponse> getAlarmEvemtList(List<CarsDeviceDetaisResponse> detaisResponses ,  Date date) { | 
|---|
|  |  |  | List<CarsAlarmResultListResponse> list = new ArrayList<>(); | 
|---|
|  |  |  | if(detaisResponses ==null || detaisResponses.size()==0){ | 
|---|
|  |  |  | return list ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | CarsAlarmListRequest param = new CarsAlarmListRequest(); | 
|---|
|  |  |  | param.setStartTime(DateUtil.getPlusTime2(DateUtil.getStartOfDay(date))); | 
|---|
|  |  |  | param.setEndTime(DateUtil.getPlusTime2(DateUtil.getEndOfDay(date))); | 
|---|
|  |  |  | param.setPageNo(1); | 
|---|
|  |  |  | param.setPageSize(100); | 
|---|
|  |  |  | for( CarsDeviceDetaisResponse de :detaisResponses ){ | 
|---|
|  |  |  | param.setDeviceCode(de.getTerminalID()); | 
|---|
|  |  |  | BaseCarsPageResponse<CarsAlarmListResponse>  data = getAlarmList(param); | 
|---|
|  |  |  | if(data!=null &&data.getResults()!=null){ | 
|---|
|  |  |  | for(CarsAlarmListResponse model :data.getResults()){ | 
|---|
|  |  |  | CarsAlarmResultListResponse tm = new CarsAlarmResultListResponse(); | 
|---|
|  |  |  | tm.setTime(model.getTime()); | 
|---|
|  |  |  | tm.setCarCode(de.getPlateNum()); | 
|---|
|  |  |  | tm.setType(model.getType()); | 
|---|
|  |  |  | tm.setSubType(model.getSubType()); | 
|---|
|  |  |  | Date tmDate = DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss", tm.getTime() ); | 
|---|
|  |  |  | if(tmDate!=null){ | 
|---|
|  |  |  | tm.setTimeStamp(tmDate.getTime()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | tm.setTitle(HKConstants.CarsEventType.getName(tm.getType(),tm.getSubType())); | 
|---|
|  |  |  | list.add(tm); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Collections.sort(list, (o1, o2) -> { | 
|---|
|  |  |  | return (int) (o2.getTimeStamp() - o1.getTimeStamp()); //按时间倒序 | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | System.out.println(JSONObject.toJSONString(list)); | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String sendRequest(String url,TreeMap<String, String> map){ | 
|---|
|  |  |  | try { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | map.put("random", System.currentTimeMillis()+""); | 
|---|
|  |  |  | StringBuilder sortQueryStringTmp = new StringBuilder(); | 
|---|
|  |  |  | for(Map.Entry<String, String> entry : map.entrySet()){ | 
|---|
|  |  |  | sortQueryStringTmp | 
|---|
|  |  |  | 
|---|
|  |  |  | //与下方的HttpGet对应,采用的是HttpMethod.GET | 
|---|
|  |  |  | String sign = sign(ACCESS_SECRET + "&", map, HttpMethod.GET); | 
|---|
|  |  |  | url += "?Signature=" + specialUrlEncode(sign) + sortQueryStringTmp.toString(); | 
|---|
|  |  |  | RequestConfig requestConfig = RequestConfig.custom() | 
|---|
|  |  |  | .setSocketTimeout(5000)  // 设置读取超时时间(单位:毫秒) | 
|---|
|  |  |  | .setConnectTimeout(5000) // 设置连接超时时间(单位:毫秒) | 
|---|
|  |  |  | .build(); | 
|---|
|  |  |  | CloseableHttpClient httpClient = HttpClientBuilder.create().build(); | 
|---|
|  |  |  | //与上方的HttpMethod.GET对应,使用HttpGet | 
|---|
|  |  |  | HttpGet httpDelete = new HttpGet(url); | 
|---|
|  |  |  | httpDelete.setConfig(requestConfig); | 
|---|
|  |  |  | CloseableHttpResponse response = httpClient.execute(httpDelete); | 
|---|
|  |  |  | return  EntityUtils.toString(response.getEntity()); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | System.err.println(url); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static  CarsGpsResponse   getLatestGpsInfo(String deviceCode)   { | 
|---|
|  |  |  | String url = BASE_URL + "gps/latest/"; | 
|---|
|  |  |  | TreeMap<String, String> BASE_PARAMS = getBaseParams(); | 
|---|
|  |  |  | BASE_PARAMS.put("deviceCode", deviceCode);//设备型号秘钥 | 
|---|
|  |  |  | String str = sendRequest(url,BASE_PARAMS); | 
|---|
|  |  |  | TypeReference typeReference = | 
|---|
|  |  |  | new TypeReference<BaseCarsResponse<CarsGpsResponse>>(){}; | 
|---|
|  |  |  | BaseCarsResponse<CarsGpsResponse>   result = JSONObject.parseObject(str, typeReference.getType()); | 
|---|
|  |  |  | if(result!=null && Constants.equalsInteger(result.getStatus(),0)){ | 
|---|
|  |  |  | return  result.getData(); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String url = BASE_URL + "gps/latest/"; | 
|---|
|  |  |  | TreeMap<String, String> BASE_PARAMS = getBaseParams(); | 
|---|
|  |  |  | BASE_PARAMS.put("deviceCode", deviceCode);//设备型号秘钥 | 
|---|
|  |  |  | String str = sendRequest(url,BASE_PARAMS); | 
|---|
|  |  |  | TypeReference typeReference = | 
|---|
|  |  |  | new TypeReference<BaseCarsResponse<CarsGpsResponse>>(){}; | 
|---|
|  |  |  | BaseCarsResponse<CarsGpsResponse>   result = JSONObject.parseObject(str, typeReference.getType()); | 
|---|
|  |  |  | if(result!=null && Constants.equalsInteger(result.getStatus(),0)){ | 
|---|
|  |  |  | return  result.getData(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static   Map<String,Integer>   getDeviceStatusList(List<String> code)   { | 
|---|
|  |  |  | 
|---|
|  |  |  | String url = BASE_URL + "device/list/"; | 
|---|
|  |  |  | TreeMap<String, String> BASE_PARAMS = getBaseParams(); | 
|---|
|  |  |  | if(StringUtils.isNotBlank(param.getProductKey())){ | 
|---|
|  |  |  | BASE_PARAMS.put("productKey", "");//设备型号秘钥 | 
|---|
|  |  |  | BASE_PARAMS.put("productKey", param.getProductKey());//设备型号秘钥 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BASE_PARAMS.put("pageSize",Constants.equalsInteger(param.getPageSize(),0)? "100":param.getPageSize().toString());//页面大小 | 
|---|
|  |  |  | BASE_PARAMS.put("pageNo", Constants.equalsInteger(param.getPageNo(),0)? "1":param.getPageNo().toString());//当前页 | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static BaseCarsPageResponse<CarsAlarmListResponse> getAlarmList(CarsAlarmListRequest param)  { | 
|---|
|  |  |  | String url = BASE_URL + "alarm/list/"; | 
|---|
|  |  |  | TreeMap<String, String> BASE_PARAMS = getBaseParams(); | 
|---|
|  |  |  | if(StringUtils.isNotBlank(param.getStartTime())){ | 
|---|
|  |  |  | BASE_PARAMS.put("startTime", param.getStartTime()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(StringUtils.isNotBlank(param.getEndTime())){ | 
|---|
|  |  |  | BASE_PARAMS.put("endTime", param.getEndTime()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BASE_PARAMS.put("deviceCode", param.getDeviceCode()); | 
|---|
|  |  |  | BASE_PARAMS.put("pageSize",Constants.equalsInteger(param.getPageSize(),0)? "100":param.getPageSize().toString());//页面大小 | 
|---|
|  |  |  | BASE_PARAMS.put("pageNo", Constants.equalsInteger(param.getPageNo(),0)? "1":param.getPageNo().toString());//当前页 | 
|---|
|  |  |  | String str = sendRequest(url,BASE_PARAMS); | 
|---|
|  |  |  | TypeReference typeReference = | 
|---|
|  |  |  | new TypeReference<BaseCarsResponse<BaseCarsPageResponse<CarsAlarmListResponse>>>(){}; | 
|---|
|  |  |  | BaseCarsResponse<BaseCarsPageResponse<CarsAlarmListResponse>>   result = JSONObject.parseObject(str, typeReference.getType()); | 
|---|
|  |  |  | if(result!=null && Constants.equalsInteger(result.getStatus(),0)){ | 
|---|
|  |  |  | return  result.getData(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return  null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|