-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWeatherConnector.php
427 lines (416 loc) · 16.3 KB
/
WeatherConnector.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php /*
* GNU General Public License v3.0
* Contributors: ADD YOUR NAME HERE, Mike P. Sinn
*/ /** @noinspection SpellCheckingInspection */
namespace App\DataSources\Connectors;
use App\DataSources\TooEarlyException;
use App\Exceptions\IncompatibleUnitException;
use App\Exceptions\InvalidVariableValueAttributeException;
use App\Exceptions\InvalidVariableValueException;
use App\Exceptions\TooSlowException;
use App\Slim\Controller\Connector\ConnectorException;
use App\Types\TimeHelper;
use App\Units\PartsPerMillionUnit;
use App\Utils\Env;
use App\Utils\Stats;
use App\Variables\CommonVariables\EnvironmentCommonVariables\AirQualityIndexCommonVariable;
use Cmfcmf\OpenWeatherMap;
use DateTime;
use Http\Client\Exception\HttpException;
use Http\Factory\Guzzle\RequestFactory;
use App\Exceptions\NoGeoDataException;
use App\DataSources\LocationBasedConnector;
use App\Slim\Model\QMUnit;
use App\Units\HoursUnit;
use App\Units\IndexUnit;
use App\Units\MilesPerHourUnit;
use App\Units\MillimetersUnit;
use App\Units\PercentUnit;
use App\VariableCategories\EnvironmentVariableCategory;
use App\Variables\CommonVariables\EnvironmentCommonVariables\AverageDailyOutdoorTemperatureCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\BarometricPressureCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\CloudCoverCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\DailyHighOutdoorTemperatureCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\DailyLowOutdoorTemperatureCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\OutdoorHumidityCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\PrecipitationCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\TimeBetweenSunriseAndSunsetCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\WindSpeedCommonVariable;
use App\Variables\CommonVariables\EnvironmentCommonVariables\UVIndexCommonVariable;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
class WeatherConnector extends LocationBasedConnector {
const POLLUTION_CO = "CO";
const POLLUTION_NO_2 = "NO2";
const POLLUTION_O3 = "O3";
const POLLUTION_SO_2 = "SO2";
protected $useFileResponsesInTesting = false; // TODO: Serialize OWM response
protected const AFFILIATE = false;
protected const BACKGROUND_COLOR = '#1e2023';
protected const CLIENT_REQUIRES_SECRET = false;
protected const DEVELOPER_CONSOLE = 'https://developer.worldweatheronline.com';
public const DISPLAY_NAME = 'Weather';
protected const ENABLED = 1;
protected const GET_IT_URL = '';
protected const LOGO_COLOR = '#2d2d2d';
protected const LONG_DESCRIPTION = 'Automatically import temperature, humidity, and ultraviolet light exposure.';
protected const SHORT_DESCRIPTION = 'Tracks weather.';
public static $BASE_API_URL = 'https://api.openweathermap.org';
/**
* @var string The basic api url to fetch weather data from.
*/
private $weatherUrl = 'https://api.openweathermap.org/data/2.5/weather?';
/**
* @var string The basic api url to fetch weather group data from.
*/
private $weatherGroupUrl = 'https://api.openweathermap.org/data/2.5/group?';
/**
* @var string The basic api url to fetch weekly forecast data from.
*/
private $weatherHourlyForecastUrl = 'https://api.openweathermap.org/data/2.5/forecast?';
/**
* @var string The basic api url to fetch daily forecast data from.
*/
private $weatherDailyForecastUrl = 'https://api.openweathermap.org/data/2.5/forecast/daily?';
/**
* @var string The basic api url to fetch uv index data from.
*/
private $uvIndexUrl = 'https://api.openweathermap.org/data/2.5/uvi';
/**
* @var string The basic api url to fetch air pollution data from.
*/
private $airPollutionUrl = 'https://api.openweathermap.org/pollution/v1/';
protected $allowMeasurementsForCurrentDay = true;
public $affiliate = self::AFFILIATE;
public $backgroundColor = self::BACKGROUND_COLOR;
public $clientRequiresSecret = self::CLIENT_REQUIRES_SECRET;
public $displayName = self::DISPLAY_NAME;
public $enabled = self::ENABLED;
public $getItUrl = self::GET_IT_URL;
public $id = self::ID;
public $image = self::IMAGE;
public $logoColor = self::LOGO_COLOR;
public $longDescription = self::LONG_DESCRIPTION;
public $name = self::NAME;
public $shortDescription = self::SHORT_DESCRIPTION;
public $synonyms = ['Weather'];
public const ID = 13;
public const NAME = 'worldweatheronline';
/**
* @return void
* @throws ConnectorException
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
public function importData(): void {
try {
$this->importPollution();
} catch (TooEarlyException|InvalidVariableValueAttributeException|
NoGeoDataException|TooSlowException $e) {
$this->exceptionIfTesting($e);
}
$this->importUVIndex();
$this->importWeather();
$this->saveMeasurements();
}
/**
* @throws ConnectorException
* @throws InvalidVariableValueAttributeException
* @throws NoGeoDataException
* @throws TooEarlyException
* @throws TooSlowException
*/
private function importPollution(){
$lat = $this->getLatitude();
$lon = $this->getLongitude();
$start = $this->getFromTime();
$fromAt = db_date($start);
$end = $this->getEndTime();
$endAt = db_date($end);
if($start > $end){
le("Start time $fromAt is after end time $endAt");
}
/** @noinspection HttpUrlsUsage */
$response = $this->getRequest(
"http://api.openweathermap.org/data/2.5/air_pollution/history?lat=$lat&lon=$lon&start=$start&end=$end&appid=".$this->getAppId());
$typesByDate = [];
foreach ($response->list as $day) {
$timestamp = $day->dt;
$rounded = Stats::roundToNearestMultipleOf($timestamp, 86400);
$startAt = db_date($rounded);
foreach ($day->components as $type => $value) {
$typesByDate[$startAt][$type][] = $value;
}
$typesByDate[$startAt][AirQualityIndexCommonVariable::NAME][] = $day->main->aqi;
}
foreach ($typesByDate as $startAt => $types) {
if($startAt <= $fromAt){
continue;
}
foreach ($types as $type => $values) {
$avg = array_sum($values) / count($values);
if($type === AirQualityIndexCommonVariable::NAME){
$this->addWeather(AirQualityIndexCommonVariable::NAME, $avg, $startAt, IndexUnit::NAME);
} else {
$this->savePollutionComponent($type, $avg, $startAt);
}
}
}
//$this->getPollution(self::POLLUTION_O3);
//$this->getPollution(self::POLLUTION_NO_2);
//$this->getPollution(self::POLLUTION_SO_2);
//$this->getPollution(self::POLLUTION_CO);
}
/**
* @param int|string $type
* @param mixed $value
* @param mixed $day
* @return void
* @throws InvalidVariableValueAttributeException
* @throws TooEarlyException
* @throws TooSlowException
*/
public function savePollutionComponent(string $type, float $value, string $startAt): void{
$name = strtoupper($type)." Pollution";
if($type === "no"){
$name = "Nitrous Oxide NO Pollution";
}
if($type === "co"){
$name = "Carbon Monoxide CO Pollution";
}
if($type === "nh3"){
$name = "Ammonia NH3 Pollution";
}
if($type === "o3"){
$name = "Ground-Level Ozone (O3) Pollution Pollution";
}
if($type === "pm2_5"){
$name = "PM2.5 Fine Particulate Matter Pollution";
}
if($type === "pm10"){
$name = "PM10 Particlulate Matter Pollution";
}
if($type === "so2"){
$name = "Sulfur Dioxide SO2 Pollution";
}
$this->addWeather($name, $value, $startAt, PartsPerMillionUnit::NAME);
}
/**
* @param string $type
* @throws \App\DataSources\TooEarlyException
* @throws \App\Exceptions\InvalidVariableValueAttributeException
* @throws \App\Exceptions\NoGeoDataException
* @throws \App\Exceptions\TooSlowException
* @throws \Cmfcmf\OpenWeatherMap\Exception
*/
private function getPollution(string $type){
$owm = $this->getOpenWeatherMapClient();
try {
$data = $owm->getAirPollution($type, (string)$this->getLatitude(), (string)$this->getLongitude());
$this->addWeather("$type Pollution", $data->value->getValue(), $data->time->getTimestamp(),
$data->value->getUnit());
} catch (HttpException $e){
if(strpos($e->getMessage(), "404 Not Found") !== false ||
strpos($e->getMessage(), "502") !== false){
$geo = $this->getGeoData();
$str = $geo->print();
$this->logError("Could not get $type pollution for $str beause: ".$e->getMessage());
} else {
le($e);
}
}
}
/**
* @throws InvalidVariableValueAttributeException
* @throws OpenWeatherMap\Exception
* @throws TooSlowException
*/
private function importWeather() {
$owm = $this->getOpenWeatherMapClient();
$this->setCurrentUrl($this->weatherUrl);
$weather = $owm->getWeather($this->getLatLongQueryParams());
//$this->saveConnectorRequestResponse(__FUNCTION__.": Can't get it from OpenWeatherMap", $weather);
$this->savePrecipitation($weather);
$this->savePressure($weather);
$this->saveCloudCover($weather);
$this->saveHumidity($weather);
$this->saveTemp($weather);
$this->addTimeBetweenSunriseAndSunsetMeasurement($weather);
$this->saveWind($weather);
}
/**
* @param $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function addTimeBetweenSunriseAndSunsetMeasurement($weather){
$set = $weather->sun->set->getTimestamp();
$rise = $weather->sun->rise->getTimestamp();
$hoursBetweenSunriseAndSunset = ($set - $rise) / 3600;
$this->addWeather(TimeBetweenSunriseAndSunsetCommonVariable::NAME,
$hoursBetweenSunriseAndSunset, $rise, HoursUnit::NAME);
}
/**
* @throws TooSlowException
* @throws InvalidVariableValueAttributeException
* @throws \Exception
*/
public function importUVIndex() {
$v = $this->getWeatherUserVariable(UVIndexCommonVariable::NAME, IndexUnit::NAME);
$latest = $v->getLatestTaggedMeasurementAt();
if(strtotime($latest) > time() - 86400){
$this->logInfo("Not importing UVIndex because getLatestTaggedMeasurementAt is ".
TimeHelper::timeSinceHumanString($latest));
return;
}
if (!$latest) {
$latest = $this->getFromAt();
//$latest = "2012-11-15"; // I already tried increasing this but it didn't fix 504 Gateway Time-out
if(strtotime($latest) > time() - 86400){
$this->logInfo("Not importing UVIndex because getFromAt is ".
TimeHelper::timeSinceHumanString($latest));
return;
}
}
$owm = $this->getOpenWeatherMapClient();
$this->setCurrentFromAndEndTime($latest, 0);
try {
$history = $owm->getHistoricUVIndex($this->getLatitude(), $this->getLongitude(),
new DateTime($latest), new DateTime("now"));
$this->saveConnectorRequestResponse($this->uvIndexUrl.": Can't get full URL from OpenWeatherMap", $history);
} catch (OpenWeatherMap\Exception $e) {
$this->logError("Could not get UV Index because ". $e->getMessage());
return;
} catch (NoGeoDataException $e) {
$this->logError(__METHOD__.": ".$e->getMessage());
return;
}
$this->setCurrentUrl($this->uvIndexUrl);
foreach ($history as $day) {
$this->addMeasurement($v->name,
$day->time->getTimestamp(),
$day->uvIndex,
IndexUnit::NAME,
EnvironmentVariableCategory::NAME,
[],
86400);
}
}
/**
* @return OpenWeatherMap
*/
private function getOpenWeatherMapClient(): OpenWeatherMap {
// Create OpenWeatherMap object.
// Don't use caching (take a look into Examples/Cache.php to see how it works).
// You can use every PSR-17 compatible HTTP request factory
// and every PSR-18 compatible HTTP client. This example uses
// `http-interop/http-factory-guzzle` and `php-http/guzzle6-adapter`
// which you need to install separately.
$c = new GuzzleAdapter($this->getHttpClient());
//$c = $this->getHttpClient();
return new OpenWeatherMap($this->getAppId(), $c, new RequestFactory());
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function savePressure(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$uv = $this->getQMUserVariable(BarometricPressureCommonVariable::NAME);
$userUnit = $uv->getUserUnit();
$providedUnit = QMUnit::find($weather->pressure->getUnit());
try {
$inUserUnit = $providedUnit->convertTo($weather->pressure->getValue(), $userUnit->id, $uv);
} catch (IncompatibleUnitException | InvalidVariableValueException $e) {
/** @var \LogicException $e */
throw $e;
}
$this->addWeather(BarometricPressureCommonVariable::NAME,
$inUserUnit,
$startTime,
$userUnit->name);
}
/**
* @return array
*/
private function getLatLongQueryParams(){
try {
$query = [
'lat' => $this->getLatitude(),
'lon' => $this->getLongitude()
];
} catch (NoGeoDataException $e) {
$this->logInfo(__METHOD__.": ".$e->getMessage());
if($zip = $this->getZip()){
$query = 'zip:' . $zip;
}
}
return $query;
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
* @throws TooSlowException
*/
private function saveTemp(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$low = $weather->temperature->min->getValue();
$high = $weather->temperature->max->getValue();
$tempUnit = $weather->temperature->now->getUnit();
$avg = ($low + $high) / 2;
if($avg < -20){le("Low is $low $tempUnit and high is $high $tempUnit and avg is $avg $tempUnit");}
$this->addWeather(AverageDailyOutdoorTemperatureCommonVariable::NAME, $avg, $startTime,
$tempUnit);
$this->addWeather(DailyHighOutdoorTemperatureCommonVariable::NAME, $high, $startTime, $tempUnit);
$this->addWeather(DailyLowOutdoorTemperatureCommonVariable::NAME, $low, $startTime, $tempUnit);
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function saveWind(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$this->addWeather(WindSpeedCommonVariable::NAME, $weather->wind->speed->getValue(), $startTime,
MilesPerHourUnit::NAME);
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function saveHumidity(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$this->addWeather(OutdoorHumidityCommonVariable::NAME, $weather->humidity->getValue(), $startTime,
$weather->humidity->getUnit());
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function saveCloudCover(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$this->addWeather(CloudCoverCommonVariable::NAME, $weather->clouds->getValue(), $startTime, PercentUnit::NAME);
}
/**
* @param OpenWeatherMap\CurrentWeather $weather
* @throws InvalidVariableValueAttributeException
* @throws TooSlowException
*/
private function savePrecipitation(OpenWeatherMap\CurrentWeather $weather): void{
$startTime = $weather->lastUpdate->getTimestamp();
$this->addWeather(PrecipitationCommonVariable::NAME, $weather->precipitation->getValue(), $startTime,
MillimetersUnit::NAME);
}
/**
* @throws NoGeoDataException
*/
private function getGeoData(): \App\Models\IpDatum{
return $this->getUser()->getIpGeoLocation();
}
private function getAppId(): string{
return Env::getRequired('CONNECTOR_OPEN_WEATHER_MAP_API_KEY');
}
}