This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deif-getdata-cached-v2.php
158 lines (127 loc) · 4.51 KB
/
deif-getdata-cached-v2.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
<?php
$callback = isset($_GET['callback']) ? $_GET['callback'] : '';
$seriesID = isset($_GET['sID']) ? $_GET['sID'] : ''; // Hvad skal der hentes? Måling/signal/gns/avg?
$devicesID = isset($_GET['dID']) ? $_GET['dID'] : ''; // Liste over enheder skal skal med i data. Der vil kun være mere end en, hvis seriesID er gns/avg. Numerisk ID på enheder.
$typeID = isset($_GET['tID']) ? $_GET['tID'] : ''; // Hvad for noget data hentes, hvad er "topic" i databasen. Numerisk ID på det.
$updatemem = false;
$cachetime = strtotime("today midnight"); //midnat dagen før.
$lasttime = 0;
$time = "";
$maxAge = strtotime("now")-$cachetime;
header('Content-Type: application/json;charset=UTF-8');
//header ("Expires: " . date ('D, d M Y H:i:s \G\M\T', strtotime("today midnight")));
//header ("Cache-Control: max-age=" . $maxAge);
//CACHE
$mem = new Memcached();
$mem->addServer("127.0.0.1", 11211);
$memkey = $seriesID.";".$devicesID.";".$typeID;
$time = $mem->get($memkey."time"."v2")*1000;
if ($time < $cachetime) $updatemem = true;
else $updatemem = false;
/*
echo "Update? ".$updatemem."\n";
echo "cachetime ".$cachetime."\n";
echo "time: ".$time."\n";*/
if ($updatemem) {
//servername, username, password og db name:
include 'datalogin.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($seriesID == 2 || $seriesID == 3) {
$devicesID = explode(",",$devicesID);
$typeID = explode(",",$typeID);
}
if ($seriesID == 0) {
$sql = "SELECT ";
$sql.= "datetime, ";
if ($typeID == "active_power_a" || $typeID == "active_power_b" || $typeID == "active_power_c" || $typeID == "active_power_sum") $sql.= "ABS(";
$sql.= "AVG(".$typeID.")";
if ($typeID == "active_power_a" || $typeID == "active_power_b" || $typeID == "active_power_c" || $typeID == "active_power_sum") $sql.= ")";
$sql.= " as value FROM `deif` ";
$sql.= " WHERE `deif`.`deifid` = '".$devicesID."'";
$sql.= " AND `timestamp`<".$cachetime."000";
$sql.= " GROUP BY datetime";
$sql.= " ORDER BY ";
$sql.= "`deif`.`datetime`";
$sql.= " ASC;";
} elseif ($seriesID == 2 || $seriesID == 3) { // SUM/AVG samle sql.
$sql = "SELECT ";
$sql.= "minute as datetime, ";
if ($seriesID == 3) $sql.= "SUM";
else $sql.= "AVG";
$sql.= "(value) as value FROM (";
$sql.= "SELECT ";//Inner select)
$sql.= "minute, ";
$cases = [[]];
foreach ($devicesID as $key => $value) {
$cases[$value][] = $typeID[$key];
}
//var_dump($cases);
$sql.= " CASE ";
foreach ($cases as $key => $case) {
if ($key != "0") {
$sql.= " WHEN deifid='".$key."' THEN (";
$casn = 0;
foreach ($case as $keyy => $value) {
if ($keyy != 0) $sql.= "+";
if ($value == "active_power_a" || $value == "active_power_b" || $value == "active_power_c" || $value == "active_power_sum") $sql.= "ABS(";
$sql.= "AVG(".$value.")";
if ($value == "active_power_a" || $value == "active_power_b" || $value == "active_power_c" || $value == "active_power_sum") $sql.= ")";
$casn++;
}
$sql.= ") ";
if ($seriesID == 2) $sql.= "/ ".$casn." ";
}
}
$sql.= " ELSE NULL END ";
$sql.= " as value FROM `deif` ";
$sql.= " WHERE (";
foreach ($devicesID as $key => $value) {
if ($key != 0) $sql.= " OR ";
$sql.= "`deif`.`deifid` = '".$value."'";
}
$sql.= ")";
if ($time != '') $sql.= " AND `timestamp`>".$time."000";
else $sql.= " AND `timestamp`<".$cachetime."000";
$sql.= " GROUP BY minute, deifid";
$sql.= ") AS A";
$sql.= " GROUP BY minute";
$sql.= " ORDER BY ";
$sql.= "`A`.`minute`";
$sql.= " ASC;";
}
$result = $conn->query($sql);
$started = false;
$content = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($started) { $content.= ",";}
$content.= "[";
$lasttime = strtotime($row["datetime"])*1000;
$content.= $lasttime;
$content.= ",";
$content.= (float)$row["value"];
$content.= "]";
$started = true;
}
}
$conn->close();
if ($lasttime == 0) {
$lasttime = $cachetime+1000;
$time = 0;
}
}
echo $callback;
echo "([";
if ($updatemem) echo $content;
else echo $mem->get($memkey."v2");
if ($updatemem) {
$mem->set($memkey."v2", $content);
if ($lasttime/1000 > $time) {
$mem->set($memkey."time"."v2", $lasttime/1000);
}
}
echo "]);";
?>