fix: select current IMGW Hybrid records
This commit is contained in:
@@ -32,17 +32,22 @@ function getCondition(weatherCode: number | null, rainfall10m: number | null, sn
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCurrentUtcHour() {
|
||||
return new Date().toISOString().slice(0, 13);
|
||||
}
|
||||
|
||||
export function normalizeImgwCurrentWeather(payload: RawImgwHybridWeatherResponse): ImgwCurrentWeather | null {
|
||||
if (!payload.data?.Valid || !Array.isArray(payload.data.Data)) return null;
|
||||
|
||||
const rows = payload.data.Data
|
||||
.filter((candidate): candidate is RawImgwHybridWeatherRow => {
|
||||
if (!candidate || typeof candidate !== "object") return false;
|
||||
return candidate.Type === "Type_Ten_Minutes" && normalizeDate(candidate.Date) !== null;
|
||||
return (candidate.Type === "Type_Ten_Minutes" || candidate.Type === "Type_Hour") && normalizeDate(candidate.Date) !== null;
|
||||
})
|
||||
.sort((left, right) => String(left.Date).localeCompare(String(right.Date)));
|
||||
const fullRow = rows.find((candidate) => typeof candidate.MODEL === "string" && candidate.MODEL.includes("AROME"));
|
||||
const precipitationRow = rows.find((candidate) => candidate.Precipitation10m !== undefined);
|
||||
const currentUtcHour = getCurrentUtcHour();
|
||||
const fullRow = rows.find((candidate) => String(candidate.Date).startsWith(currentUtcHour) && candidate.Temperature !== undefined);
|
||||
const precipitationRow = rows.find((candidate) => String(candidate.Date).startsWith(currentUtcHour) && candidate.Precipitation10m !== undefined);
|
||||
const row = fullRow ?? precipitationRow;
|
||||
if (!row) return null;
|
||||
|
||||
@@ -53,7 +58,7 @@ export function normalizeImgwCurrentWeather(payload: RawImgwHybridWeatherRespons
|
||||
const weatherCode = getWeatherCode(row.Icon10);
|
||||
|
||||
return {
|
||||
coverage: fullRow ? "full" : "precipitation-only",
|
||||
coverage: fullRow?.Type === "Type_Ten_Minutes" ? "full" : fullRow ? "hourly" : "precipitation-only",
|
||||
measuredAt,
|
||||
temperature: toCelsius(row.Temperature),
|
||||
feelsLike: toCelsius(row.Chill),
|
||||
|
||||
Reference in New Issue
Block a user