添加天气日志
This commit is contained in:
parent
940b9999b9
commit
fcdac8ba40
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -1,60 +1,32 @@
|
|||
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<String, Weather> weatherCache;
|
||||
|
||||
public WeatherDomainImpl() {
|
||||
this.weatherCache = Caffeine.newBuilder()
|
||||
.expireAfter(new Expiry<String, Weather>() {
|
||||
@Override
|
||||
public long expireAfterCreate(String key, Weather value, long currentTime) {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@Cacheable(value = DeviceCacheConfig.WEATHER_CACHE, key = "#lat + ',' + #lon", unless = "#result == null")
|
||||
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";
|
||||
|
|
@ -69,7 +41,6 @@ public class WeatherDomainImpl implements IWeatherDomain {
|
|||
|
||||
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);
|
||||
|
|
@ -82,12 +53,13 @@ public class WeatherDomainImpl implements IWeatherDomain {
|
|||
String conditionId = weatherResponse.getData().getCondition().getConditionId();
|
||||
weather.setRainfall(convertConditionIdToRainfall(conditionId));
|
||||
}
|
||||
|
||||
log.info("lat {} log {} weather {}", lat, lon,JSON.toJSONString(weather));
|
||||
return weather;
|
||||
} catch (Exception ignored) {
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("lat {} log {} ", lat, lon,e);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue