fix: select full IMGW Hybrid current record
This commit is contained in:
@@ -32,8 +32,20 @@ function getCondition(weatherCode: number | null, rainfall10m: number | null, sn
|
||||
return null;
|
||||
}
|
||||
|
||||
function getCurrentUtcHour() {
|
||||
return new Date().toISOString().slice(0, 13);
|
||||
function hasNumericValue(value: unknown) {
|
||||
return toNumber(value) !== null;
|
||||
}
|
||||
|
||||
function isFullWeatherRow(candidate: RawImgwHybridWeatherRow) {
|
||||
return hasNumericValue(candidate.Temperature)
|
||||
&& hasNumericValue(candidate.Chill)
|
||||
&& hasNumericValue(candidate.Humidity)
|
||||
&& hasNumericValue(candidate.Wind_Speed)
|
||||
&& hasNumericValue(candidate.PressureMSL);
|
||||
}
|
||||
|
||||
function hasPrecipitationValue(candidate: RawImgwHybridWeatherRow) {
|
||||
return hasNumericValue(candidate.Precipitation10m) || hasNumericValue(candidate.Rain10m) || hasNumericValue(candidate.Snow10m);
|
||||
}
|
||||
|
||||
export function normalizeImgwCurrentWeather(payload: RawImgwHybridWeatherResponse): ImgwCurrentWeather | null {
|
||||
@@ -45,16 +57,19 @@ export function normalizeImgwCurrentWeather(payload: RawImgwHybridWeatherRespons
|
||||
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 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 fullRow = rows.find((candidate) => candidate.Type === "Type_Ten_Minutes" && isFullWeatherRow(candidate))
|
||||
?? rows.find((candidate) => candidate.Type === "Type_Hour" && isFullWeatherRow(candidate));
|
||||
const precipitationRow = fullRow && hasPrecipitationValue(fullRow)
|
||||
? fullRow
|
||||
: rows.find((candidate) => candidate.Type === "Type_Ten_Minutes" && hasPrecipitationValue(candidate));
|
||||
const row = fullRow ?? precipitationRow;
|
||||
if (!row) return null;
|
||||
|
||||
const measuredAt = normalizeDate(row.Date);
|
||||
if (!measuredAt) return null;
|
||||
const rainfall10m = toNumber(row.Rain10m);
|
||||
const snowfall10m = toNumber(row.Snow10m);
|
||||
const precipitationSource = precipitationRow ?? row;
|
||||
const rainfall10m = toNumber(precipitationSource.Rain10m);
|
||||
const snowfall10m = toNumber(precipitationSource.Snow10m);
|
||||
const weatherCode = getWeatherCode(row.Icon10);
|
||||
|
||||
return {
|
||||
@@ -66,7 +81,7 @@ export function normalizeImgwCurrentWeather(payload: RawImgwHybridWeatherRespons
|
||||
windDirection: toNumber(row.Wind_Dir),
|
||||
humidity: toNumber(row.Humidity),
|
||||
pressure: toHectopascals(row.PressureMSL),
|
||||
precipitation10m: toNumber(row.Precipitation10m),
|
||||
precipitation10m: toNumber(precipitationSource.Precipitation10m),
|
||||
rainfall10m,
|
||||
snowfall10m,
|
||||
cloudCover: toNumber(row.Cloud),
|
||||
|
||||
Reference in New Issue
Block a user