package com.persagy.framework.util; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import com.persagy.core.constant.SystemConstant; import com.persagy.functions.weather.constant.Const; import com.persagy.functions.weather.dto.AlarmDTO; import com.persagy.functions.weather.dto.DayPredictDTO; import com.persagy.functions.weather.dto.Hour24PredictDTO; import com.persagy.functions.weather.dto.NowAirDTO; import com.persagy.functions.weather.dto.NowWeatherDTO; import com.persagy.functions.weather.dto.WeatherResponseDTO; @SuppressWarnings("unchecked") public class WServiceUtil { private static final Logger log = Logger.getLogger(WServiceUtil.class); public static Map getLocation(String location) { Map nowObj = new HashMap<>(); String url = Const.ServiceURL.nowWeather; url = url.replace("LOCATION", location); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.nowWeather); Map resultMap = (Map) SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); Map dataItem = (Map) resultArray.get(0); nowObj = (Map) dataItem.get("location"); } catch (Exception e) { log.error("请求实时天气数据失败!location=" + location + e.getMessage()); } return nowObj; } /** * 查询天气实况 * @param location 城市标识 * @return */ public static Map getNowWeather(String location) { Map nowObj = new HashMap(); String url = Const.ServiceURL.nowWeather; url = url.replace(Const.LOCATION, location); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.nowWeather); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); Map dataItem = resultArray.get(0); nowObj = (Map) dataItem.get("now"); nowObj.put("last_update", dataItem.get("last_update")); }catch (Exception e) { log.error("请求实时天气数据失败!location="+location + e.getMessage()); } return nowObj; } /** * 查询天气实况 * @param location 城市标识 * @return null */ public static NowWeatherDTO getNowWeatherObj(String location) { Map nowObj = getNowWeather(location); if(!nowObj.isEmpty()) { try { return map2Obj(nowObj, NowWeatherDTO.class); } catch (Exception e) { log.error("map转对象失败!", e); } } return null; } /** * 查询空气质量实况数据 * @param location 城市标识 * @return */ public static Map getNowAir(String location) { Map nowObj = new HashMap(); String url = Const.ServiceURL.nowAir; url = url.replace(Const.LOCATION, location); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.nowAir); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); Map dataItem = resultArray.get(0); Map cityMap = (Map) dataItem.get("air"); nowObj = (Map) cityMap.get("city"); }catch (Exception e) { log.error("请求空气质量实况数据失败!location="+location, e); } return nowObj; } /** * 查询空气质量实况数据 * @param location 城市标识 * @return null */ public static NowAirDTO getNowAirObj(String location) { Map nowObj = getNowAir(location); if(!nowObj.isEmpty()) { try { return map2Obj(nowObj, NowAirDTO.class); }catch (Exception e) { log.error("map转对象失败!", e); } } return null; } /** * 查询逐日天气预报和昨日天气数据 * @param location 城市标识 * @param start >=-2, <15 * @param end >-2, <=15 * @return */ public static List> getDayPredict(String location, int start, int end) { List> dailyArray = new ArrayList>(); String url = Const.ServiceURL.dayPredict; url = url.replace(Const.LOCATION, location).replace(Const.DAYSTART, start+"").replace(Const.DAYEND, end+""); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.dayPredict); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); Map dataItem = resultArray.get(0); dailyArray = (List>) dataItem.get("daily"); String last_update = (String) dataItem.get("last_update"); if (dailyArray != null) { for (Map item : dailyArray) { item.put("last_update", last_update); } } }catch (Exception e) { log.error("查询逐日天气预报和昨日天气数据失败!location="+location+",start="+start+",end"+end, e); } return dailyArray; } /** * 查询逐日天气预报和昨日天气数据 * @param location 城市标识 * @param start >=-2, <15 * @param end >-2, <=15 * @return */ public static List getDayPredictObj(String location, int start, int end) { List dataList = new ArrayList<>(); List> dailyArray = getDayPredict(location, start, end); if(!dailyArray.isEmpty()) { try { for(Map map : dailyArray) { dataList.add(map2Obj(map, DayPredictDTO.class)); } }catch (Exception e) { dataList.clear(); log.error("map转对象失败!", e); } } return dataList; } /** * 获取24小时逐小时天气预报 * @param location 城市标识 * @return */ public static List> getHour24Predict(String location) { List> hourDataArray = new ArrayList>(); Map dataMap = getHour24PredictWithLocation(location); if(!dataMap.containsKey("hourly")) { hourDataArray = (List>) dataMap.get("hourly"); } return hourDataArray; } /** * 获取24小时逐小时天气预报 * @param location * @return {"location":{}, "hourly":[]} */ public static Map getHour24PredictWithLocation(String location) { Map dataItem = new HashMap<>(); String url = Const.ServiceURL.hour24Predict; url = url.replace(Const.LOCATION, location); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.hour24Predict); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); dataItem = resultArray.get(0); }catch (Exception e) { log.error("查询逐日天气预报和昨日天气数据失败!location="+location, e); } return dataItem; } /** * 获取24小时逐小时天气预报 * @param location 城市标识 * @return */ public static List getHour24PredictObj(String location) { List hourDataList = new ArrayList<>(); List> hourDataMapList = getHour24Predict(location); if(!hourDataMapList.isEmpty()) { try { for(Map map : hourDataMapList) { hourDataList.add(map2Obj(map, Hour24PredictDTO.class)); } }catch (Exception e) { hourDataList.clear(); log.error("map转对象失败!", e); } } return hourDataList; } /** * 获取过去24小时历史天气 * @param location * @return */ public static List> getHour24History(String location) { List> hourDataArray = new ArrayList>(); String url = Const.ServiceURL.hour24History; url = url.replace(Const.LOCATION, location); try { String result = HttpUtil.get(url); Const.requestAdd(Const.API.hour24History); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); Map dataItem = resultArray.get(0); hourDataArray = (List>) dataItem.get("hourly_history"); }catch (Exception e) { log.error("获取过去24小时历史天气数据失败!location="+location, e); } return hourDataArray; } /** * 获取过去24小时历史天气 * @param location * @return */ public static List getHour24HistoryObj(String location) { List dataList = new ArrayList<>(); List> dataMapList = getHour24History(location); if(!dataMapList.isEmpty()) { try { for(Map map : dataMapList) { dataList.add(map2Obj(map, NowWeatherDTO.class)); } }catch (Exception e) { dataList.clear(); log.error("map转对象失败!", e); } } return dataList; } /** * 获取气象灾害预警信息 * @return */ public static List> getAlarms() { List> dataList = new ArrayList>(); try { String result = HttpUtil.get(Const.ServiceURL.alarm); Const.requestAdd(Const.API.alarm); Map resultMap = SystemConstant.jsonMapper.readValue(result, Map.class); List> resultArray = (List>) resultMap.get("results"); for(Map dataItem : resultArray) { Map location = (Map) dataItem.get("location"); List> alarmArray = (List>) dataItem.get("alarms"); for(Map alarmItem : alarmArray) { alarmItem.putAll(location); dataList.add(alarmItem); } } }catch (Exception e) { log.error("获取气象灾害预警数据失败!", e); } return dataList; } /** * 获取气象灾害预警信息 * @return [预警信息对象] 或 空集合 */ public static List getAlarmObjs() { List resultList = new ArrayList<>(); List> alarmListMap = getAlarms(); if(!alarmListMap.isEmpty()) { try { for(Map map : alarmListMap) { resultList.add(map2Obj(map, AlarmDTO.class)); } }catch (Exception e) { resultList.clear(); log.error("map转对象失败!", e); } } return resultList; } @SuppressWarnings("unused") private static T map2Obj(Map map, Class clz) throws Exception { T t = clz.newInstance(); for(String key : map.keySet()) { Method m = null; try { m = clz.getMethod("set"+key.substring(0,1).toUpperCase()+key.substring(1), String.class); }catch (Exception e) { } if(m != null) { m.invoke(t, map.get(key)); } } return t; } public static void main(String[] args) { getHour24HistoryObj("WW8P3NH2TPDT"); System.out.println(); } }