-
Notifications
You must be signed in to change notification settings - Fork 10
/
get_instant_forecast.php
109 lines (97 loc) · 4.41 KB
/
get_instant_forecast.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
<?php
$start_time = microtime(true);
require_once( 'common.php' );
require_once( 'user.php' );
/*
$util::logError( 'Forecast is DISABLED for now.' );
$returnString = "<p><img src='images/Alert.png'/>Forecast is presently disabled.</p>";
echo $returnString;
exit;
*/
//$util::logDebug( '0' );
/* Put useful comments here and either merge code with get_instant_status.php or make this a virtual clone of that file */
$uname = (isset($_REQUEST['user'])) ? $_REQUEST['user'] : null; // Set uname to chosen user name (or null if not chosen)
$session = (isset($_REQUEST['session'])) ? $_REQUEST['session'] : null; // Set session to chosen session id (or null if not chosen)
try{
$user = new USER( $uname, $session );
}
catch( Exception $e ){
$util::logError( 'Error while creating user. Might be invalid session?' );
$returnString = "<p><img src='images/Alert.png'/>Forecast is presently unavailable.</p>";
echo $returnString;
exit;
}
//if( ! $util::checkThermostat( $user ) ) return; // <-- This is not a good way to check session
$lastZIP = '';
$returnString = '';
if( $weatherConfig[ 'useForecast' ] ){
// Only check forecast if we're asking for it.
try{
foreach( $user->locations as $locationRec ){
try{
$util::logDebug( '1 zipcode = ' . $locationRec['location_string'] );
if( $lastZIP != $locationRec['location_string'] ){
// Only get outside info for subsequent locations if the location has changed
$lastZIP = $locationRec['location_string'];
$externalWeatherAPI = new ExternalWeather( $weatherConfig );
/** Bad code follows.
* I'm directly loading the data structure from weatherunderground and I ought to be using my own structure
*
* So the following code is specific to one data supplier and not generic at all.
* To fix it, I need to change not only this, but also ExternalWeather.php
*/
/** Get environmental info
*
*/
$util::logDebug( '2' );
$forecastData = $externalWeatherAPI->getOutdoorForecast( $lastZIP, $util );
$util::logDebug( '3' );
// Format data for screen presentation
if( is_array( $forecastData ) ){
$returnString .= "<p>The forecast for {$lastZIP} is</p><br><table><tr>";
foreach( $forecastData as $day ){
$returnString .= "<td style='text-align: center;'>{$day->date->weekday}</td>";
}
$returnString .= "</tr><tr>";
foreach( $forecastData as $day ){
$returnString .= "<td style='text-align: center; width: 90px;'><img src='$day->icon_url' alt='$day->icon' title='$day->conditions'></td>";
}
$returnString .= "</tr><tr>";
foreach( $forecastData as $day ){
if( $weatherConfig[units] == 'C' ){
$tth = $day->high->celsius;
$ttl = $day->low->celsius;
}
else{
// If it's not C assume it is F (what, you want Kelvin or Rankine?)
$tth = $day->high->fahrenheit;
$ttl = $day->low->fahrenheit;
}
$returnString .= "<td style='text-align: center;'>$tth°$weatherConfig[units] / $ttl°$weatherConfig[units]</td>";
}
$returnString .= '</tr></table>';
}
else{
$util::logError( 'Expected to get an array back from $externalWeatherAPI->getOutdoorForcast( $lastZIP ) but did not. $lastZIP is [[[' . $lastZIP . ']]]' );
$util::logInfo( 'return data is [[[' . $forecastData . ']]]' );
$returnString .= 'No response from forecast provider.';
}
}
}
catch( Exception $e ){
$util::logError( 'External forecast failed: ' . $e->getMessage() );
// Need to add the Alert icon to the sprite map and set relative position in the thermo.css file
$returnString = $returnString . "<p><img src='images/Alert.png'/>Presently unable to read forecast.</p>";
}
}
}
catch( Exception $e ){
$util::logError( 'Some bugs failure or other ' . $e->getMessage() );
$returnString = "<p><img src='images/Alert.png'/>Presently unable to read forecast.</p>";
}
}
// This is a little hacky, but change all http to https
$returnString = str_replace('http://', 'https://', $returnString );
echo $returnString;
//$util::logDebug( 'execution time was ' . (microtime(true) - $start_time) . ' seconds.' );
?>