-
Notifications
You must be signed in to change notification settings - Fork 7
/
current.php
165 lines (132 loc) · 5.99 KB
/
current.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
<?php
include 'database.php';
header('content-type: application/json; charset=utf-8');
header("access-control-allow-origin: *");
$con = new mysqli(DbSettings::$Address,DbSettings::$Username,DbSettings::$Password,DbSettings::$Schema);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = $con->query('call GETRECENTOBS');
$fieldcount = mysqli_num_fields($result);
echo "{ "; // Open document object
echo "\r\n\t\"WeatherObservations\" : {"; // Open weather observations object
if ($result->num_rows > 0) {
$fields = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
array_push($fields, $fieldinfo->name);
}
$numberOfRows = 0;
while($row = mysqli_fetch_array($result)) { // Rows
$numberOfRows++;
if ($numberOfRows > 1) {
echo ", ";
}
echo "\r\n\t\t\"Observation" . $numberOfRows . "\" : {";
if ($numberOfRows == 1) {
$feelsLike = calculateFeelsLike($row["AMBIENT_TEMPERATURE"], $row["HUMIDITY"], $row["WIND_SPEED"]);
if (!Settings::$showMetricAndCelsiusMeasurements == "1") {
$feelsLike = convertCelsiusToFahrenheit($feelsLike);
}
echo "\r\n\t\t\t\"FEELS_LIKE\" : " . "\"" . $feelsLike . "\",";
}
for ($i = 0; $i < $fieldcount; $i++) { // Columns
$fieldName = $fields[$i];
$fieldValue = $row[$i];
if (strpos($fieldName, "_TEMPERATURE")) {
if (Settings::$showMetricAndCelsiusMeasurements == "1") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° C\",";
}
else {
$fieldValue = convertCelsiusToFahrenheit($fieldValue);
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° F\",";
}
}
if ($fieldName == "DEWPOINT") {
if (Settings::$showMetricAndCelsiusMeasurements == "1") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° C\",";
}
else {
$fieldValue = convertCelsiusToFahrenheit($fieldValue);
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° F\",";
}
}
if ($fieldName == "RAINFALL") {
if (Settings::$showMetricAndCelsiusMeasurements == "1") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " mm\",";
}
else {
$fieldValue = convertMillibarsToInches($fieldValue);
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " in\",";
}
}
if ($fieldName == "HUMIDITY") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " %\",";
}
if (strpos($fieldName, "_SPEED")) {
if (Settings::$showMetricAndCelsiusMeasurements == "1") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " Km/H\",";
}
else {
$fieldValue = convertKilometersToMiles($fieldValue);
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " MPH\",";
}
}
if (strpos($fieldName, "_PRESSURE")) {
$mslp = calculateMeanSeaLevelPressure($fieldValue, Settings::$stationElevationInMeters);
if (Settings::$showPressureInMillibars == "1") {
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $mslp . " mb\",";
}
else {
$fieldValue = convertMillibarsToInches($mslp);
echo "\r\n\t\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . " in\",";
}
}
echo "\r\n\t\t\t\"" . $fieldName . "\" : " . "\"" . $fieldValue . "\"";
if ($i+1 < $fieldcount) {
echo ",";
}
}
echo "\r\n\t\t}";
}
$result->close();
$con->next_result();
}
echo "\r\n\t}"; // Close weather observations object
$result = $con->query('call GETDAILYRECORDS');
$fieldcount = mysqli_num_fields($result);
echo ",\r\n\t\"DailyStats\" : {"; // Open daily stats object
if ($result->num_rows > 0) {
$fields = array();
while ($fieldinfo = mysqli_fetch_field($result)) {
array_push($fields, $fieldinfo->name);
}
$row = mysqli_fetch_array($result); // Only one row of data
for ($i = 0; $i < $fieldcount; $i++) { // Columns
$fieldInfo = mysqli_fetch_field($result);
$fieldName = $fields[$i];
$fieldValue = $row[$i];
if (($fieldName == "LowSinceMidnight") || ($fieldName == "HighSinceMidnight")) {
if (Settings::$showMetricAndCelsiusMeasurements == "1") {
echo "\r\n\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° C\",";
}
else {
$fieldValue = convertCelsiusToFahrenheit($fieldValue);
echo "\r\n\t\t\"" . $fieldName . "_STRING\" : " . "\"" . $fieldValue . "° F\",";
}
}
echo "\r\n\t\t\"" . $fieldName . "\" : " . "\"" . $fieldValue . "\"";
if ($i+1 < $fieldcount) {
echo ",";
}
}
}
echo "\r\n\t}"; // Close daily stats object
echo ",\r\n\t\"Settings\" : {"; // Open settings object
echo "\r\n\t\t\"showMetricAndCelsiusMeasurements\" : " . "\"" . Settings::$showMetricAndCelsiusMeasurements . "\",";
echo "\r\n\t\t\"showPressureInMillibars\" : " . "\"" . Settings::$showPressureInMillibars . "\"";
echo "\r\n\t}"; // Close settings object
echo "\r\n}"; // Close document object
$result->close();
mysqli_close($con);
?>