CZT
2023-09-06 71c4fa1e27f75ae4b765c95c67a3069c84dc72ba
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.ld.igds.timer;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.data.ConfigData;
import com.ld.igds.io.constant.ProtocolEnum;
import com.ld.igds.models.WeatherConf;
import com.ld.igds.models.WeatherInfo;
import com.ld.igds.timer.rk.RkData;
import com.ld.igds.timer.rk.RkDto;
import com.ld.igds.timer.rk.RkResult;
import com.ld.igds.util.HttpUtils;
import com.ld.igds.util.RedisUtil;
import com.ld.igds.weather.CoreWeatherService;
import com.ld.igds.weather.WeatherUtil;
import com.ld.igds.weather.notify.NotifyWeatherService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @author: andy.jia
 * @description: 气象信息获取定时器
 * @version: <p>
 * <p>
 * 风正致远购买外网气象服务:
 * 气象服务:http://www.yiketianqi.com/
 *
 *
 * <p> 建大仁科的4G协议
 */
@Slf4j
@Component
public class WeatherScheduled {
 
    @Resource(name = NotifyWeatherService.BEAN_ID)
    private NotifyWeatherService notifyService;
 
    @Resource(name = CoreWeatherService.BEAN_ID)
    private CoreWeatherService weatherService;
 
    @Autowired
    private ConfigData configData;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RestTemplate restTemplate;
 
    public static String DEFAULT_URL = "https://v0.yiketianqi.com/api?unescape=1&version=v61&appid=49421971&appsecret=JmJE48Fv&cityid={cityid}";
    public static String DEFAULT_APP_ID = "49421971";
    public static String DEFAULT_APP_SECRET = "JmJE48Fv";
 
    /**
     * 建大仁科4G平台缓存key
     */
    public static String RK_WEATHER_V30_4G = "RK_WEATHER_V30_4G";
 
 
    /**
     * 定时获取气象信息,请注意,由于气象支持外网和自定义气象站,系统默认只执行其中一种方式
     */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void scheduled() {
 
        log.info("===========================系统定时获获取气象信息======================");
 
        // 获取气象信息配置
        List<WeatherConf> list = weatherService.getConfData(null, null);
 
        if (null == list || list.isEmpty()) return;
 
        Date date = new Date();
 
        for (WeatherConf conf : list) {
 
            if (null == conf.getWanTag()) conf.setWanTag(Constant.YN_N);
            if (null == conf.getProtocol()) conf.setProtocol(ProtocolEnum.TCP_DEFAULT.getCode());
 
 
            //调用风正致远默认外网气象
            if (Constant.YN_Y.equals(conf.getWanTag())) {
                getWeatherByWeb(date, conf);
                continue;
            }
 
            //调用建大仁科的气象
            if (ProtocolEnum.RK_WEATHER_V30.getCode().equals(conf.getProtocol())) {
                getWeatherByRkV30(date, conf);
                continue;
            }
 
 
        }
    }
 
    /**
     * 系统调用风正致远购买的外网天气气象协议
     *
     * @param date
     * @param conf
     * @return
     */
    public WeatherInfo getWeatherByWeb(Date date, WeatherConf conf) {
 
        if (StringUtils.isEmpty(conf.getCityId())) return null;
 
        try {
 
            if (null != configData.getWeatherPath()) {
                DEFAULT_URL = configData.getWeatherPath();
            }
 
            String url = DEFAULT_URL.replace("{cityid}", conf.getCityId())
                    .replace("{appid}", DEFAULT_APP_ID)
                    .replace("{appsecret}", DEFAULT_APP_SECRET);
 
            log.info("气象调用获取-url = {}", url);
 
            String result = HttpUtils.doGet(url);
            if (null == result) {
                log.error("当前外网获取气象信息失败……");
                return null;
            }
 
            WeatherWebDto dto = JSON.parseObject(result, WeatherWebDto.class);
 
            if (StringUtils.isNoneEmpty(dto.getErrcode())) {
                log.error("当前外网获取气象信息异常:{}", dto.getErrmsg());
                return null;
            }
            log.info("气象返回结果={}", dto.toString());
 
            WeatherInfo info = new WeatherInfo();
            info.setId(WeatherUtil.buildWeatherId(conf.getDeptId(), date));
            info.setAirLevel(dto.getAir_level());
            info.setCompanyId(conf.getCompanyId());
            info.setDeptId(conf.getDeptId());
            info.setHumidity(dto.getHumidity());
            info.setPm25(dto.getAir_pm25());
            info.setPressure(dto.getPressure());
            info.setSource(WeatherUtil.SOURCE_02);
            info.setTemp(dto.getTem());
            info.setWeather(dto.getWea());
            info.setWindDirection(dto.getWin());
            info.setWindSpeed(dto.getWin_speed());
            info.setWindMeter(dto.getWin_meter());
            info.setCity(dto.getCity());
            info.setUpdateTime(date);
 
            notifyService.notify(info);
            return info;
        } catch (Exception e) {
            log.error("气象信息获取异常:{}", e.getMessage(), e);
        }
        return null;
    }
 
    /**
     * 根据建大仁科云V30协议获取气象信息
     *
     * @param date
     * @param conf
     */
    private void getWeatherByRkV30(Date date, WeatherConf conf) {
 
        //若用户、密码、IP地址有一为空则返回
        if(StringUtils.isEmpty(conf.getAccount())
                || StringUtils.isEmpty(conf.getPassword())
                || StringUtils.isEmpty(conf.getIp())){
            return;
        }
 
        //获取身份认证token
        String token = getToken(conf);
 
        //设置请求头参数
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("authorization", token);
 
        String url = conf.getIp() + "/api/data/getRealTimeData";
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpEntity<String> ans = restTemplate.exchange(url, HttpMethod.GET,
                new HttpEntity<>(null, httpHeaders), String.class);
        String body = ans.getBody();
        log.debug("-----当前建大仁科平台获取气象信息-----,返回数据={}", body);
        RkResult response = JSONObject.parseObject(body, RkResult.class);
        if(StringUtils.isEmpty(response.getCode()) || !"1000".equals(response.getCode())){
            log.error("当前建大仁科平台获取气象信息失败......");
            return;
        }
        List<JSONObject> result = JSON.parseArray(JSONObject.toJSONString(response.getData()), JSONObject.class);
 
        JSONArray lists = (JSONArray)result.get(0).get("dataItem");
        List<RkDto> list = JSON.parseArray(JSONObject.toJSONString(lists), RkDto.class);
        if(null == list || list.isEmpty()){
            return;
        }
        WeatherInfo info = new WeatherInfo();
        info.setId(WeatherUtil.buildWeatherId(conf.getDeptId(), date));
        info.setCompanyId(conf.getCompanyId());
        info.setDeptId(conf.getDeptId());
        info.setSource(WeatherUtil.SOURCE_01);
 
        List<RkData> dataList;
        List<RkData> weatherList = new ArrayList<>();
        //根据地址判断选项
        for (RkDto rkDto : list) {
            //温湿度
            if("11".equals(rkDto.getNodeId())){
                dataList = rkDto.getRegisterItem();
                for (RkData rkData : dataList) {
                    //温度
                    if("1".equals(rkData.getRegisterId())){
                        info.setTemp(rkData.getData());
                    }
                    //湿度
                    if("2".equals(rkData.getRegisterId())){
                        info.setHumidity(rkData.getData());
                    }
                }
            }
            //风力风速
            if("1".equals(rkDto.getNodeId())){
                dataList = rkDto.getRegisterItem();
                for (RkData rkData : dataList) {
                    //风力
                    if("1".equals(rkData.getRegisterId())){
                        info.setWindSpeed(rkData.getData() + "级");
                    }
                    //风速
                    if("2".equals(rkData.getRegisterId())){
                        info.setWindMeter(rkData.getData());
                    }
                }
            }
            //风向
            if("2".equals(rkDto.getNodeId())){
                dataList = rkDto.getRegisterItem();
                for (RkData rkData : dataList) {
                    //风向
                    if("1".equals(rkData.getRegisterId())){
                        info.setWindDirection(rkData.getData());
                    }
                }
            }
            //大气压
            if("14".equals(rkDto.getNodeId())){
                dataList = rkDto.getRegisterItem();
                for (RkData rkData : dataList) {
                    //大气压
                    if("2".equals(rkData.getRegisterId())){
                        info.setPressure(rkData.getData());
                    }
                }
            }
            //雨量
            if("21".equals(rkDto.getNodeId())){
                dataList = rkDto.getRegisterItem();
                for (RkData rkData : dataList) {
//                    //瞬时雨量
//                    if("1".equals(rkData.getRegisterId())){
//                        info.setRainfall(rkData.getData());
//                    }
                    //当前雨量
                    if("2".equals(rkData.getRegisterId())){
                        info.setRainfall(rkData.getData());
                    }
                }
            }
            //有无雨雪
            if("16".equals(rkDto.getNodeId())){
                weatherList = rkDto.getRegisterItem();
            }
        }
        for (RkData rkData : weatherList) {
            //天气
            if("1".equals(rkData.getRegisterId())){
                info.setWeather(rkData.getData());
                //有雨雪时判断当前雨量
                if("有雨雪".equals(rkData.getData())){
                    Double rainfall = Double.valueOf(info.getRainfall());
                    if(rainfall <= 10){
                        info.setWeather("小雨");
                    }
                    if(rainfall > 10 && rainfall <= 25){
                        info.setWeather("中雨");
                    }
                    if(rainfall > 25 && rainfall <= 50){
                        info.setWeather("大雨");
                    }
                    if(rainfall > 50){
                        info.setWeather("暴雨");
                    }
                }
            }
        }
        info.setUpdateTime(date);
        info.setCity(conf.getCity());
        notifyService.notify(info);
    }
 
    /**
     * 获取身份认证的授权accessToken
     * @param conf
     * @return
     */
    private String getToken(WeatherConf conf) {
        //默认先从缓存获取
        String key = RedisConst.buildKey(conf.getCompanyId(), RK_WEATHER_V30_4G, conf.getAccount());
        String token = (String) redisUtil.get(key);
 
        //若缓存获取为空,则通过接口获取token
        if (null == token) {
            String url = conf.getIp() + "/api/getToken?loginName=" + conf.getAccount() + "&password=" + conf.getPassword();
            RkResult response = restTemplate.getForObject(url, RkResult.class);
            if (null == response) {
                log.error("当前建大仁科平台获取身份认证信息失败...");
                return null;
            }
            log.info("获取的身份认证信息={}", response);
 
            if(StringUtils.isEmpty(response.getCode()) || !"1000".equals(response.getCode())){
                log.error("当前建大仁科平台获取身份认证信息失败......");
                return null;
            }
            JSONObject result = JSON.parseObject(JSONObject.toJSONString(response.getData()));
            if (null == result) {
                return null;
            }
            token = (String) result.get("token");
 
            //将token存入缓存,
            redisUtil.set(key, token, 2*60*60);
        }
        return token;
    }
 
}