소스 검색

新增 获取天气信息公共工具

zhaiyifei 1 년 전
부모
커밋
675965446c

+ 49 - 0
src/main/java/com/yunfeiyun/agmp/iot/common/enums/EnumDevRecogType.java

@@ -0,0 +1,49 @@
+package com.yunfeiyun.agmp.iot.common.enums;
+
+/**
+ * 设备识别类型
+ */
+public enum EnumDevRecogType {
+    /**
+     * 识别类型
+     */
+    DISABLE("0", "禁用识别"),
+    PEST_RECOG("1", "虫情识别");
+
+    private final String code;
+    private final String name;
+
+    EnumDevRecogType(String code, String name)
+    {
+        this.code = code;
+        this.name = name;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    public String getName()
+    {
+        return name;
+    }
+
+    public boolean equal(String code){
+        return this.code.equals(code);
+    }
+
+    /**
+     * 根据code查找
+     * @param code 枚举code
+     * @return 枚举对象
+     */
+    public static EnumDevRecogType findEnumByCode(String code) {
+        for (EnumDevRecogType item : EnumDevRecogType.values()) {
+            if (item.getCode().equals(code)) {
+                return item;
+            }
+        }
+        return null;
+    }
+}

+ 0 - 22
src/main/java/com/yunfeiyun/agmp/iot/common/model/weather/IotWeatherModel.java

@@ -1,22 +0,0 @@
-package com.yunfeiyun.agmp.iot.common.model.weather;
-
-import lombok.Data;
-
-@Data
-public class IotWeatherModel {
-    /**	城市ID*/
-    private String cityid ;
-    /**只能为 ‘1’  或  ‘7’,1为当日天气,7为7日天气*/
-    private String day_type;
-    /**用户名*/
-    private String username;
-    /**密码*/
-    private String password;
-
-    /**省*/
-    private String province;
-    /**市*/
-    private String city;
-    /**县*/
-    private String district;
-}

+ 2 - 1
src/main/java/com/yunfeiyun/agmp/iot/common/service/IotAddressService.java

@@ -3,6 +3,7 @@ package com.yunfeiyun.agmp.iot.common.service;
 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.service.WeatherService;
 import com.yunfeiyun.agmp.common.utils.StringUtils;
 import com.yunfeiyun.agmp.iot.common.model.address.IotAddressModel;
 import lombok.extern.slf4j.Slf4j;
@@ -34,7 +35,7 @@ public class IotAddressService {
 
 
     @Autowired
-    private IotWeatherService iotWeatherService;
+    private WeatherService weatherService;
 
     public IotAddressModel getAddressByLngLat(BigDecimal lng, BigDecimal lat) {
         HttpURLConnection conn;

+ 0 - 126
src/main/java/com/yunfeiyun/agmp/iot/common/service/IotWeatherService.java

@@ -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;
-    }
-}