fuyuchuan 5 日 前
コミット
d77dbd1ea8

+ 5 - 5
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/DeviceDataSyncService.java

@@ -26,12 +26,12 @@ public class DeviceDataSyncService {
      * fixedDelay:任务执行完成后固定延迟29分钟执行下一次
      * initialDelay:初始化后立即执行第一次任务
      */
-    // @Scheduled(fixedDelay = 14 * 60 * 1000, initialDelay = 0)
+    //@Scheduled(fixedDelay = 14 * 60 * 1000, initialDelay = 0)
     // public void scheduledDeviceDataSync() {
-    //     Integer tenantId = 1205;
-    //     Long engineeringId = 3101070011L;
-    //     String username = "3101070011";
-    //     String password = "5RqhJ7VG";
+    //     Integer tenantId = 1208;
+    //     Long engineeringId = 3101130019L;
+    //     String username = "3101130019";
+    //     String password = "ptrEQZK2";
     //     log.info("开始执行设备数据同步定时任务,租户ID:{},工程ID:{}", tenantId, engineeringId);
     //
     //     try {

+ 13 - 14
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/HttpClientUtils.java

@@ -1,6 +1,7 @@
 package com.usky.cdi.service.util;
 
 import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
@@ -20,11 +21,16 @@ import java.util.List;
 import java.util.Map;
 
 public class HttpClientUtils {
-    public static String doGet(String url, Map<String, String> param) {
-
-        // 创建Httpclient对象
-        CloseableHttpClient httpclient = HttpClients.createDefault();
+    // 使用静态连接池,避免每次请求都创建和关闭连接
+    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.custom()
+            .setDefaultRequestConfig(RequestConfig.custom()
+                    .setConnectTimeout(5000)  // 连接超时时间
+                    .setSocketTimeout(5000)   // 读取超时时间
+                    .setConnectionRequestTimeout(5000)  // 请求超时时间
+                    .build())
+            .build();
 
+    public static String doGet(String url, Map<String, String> param) {
         String resultString = "";
         CloseableHttpResponse response = null;
         try {
@@ -41,7 +47,7 @@ public class HttpClientUtils {
             HttpGet httpGet = new HttpGet(uri);
 
             // 执行请求
-            response = httpclient.execute(httpGet);
+            response = HTTP_CLIENT.execute(httpGet);
             // 判断返回状态是否为200
             if (response.getStatusLine().getStatusCode() == 200) {
                 resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
@@ -53,7 +59,6 @@ public class HttpClientUtils {
                 if (response != null) {
                     response.close();
                 }
-                httpclient.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
@@ -66,8 +71,6 @@ public class HttpClientUtils {
     }
 
     public static String doPost(String url, Map<String, String> param) {
-        // 创建Httpclient对象
-        CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse response = null;
         String resultString = "";
         try {
@@ -84,7 +87,7 @@ public class HttpClientUtils {
                 httpPost.setEntity(entity);
             }
             // 执行http请求
-            response = httpClient.execute(httpPost);
+            response = HTTP_CLIENT.execute(httpPost);
             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
         } catch (Exception e) {
             e.printStackTrace();
@@ -93,7 +96,6 @@ public class HttpClientUtils {
                 if (response != null) {
                     response.close();
                 }
-                httpClient.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
@@ -107,8 +109,6 @@ public class HttpClientUtils {
     }
 
     public static String doPostJson(String url, String json) {
-        // 创建Httpclient对象
-        CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse response = null;
         String resultString = "";
         try {
@@ -118,7 +118,7 @@ public class HttpClientUtils {
             StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
             httpPost.setEntity(entity);
             // 执行http请求
-            response = httpClient.execute(httpPost);
+            response = HTTP_CLIENT.execute(httpPost);
             resultString = EntityUtils.toString(response.getEntity(), "utf-8");
         } catch (Exception e) {
             e.printStackTrace();
@@ -127,7 +127,6 @@ public class HttpClientUtils {
                 if (response != null) {
                     response.close();
                 }
-                httpClient.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }

+ 64 - 23
service-cdi/service-cdi-biz/src/main/java/com/usky/cdi/service/util/WeatherFetcher.java

@@ -1,36 +1,68 @@
 package com.usky.cdi.service.util;
 
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.util.HashMap;
-import java.util.Map;
-
+import lombok.extern.slf4j.Slf4j;
 import org.json.JSONObject;
 
 /**
- *
+ * 天气数据获取工具类
+ * 使用OpenWeatherMap API获取天气数据,并进行缓存
  * @author fyc
  * @email yuchuan.fu@chinausky.com
  * @date 2025/12/15
  */
-
+@Slf4j
 public class WeatherFetcher {
     private static final String API_KEY = "d04f0f84421a99b9b66dd99243c36db4";
     private static final String CITY_NAME = "shanghai,cn";
     private static final String API_URL = "https://api.openweathermap.org/data/2.5/weather?q=" + CITY_NAME + "&appid=" + API_KEY;
 
+    // 缓存相关字段
+    private static final Map<String, Double> weatherCache = new ConcurrentHashMap<>();
+    private static long lastUpdateTime = 0;
+    private static final long CACHE_EXPIRE_TIME = 30 * 60 * 1000;  // 缓存30分钟
+
+    // 默认天气数据,当API调用失败时使用
+    private static final double DEFAULT_TEMPERATURE = 15.0;
+    private static final int DEFAULT_HUMIDITY = 40;
+
+    /**
+     * 获取天气数据
+     * 1. 优先使用缓存数据(如果缓存有效)
+     * 2. 缓存无效时,调用API获取新数据
+     * 3. API调用失败时,使用默认值
+     *
+     * @return 包含温度和湿度的Map
+     */
     public static Map<String, Double> fetchWeather() {
-        double tempCelsius = 0.0;
-        int humidity = 0;
+        // 1. 检查缓存是否有效
+        long currentTime = System.currentTimeMillis();
+        if (!weatherCache.isEmpty() && (currentTime - lastUpdateTime) < CACHE_EXPIRE_TIME) {
+            log.debug("使用缓存的天气数据,温度:{}°C,湿度:{}%",
+                    weatherCache.get("temperature"), weatherCache.get("humidity"));
+            return new HashMap<>(weatherCache);
+        }
+
+        double tempCelsius = DEFAULT_TEMPERATURE;
+        int humidity = DEFAULT_HUMIDITY;
+
         try {
+            log.debug("开始调用OpenWeatherMap API获取天气数据");
+
             // 1. 构造请求URL
             URL url = new URL(API_URL);
 
             // 2. 建立连接并发送请求
             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
             conn.setRequestMethod("GET");
+            conn.setConnectTimeout(5000); // 连接超时:5秒
+            conn.setReadTimeout(5000);    // 读取超时:5秒
 
             // 3. 读取响应
             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
@@ -41,11 +73,11 @@ public class WeatherFetcher {
             }
             reader.close();
 
-            // 4. 解析JSON数据(这里使用org.json库)
+            // 4. 解析JSON数据(使用org.json库)
             JSONObject jsonResponse = new JSONObject(response.toString());
             JSONObject main = jsonResponse.getJSONObject("main");
 
-            // 1. 提取基础信息
+            // 提取基础信息
             String cityName = jsonResponse.getString("name");
             int timezoneOffset = jsonResponse.getInt("timezone");
 
@@ -56,30 +88,39 @@ public class WeatherFetcher {
             double feelsLikeKelvin = main.getDouble("feels_like");
             double feelsLikeCelsius = feelsLikeKelvin - 273.15;
 
-            // 3. 提取天气状况描述
+            // 提取天气状况描述
             JSONObject weather = jsonResponse.getJSONArray("weather").getJSONObject(0);
             String description = weather.getString("description");
 
-            // 4. 输出解析结果
-            System.out.println("=== 天气解析结果 ===");
-            System.out.println("城市: " + cityName);
-            System.out.println("温度: " + String.format("%.2f", tempCelsius) + "°C (原始: " + tempKelvin + "K)");
-            System.out.println("体感温度: " + String.format("%.2f", feelsLikeCelsius) + "°C");
-            System.out.println("湿度: " + humidity + "%");
-            System.out.println("天气状况: " + description);
-            System.out.println("时区偏移: " + (timezoneOffset / 3600) + "小时");
+            // 记录日志,不输出到控制台
+            log.debug("=== 天气解析结果 ===");
+            log.debug("城市: {}", cityName);
+            log.debug("温度: {:.2f}°C (原始: {}K)", tempCelsius, tempKelvin);
+            log.debug("体感温度: {:.2f}°C", feelsLikeCelsius);
+            log.debug("湿度: {}%", humidity);
+            log.debug("天气状况: {}", description);
+            log.debug("时区偏移: {}小时", (timezoneOffset / 3600));
 
-            // 5. 检查是否包含臭氧数据
+            // 检查是否包含臭氧数据
             if (jsonResponse.has("air_quality") || jsonResponse.has("o3") || jsonResponse.has("components")) {
-                System.out.println("包含空气质量数据");
+                log.debug("包含空气质量数据");
             } else {
-                System.out.println("提示: 当前数据不包含臭氧浓度等空气质量指标");
+                log.debug("当前数据不包含臭氧浓度等空气质量指标");
             }
 
+            // 更新缓存
+            weatherCache.clear();
+            weatherCache.put("temperature", tempCelsius);
+            weatherCache.put("humidity", (double) humidity);
+            lastUpdateTime = currentTime;
+            log.debug("天气数据缓存更新成功");
+
         } catch (Exception e) {
-            System.err.println("解析JSON时出错: " + e.getMessage());
-            e.printStackTrace();
+            log.error("获取天气数据失败:{}", e.getMessage());
+            // 异常时使用默认值
+            log.warn("使用默认天气数据,温度:{}°C,湿度:{}%", DEFAULT_TEMPERATURE, DEFAULT_HUMIDITY);
         }
+
         Map<String, Double> resultMap = new HashMap<>();
         resultMap.put("temperature", tempCelsius);
         resultMap.put("humidity", (double) humidity);