1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Models; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
class Weather implements ContainerInjectableInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
use ContainerInjectableTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* location, geolocation and accesskey |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
public $location; |
18
|
|
|
public $geolocation; |
19
|
|
|
protected $accessKey; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set access key for openweather. |
25
|
|
|
* @method __construct |
26
|
|
|
*/ |
27
|
5 |
|
public function __construct() |
28
|
|
|
{ |
29
|
5 |
|
$prep = require ANAX_INSTALL_PATH . "/config/keys.php"; |
30
|
5 |
|
$this->accessKey = $prep["openWeather"]; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Convert input from page form into latitude and longitud. |
37
|
|
|
* @method loadGeolocation |
38
|
|
|
* @param string $location |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
5 |
|
public function loadGeolocation(string $location) |
42
|
|
|
{ |
43
|
5 |
|
$this->location = html_entity_decode($location); |
|
|
|
|
44
|
|
|
|
45
|
5 |
|
$base = 'https://nominatim.openstreetmap.org/'; |
46
|
5 |
|
$init = curl_init("{$base}?format=json&addressdetails=1&q={$this->location}&limit=1&[email protected]"); |
47
|
5 |
|
curl_setopt($init, CURLOPT_RETURNTRANSFER, true); |
48
|
5 |
|
$json = curl_exec($init); |
49
|
5 |
|
curl_close($init); |
50
|
|
|
|
51
|
5 |
|
$geoArr = json_decode($json, true); |
|
|
|
|
52
|
|
|
|
53
|
5 |
|
if (!$this->location) : |
54
|
1 |
|
$this->geolocation = array( |
55
|
1 |
|
"status" => "error", |
56
|
|
|
"message" => "No input detected" |
57
|
|
|
); |
58
|
4 |
|
elseif ($geoArr == null) : |
59
|
1 |
|
$this->geolocation = array( |
60
|
1 |
|
"status" => "error", |
61
|
1 |
|
"message" => "{$location} couldnt be converted to a lontitud, latitude" |
62
|
|
|
); |
63
|
|
|
else : |
64
|
3 |
|
$this->geolocation = array( |
65
|
3 |
|
"latitude" => $geoArr[0]['lat'], |
66
|
3 |
|
"longitude" => $geoArr[0]['lon'], |
67
|
3 |
|
"status" => "success", |
68
|
3 |
|
"geolocation" => $geoArr[0]['address'] |
69
|
|
|
); |
70
|
|
|
endif; |
71
|
|
|
|
72
|
5 |
|
return $this->geolocation; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Load 7 upcoming weather data from "openweathermap" - no multicurl! |
79
|
|
|
* @method loadWeather |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
2 |
|
public function loadWeather() |
83
|
|
|
{ |
84
|
2 |
|
if ($this->geolocation['status'] == "success") : |
85
|
1 |
|
$base = 'https://api.openweathermap.org/data/2.5/'; |
86
|
1 |
|
$init = curl_init("{$base}onecall?lat={$this->geolocation['latitude']}&lon={$this->geolocation['longitude']}&units=metric&exclude=current,minutely,hourly,alerts&appid={$this->accessKey}"); |
87
|
1 |
|
curl_setopt($init, CURLOPT_RETURNTRANSFER, true); |
88
|
1 |
|
$json = curl_exec($init); |
89
|
1 |
|
curl_close($init); |
90
|
|
|
|
91
|
1 |
|
return json_decode($json, true); |
|
|
|
|
92
|
|
|
endif; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Multicurl for historic data "openweathermap". |
99
|
|
|
* @method multiCurl |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
2 |
|
public function multiCurl() |
103
|
|
|
{ |
104
|
2 |
|
if ($this->geolocation['status'] == "success") : |
105
|
1 |
|
$nodes = $this->convertDays(5); |
106
|
|
|
|
107
|
1 |
|
$nodeCount = count($nodes); |
108
|
|
|
|
109
|
1 |
|
$curlArr = array(); |
110
|
1 |
|
$master = curl_multi_init(); |
111
|
|
|
|
112
|
1 |
|
for ($i = 0; $i < $nodeCount; $i++) |
113
|
|
|
{ |
114
|
1 |
|
$url =$nodes[$i]; |
115
|
1 |
|
$curlArr[$i] = curl_init($url); |
116
|
1 |
|
curl_setopt($curlArr[$i], CURLOPT_RETURNTRANSFER, true); |
117
|
1 |
|
curl_multi_add_handle($master, $curlArr[$i]); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
do { |
121
|
1 |
|
curl_multi_exec($master, $running); |
|
|
|
|
122
|
1 |
|
} while ($running > 0); |
123
|
|
|
|
124
|
1 |
|
$data = array(); |
125
|
|
|
|
126
|
1 |
|
for ($i = 0; $i < $nodeCount; $i++) |
127
|
|
|
{ |
128
|
1 |
|
$results = curl_multi_getcontent($curlArr[$i]); |
129
|
1 |
|
array_push($data, json_decode($results, true)); |
130
|
|
|
} |
131
|
1 |
|
return $data; |
132
|
|
|
endif; |
133
|
1 |
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Convert days end prepare URL:s for multicurl. |
139
|
|
|
* @method convertDays |
140
|
|
|
* @param int $days |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
1 |
|
public function convertDays(int $days) |
144
|
|
|
{ |
145
|
1 |
|
$arr = array(); |
146
|
|
|
|
147
|
1 |
|
$startdate = strtotime("-{$days} days"); |
148
|
1 |
|
$enddate = strtotime("+{$days} days", $startdate); |
149
|
|
|
|
150
|
1 |
|
while ($startdate < $enddate) { |
151
|
1 |
|
array_push($arr, "https://api.openweathermap.org/data/2.5/onecall/timemachine?lat={$this->geolocation['latitude']}&lon={$this->geolocation['longitude']}&unit=metrics&dt={$startdate}&appid={$this->accessKey}"); |
152
|
1 |
|
$startdate = strtotime("+1 day", $startdate); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return $arr; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..