|
@@ -1,126 +0,0 @@
|
|
|
-package com.yunfeiyun.agmp.iot.common.service;
|
|
|
|
|
-
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
-import com.yunfeiyun.agmp.common.enums.RedisCacheKey;
|
|
|
|
|
-import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
|
|
|
|
|
-import com.yunfeiyun.agmp.common.utils.ConfigUtils;
|
|
|
|
|
-import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
|
|
-import com.yunfeiyun.agmp.iot.common.model.weather.IotWeatherModel;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
-import java.io.InputStreamReader;
|
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
|
-import java.net.HttpURLConnection;
|
|
|
|
|
-import java.net.URL;
|
|
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
-
|
|
|
|
|
-@Service
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class IotWeatherService {
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private RedisCacheManager redisCacheManager;
|
|
|
|
|
-
|
|
|
|
|
- public final String WEATHER_API = "http://open.nyzhwlw.com:10001/yf_weather";
|
|
|
|
|
- private final RedisCacheKey IOT_WEATHER = RedisCacheKey.IOT_WEATHER;
|
|
|
|
|
-
|
|
|
|
|
- private String getWeather(IotWeatherModel iotWeatherModel) {
|
|
|
|
|
-
|
|
|
|
|
- HttpURLConnection conn;
|
|
|
|
|
- try {
|
|
|
|
|
- URL url = new URL(WEATHER_API);
|
|
|
|
|
- conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
- conn.setRequestMethod("POST");
|
|
|
|
|
- conn.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
- conn.setDoOutput(true);
|
|
|
|
|
- OutputStream outputStream = conn.getOutputStream();
|
|
|
|
|
- outputStream.write(JSON.toJSONBytes(iotWeatherModel));
|
|
|
|
|
- outputStream.flush();
|
|
|
|
|
- outputStream.close();
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- log.error("请求构造失败",e);
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- try(InputStream in = conn.getInputStream();
|
|
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))){
|
|
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
|
|
- String line;
|
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
|
- result.append(line);
|
|
|
|
|
- }
|
|
|
|
|
- return result.toString();
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- log.error("请求失败",e);
|
|
|
|
|
- return null;
|
|
|
|
|
- }finally {
|
|
|
|
|
- conn.disconnect();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public JSONObject getWeatherByCityId(String cityid) {
|
|
|
|
|
- JSONObject weatherInfo = null;
|
|
|
|
|
- if (redisCacheManager.hasKey(IOT_WEATHER, cityid)){
|
|
|
|
|
- weatherInfo = redisCacheManager.getCacheObject(IOT_WEATHER, cityid);
|
|
|
|
|
- }else{
|
|
|
|
|
- IotWeatherModel iotWeatherModel;
|
|
|
|
|
- try {
|
|
|
|
|
- String param = ConfigUtils.ConfigKeyEnum.PROJECT_LOCATION_WEATHER_PARAM.valueOf();
|
|
|
|
|
- iotWeatherModel = JSONObject.parseObject(param, IotWeatherModel.class);
|
|
|
|
|
- iotWeatherModel.setDay_type("1");
|
|
|
|
|
- iotWeatherModel.setCityid(cityid);
|
|
|
|
|
- String weatherDataStr = getWeather(iotWeatherModel);
|
|
|
|
|
- if (StringUtils.isNotEmpty(weatherDataStr)){
|
|
|
|
|
- try{
|
|
|
|
|
- weatherInfo = JSONObject.parseObject(weatherDataStr).getJSONObject("data").getJSONObject("content");
|
|
|
|
|
- redisCacheManager.setCacheObject(IOT_WEATHER, cityid, weatherInfo, 3600, TimeUnit.SECONDS);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("解析气象信息异常: ", e);
|
|
|
|
|
- }
|
|
|
|
|
- }else{
|
|
|
|
|
- log.info("获取接口返回内容empty");
|
|
|
|
|
- }
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- log.error("获取气象信息异常",e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return weatherInfo;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public JSONObject getWeatherByAddress(String province, String city, String district) {
|
|
|
|
|
- JSONObject weatherInfo = null;
|
|
|
|
|
- String k = province + city + district;
|
|
|
|
|
- if (redisCacheManager.hasKey(IOT_WEATHER, k)){
|
|
|
|
|
- weatherInfo = redisCacheManager.getCacheObject(IOT_WEATHER, k);
|
|
|
|
|
- }else{
|
|
|
|
|
- IotWeatherModel iotWeatherModel;
|
|
|
|
|
- try {
|
|
|
|
|
- String param = ConfigUtils.ConfigKeyEnum.PROJECT_LOCATION_WEATHER_PARAM.valueOf();
|
|
|
|
|
- iotWeatherModel = JSONObject.parseObject(param, IotWeatherModel.class);
|
|
|
|
|
- iotWeatherModel.setDay_type("1");
|
|
|
|
|
- iotWeatherModel.setCityid("");
|
|
|
|
|
- iotWeatherModel.setProvince(province);
|
|
|
|
|
- iotWeatherModel.setCity(city);
|
|
|
|
|
- iotWeatherModel.setDistrict(district);
|
|
|
|
|
- String weatherDataStr = getWeather(iotWeatherModel);
|
|
|
|
|
- if (StringUtils.isNotEmpty(weatherDataStr)){
|
|
|
|
|
- try{
|
|
|
|
|
- weatherInfo = JSONObject.parseObject(weatherDataStr).getJSONObject("data").getJSONObject("content");
|
|
|
|
|
- redisCacheManager.setCacheObject(IOT_WEATHER, k, weatherInfo, 3600, TimeUnit.SECONDS);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("解析气象信息异常: ", e);
|
|
|
|
|
- }
|
|
|
|
|
- }else{
|
|
|
|
|
- log.info("获取接口返回内容empty");
|
|
|
|
|
- }
|
|
|
|
|
- }catch (Exception e){
|
|
|
|
|
- log.error("获取气象信息异常",e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return weatherInfo;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|