puzhibing
2023-05-25 380c0e958fbc3f2b42a3e38ae5dc9af3254b8f5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.agentdriving.driver.modular.system.util.juhe;
 
import com.agentdriving.driver.core.util.ToolUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.agentdriving.driver.modular.system.util.httpClinet.HttpClientUtil;
import com.agentdriving.driver.modular.system.util.httpClinet.HttpResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.List;
 
 
/**
* 天气工具类
* @author pzb
* @Date 2023/2/18 16:54
*/
public class WeatherUtil {
 
    static Logger logger = LoggerFactory.getLogger("ServiceLog");
 
    private final static String weather_type = "[{\"wid\":\"00\",\"weather\":\"晴\"},{\"wid\":\"01\",\"weather\":\"多云\"},{\"wid\":\"02\",\"weather\":\"阴\"},{\"wid\":\"03\",\"weather\":\"阵雨\"},{\"wid\":\"04\",\"weather\":\"雷阵雨\"},{\"wid\":\"05\",\"weather\":\"雷阵雨伴有冰雹\"},{\"wid\":\"06\",\"weather\":\"雨夹雪\"},{\"wid\":\"07\",\"weather\":\"小雨\"},{\"wid\":\"08\",\"weather\":\"中雨\"},{\"wid\":\"09\",\"weather\":\"大雨\"},{\"wid\":\"10\",\"weather\":\"暴雨\"},{\"wid\":\"11\",\"weather\":\"大暴雨\"},{\"wid\":\"12\",\"weather\":\"特大暴雨\"},{\"wid\":\"13\",\"weather\":\"阵雪\"},{\"wid\":\"14\",\"weather\":\"小雪\"},{\"wid\":\"15\",\"weather\":\"中雪\"},{\"wid\":\"16\",\"weather\":\"大雪\"},{\"wid\":\"17\",\"weather\":\"暴雪\"},{\"wid\":\"18\",\"weather\":\"雾\"},{\"wid\":\"19\",\"weather\":\"冻雨\"},{\"wid\":\"20\",\"weather\":\"沙尘暴\"},{\"wid\":\"21\",\"weather\":\"小到中雨\"},{\"wid\":\"22\",\"weather\":\"中到大雨\"},{\"wid\":\"23\",\"weather\":\"大到暴雨\"},{\"wid\":\"24\",\"weather\":\"暴雨到大暴雨\"},{\"wid\":\"25\",\"weather\":\"大暴雨到特大暴雨\"},{\"wid\":\"26\",\"weather\":\"小到中雪\"},{\"wid\":\"27\",\"weather\":\"中到大雪\"},{\"wid\":\"28\",\"weather\":\"大到暴雪\"},{\"wid\":\"29\",\"weather\":\"浮尘\"},{\"wid\":\"30\",\"weather\":\"扬沙\"},{\"wid\":\"31\",\"weather\":\"强沙尘暴\"},{\"wid\":\"53\",\"weather\":\"霾\"}]";
 
    private final static String bad_weather = "[{\"wid\":\"03\",\"weather\":\"阵雨\"},{\"wid\":\"04\",\"weather\":\"雷阵雨\"},{\"wid\":\"05\",\"weather\":\"雷阵雨伴有冰雹\"},{\"wid\":\"06\",\"weather\":\"雨夹雪\"},{\"wid\":\"07\",\"weather\":\"小雨\"},{\"wid\":\"08\",\"weather\":\"中雨\"},{\"wid\":\"09\",\"weather\":\"大雨\"},{\"wid\":\"10\",\"weather\":\"暴雨\"},{\"wid\":\"11\",\"weather\":\"大暴雨\"},{\"wid\":\"12\",\"weather\":\"特大暴雨\"},{\"wid\":\"13\",\"weather\":\"阵雪\"},{\"wid\":\"14\",\"weather\":\"小雪\"},{\"wid\":\"15\",\"weather\":\"中雪\"},{\"wid\":\"16\",\"weather\":\"大雪\"},{\"wid\":\"17\",\"weather\":\"暴雪\"},{\"wid\":\"19\",\"weather\":\"冻雨\"},{\"wid\":\"20\",\"weather\":\"沙尘暴\"},{\"wid\":\"21\",\"weather\":\"小到中雨\"},{\"wid\":\"22\",\"weather\":\"中到大雨\"},{\"wid\":\"23\",\"weather\":\"大到暴雨\"},{\"wid\":\"24\",\"weather\":\"暴雨到大暴雨\"},{\"wid\":\"25\",\"weather\":\"大暴雨到特大暴雨\"},{\"wid\":\"26\",\"weather\":\"小到中雪\"},{\"wid\":\"27\",\"weather\":\"中到大雪\"},{\"wid\":\"28\",\"weather\":\"大到暴雪\"},{\"wid\":\"30\",\"weather\":\"扬沙\"},{\"wid\":\"31\",\"weather\":\"强沙尘暴\"},{\"wid\":\"53\",\"weather\":\"霾\"}]";
 
    private final static String key = "edc965edb129580401ed211eaa4ae8ab";
 
    private static HttpClientUtil httpClientUtil = new HttpClientUtil();
 
 
    /**
     * 获取天气实况数据
     * @param city  要查询的城市名称/id,城市名称如:温州、上海、北京,需要utf8 urlencode
     * @return
     */
    public static Realtime queryNowWeather(String city){
        if(ToolUtil.isEmpty(city)){
            return null;
        }
        String url = "http://apis.juhe.cn/simpleWeather/query?city=" + city + "&key=" + key;
        HttpResult httpResult = null;
        try {
            httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(httpResult.getCode() != 200){
            logger.debug("查询天气失败:" + httpResult.getData());
            return null;
        }
        String data = httpResult.getData();
        JSONObject jsonObject = JSON.parseObject(data);
        Integer error_code = jsonObject.getInteger("error_code");
        if(0 != error_code){
            logger.debug("查询天气失败:" + jsonObject.getString("reason"));
            return null;
        }
        Realtime realtime = jsonObject.getObject("realtime", Realtime.class);
        return realtime;
    }
 
 
    /**
     * 查询是否是恶劣天气
     * @param city
     * @return
     */
    public static boolean isBadWeather(String city){
        if(null == city){
            return false;
        }
        Realtime realtime = queryNowWeather(city);
        if(null == realtime){
            return false;
        }
        String wid = realtime.getWid();
        JSONArray badWeather = JSON.parseArray(bad_weather);
        for (int i = 0; i < badWeather.size(); i++) {
            JSONObject jsonObject = badWeather.getJSONObject(i);
            String wid1 = jsonObject.getString("wid");
            if(wid.equals(wid1)){
                return true;
            }
        }
        return false;
    }
 
 
    /**
     * 获取支持城市列表
     * @return
     */
    public static List<WeatherCityInfo> queryCityList(){
        String url = "http://apis.juhe.cn/simpleWeather/cityList?key=" + key;
        HttpResult httpResult = null;
        try {
            httpResult = httpClientUtil.pushHttpRequset("GET", url, null, null, "json");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(httpResult.getCode() != 200){
            logger.debug("查询支持城市失败:" + httpResult.getData());
            return null;
        }
        String data = httpResult.getData();
        JSONObject jsonObject = JSON.parseObject(data);
        Integer error_code = jsonObject.getInteger("error_code");
        if(0 != error_code){
            logger.debug("查询支持城失败:" + jsonObject.getString("reason"));
            return null;
        }
        JSONArray result = jsonObject.getJSONArray("result");
        List<WeatherCityInfo> weatherCities = result.toJavaList(WeatherCityInfo.class);
        return weatherCities;
    }
 
}