Skip to content

Commit

Permalink
Fix moth trap weather values
Browse files Browse the repository at this point in the history
  • Loading branch information
kazlauskis committed Nov 19, 2024
1 parent 942f5ae commit f8026b4
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/common/services/openWeather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,38 @@ type Weather = {
windSpeed: string | null;
};

type HistoricalWeatherData = {
dt: number;
sunrise: number;
sunset: number;
temp: number;
feels_like: number;
pressure: number;
humidity: number;
dew_point: number;
uvi: number;
clouds: number;
visibility: number;
wind_speed: number;
wind_deg: number;
wind_gust: number;
weather: {
id: number;
main: string;
description: string;
icon: string;
}[];
rain: {
'1h': number;
};
};

export interface HistoricalWeatherRemoteRes {
lat: number;
lon: number;
timezone: string;
timezone_offset: number;
current: {
dt: number;
sunrise: number;
sunset: number;
temp: number;
feels_like: number;
pressure: number;
humidity: number;
dew_point: number;
uvi: number;
clouds: number;
visibility: number;
wind_speed: number;
wind_deg: number;
wind_gust: number;
weather: { id: number; main: string; description: string; icon: string }[];
rain: { '1h': number };
};
data: HistoricalWeatherData[]
}

const url = config.weatherSiteUrl;
Expand Down Expand Up @@ -151,20 +160,18 @@ export const fetchHistoricalWeather = async (
const unixTimestamp = Math.floor(new Date(date).getTime() / 1000);

try {
const res = await axios(
const res = await axios<HistoricalWeatherRemoteRes>(
`${url}/data/3.0/onecall/timemachine?lat=${latitude}&lon=${longitude}&dt=${unixTimestamp}&appid=${config.weatherSiteApiKey}&only_current=true&units=metric`
);

const normaliseResponseValues = ({
current,
}: HistoricalWeatherRemoteRes): Weather => ({
const normaliseResponseValues = (current:HistoricalWeatherData): Weather => ({
temperature: getTemperature(current?.temp),
windSpeed: getWindSpeed(current?.wind_speed),
windDirection: getWindDirection(current?.wind_deg),
cloud: getCloud(current.clouds),
});

return normaliseResponseValues(res.data);
return normaliseResponseValues(res.data.data?.[0]);
} catch (error) {
console.error(error);
}
Expand Down

0 comments on commit f8026b4

Please sign in to comment.