forked from oyarzun/nestgraph
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfetch.php
More file actions
146 lines (131 loc) · 4.67 KB
/
fetch.php
File metadata and controls
146 lines (131 loc) · 4.67 KB
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
<?php
require 'inc/config.php';
require 'inc/class.db.php';
require 'collectEnergy.php';
define('DEFAULT_ID', 1);
define('DEFAULT_HRS', 72);
define('DEFAULT_DATA', "log");
$hrs = DEFAULT_HRS;
if (!empty($_GET["hrs"])) {
$hrs = $_GET["hrs"];
}
$id = DEFAULT_ID;
if (!empty($_GET["id"])) {
$id = $_GET["id"];
}
$data_type = DEFAULT_DATA;
if(!empty($_GET["data"])) {
$data_type = $_GET["data"];
}
$start_and_end = false;
if(!empty($_GET["start"]) && !empty($_GET["end"])) {
$start_and_end = true;
$time_start = $_GET["start"];
$time_end = $_GET["end"];
}
$json = array();
try {
$db = new DB($config);
if($data_type == "energy")
{
if($stmt = $db->res->prepare("SELECT energyDate, total_heating_time, total_cooling_time, " .
"total_fan_cooling_time, total_humidifier_time, total_dehumidifier_time, leafs, daily_temp_avg, daily_temp_min, daily_temp_max " .
"from energy_data where device_id=? order by energyDate"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($energy_date, $heating, $cooling, $fan, $humid, $dehumid, $leafs, $temp_avg, $temp_min, $temp_max);
header('Content-type: application/json');
$i=0;
while ($stmt->fetch()) {
$json[$i]['timestamp'] = $energy_date;
$json[$i]['heating'] = $heating;
$json[$i]['cooling'] = $cooling;
$json[$i]['fan'] = $fan;
$json[$i]['humid'] = $humid;
$json[$i]['dehumid'] = $dehumid;
$json[$i]['leaf'] = $leafs;
$json[$i]['temperature_avg'] = $temp_avg;
$json[$i]['temperature_min'] = $temp_min;
$json[$i]['temperature_max'] = $temp_max;
$i++;
}
print json_encode($json);
$stmt->close();
}
}
else if($data_type == "dailyTemp")
{
if($start_and_end) {
print compute_daily_temps($db, $time_start, $id);
}
}
else if($data_type == "cycles")
{
$where_stmt = "cycleDate>=DATE_SUB(NOW(), INTERVAL " . $hrs. " HOUR)";
if($start_and_end) {
//$where_stmt = "timestamp BETWEEN \"" . $time_start . "\" AND \"" . $time_end . "\"";
$where_stmt = "cycleDate BETWEEN " . $time_start . " AND " . $time_end;
}
$sql_query = "SELECT cycleDate, cycleNum, start, duration, type from cycles_data where device_id=? and " . $where_stmt . " order by cycleDate";
//print $sql_query;
if($stmt = $db->res->prepare( $sql_query)) {
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($cycle_date, $num, $start, $duration, $type);
header('Content-type: application/json');
$i=0;
while ($stmt->fetch()) {
$json[$i]['timestamp'] = $cycle_date;
$json[$i]['cycle_num'] = $num;
$json[$i]['start'] = $start;
$json[$i]['duration'] = $duration;
$json[$i]['type'] = $type;
$i++;
}
print json_encode($json);
$stmt->close();
}
}
else
{
$where_stmt = "timestamp>=DATE_SUB(NOW(), INTERVAL " . $hrs. " HOUR)";
if($start_and_end) {
//$where_stmt = "timestamp BETWEEN \"" . $time_start . "\" AND \"" . $time_end . "\"";
$where_stmt = "timestamp BETWEEN " . $time_start . " AND " . $time_end;
}
$sql_query = "SELECT timestamp, heating, cooling, fan, autoAway, manualAway, leaf, target, target2, current, humidity, outsideTemperature, outsideHumidity, outsidePressure, updated from data where device_id=? and " . $where_stmt . " order by timestamp";
//print $sql_query;
if ($stmt = $db->res->prepare( $sql_query)) {
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($timestamp, $heating, $cooling, $fan, $autoAway, $manualAway, $leaf, $target, $target2, $current, $humidity, $outsideTemperature, $outsideHumidity, $outsidePressure, $updated);
header('Content-type: application/json');
$i=0;
while ($stmt->fetch()) {
$json[$i]['timestamp'] = $timestamp;
$json[$i]['heating'] = $heating;
$json[$i]['cooling'] = $cooling;
$json[$i]['fan'] = $fan;
$json[$i]['autoAway'] = $autoAway;
$json[$i]['manualAway'] = $manualAway;
$json[$i]['leaf'] = $leaf;
$json[$i]['target'] = $target;
$json[$i]['target2'] = $target2;
$json[$i]['current'] = $current;
$json[$i]['humidity'] = $humidity;
$json[$i]['outsideTemperature'] = $outsideTemperature;
$json[$i]['outsideHumidity'] = $outsideHumidity;
$json[$i]['outsidePressure'] = $outsidePressure;
$json[$i]['updated'] = $updated;
$i++;
}
print json_encode($json);
$stmt->close();
}
}
$db->close();
} catch (Exception $e) {
$errors[] = ("DB connection error! <code>" . $e->getMessage() . "</code>.");
}
?>