diff --git a/src/main/java/com/ruoyi/device/domain/api/IWeatherDomain.java b/src/main/java/com/ruoyi/device/domain/api/IWeatherDomain.java index 02a4f9d..5671aaf 100644 --- a/src/main/java/com/ruoyi/device/domain/api/IWeatherDomain.java +++ b/src/main/java/com/ruoyi/device/domain/api/IWeatherDomain.java @@ -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); } 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 6bc5bcf..9cbfc41 100644 --- a/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java +++ b/src/main/java/com/ruoyi/device/domain/impl/WeatherDomainImpl.java @@ -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; } } diff --git a/src/main/java/com/ruoyi/device/service/impl/BufferDeviceImpl.java b/src/main/java/com/ruoyi/device/service/impl/BufferDeviceImpl.java index 93408b5..9d6c458 100644 --- a/src/main/java/com/ruoyi/device/service/impl/BufferDeviceImpl.java +++ b/src/main/java/com/ruoyi/device/service/impl/BufferDeviceImpl.java @@ -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) {