WServiceUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. package com.persagy.framework.util;
  2. import java.lang.reflect.Method;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.apache.log4j.Logger;
  8. import com.persagy.core.constant.SystemConstant;
  9. import com.persagy.functions.weather.constant.Const;
  10. import com.persagy.functions.weather.dto.AlarmDTO;
  11. import com.persagy.functions.weather.dto.DayPredictDTO;
  12. import com.persagy.functions.weather.dto.Hour24PredictDTO;
  13. import com.persagy.functions.weather.dto.NowAirDTO;
  14. import com.persagy.functions.weather.dto.NowWeatherDTO;
  15. import com.persagy.functions.weather.dto.WeatherResponseDTO;
  16. @SuppressWarnings("unchecked")
  17. public class WServiceUtil {
  18. private static final Logger log = Logger.getLogger(WServiceUtil.class);
  19. public static Map<String, Object> getLocation(String location) {
  20. Map<String, Object> nowObj = new HashMap<>();
  21. String url = Const.ServiceURL.nowWeather;
  22. url = url.replace("LOCATION", location);
  23. try {
  24. String result = HttpUtil.get(url);
  25. Const.requestAdd(Const.API.nowWeather);
  26. Map<String, Object> resultMap = (Map<String, Object>) SystemConstant.jsonMapper.readValue(result,
  27. Map.class);
  28. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  29. Map<String, Object> dataItem = (Map<String, Object>) resultArray.get(0);
  30. nowObj = (Map<String, Object>) dataItem.get("location");
  31. } catch (Exception e) {
  32. log.error("请求实时天气数据失败!location=" + location + e.getMessage());
  33. }
  34. return nowObj;
  35. }
  36. /**
  37. * 查询天气实况
  38. * @param location 城市标识
  39. * @return
  40. */
  41. public static Map<String, Object> getNowWeather(String location) {
  42. Map<String, Object> nowObj = new HashMap<String, Object>();
  43. String url = Const.ServiceURL.nowWeather;
  44. url = url.replace(Const.LOCATION, location);
  45. try {
  46. String result = HttpUtil.get(url);
  47. Const.requestAdd(Const.API.nowWeather);
  48. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  49. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  50. Map<String, Object> dataItem = resultArray.get(0);
  51. nowObj = (Map<String, Object>) dataItem.get("now");
  52. nowObj.put("last_update", dataItem.get("last_update"));
  53. }catch (Exception e) {
  54. log.error("请求实时天气数据失败!location="+location + e.getMessage());
  55. }
  56. return nowObj;
  57. }
  58. /**
  59. * 查询天气实况
  60. * @param location 城市标识
  61. * @return null
  62. */
  63. public static NowWeatherDTO getNowWeatherObj(String location) {
  64. Map<String, Object> nowObj = getNowWeather(location);
  65. if(!nowObj.isEmpty()) {
  66. try {
  67. return map2Obj(nowObj, NowWeatherDTO.class);
  68. } catch (Exception e) {
  69. log.error("map转对象失败!", e);
  70. }
  71. }
  72. return null;
  73. }
  74. /**
  75. * 查询空气质量实况数据
  76. * @param location 城市标识
  77. * @return
  78. */
  79. public static Map<String, Object> getNowAir(String location) {
  80. Map<String, Object> nowObj = new HashMap<String, Object>();
  81. String url = Const.ServiceURL.nowAir;
  82. url = url.replace(Const.LOCATION, location);
  83. try {
  84. String result = HttpUtil.get(url);
  85. Const.requestAdd(Const.API.nowAir);
  86. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  87. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  88. Map<String, Object> dataItem = resultArray.get(0);
  89. Map<String, Object> cityMap = (Map<String, Object>) dataItem.get("air");
  90. nowObj = (Map<String, Object>) cityMap.get("city");
  91. }catch (Exception e) {
  92. log.error("请求空气质量实况数据失败!location="+location, e);
  93. }
  94. return nowObj;
  95. }
  96. /**
  97. * 查询空气质量实况数据
  98. * @param location 城市标识
  99. * @return null
  100. */
  101. public static NowAirDTO getNowAirObj(String location) {
  102. Map<String, Object> nowObj = getNowAir(location);
  103. if(!nowObj.isEmpty()) {
  104. try {
  105. return map2Obj(nowObj, NowAirDTO.class);
  106. }catch (Exception e) {
  107. log.error("map转对象失败!", e);
  108. }
  109. }
  110. return null;
  111. }
  112. /**
  113. * 查询逐日天气预报和昨日天气数据
  114. * @param location 城市标识
  115. * @param start >=-2, <15
  116. * @param end >-2, <=15
  117. * @return
  118. */
  119. public static List<Map<String, Object>> getDayPredict(String location, int start, int end) {
  120. List<Map<String, Object>> dailyArray = new ArrayList<Map<String, Object>>();
  121. String url = Const.ServiceURL.dayPredict;
  122. url = url.replace(Const.LOCATION, location).replace(Const.DAYSTART, start+"").replace(Const.DAYEND, end+"");
  123. try {
  124. String result = HttpUtil.get(url);
  125. Const.requestAdd(Const.API.dayPredict);
  126. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  127. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  128. Map<String, Object> dataItem = resultArray.get(0);
  129. dailyArray = (List<Map<String, Object>>) dataItem.get("daily");
  130. String last_update = (String) dataItem.get("last_update");
  131. if (dailyArray != null) {
  132. for (Map<String, Object> item : dailyArray) {
  133. item.put("last_update", last_update);
  134. }
  135. }
  136. }catch (Exception e) {
  137. log.error("查询逐日天气预报和昨日天气数据失败!location="+location+",start="+start+",end"+end, e);
  138. }
  139. return dailyArray;
  140. }
  141. /**
  142. * 查询逐日天气预报和昨日天气数据
  143. * @param location 城市标识
  144. * @param start >=-2, <15
  145. * @param end >-2, <=15
  146. * @return
  147. */
  148. public static List<DayPredictDTO> getDayPredictObj(String location, int start, int end) {
  149. List<DayPredictDTO> dataList = new ArrayList<>();
  150. List<Map<String, Object>> dailyArray = getDayPredict(location, start, end);
  151. if(!dailyArray.isEmpty()) {
  152. try {
  153. for(Map<String, Object> map : dailyArray) {
  154. dataList.add(map2Obj(map, DayPredictDTO.class));
  155. }
  156. }catch (Exception e) {
  157. dataList.clear();
  158. log.error("map转对象失败!", e);
  159. }
  160. }
  161. return dataList;
  162. }
  163. /**
  164. * 获取24小时逐小时天气预报
  165. * @param location 城市标识
  166. * @return
  167. */
  168. public static List<Map<String, Object>> getHour24Predict(String location) {
  169. List<Map<String, Object>> hourDataArray = new ArrayList<Map<String, Object>>();
  170. Map<String, Object> dataMap = getHour24PredictWithLocation(location);
  171. if(!dataMap.containsKey("hourly")) {
  172. hourDataArray = (List<Map<String, Object>>) dataMap.get("hourly");
  173. }
  174. return hourDataArray;
  175. }
  176. /**
  177. * 获取24小时逐小时天气预报
  178. * @param location
  179. * @return {"location":{}, "hourly":[]}
  180. */
  181. public static Map<String, Object> getHour24PredictWithLocation(String location) {
  182. Map<String, Object> dataItem = new HashMap<>();
  183. String url = Const.ServiceURL.hour24Predict;
  184. url = url.replace(Const.LOCATION, location);
  185. try {
  186. String result = HttpUtil.get(url);
  187. Const.requestAdd(Const.API.hour24Predict);
  188. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  189. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  190. dataItem = resultArray.get(0);
  191. }catch (Exception e) {
  192. log.error("查询逐日天气预报和昨日天气数据失败!location="+location, e);
  193. }
  194. return dataItem;
  195. }
  196. /**
  197. * 获取24小时逐小时天气预报
  198. * @param location 城市标识
  199. * @return
  200. */
  201. public static List<Hour24PredictDTO> getHour24PredictObj(String location) {
  202. List<Hour24PredictDTO> hourDataList = new ArrayList<>();
  203. List<Map<String, Object>> hourDataMapList = getHour24Predict(location);
  204. if(!hourDataMapList.isEmpty()) {
  205. try {
  206. for(Map<String, Object> map : hourDataMapList) {
  207. hourDataList.add(map2Obj(map, Hour24PredictDTO.class));
  208. }
  209. }catch (Exception e) {
  210. hourDataList.clear();
  211. log.error("map转对象失败!", e);
  212. }
  213. }
  214. return hourDataList;
  215. }
  216. /**
  217. * 获取过去24小时历史天气
  218. * @param location
  219. * @return
  220. */
  221. public static List<Map<String, Object>> getHour24History(String location) {
  222. List<Map<String, Object>> hourDataArray = new ArrayList<Map<String, Object>>();
  223. String url = Const.ServiceURL.hour24History;
  224. url = url.replace(Const.LOCATION, location);
  225. try {
  226. String result = HttpUtil.get(url);
  227. Const.requestAdd(Const.API.hour24History);
  228. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  229. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  230. Map<String, Object> dataItem = resultArray.get(0);
  231. hourDataArray = (List<Map<String, Object>>) dataItem.get("hourly_history");
  232. }catch (Exception e) {
  233. log.error("获取过去24小时历史天气数据失败!location="+location, e);
  234. }
  235. return hourDataArray;
  236. }
  237. /**
  238. * 获取过去24小时历史天气
  239. * @param location
  240. * @return
  241. */
  242. public static List<NowWeatherDTO> getHour24HistoryObj(String location) {
  243. List<NowWeatherDTO> dataList = new ArrayList<>();
  244. List<Map<String, Object>> dataMapList = getHour24History(location);
  245. if(!dataMapList.isEmpty()) {
  246. try {
  247. for(Map<String, Object> map : dataMapList) {
  248. dataList.add(map2Obj(map, NowWeatherDTO.class));
  249. }
  250. }catch (Exception e) {
  251. dataList.clear();
  252. log.error("map转对象失败!", e);
  253. }
  254. }
  255. return dataList;
  256. }
  257. /**
  258. * 获取气象灾害预警信息
  259. * @return
  260. */
  261. public static List<Map<String, Object>> getAlarms() {
  262. List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
  263. try {
  264. String result = HttpUtil.get(Const.ServiceURL.alarm);
  265. Const.requestAdd(Const.API.alarm);
  266. Map<String, Object> resultMap = SystemConstant.jsonMapper.readValue(result, Map.class);
  267. List<Map<String, Object>> resultArray = (List<Map<String, Object>>) resultMap.get("results");
  268. for(Map<String, Object> dataItem : resultArray) {
  269. Map<String, Object> location = (Map<String, Object>) dataItem.get("location");
  270. List<Map<String, Object>> alarmArray = (List<Map<String, Object>>) dataItem.get("alarms");
  271. for(Map<String, Object> alarmItem : alarmArray) {
  272. alarmItem.putAll(location);
  273. dataList.add(alarmItem);
  274. }
  275. }
  276. }catch (Exception e) {
  277. log.error("获取气象灾害预警数据失败!", e);
  278. }
  279. return dataList;
  280. }
  281. /**
  282. * 获取气象灾害预警信息
  283. * @return [预警信息对象] 或 空集合
  284. */
  285. public static List<AlarmDTO> getAlarmObjs() {
  286. List<AlarmDTO> resultList = new ArrayList<>();
  287. List<Map<String, Object>> alarmListMap = getAlarms();
  288. if(!alarmListMap.isEmpty()) {
  289. try {
  290. for(Map<String, Object> map : alarmListMap) {
  291. resultList.add(map2Obj(map, AlarmDTO.class));
  292. }
  293. }catch (Exception e) {
  294. resultList.clear();
  295. log.error("map转对象失败!", e);
  296. }
  297. }
  298. return resultList;
  299. }
  300. @SuppressWarnings("unused")
  301. private static <T extends WeatherResponseDTO> T map2Obj(Map<String, Object> map, Class<T> clz) throws Exception {
  302. T t = clz.newInstance();
  303. for(String key : map.keySet()) {
  304. Method m = null;
  305. try {
  306. m = clz.getMethod("set"+key.substring(0,1).toUpperCase()+key.substring(1), String.class);
  307. }catch (Exception e) {
  308. }
  309. if(m != null) {
  310. m.invoke(t, map.get(key));
  311. }
  312. }
  313. return t;
  314. }
  315. public static void main(String[] args) {
  316. getHour24HistoryObj("WW8P3NH2TPDT");
  317. System.out.println();
  318. }
  319. }