-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetjson.dev.php
312 lines (272 loc) · 9.69 KB
/
getjson.dev.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
<?php
// WViewPlot 1.0
// 2014-2015 - Henrik Enquist <[email protected]>
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
$database = "/var/lib/wview/archive/wview-archive.sdb";
//------------list of sensors and their units--------------
$sensorList = array( 'dateTime' => 's',
'usUnits' => 'none',
'interval' => 's',
'barometer' => 'mbar', 'pressure' => 'mbar', 'altimeter' => 'mbar',
'inTemp' => 'deg C', 'outTemp' => 'deg C',
'inHumidity' => 'percent', 'outHumidity' => 'percent',
'windSpeed' => 'm/s', 'windDir' => 'deg.', 'windGust' => 'm/s', 'windGustDir' => 'deg.',
'rainRate' => 'mm/h', 'rain' => 'mm', 'rainCumulative' => 'mm',
'dewpoint' => 'deg C', 'windchill' => 'deg C', 'heatindex' => 'deg C',
'ET' => 'none',
'radiation' => 'W/m2',
'UV' => 'none',
'extraTemp1' => 'deg C', 'extraTemp2' => 'deg C', 'extraTemp3' => 'deg C',
'sunHeating' => 'deg C',
'soilTemp1' => 'deg C', 'soilTemp2' => 'deg C', 'soilTemp3' => 'deg C', 'soilTemp4' => 'deg C',
'leafTemp1' => 'deg C', 'leafTemp2' => 'deg C',
'extraHumid1' => 'percent', 'extraHumid2' => 'percent',
'soilMoist1' => 'percent', 'soilMoist2' => 'percent', 'soilMoist3' => 'percent', 'soilMoist4' => 'percent',
'leafWet1' => 'percent', 'leafWet2' => 'percent',
'rxCheckPercent' => 'percent',
'txBatteryStatus' => 'percent',
'consBatteryVoltage' => 'V',
'hail' => 'mm', 'hailRate' => 'mm/h',
'heatingTemp' => 'deg C', 'heatingVoltage' => 'V', 'supplyVoltage' => 'V', 'referenceVoltage' => 'V',
'windBatteryStatus' => 'percent', 'rainBatteryStatus' => 'percent', 'outTempBatteryStatus' => 'percent', 'inTempBatteryStatus' => 'percent',
);
//---------------function to convert values in US units to metric----------
function makeMetric($value,$unit) {
if (is_null($value)) {
$metricvalue = NULL;
}
else {
if ($unit === 'mm') {
$metricvalue = $value*25.4; //in to mm
}
else if ($unit === 'mm/h') {
$metricvalue = $value*25.4; //in/h to mm/h
}
else if ($unit === 'mbar') {
$metricvalue = $value*33.86; //in Hg to mbar
}
else if ($unit === 'deg C') {
$metricvalue = ($value-32)*5/9; // deg F to deg C
}
else if ($unit === 'm/s') {
$metricvalue = $value*0.44704; //mph to m/s
}
else {
$metricvalue = $value; //no conversion needed
}
}
return $metricvalue;
}
// ----------function to cumulate values-----------------
function cumsum($values = array()) {
// modified from http://stackoverflow.com/questions/12715255/cumulative-array
// re-index the array for guaranteed-success with the for-loop
$values = array_values($values);
$cumulated = array();
array_push($cumulated,$values[0]);
$count = count($values);
if ($count == 1) {
// there is only a single element in the array; no need to loop through it
return $cumulated;
} else {
// iterate through each element (starting with the second) and add
// the prior-element's value to the current
for ($i = 1; $i < $count; $i++) {
$cumulated[] = $values[$i] + $cumulated[$i - 1];
}
}
return $cumulated;
}
// ----------function to subtract arrays elementwise-----------------
function arraySubtract($values1 = array(), $values2 = array()) {
$values1 = array_values($values1);
$values2 = array_values($values2);
$arraySubt = array();
//array_push($cumulated,$values[0]);
$count = count($values1);
for ($i = 0; $i < $count; $i++) {
if ( !is_null($values1[$i]) && !is_null($values2[$i]) ) {
$arraySubt[] = $values1[$i] - $values2[$i];
}
else {
$arraySubt[] = NULL;
}
}
return $arraySubt;
}
// ----------function to add constant to array elementwise-----------------
function arrayAddConstant($values = array(), $constval) {
$values = array_values($values);
$arrayAdded = array();
//array_push($cumulated,$values[0]);
$count = count($values);
for ($i = 0; $i < $count; $i++) {
if (!is_null($values[$i])) {
$arrayAdded[] = $values[$i] + $constval;
}
else {
$arrayAdded[] = NULL;
}
}
return $arrayAdded;
}
//-------------- main program-------------------
//parse url arguments,check if the request is valid
$validrequest = 1;
$message = 'ok';
$daysEnd = htmlspecialchars($_GET["dE"]);
$daysStart = htmlspecialchars($_GET["dS"]);
$dateEnd = htmlspecialchars($_GET["dateEnd"]);
$dateStart = htmlspecialchars($_GET["dateStart"]);
$absDates = htmlspecialchars($_GET["dAbs"]);
$aggregate = htmlspecialchars($_GET["aggr"]);
$sensors = array(); //empty array for sensor names
if(isset($_GET['s'])) { //sensors set?
$one=$_GET['s'];
foreach ($one as $a=>$value) {
array_push($sensors,$value); //fetch list of requested sensors
}
} else {
$validrequest = 0;
$message = 'no sensors selected';
}
//start and end of time interval (unix epoch)
if ($absDates == 1) {
$tsStart = strtotime($dateStart);
$tsEnd = strtotime($dateEnd) + 3600*24;
}
else {
$tsStart = time()-$daysStart*3600*24;
$tsEnd = time()-$daysEnd*3600*24;
}
if ($tsEnd<=$tsStart) {
$validrequest = 0;
$message = 'invalid dates selected';
}
if ($validrequest == 1) {
// string containing column names
$sensorstring = '';
$sensorstringAggr = '';
//check requested sensors, direct from database or calculated?
$sensorsDB = array("dateTime"); //always include dateTime
foreach ($sensors as $sens) {
//add columns needed to calculate the virtual sensors
if ($sens === "rainCumulative") {
array_push($sensorsDB,"rain");
}
elseif ($sens === "sunHeating") {
array_push($sensorsDB,"outTemp");
array_push($sensorsDB,"extraTemp3");
}
else {
array_push($sensorsDB,$sens);
}
}
//remove duplicates, don't request things twice..
$sensorsDB = array_unique($sensorsDB);
//build string of database names
foreach ($sensorsDB as $sens) {
//no averaging
$sensorstring = $sensorstring . $sens . " AS " . $sens . "Ren,";
//with averaging
if ($sens === "rain" OR $sens === "hail") {
// rain and hail should be summed, not avaraged
$sensorstringAggr = $sensorstringAggr . "SUM(" . $sens . ") AS " . $sens . "Ren,";
}
else {
$sensorstringAggr = $sensorstringAggr . "AVG(" . $sens . ") AS " . $sens . "Ren,";
}
}
$sensorstring = rtrim($sensorstring, ","); //remove extra comma at the end
$sensorstringAggr = rtrim($sensorstringAggr, ","); //remove extra comma at the end
if ($aggregate === "auto") {
$nbrDays = ($tsEnd - $tsStart)/(3600*24);
if ($nbrDays >= 31) { //aggregate 1 day
$aggregate = "1day";
}
elseif ($nbrDays >= 14) { //aggregate 4 hours
$aggregate = "4hours";
}
elseif ($nbrDays >= 2) { //aggregate 1 hour
$aggregate = "1hour";
}
else { //no aggregation
$aggregate = "none";
}
}
$dbquery = "SELECT " . $sensorstringAggr . " FROM archive WHERE dateTime>" . $tsStart . " AND dateTime<" . $tsEnd;
if ($aggregate === "1day") { //aggregate 1 day
$groupby = "GROUP BY strftime('%Y-%m-%d', dateTime, 'unixepoch')";
}
elseif ($aggregate === "1hour") { //aggregate 1 hour
$groupby = "GROUP BY strftime('%Y-%m-%dT%H', dateTime, 'unixepoch')";
}
elseif ($aggregate === "4hours") { //aggregate 4 hours
//$groupby = "GROUP BY strftime('%Y-%m-%dT%H:00:00.000', 4*3600*(dateTime/(4*3600)),'unixepoch')"; //this also works
$groupby = "GROUP BY strftime('%Y-%m-%dT', dateTime,'unixepoch') || (strftime('%H', dateTime, 'unixepoch')/4)";
}
else { //no aggregation
$dbquery = "SELECT " . $sensorstring . " FROM archive WHERE dateTime>" . $tsStart . " AND dateTime<" . $tsEnd;
$groupby = "";
}
$statementstr = $dbquery . " " . $groupby . ";";
//open sqlite3 database
$db = new SQLite3($database, SQLITE3_OPEN_READONLY);
//run query
$statement = $db->prepare($statementstr);
$result = $statement->execute();
//create an array to store the data
$data = array();
//push in arrays for each column from database query
foreach ($sensorsDB as $sens) {
array_push($data[$sens], array());
}
//get units for the sensors
$units = array();
foreach ($sensorsDB as $sens) {
$units[$sens] = $sensorList[$sens];
}
//get the data from query result and convert to metric,
while ($row = $result->fetchArray(SQLITE3_ASSOC)) { //loop for each row in sqlite output
foreach ($sensorsDB as $sens) {
//get each sensor value
$data[$sens][] = makeMetric($row[$sens . "Ren"],$units[$sens]);
}
}
//copy data to output structure, calculate virtual sensors as needed
$dataout["dateTime"] = $data["dateTime"];
$sensorsout = array("dateTime");
$unitsout = array();
$unitsout["dateTime"] = $sensorList["dateTime"];
foreach ($sensors as $sens) {
array_push($sensorsout,$sens);
$unitsout[$sens] = $sensorList[$sens];
//check if sens must be calculated
if ($sens === "sunHeating") {
$tempdiff = arraySubtract($data["extraTemp3"],$data["outTemp"]); //calculate difference
$dataout[$sens] = arrayAddConstant($tempdiff,1.3); //add offset
}
elseif ($sens === "rainCumulative") {
$dataout[$sens] = cumsum($data["rain"]); //cumulate rain
}
else {
$dataout[$sens] = $data[$sens]; //just copy
}
}
//status message, not yet used for anything..
//$message = 'ok';
} //end if validrequest
//generate output json
echo json_encode(array('sensors' => $sensorsout, 'units' => $unitsout, 'data' => $dataout, 'message' => $message));
?>