-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.php
127 lines (112 loc) · 3.55 KB
/
query.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
<?php
/*
* Parameters:
* - To query by time:
* - startTime
* - endTime
* - To query by device:
* - deviceId
* - To query by buoy:
* - latitude
* - longitude
*
* - If dl is set to 1, it will prompt for download
* - If dl is set to 2, it will be a MATLAB export
*
* If you don't get all the attributes to query by a certain criterion, that
* criterion will be ignored.
*
* Query parameters can be mixed and matched
*
* Return: a JSON object. If there's an error, the attribute 'error' will be set
* to true, and the attribute 'message' may have a useful message. Otherwise,
* results of the query are included in an array on the 'readings' attribute.
*
*/
require_once('database.php');
function shouldQueryOnDeviceId() {
return !empty($_GET['deviceId']);
}
function shouldQueryOnBuoy() {
return !empty($_GET['latitude']) && !empty($_GET['longitude']);
}
function shouldQueryOnTime() {
return !empty($_GET['startTime']) && !empty($_GET['endTime']);
}
function shouldQuery() {
return shouldQueryOnDeviceId() || shouldQueryOnBuoy() || shouldQueryOnTime();
}
$query = 'SELECT r.device_id, time, dimension0, dimension1' .
' FROM reading r';
$response = ['readings' => []];
if (shouldQueryOnBuoy()) {
$query .= ', buoy_device bd WHERE r.device_id = bd.device_id AND '
. '`latitude` = :latitude AND `longitude` = :longitude';
} else if (shouldQuery()) {
$query .= ' WHERE ';
}
if (shouldQueryOnDeviceId()) {
if (shouldQueryOnBuoy()) {
$query .= ' AND ';
}
$query .= 'r.`device_id` = :device_id';
}
if (shouldQueryOnTime()) {
if (shouldQueryOnDeviceId() || shouldQueryOnBuoy()) {
$query .= ' AND ';
}
$query .= '`time` >= :start_time AND `time` <= :end_time';
}
$pdo = Database::connect();
$get_readings = $pdo->prepare($query);
if (shouldQueryOnDeviceId()) {
$get_readings->bindValue(':device_id', $_GET['deviceId']);
}
if (shouldQueryOnBuoy()) {
$get_readings->bindValue(':longitude', $_GET['longitude']);
$get_readings->bindValue(':latitude', $_GET['latitude']);
}
if (shouldQueryOnTime()) {
$get_readings->bindValue(':start_time', $_GET['startTime']);
$get_readings->bindValue(':end_time', $_GET['endTime']);
}
try {
$get_readings->execute();
while ($reading = $get_readings->fetch()) {
$response['readings'][] = $reading;
}
} catch (PDOException $e) {
$response = ['error' => true, 'message' => $e->getMessage()];
}
Database::disconnect();
if (isset($_GET['dl'])) {
if ($_GET['dl'] == 1) {
$fn = 'export.csv';
header('Content-Disposition: attachment; filename="' . $fn . '"');
$stdout = fopen('php://output', 'w');
fputcsv($stdout, ['Device ID', 'Time', 'Dimension0', 'Dimension1']);
foreach ($response['readings'] as $reading) {
fputcsv($stdout, $reading);
}
fflush($stdout);
fclose($stdout);
} elseif ($_GET['dl'] == 2) {
$fn = 'export.m';
header('Content-Disposition: attachment; filename="' . $fn . '"');
echo 'readings = [];' . PHP_EOL;
foreach ($response['readings'] as $reading) {
$matlabFmtReading = 'struct(';
$first = true;
foreach ($reading as $k => $v) {
if ($first) { $first = false; }
else { $matlabFmtReading .= ','; }
$matlabFmtReading .= "'$k'" . ',' . "'$v'";
}
$matlabFmtReading .= ')';
echo 'readings = [readings ' . $matlabFmtReading . '];' . PHP_EOL;
}
}
exit;
}
echo json_encode($response);
?>