修改天气的缓存方式

This commit is contained in:
孙小云 2026-01-31 11:02:39 +08:00
parent 178829ee35
commit ff3fbce1f3
3 changed files with 22 additions and 6 deletions

View File

@ -3,5 +3,5 @@ package com.ruoyi.device.domain.api;
import com.ruoyi.device.domain.model.weather.Weather;
public interface IWeatherDomain {
Weather weatherInfo(String lat, String lon);
Weather weatherInfo(String lat, String lon,String dockerDeviceIotId);
}

View File

@ -25,8 +25,10 @@ public class WeatherDomainImpl implements IWeatherDomain {
private WeatherProperties weatherProperties;
@Override
@Cacheable(value = DeviceCacheConfig.WEATHER_CACHE, key = "#lat + ',' + #lon", unless = "#result == null")
public Weather weatherInfo(String lat, String lon) {
@Cacheable(value = DeviceCacheConfig.WEATHER_CACHE, key = "#dockerDeviceIotId", unless = "#result == null")
public Weather weatherInfo(String lat, String lon, String dockerDeviceIotId) {
log.info("开始获取天气信息 - lat: {}, lon: {}", lat, lon);
String host = weatherProperties.getHost();
String path = weatherProperties.getPath();
String method = "POST";
@ -39,11 +41,18 @@ public class WeatherDomainImpl implements IWeatherDomain {
bodys.put("lon", lon);
bodys.put("token", weatherProperties.getToken());
log.info("天气API配置 - host: {}, path: {}, token: {}", host, path, weatherProperties.getToken());
Weather weather = new Weather();
try {
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
String json = EntityUtils.toString(response.getEntity());
log.info("天气API原始响应 - json: {}", json);
WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class);
log.info("天气API解析结果 - weatherResponse: {}, code: {}",
weatherResponse != null ? "not null" : "null",
weatherResponse != null ? weatherResponse.getCode() : "N/A");
if (weatherResponse != null && weatherResponse.getCode() == 0) {
String windLevel = weatherResponse.getData().getCondition().getWindLevel();
@ -52,12 +61,19 @@ public class WeatherDomainImpl implements IWeatherDomain {
weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity()));
String conditionId = weatherResponse.getData().getCondition().getConditionId();
weather.setRainfall(convertConditionIdToRainfall(conditionId));
log.info("天气数据解析成功 - windSpeed: {}, temp: {}, humidity: {}, rainfall: {}",
weather.getWindSpeed(), weather.getEnvironmentTemperature(),
weather.getEnvironmentHumidity(), weather.getRainfall());
} else {
log.warn("天气API返回异常 - weatherResponse: {}, code: {}",
JSON.toJSONString(weatherResponse),
weatherResponse != null ? weatherResponse.getCode() : "null");
}
log.info("lat {} log {} weather {}", lat, lon,JSON.toJSONString(weather));
log.info("lat {} log {} weather {}", lat, lon, JSON.toJSONString(weather));
return weather;
} catch (Exception e) {
log.error("lat {} log {} ", lat, lon,e);
log.error("获取天气信息异常 - lat: {}, lon: {}", lat, lon, e);
return null;
}
}

View File

@ -364,7 +364,7 @@ public class BufferDeviceImpl implements IBufferDeviceService
dockerDeviceIotId, dto.getLatitude(), dto.getLongitude());
if (dto.getLatitude() != null && dto.getLongitude() != null) {
Weather weather = iWeatherDomain.weatherInfo(dto.getLatitude().toString(), dto.getLongitude().toString());
Weather weather = iWeatherDomain.weatherInfo(dto.getLatitude().toString(), dto.getLongitude().toString(),dockerDeviceIotId);
log.info("天气API返回结果 - weather: {}", JSON.toJSONString(weather));
if (weather != null) {