diff --git a/src/main/java/com/ruoyi/device/config/DeviceCacheConfig.java b/src/main/java/com/ruoyi/device/config/DeviceCacheConfig.java index bd235b5..2ef8455 100644 --- a/src/main/java/com/ruoyi/device/config/DeviceCacheConfig.java +++ b/src/main/java/com/ruoyi/device/config/DeviceCacheConfig.java @@ -37,6 +37,7 @@ public class DeviceCacheConfig { public static final String AIRCRAFT_PAYLOAD_CACHE = "aircraftPayload"; public static final String THINGSBOARD_ATTRIBUTES_CACHE = "thingsboardAttributes"; public static final String THINGSBOARD_TELEMETRY_CACHE = "thingsboardTelemetry"; + public static final String WEATHER_CACHE = "weather"; /** * 配置缓存管理器 @@ -76,6 +77,9 @@ public class DeviceCacheConfig { // ThingsBoard 设备遥测:15秒(遥测数据,实时性要求高) cacheConfigurations.put(THINGSBOARD_TELEMETRY_CACHE, defaultConfig.entryTtl(Duration.ofSeconds(15))); + // 天气信息:5分钟(天气数据,变化较慢) + cacheConfigurations.put(WEATHER_CACHE, defaultConfig.entryTtl(Duration.ofMinutes(5))); + return RedisCacheManager.builder(connectionFactory) .cacheDefaults(defaultConfig) .withInitialCacheConfigurations(cacheConfigurations) diff --git a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java index f1f7ba5..6bc5bcf 100644 --- a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java +++ b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java @@ -1,93 +1,65 @@ package com.ruoyi.device.domain.impl; import com.alibaba.fastjson2.JSON; -import com.github.benmanes.caffeine.cache.Cache; -import com.github.benmanes.caffeine.cache.Caffeine; -import com.github.benmanes.caffeine.cache.Expiry; +import com.ruoyi.device.config.DeviceCacheConfig; import com.ruoyi.device.domain.api.IWeatherDomain; import com.ruoyi.device.domain.impl.weather.HttpUtils; import com.ruoyi.device.domain.model.weather.Weather; import com.ruoyi.device.domain.model.weather.WeatherResponse; import com.ruoyi.device.domain.config.WeatherProperties; +import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; -import java.util.Random; -import java.util.concurrent.TimeUnit; @Component +@Slf4j public class WeatherDomainImpl implements IWeatherDomain { @Autowired private WeatherProperties weatherProperties; - private final Random random = new Random(); - private final Cache weatherCache; + @Override + @Cacheable(value = DeviceCacheConfig.WEATHER_CACHE, key = "#lat + ',' + #lon", unless = "#result == null") + public Weather weatherInfo(String lat, String lon) { + String host = weatherProperties.getHost(); + String path = weatherProperties.getPath(); + String method = "POST"; + Map headers = new HashMap<>(); + headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); + headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + Map querys = new HashMap<>(); + Map bodys = new HashMap<>(); + bodys.put("lat", lat); + bodys.put("lon", lon); + bodys.put("token", weatherProperties.getToken()); - public WeatherDomainImpl() { - this.weatherCache = Caffeine.newBuilder() - .expireAfter(new Expiry() { - @Override - public long expireAfterCreate(String key, Weather value, long currentTime) { - long randomMinutes = 1 + random.nextInt(10); - return TimeUnit.MINUTES.toNanos(randomMinutes); - } + Weather weather = new Weather(); + try { + HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); + String json = EntityUtils.toString(response.getEntity()); + WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class); - @Override - public long expireAfterUpdate(String key, Weather value, long currentTime, long currentDuration) { - long randomMinutes = 1 + random.nextInt(10); - return TimeUnit.MINUTES.toNanos(randomMinutes); - } - - @Override - public long expireAfterRead(String key, Weather value, long currentTime, long currentDuration) { - return currentDuration; - } - }) - .build(); - } - - - public Weather weatherInfo(String lat, String lon) { - String cacheKey = lat + "," + lon; - return weatherCache.get(cacheKey, key -> { - String host = weatherProperties.getHost(); - String path = weatherProperties.getPath(); - String method = "POST"; - Map headers = new HashMap<>(); - headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode()); - headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); - Map querys = new HashMap<>(); - Map bodys = new HashMap<>(); - bodys.put("lat", lat); - bodys.put("lon", lon); - bodys.put("token", weatherProperties.getToken()); - - Weather weather = new Weather(); - try { - - HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); - String json = EntityUtils.toString(response.getEntity()); - WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class); - - if (weatherResponse != null && weatherResponse.getCode() == 0) { - String windLevel = weatherResponse.getData().getCondition().getWindLevel(); - weather.setWindSpeed(convertWindLevelToSpeed(windLevel)); - weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp())); - weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity())); - String conditionId = weatherResponse.getData().getCondition().getConditionId(); - weather.setRainfall(convertConditionIdToRainfall(conditionId)); - } - - return weather; - } catch (Exception ignored) { - return null; + if (weatherResponse != null && weatherResponse.getCode() == 0) { + String windLevel = weatherResponse.getData().getCondition().getWindLevel(); + weather.setWindSpeed(convertWindLevelToSpeed(windLevel)); + weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp())); + weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity())); + String conditionId = weatherResponse.getData().getCondition().getConditionId(); + weather.setRainfall(convertConditionIdToRainfall(conditionId)); } - }); + log.info("lat {} log {} weather {}", lat, lon,JSON.toJSONString(weather)); + return weather; + + } catch (Exception e) { + log.error("lat {} log {} ", lat, lon,e); + return null; + } } /**