-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather-api.php
223 lines (187 loc) · 6.65 KB
/
weather-api.php
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
<?php
/**
* Converts a degree to a direction.
* @param int $degree The degree to convert.
* @return string The direction.
*/
function degree_to_direction($degree) {
$directions = [
"north",
"nne",
"northeast",
"ene",
"east",
"ese",
"southeast",
"sse",
"south",
"ssw",
"southwest",
"wsw",
"west",
"wnw",
"northwest",
"nnw"
];
$index = round($degree / 22.5) % 16;
return $directions[$index];
}
function fetch_current_weather_data() {
$station_url = 'https://movil.puertos.es/cma2/app/CMA/adhoc/station_data?station=';
$urls = [
'barcelona_buoy' => $station_url . '1731¶ms=WaterTemp,Hm0,Hmax,MeanDir180,MeanDir',
'barcelona_port' => $station_url . '4752¶ms=AirTemp,WindSpeed,WindSpeedMax,WindDir180,WindDir',
];
$mh = curl_multi_init();
$requests = [];
foreach ( $urls as $location => $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$requests[$location] = $ch;
curl_multi_add_handle( $mh, $ch );
}
$active = null;
do {
$status = curl_multi_exec( $mh, $active );
if ( $active ) {
curl_multi_select( $mh );
}
} while ( $active && $status == CURLM_OK );
$responses = [];
foreach ( $requests as $location => $ch ) {
if ( curl_errno( $ch ) ) {
error_log( 'Spearfishing Weather cUrl error: ' . $location . ' - ' . curl_error( $ch ) );
} else {
$responses[$location] = json_decode( curl_multi_getcontent( $ch ), true );
}
curl_multi_remove_handle( $mh, $ch );
curl_close( $ch );
}
curl_multi_close( $mh );
$current_weather_data = [];
foreach ( $responses as $location => $response ) {
$latest_data = end( $response['content'][1] );
switch ( $location ) {
case 'barcelona_buoy' :
$current_weather_data['wave_temperature'] = round( $latest_data[1][0], 1 );
$current_weather_data['wave_height'] = round( $latest_data[2][0], 1 );
$current_weather_data['wave_height_max'] = round( $latest_data[3][0], 1 );
$current_weather_data['wave_direction_to_degree'] = $latest_data[4][0] ?? null;
$current_weather_data['wave_direction_from_degree'] = $latest_data[5][0] ?? null;
$current_weather_data['wave_direction'] = degree_to_direction( $latest_data[5][0] ?? null );
break;
case 'barcelona_port' :
$current_weather_data['wind_temperature'] = round( $latest_data[1][0], 1 );
$current_weather_data['wind_speed'] = round( $latest_data[2][0] * 2.237, 1 ); // M/S converted to MPH
$current_weather_data['wind_speed_max'] = round( $latest_data[3][0] * 2.237, 1 ); // M/S converted to MPH
$current_weather_data['wind_direction_to_degree'] = $latest_data[4][0] ?? null;
$current_weather_data['wind_direction_from_degree'] = $latest_data[5][0] ?? null;
$current_weather_data['wind_direction'] = degree_to_direction( $latest_data[5][0] ?? null );
break;
}
}
return $current_weather_data;
}
/**
* Gets the current weather data and caches it for 10 minutes.
* @return array The current weather data.
*/
function get_current_weather_data() {
$cache_key = 'spearfishing_current_weather_data';
$cached_response = wp_cache_get( $cache_key );
if ( ! $cached_response ) {
$current_weather_data = fetch_current_weather_data();
wp_cache_set( $cache_key, $current_weather_data, '', 600 );
return $current_weather_data;
}
return $cached_response;
}
function fetch_forecast_weather_data() {
$forecast_url = 'https://movil.puertos.es/simo/item/';
$urls = [
'wave' => $forecast_url . 'Siwana/712018014/forecast_view?fields=Datetime,Hm0,MeanDir180,MeanDir',
'wind' => $forecast_url . 'Atmosfera/712018014/forecast_view?fields=Datetime,WindSpeed,WindDir180,WindDir',
];
$mh = curl_multi_init();
$requests = [];
foreach ( $urls as $type => $url ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$requests[$type] = $ch;
curl_multi_add_handle( $mh, $ch );
}
$active = null;
do {
$status = curl_multi_exec( $mh, $active );
if ( $active ) {
curl_multi_select( $mh );
}
} while ( $active && $status == CURLM_OK );
$responses = [];
foreach ( $requests as $type => $ch ) {
if ( curl_errno( $ch ) ) {
error_log( 'Spearfishing Weather cUrl error: ' . $type . ' - ' . curl_error( $ch ) );
} else {
$responses[$type] = json_decode( curl_multi_getcontent( $ch ), true );
}
curl_multi_remove_handle( $mh, $ch );
curl_close( $ch );
}
curl_multi_close( $mh );
$forecast_weather_data = [];
$barcelona_timezone = new DateTimeZone( 'Europe/Madrid' );
foreach ( $responses as $type => $response ) {
foreach ( $response['content'] as $data ) {
$timestamp = $data[0];
$epoch_time = new DateTime( "@$timestamp", $barcelona_timezone );
$hour = $epoch_time->format('H');
$day = $epoch_time->format('j-n');
// Only get data for 6am to 9pm
if ( $hour < 7 || $hour > 20 ) {
continue;
}
if ( ! isset( $forecast_weather_data[$day][$hour] ) ) {
$forecast_weather_data[$day][$hour] = [];
}
switch ( $type ) {
case 'wind':
$forecast_weather_data[$day][$hour]['wind_speed'] = number_format( round( $data[1] * 2.237, 1), 1, '.', ''); // M/S converted to MPH
$forecast_weather_data[$day][$hour]['wind_direction_to_degree'] = $data[2];
$forecast_weather_data[$day][$hour]['wind_direction_from_degree'] = $data[3];
break;
case 'wave':
$forecast_weather_data[$day][$hour]['wave_height'] = number_format( round( $data[1], 1 ), 1, '.', '');
$forecast_weather_data[$day][$hour]['wave_direction_to_degree'] = $data[2];
$forecast_weather_data[$day][$hour]['wave_direction_from_degree'] = $data[3];
break;
}
}
}
$filtered_forecast_weather_data = calculate_hour_rating( $forecast_weather_data );
return $filtered_forecast_weather_data;
}
function calculate_hour_rating( $data ) {
return array_map(
fn( $hours ) => array_map(
fn( $hour_data ) => array_merge( $hour_data, ['rating' => $hour_data['wind_speed'] + $hour_data['wave_height'] * 10]),
$hours
),
$data
);
}
/**
* Gets the forecast weather data and caches it for one hour.
* @return array The forecast weather data.
*/
function get_forecast_weather_data() {
$cache_key = 'spearfishing_forecast_weather_data';
$cached_response = wp_cache_get( $cache_key );
if ( ! $cached_response ) {
$forecast_weather_data = fetch_forecast_weather_data();
wp_cache_set( $cache_key, $forecast_weather_data, '', MINUTE_IN_SECONDS * 30 );
return $forecast_weather_data;
}
return $cached_response;
}