From 7555c4c72b0c7dd033b1b34bc9e00c198230e0b4 Mon Sep 17 00:00:00 2001 From: yarnom Date: Tue, 8 Jul 2025 12:15:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=A9=E6=B0=94=E9=A2=84=E6=B5=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=9B=B4=E6=9B=BF=E4=B8=BA=E5=BC=80=E6=BA=90?= =?UTF-8?q?=E7=9A=84=20Open-Meteo=20-=20=E9=BB=98=E8=AE=A4=E6=83=85?= =?UTF-8?q?=E5=86=B5=E4=B8=8B=EF=BC=8C=E5=B0=86=E7=BB=93=E5=90=88=E6=9C=80?= =?UTF-8?q?=E9=80=82=E5=90=88=E7=9A=84=E5=A4=A9=E6=B0=94=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=EF=BC=88auto=EF=BC=89=20-=20=E9=A2=84=E6=8A=A5=203=E5=A4=A9?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weather-forecast.js | 90 +++++++++++++------ .../templates/page/device_overview.html | 6 +- 2 files changed, 66 insertions(+), 30 deletions(-) diff --git a/sec-beidou/src/main/resources/static/js/device-overview-module/weather-forecast.js b/sec-beidou/src/main/resources/static/js/device-overview-module/weather-forecast.js index e9cf8d2a..20b6d777 100644 --- a/sec-beidou/src/main/resources/static/js/device-overview-module/weather-forecast.js +++ b/sec-beidou/src/main/resources/static/js/device-overview-module/weather-forecast.js @@ -1,7 +1,6 @@ var WeatherForecast = (function() { 'use strict'; - var weatherApiKey = 'Uxh4IdMuAvhSiBnsf4UUDVGF4e3YAp2B'; var weatherEnabled = false; var weatherData = null; var currentForecastIndex = 0; @@ -81,7 +80,6 @@ var WeatherForecast = (function() { contentElement.innerHTML = '
' + '' + - '

请确保网络可访问 Windy API 服务

' + '

正在获取天气预测数据...

' + '
'; } @@ -140,23 +138,19 @@ var WeatherForecast = (function() { } function fetchWeatherData(lat, lon) { - var requestBody = { - "lat": parseFloat(lat.toFixed(2)), - "lon": parseFloat(lon.toFixed(2)), - "model": "gfs", - "parameters": ["temp", "wind", "precip", "pressure", "rh", "windGust"], - "levels": ["surface"], - "key": weatherApiKey, - "hours": 72 - }; + var url = 'https://api.open-meteo.com/v1/forecast?' + + 'latitude=' + lat.toFixed(4) + + '&longitude=' + lon.toFixed(4) + + '¤t=temperature_2m,wind_speed_10m,wind_direction_10m,relative_humidity_2m,surface_pressure' + + '&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m,wind_direction_10m,precipitation,surface_pressure,wind_gusts_10m' + + '&forecast_days=3' + + '&timezone=auto'; - fetch('https://api.windy.com/api/point-forecast/v2', { - method: 'POST', + fetch(url, { + method: 'GET', headers: { - 'Content-Type': 'application/json', 'Accept': 'application/json' - }, - body: JSON.stringify(requestBody) + } }) .then(function(response) { if (!response.ok) { @@ -165,7 +159,7 @@ var WeatherForecast = (function() { return response.json(); }) .then(function(data) { - weatherData = data; + weatherData = transformOpenMeteoData(data); var currentTime = new Date().getTime(); var closestIndex = 0; @@ -203,6 +197,40 @@ var WeatherForecast = (function() { }); } + function transformOpenMeteoData(data) { + var transformed = { + ts: [], + 'temp-surface': [], + 'wind-speed': [], + 'wind-direction': [], + 'precipitation': [], + 'rh-surface': [], + 'pressure-surface': [], + 'gust-surface': [] + }; + + if (!data.hourly || !data.hourly.time) { + return transformed; + } + + for (var i = 0; i < data.hourly.time.length; i++) { + transformed.ts.push(new Date(data.hourly.time[i]).getTime()); + + transformed['temp-surface'].push(data.hourly.temperature_2m[i]); + + var windSpeedMs = data.hourly.wind_speed_10m[i] ? data.hourly.wind_speed_10m[i] / 3.6 : null; + transformed['wind-speed'].push(windSpeedMs); + transformed['wind-direction'].push(data.hourly.wind_direction_10m[i]); + transformed['precipitation'].push(data.hourly.precipitation[i]); + transformed['rh-surface'].push(data.hourly.relative_humidity_2m[i]); + transformed['pressure-surface'].push(data.hourly.surface_pressure[i]); + var gustMs = data.hourly.wind_gusts_10m[i] ? data.hourly.wind_gusts_10m[i] / 3.6 : null; + transformed['gust-surface'].push(gustMs); + } + + return transformed; + } + function displayCurrentForecast() { if (!weatherData || !weatherData.ts || weatherData.ts.length === 0) { displayWeatherError('无可用的天气预测数据'); @@ -213,22 +241,22 @@ var WeatherForecast = (function() { var forecastHtml = '
'; if (weatherData['temp-surface'] && weatherData['temp-surface'][i] !== null) { - var temp = (weatherData['temp-surface'][i] - 273.15).toFixed(1); + var temp = weatherData['temp-surface'][i].toFixed(1); forecastHtml += createWeatherParam('温度', temp + '°C'); } - if (weatherData['wind_u-surface'] && weatherData['wind_v-surface'] && - weatherData['wind_u-surface'][i] !== null && weatherData['wind_v-surface'][i] !== null) { - var windU = weatherData['wind_u-surface'][i]; - var windV = weatherData['wind_v-surface'][i]; - var windSpeed = Math.sqrt(windU * windU + windV * windV).toFixed(1); - var windDir = getWindDirection(windU, windV); + if (weatherData['wind-speed'] && weatherData['wind-speed'][i] !== null) { + var windSpeed = weatherData['wind-speed'][i].toFixed(1); forecastHtml += createWeatherParam('风速', windSpeed + ' m/s'); + } + + if (weatherData['wind-direction'] && weatherData['wind-direction'][i] !== null) { + var windDir = getWindDirectionFromDegrees(weatherData['wind-direction'][i]); forecastHtml += createWeatherParam('风向', windDir); } - if (weatherData['past3hprecip-surface'] && weatherData['past3hprecip-surface'][i] !== null) { - var precip = weatherData['past3hprecip-surface'][i].toFixed(1); + if (weatherData['precipitation'] && weatherData['precipitation'][i] !== null) { + var precip = weatherData['precipitation'][i].toFixed(1); forecastHtml += createWeatherParam('降水', precip + ' mm'); } @@ -238,7 +266,7 @@ var WeatherForecast = (function() { } if (weatherData['pressure-surface'] && weatherData['pressure-surface'][i] !== null) { - var pressure = (weatherData['pressure-surface'][i] / 100).toFixed(0); + var pressure = weatherData['pressure-surface'][i].toFixed(0); forecastHtml += createWeatherParam('气压', pressure + ' hPa'); } @@ -291,6 +319,14 @@ var WeatherForecast = (function() { return directions[index]; } + function getWindDirectionFromDegrees(degrees) { + if (degrees === null || degrees === undefined) return '无风'; + + var directions = ['北', '东北', '东', '东南', '南', '西南', '西', '西北']; + var index = Math.round(degrees / 45) % 8; + return directions[index]; + } + function isEnabled() { return weatherEnabled; } diff --git a/sec-beidou/src/main/resources/templates/page/device_overview.html b/sec-beidou/src/main/resources/templates/page/device_overview.html index 31714f41..a120d556 100644 --- a/sec-beidou/src/main/resources/templates/page/device_overview.html +++ b/sec-beidou/src/main/resources/templates/page/device_overview.html @@ -851,7 +851,7 @@