updated removed
[home-automation.git] / pxweather.class.php
1 <?php\r
2 \r
3 /*\r
4  *      pxWeather - PHP/XML Weather Retrieval\r
5  *      by Jonathan Abbett\r
6  *      jonathan[at]abbett[dot]org\r
7  *\r
8  *\r
9  *\r
10  */\r
11 \r
12 require_once("xmlize-php5.inc.php");\r
13 \r
14 /*\r
15 ** Constants\r
16 */\r
17 define("PXWEATHER_URL",         0);             // to set $_url\r
18 define("PXWEATHER_CACHE",       1);             // to set $_cache\r
19 define("PXWEATHER_CACHEFOR",    2);             // to set $_age\r
20 define("PXWEATHER_CACHEAT",     3);             // to set $_cachepath\r
21 \r
22 class Weather {\r
23 \r
24 \r
25         // The URL from whence to retreive XML weather data\r
26         var $_url       = "http://www.weather.unisys.com/forexml.cgi";\r
27         // If true, will use a local file cache\r
28         var $_cache     = false;\r
29         // How many minutes to keep the cached data\r
30         var $_age       = 60;\r
31         // If cache-enabled, use this directory for storing data\r
32         var $_cachepath = "cache";\r
33 \r
34 \r
35 \r
36 \r
37         var $_xml       = null;         // to store our xml array\r
38         var $_city      = "boston";     // The city we're interested in\r
39         var $_force     = false;        // If true, will override the cache and fetch data fresh\r
40 \r
41 \r
42         /*\r
43         ** Constructor\r
44         */\r
45         function Weather($city = "Boston", $force = false) {\r
46 \r
47                 $this->_city = $city;\r
48                 $this->_force = $force;\r
49 \r
50         }\r
51 \r
52         function setOption($option, $value) {\r
53 \r
54                 switch($option) {\r
55                         case EASYWEATHER_URL:\r
56                                 $this->_url = $value;\r
57                                 break;\r
58                         case EASYWEATHER_CACHE:\r
59                                 $this->_cache = $value;\r
60                                 break;\r
61                         case EASYWEATHER_CACHEFOR:\r
62                                 $this->_age = $value;\r
63                                 break;\r
64                         case EASYWEATHER_CACHEAT:\r
65                                 $this->_cachepath = $value;\r
66                                 break;\r
67                 }\r
68 \r
69         }\r
70 \r
71         /*\r
72         ** Return a pretty-printed version of the XML array\r
73         */\r
74         function toString($array = null) {\r
75 \r
76                  if ($array == null) {\r
77                         $array = $this->_xml;\r
78                  }\r
79 \r
80                  echo "<pre>";\r
81                  print_r($array);\r
82                  echo "</pre>";\r
83         }\r
84 \r
85         /*\r
86         ** Return a specific field from the current weather report\r
87         */\r
88         function getCurrent($field) {\r
89                 $o = $this->_getObservation();\r
90 \r
91                 if (array_key_exists($field, $o)) {\r
92                         return $o[$field];\r
93                 }\r
94                 else {\r
95                         return null;\r
96                 }\r
97         }\r
98 \r
99         function getSunrise() {\r
100                 $a = $this->_getAlmanac();\r
101 \r
102                 return $a["sunrise"];\r
103         }\r
104 \r
105         function getSunset() {\r
106                 $a = $this->_getAlmanac();\r
107 \r
108                 return $a["sunset"];\r
109         }\r
110 \r
111         function getForecasts() {\r
112 \r
113                 // need to remove a layer of separation\r
114 \r
115                 $forecasts = $this->_xml["forexml"]["#"]["forecast"];\r
116 \r
117                 $returnArray = array();\r
118 \r
119                 $i = 0;\r
120 \r
121                 foreach ($forecasts as $forecast) {\r
122                         $returnArray[$i] = $forecast["@"];\r
123                         $i++;\r
124                 }\r
125 \r
126                 return $returnArray;\r
127 \r
128         }\r
129 \r
130         function getDaycasts() {\r
131 \r
132                 // need to remove a layer of separation\r
133 \r
134                 $daycasts = $this->_xml["forexml"]["#"]["daycast"];\r
135 \r
136                 $returnArray = array();\r
137 \r
138                 $i = 0;\r
139 \r
140                 foreach ($daycasts as $daycast) {\r
141                         $returnArray[$i] = $daycast["@"];\r
142                         $i++;\r
143                 }\r
144 \r
145                 return $returnArray;\r
146         }\r
147 \r
148         /*\r
149         ** Returns a weather condition string given a code (i.e 'TS')\r
150         */\r
151         function weatherString($code) {\r
152 \r
153                 switch($code) {\r
154                         case "TS":\r
155                                 return "Thunderstorms";\r
156                                 break;\r
157                         case "RA":\r
158                                 return "Rain";\r
159                                 break;\r
160                         case "MC":\r
161                                 return "Mostly cloudy";\r
162                                 break;\r
163                         case "SU":\r
164                                 return "Sunny";\r
165                                 break;\r
166                         case "MO":\r
167                                 return "Mostly clear";\r
168                                 break;\r
169                         case "PC":\r
170                                 return "Partly cloudy";\r
171                                 break;\r
172                         case "SN":\r
173                                 return "Snow";\r
174                                 break;\r
175                         case "CL":\r
176                                 return "Overcast";\r
177                                 break;\r
178                         case "FG":\r
179                                 return "Fog";\r
180                                 break;\r
181                 }\r
182 \r
183                 return null;\r
184 \r
185         }\r
186 \r
187         function load() {\r
188 \r
189                 $filename = "$this->_cachepath/weather.$this->_city.cache";\r
190 \r
191                 // if not force, check cache for valid file\r
192                 if (!$this->_force && $this->_cache) {\r
193 \r
194                         if (file_exists($filename)\r
195                                         && filemtime($filename) > (time() - ($this->_age * 60))) {\r
196 \r
197                                 $file = fopen($filename, "r");\r
198                                 $this->_xml = unserialize(fread($file, filesize($filename)));\r
199                                 fclose($file);\r
200 \r
201                                 return;\r
202                         }\r
203                 }\r
204 \r
205                 // if force, or no valid cache file, get XML fresh\r
206 \r
207                 $this->_xml = $this->_getXML();\r
208 \r
209                 if ($this->_cache) {\r
210                         $file = fopen($filename, "w");\r
211                         fwrite($file, serialize($this->_xml));\r
212                         fclose($file);\r
213                 }\r
214 \r
215         }\r
216 \r
217         function _getXML() {\r
218 \r
219                 $ch = curl_init();\r
220                 curl_setopt($ch, CURLOPT_URL,                           $this->_url . "?" . $this->_city);\r
221                 curl_setopt($ch, CURLOPT_HEADER,                        false);\r
222                 curl_setopt($ch, CURLOPT_RETURNTRANSFER,        true);\r
223
224                 #echo $this->_url . "?" . $this->_city;
225                 #exit;
226
227                 $xml = curl_exec($ch);\r
228 \r
229                 #print_r($xml);
230                 #exit;
231
232                 if (curl_errno($ch)) {\r
233                         trigger_error("cURL Error in Weather._getXML: " . curl_error($ch), E_WARNING);\r
234                         return null;\r
235                 }\r
236 \r
237                 curl_close($ch);\r
238 \r
239                 return xmlize($xml);\r
240 \r
241         }\r
242 \r
243         function _getObservation() {\r
244 \r
245                 return $this->_xml["forexml"]["#"]["observation"][0]["@"];\r
246 \r
247         }\r
248 \r
249         // array with keys "sunrise" and "sunset"\r
250         function _getAlmanac() {\r
251 \r
252                 return $this->_xml["forexml"]["#"]["almanac"][0]["@"];\r
253         }\r
254 \r
255 }\r
256         \r
257 ?>