Repository navigation
Expand file tree
/
Copy pathweather.ts
More file actions
369 lines (327 loc) · 12.1 KB
/
Copy pathweather.ts
File metadata and controls
369 lines (327 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { hasLocationPermission } from "../g2/android-permissions";
import { getCurrentLocation, type CurrentLocation } from "./location";
export type WeatherPhase = "permission-required" | "locating" | "loading" | "ready" | "error";
export type CurrentWeather = {
temperatureF: number | null;
description: string;
humidityPercent: number | null;
windSpeedMph: number | null;
windDirection: string;
timestampMs: number | null;
observed: boolean;
};
export type ForecastPeriod = {
name: string;
startTimeMs: number;
temperatureF: number | null;
shortForecast: string;
detailedForecast: string;
precipitationPercent: number | null;
windSpeed: string;
windDirection: string;
isDaytime: boolean;
};
export type WeatherState = {
phase: WeatherPhase;
status: string;
locationName: string;
current: CurrentWeather | null;
forecast: ForecastPeriod[];
lastUpdatedMs: number | null;
};
type NwsPointResponse = {
properties?: {
forecast?: unknown;
forecastHourly?: unknown;
observationStations?: unknown;
relativeLocation?: {
properties?: { city?: unknown; state?: unknown };
};
};
};
type NwsForecastResponse = {
properties?: {
periods?: NwsForecastPeriodResponse[];
};
};
type NwsForecastPeriodResponse = {
name?: unknown;
startTime?: unknown;
temperature?: unknown;
temperatureUnit?: unknown;
shortForecast?: unknown;
detailedForecast?: unknown;
probabilityOfPrecipitation?: { value?: unknown };
windSpeed?: unknown;
windDirection?: unknown;
isDaytime?: unknown;
};
type NwsStationsResponse = {
features?: Array<{ id?: unknown }>;
};
type NwsObservationResponse = {
properties?: {
timestamp?: unknown;
textDescription?: unknown;
temperature?: NwsMeasure;
relativeHumidity?: NwsMeasure;
windSpeed?: NwsMeasure;
windDirection?: NwsMeasure;
};
};
type NwsMeasure = {
value?: unknown;
unitCode?: unknown;
};
const NWS_API_ROOT = "https://api.weather.gov";
const NWS_HEADERS = {
Accept: "application/geo+json",
"User-Agent": "Faceclaw/1.0 (https://github.com/jimrandomh/faceclaw)",
};
const WEATHER_REFRESH_MS = 30 * 60 * 1000;
const FETCH_TIMEOUT_MS = 20_000;
const MAX_FORECAST_PERIODS = 14;
const DEFAULT_STATE: WeatherState = {
phase: "permission-required",
status: "Location permission is required for local weather.",
locationName: "",
current: null,
forecast: [],
lastUpdatedMs: null,
};
/** Shared weather state and periodic refresh, active only while its app is open. */
export class WeatherBridge {
private readonly listeners = new Set<(state: WeatherState) => void>();
private state: WeatherState = cloneState(DEFAULT_STATE);
private refreshHandle: ReturnType<typeof setInterval> | null = null;
private refreshInFlight: Promise<void> | null = null;
onStateChange(listener: (state: WeatherState) => void): () => void {
this.listeners.add(listener);
listener(this.snapshot());
return () => this.listeners.delete(listener);
}
snapshot(): WeatherState {
return cloneState(this.state);
}
start(): void {
if (!this.refreshHandle) {
this.refreshHandle = setInterval(() => void this.refreshNow(), WEATHER_REFRESH_MS);
}
void this.refreshNow();
}
stop(): void {
if (this.refreshHandle) {
clearInterval(this.refreshHandle);
this.refreshHandle = null;
}
}
async refreshNow(): Promise<void> {
if (this.refreshInFlight) return this.refreshInFlight;
if (!hasLocationPermission()) {
this.state = cloneState(DEFAULT_STATE);
this.emit();
return;
}
this.refreshInFlight = this.refresh();
try {
await this.refreshInFlight;
} finally {
this.refreshInFlight = null;
}
}
private async refresh(): Promise<void> {
try {
this.state = { ...this.state, phase: "locating", status: "Getting current location..." };
this.emit();
const location = await getCurrentLocation();
this.state = { ...this.state, phase: "loading", status: "Loading National Weather Service data..." };
this.emit();
const weather = await loadNwsWeather(location);
this.state = {
phase: "ready",
status: "Weather updated.",
locationName: weather.locationName,
current: weather.current,
forecast: weather.forecast,
lastUpdatedMs: Date.now(),
};
this.emit();
} catch (error) {
const message = friendlyWeatherError(error);
console.warn(`weather refresh failed: ${message}`);
this.state = {
...this.state,
phase: "error",
status: message,
};
this.emit();
}
}
private emit(): void {
const snapshot = this.snapshot();
for (const listener of this.listeners) listener(snapshot);
}
}
async function loadNwsWeather(location: CurrentLocation): Promise<{
locationName: string;
current: CurrentWeather;
forecast: ForecastPeriod[];
}> {
// NWS recommends no more than four decimals for its point lookup.
const latitude = location.latitude.toFixed(4);
const longitude = location.longitude.toFixed(4);
const point = await fetchNwsJson<NwsPointResponse>(`${NWS_API_ROOT}/points/${latitude},${longitude}`);
const properties = point.properties;
const forecastUrl = stringValue(properties?.forecast);
const hourlyUrl = stringValue(properties?.forecastHourly);
const stationsUrl = stringValue(properties?.observationStations);
if (!forecastUrl) throw new Error("NWS did not provide a forecast for this location.");
const [forecastResponse, hourlyResponse, observation] = await Promise.all([
fetchNwsJson<NwsForecastResponse>(forecastUrl),
hourlyUrl ? fetchNwsJson<NwsForecastResponse>(hourlyUrl).catch(() => null) : Promise.resolve(null),
stationsUrl ? loadLatestObservation(stationsUrl).catch(() => null) : Promise.resolve(null),
]);
const forecast = (forecastResponse.properties?.periods ?? [])
.map(normalizeForecastPeriod)
.filter((period): period is ForecastPeriod => period !== null)
.slice(0, MAX_FORECAST_PERIODS);
if (!forecast.length) throw new Error("NWS returned an empty forecast.");
const hourly = (hourlyResponse?.properties?.periods ?? [])
.map(normalizeForecastPeriod)
.find((period): period is ForecastPeriod => period !== null);
return {
locationName: pointLocationName(point, latitude, longitude),
current: observation ? normalizeObservation(observation, hourly) : currentFromHourly(hourly, forecast[0]!),
forecast,
};
}
async function loadLatestObservation(stationsUrl: string): Promise<NwsObservationResponse | null> {
const stations = await fetchNwsJson<NwsStationsResponse>(stationsUrl);
const stationUrl = stringValue(stations.features?.[0]?.id);
if (!stationUrl) return null;
return fetchNwsJson<NwsObservationResponse>(`${stationUrl}/observations/latest`);
}
async function fetchNwsJson<T>(url: string): Promise<T> {
const request = fetch(url, { headers: NWS_HEADERS });
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
const timeout = new Promise<never>((_resolve, reject) => {
timeoutHandle = setTimeout(() => reject(new Error("Weather request timed out.")), FETCH_TIMEOUT_MS);
});
try {
const response = await Promise.race([request, timeout]);
if (!response.ok) {
if (response.status === 404 && url.includes("/points/")) {
throw new Error("This location is outside National Weather Service coverage.");
}
throw new Error(`National Weather Service request failed (HTTP ${response.status}).`);
}
return (await response.json()) as T;
} finally {
if (timeoutHandle) clearTimeout(timeoutHandle);
}
}
function normalizeObservation(
response: NwsObservationResponse,
hourly: ForecastPeriod | undefined,
): CurrentWeather {
const properties = response.properties;
const temperatureF = convertTemperatureToF(properties?.temperature);
const windSpeedMph = convertSpeedToMph(properties?.windSpeed);
const windDegrees = finiteNumber(properties?.windDirection?.value);
return {
temperatureF: temperatureF ?? hourly?.temperatureF ?? null,
description: stringValue(properties?.textDescription) || hourly?.shortForecast || "Current conditions",
humidityPercent: finiteNumber(properties?.relativeHumidity?.value),
windSpeedMph,
windDirection: windDegrees === null ? hourly?.windDirection ?? "" : degreesToCompass(windDegrees),
timestampMs: timestampValue(properties?.timestamp),
observed: true,
};
}
function currentFromHourly(hourly: ForecastPeriod | undefined, fallback: ForecastPeriod): CurrentWeather {
const source = hourly ?? fallback;
return {
temperatureF: source.temperatureF,
description: source.shortForecast || "Current forecast",
humidityPercent: null,
windSpeedMph: firstNumberInText(source.windSpeed),
windDirection: source.windDirection,
timestampMs: source.startTimeMs || null,
observed: false,
};
}
function normalizeForecastPeriod(value: NwsForecastPeriodResponse): ForecastPeriod | null {
if (!value || typeof value !== "object") return null;
const name = stringValue(value.name);
const temperature = finiteNumber(value.temperature);
const unit = stringValue(value.temperatureUnit).toUpperCase();
return {
name: name || "Forecast",
startTimeMs: timestampValue(value.startTime) ?? 0,
temperatureF: temperature === null ? null : unit === "C" ? temperature * 9 / 5 + 32 : temperature,
shortForecast: stringValue(value.shortForecast),
detailedForecast: stringValue(value.detailedForecast),
precipitationPercent: finiteNumber(value.probabilityOfPrecipitation?.value),
windSpeed: stringValue(value.windSpeed),
windDirection: stringValue(value.windDirection),
isDaytime: Boolean(value.isDaytime),
};
}
function convertTemperatureToF(measure: NwsMeasure | undefined): number | null {
const value = finiteNumber(measure?.value);
if (value === null) return null;
const unit = stringValue(measure?.unitCode).toLowerCase();
if (unit.includes("degc")) return value * 9 / 5 + 32;
return value;
}
function convertSpeedToMph(measure: NwsMeasure | undefined): number | null {
const value = finiteNumber(measure?.value);
if (value === null) return null;
const unit = stringValue(measure?.unitCode).toLowerCase();
if (unit.includes("km_h")) return value * 0.621371;
if (unit.includes("m_s")) return value * 2.23694;
return value;
}
function pointLocationName(point: NwsPoi
614C
ntResponse, latitude: string, longitude: string): string {
const relative = point.properties?.relativeLocation?.properties;
const city = stringValue(relative?.city);
const state = stringValue(relative?.state);
return [city, state].filter(Boolean).join(", ") || `${latitude}, ${longitude}`;
}
function degreesToCompass(value: number): string {
const directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
return directions[Math.round((((value % 360) + 360) % 360) / 45) % directions.length]!;
}
function firstNumberInText(value: string): number | null {
const match = value.match(/\d+(?:\.\d+)?/);
return match ? Number(match[0]) : null;
}
function finiteNumber(value: unknown): number | null {
if (value === null || value === undefined || typeof value === "boolean") return null;
if (typeof value === "string" && !value.trim()) return null;
const number = typeof value === "number" ? value : Number(value);
return Number.isFinite(number) ? number : null;
}
function timestampValue(value: unknown): number | null {
if (typeof value !== "string") return null;
const timestamp = Date.parse(value);
return Number.isFinite(timestamp) ? timestamp : null;
}
function stringValue(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
function friendlyWeatherError(error: unknown): string {
const message = (error as Error)?.message || String(error);
if (/network request failed|failed to fetch|unable to resolve host/i.test(message)) {
return "Couldn't reach the National Weather Service. Check the phone's connection and retry.";
}
return message;
}
function cloneState(state: WeatherState): WeatherState {
return {
...state,
current: state.current ? { ...state.current } : null,
forecast: state.forecast.map((period) => ({ ...period })),
};
}
export const weatherBridge = new WeatherBridge();