添加天气日志

This commit is contained in:
孙小云 2026-01-31 10:39:37 +08:00
parent 940b9999b9
commit fcdac8ba40
2 changed files with 41 additions and 65 deletions

View File

@ -37,6 +37,7 @@ public class DeviceCacheConfig {
public static final String AIRCRAFT_PAYLOAD_CACHE = "aircraftPayload"; public static final String AIRCRAFT_PAYLOAD_CACHE = "aircraftPayload";
public static final String THINGSBOARD_ATTRIBUTES_CACHE = "thingsboardAttributes"; public static final String THINGSBOARD_ATTRIBUTES_CACHE = "thingsboardAttributes";
public static final String THINGSBOARD_TELEMETRY_CACHE = "thingsboardTelemetry"; public static final String THINGSBOARD_TELEMETRY_CACHE = "thingsboardTelemetry";
public static final String WEATHER_CACHE = "weather";
/** /**
* 配置缓存管理器 * 配置缓存管理器
@ -76,6 +77,9 @@ public class DeviceCacheConfig {
// ThingsBoard 设备遥测15秒遥测数据实时性要求高 // ThingsBoard 设备遥测15秒遥测数据实时性要求高
cacheConfigurations.put(THINGSBOARD_TELEMETRY_CACHE, defaultConfig.entryTtl(Duration.ofSeconds(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) return RedisCacheManager.builder(connectionFactory)
.cacheDefaults(defaultConfig) .cacheDefaults(defaultConfig)
.withInitialCacheConfigurations(cacheConfigurations) .withInitialCacheConfigurations(cacheConfigurations)

View File

@ -1,60 +1,32 @@
package com.ruoyi.device.domain.impl; package com.ruoyi.device.domain.impl;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.github.benmanes.caffeine.cache.Cache; import com.ruoyi.device.config.DeviceCacheConfig;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.ruoyi.device.domain.api.IWeatherDomain; import com.ruoyi.device.domain.api.IWeatherDomain;
import com.ruoyi.device.domain.impl.weather.HttpUtils; import com.ruoyi.device.domain.impl.weather.HttpUtils;
import com.ruoyi.device.domain.model.weather.Weather; import com.ruoyi.device.domain.model.weather.Weather;
import com.ruoyi.device.domain.model.weather.WeatherResponse; import com.ruoyi.device.domain.model.weather.WeatherResponse;
import com.ruoyi.device.domain.config.WeatherProperties; import com.ruoyi.device.domain.config.WeatherProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Component @Component
@Slf4j
public class WeatherDomainImpl implements IWeatherDomain { public class WeatherDomainImpl implements IWeatherDomain {
@Autowired @Autowired
private WeatherProperties weatherProperties; private WeatherProperties weatherProperties;
private final Random random = new Random();
private final Cache<String, Weather> weatherCache;
public WeatherDomainImpl() {
this.weatherCache = Caffeine.newBuilder()
.expireAfter(new Expiry<String, Weather>() {
@Override @Override
public long expireAfterCreate(String key, Weather value, long currentTime) { @Cacheable(value = DeviceCacheConfig.WEATHER_CACHE, key = "#lat + ',' + #lon", unless = "#result == null")
long randomMinutes = 1 + random.nextInt(10);
return TimeUnit.MINUTES.toNanos(randomMinutes);
}
@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) { public Weather weatherInfo(String lat, String lon) {
String cacheKey = lat + "," + lon;
return weatherCache.get(cacheKey, key -> {
String host = weatherProperties.getHost(); String host = weatherProperties.getHost();
String path = weatherProperties.getPath(); String path = weatherProperties.getPath();
String method = "POST"; String method = "POST";
@ -69,7 +41,6 @@ public class WeatherDomainImpl implements IWeatherDomain {
Weather weather = new Weather(); Weather weather = new Weather();
try { try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
String json = EntityUtils.toString(response.getEntity()); String json = EntityUtils.toString(response.getEntity());
WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class); WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class);
@ -82,12 +53,13 @@ public class WeatherDomainImpl implements IWeatherDomain {
String conditionId = weatherResponse.getData().getCondition().getConditionId(); String conditionId = weatherResponse.getData().getCondition().getConditionId();
weather.setRainfall(convertConditionIdToRainfall(conditionId)); weather.setRainfall(convertConditionIdToRainfall(conditionId));
} }
log.info("lat {} log {} weather {}", lat, lon,JSON.toJSONString(weather));
return weather; return weather;
} catch (Exception ignored) {
} catch (Exception e) {
log.error("lat {} log {} ", lat, lon,e);
return null; return null;
} }
});
} }
/** /**