using System.Net.Http; namespace AxCopilot.Services; internal static class WeatherWidgetService { private static string? _cached; private static DateTime _cacheTime = DateTime.MinValue; private static bool _fetching; private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(30); public static string CachedText => _cached ?? "--"; public static async Task RefreshAsync(bool internalMode) { if (internalMode) { _cached = "--"; return; } if (_cached != null && DateTime.Now - _cacheTime < Ttl) return; if (_fetching) return; _fetching = true; try { using var client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(6); var raw = await client.GetStringAsync("https://wttr.in/?format=%c+%t"); _cached = raw.Trim().Replace("+", " "); _cacheTime = DateTime.Now; } catch (Exception ex) { LogService.Warn($"날씨 위젯 갱신 실패: {ex.Message}"); _cached ??= "--"; } finally { _fetching = false; } } public static void Invalidate() { _cached = null; _cacheTime = DateTime.MinValue; } }