-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindData.tsx
157 lines (140 loc) · 4.04 KB
/
WindData.tsx
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
import { useEffect, useState } from "react";
import { StyleSheet, Text, View } from "react-native";
import { Entypo } from "@expo/vector-icons";
import * as Location from "expo-location";
interface SmhiParameter {
name: string;
unit: string;
values: number[];
}
interface SmhiHour {
validTime: string;
parameters: SmhiParameter[];
}
const fetchController = new AbortController();
const WindData = () => {
const [location, setLocation] = useState<Location.LocationObject | null>(
null
);
const [heading, setHeading] = useState<number>(0);
const [isLoading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [hourData, setHourData] = useState<SmhiHour | null>(null);
useEffect(() => {
(async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== "granted") {
console.error("Permission to access location was denied");
return;
}
const location = await Location.getCurrentPositionAsync({});
console.log("Got location", location);
setLocation(location);
})();
Location.watchHeadingAsync((loc) => {
setHeading(loc.magHeading);
});
}, []);
const getSmhiParameterValue = (name: string) =>
hourData?.parameters.find((hour) => hour.name === name);
const getWeatherData = async () => {
const lon = location?.coords.longitude.toFixed(6);
const lat = location?.coords.latitude.toFixed(6);
const url = `https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/${lon}/lat/${lat}/data.json`;
const timeoutId = setTimeout(() => {
console.error("Timed out out after 3s");
setError("Timed out out after 3s");
fetchController.abort();
}, 3000);
try {
console.log("Going to fetch", url);
const response = await fetch(url, { signal: fetchController.signal });
const json = await response.json();
console.log("Fetched weather data with", json.approvedTime);
setHourData(json.timeSeries[0]);
} catch (error) {
console.error(error);
setError(`Kunde inte ladda: ${url}`);
} finally {
setLoading(false);
clearTimeout(timeoutId);
}
};
useEffect(() => {
if (location) {
getWeatherData();
}
}, [location]);
if (error) {
return (
<View style={styles.container}>
<Text style={styles.error}>Åh nej! {error}</Text>
</View>
);
}
const arrowHeading =
(getSmhiParameterValue("wd")?.values[0] || 0) - heading + 180;
return (
<View style={styles.container}>
{isLoading && <Text>Laddar väderdata</Text>}
{!isLoading && (
<>
<Entypo
name="arrow-long-up"
size={160}
color="black"
style={[
styles.windDirection,
{
color: `hsl(${Math.abs(90 - arrowHeading / 2)}, 100%, 40%)`,
transform: [
{
rotate: `${
(getSmhiParameterValue("wd")?.values[0] || 0) -
heading +
180
}deg`,
},
],
},
]}
/>
<Text>
Vindhastighet:{" "}
<Text style={styles.unit}>
{getSmhiParameterValue("ws")?.values[0]}{" "}
{getSmhiParameterValue("ws")?.unit}
</Text>
</Text>
<Text>
Vindriktning:
<Text style={styles.unit}>
{getSmhiParameterValue("wd")?.values[0]}{" "}
{getSmhiParameterValue("wd")?.unit}
</Text>
</Text>
</>
)}
</View>
);
};
export default WindData;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
unit: {
fontSize: 36,
fontWeight: "bold",
},
error: {
color: "#c00",
fontWeight: "bold",
},
windDirection: {
marginBottom: 30,
},
});