添加天气日志

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,93 +1,65 @@
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(); @Override
private final Cache<String, Weather> weatherCache; @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<String, String> headers = new HashMap<>();
headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode());
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<>();
Map<String, String> bodys = new HashMap<>();
bodys.put("lat", lat);
bodys.put("lon", lon);
bodys.put("token", weatherProperties.getToken());
public WeatherDomainImpl() { Weather weather = new Weather();
this.weatherCache = Caffeine.newBuilder() try {
.expireAfter(new Expiry<String, Weather>() { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
@Override String json = EntityUtils.toString(response.getEntity());
public long expireAfterCreate(String key, Weather value, long currentTime) { WeatherResponse weatherResponse = JSON.parseObject(json, WeatherResponse.class);
long randomMinutes = 1 + random.nextInt(10);
return TimeUnit.MINUTES.toNanos(randomMinutes);
}
@Override if (weatherResponse != null && weatherResponse.getCode() == 0) {
public long expireAfterUpdate(String key, Weather value, long currentTime, long currentDuration) { String windLevel = weatherResponse.getData().getCondition().getWindLevel();
long randomMinutes = 1 + random.nextInt(10); weather.setWindSpeed(convertWindLevelToSpeed(windLevel));
return TimeUnit.MINUTES.toNanos(randomMinutes); weather.setEnvironmentTemperature(Double.valueOf(weatherResponse.getData().getCondition().getTemp()));
} weather.setEnvironmentHumidity(Double.valueOf(weatherResponse.getData().getCondition().getHumidity()));
String conditionId = weatherResponse.getData().getCondition().getConditionId();
@Override weather.setRainfall(convertConditionIdToRainfall(conditionId));
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<String, String> headers = new HashMap<>();
headers.put("Authorization", "APPCODE " + weatherProperties.getAppcode());
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
Map<String, String> querys = new HashMap<>();
Map<String, String> 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;
} }
}); log.info("lat {} log {} weather {}", lat, lon,JSON.toJSONString(weather));
return weather;
} catch (Exception e) {
log.error("lat {} log {} ", lat, lon,e);
return null;
}
} }
/** /**