-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
207 lines (178 loc) · 7.06 KB
/
index.php
File metadata and controls
207 lines (178 loc) · 7.06 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
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
<?php
// Make sure you set the database connection on the next line:
$mysqli = new mysqli("HOST", "USER", "PASSWORD", "DATABASE");
/* check connection */
if ($mysqli->connect_errno) {
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$lastTemps = getLastTemps($mysqli);
$lastAVGTemps = getAVGTemps($mysqli);
$last24hourValues = last24hourValues($mysqli);
$mysqli->close();
if($_GET['format'] == json) {
header('Content-Type: application/json');
print json_encode($lastTemps);
exit;
}
header('Refresh: 150; url=index.php');
function getLastTemps($mysqli) {
$stmt = $mysqli->prepare("SELECT temp1, temp2, created_at FROM temps ORDER BY created_at DESC LIMIT 1");
$stmt->execute();
$stmt->bind_result($res['temp1'], $res['temp2'], $res['created_at']);
$stmt->fetch();
return $res;
}
function getAVGTemps($mysqli) {
$stmt = $mysqli->prepare("SELECT AVG(temp1), AVG(temp2) FROM temps WHERE created_at >= SYSDATE() - INTERVAL 1 DAY");
$stmt->execute();
$stmt->bind_result($res['temp1'], $res['temp2']);
$stmt->fetch();
return $res;
}
function last24hourValues($mysqli) {
$stmt = $mysqli->prepare("SELECT temp1, temp2, created_at FROM temps WHERE created_at >= SYSDATE() - INTERVAL 1 DAY");
$stmt->execute();
$stmt->bind_result($res['temp1'], $res['temp2'], $res['created_at']);
$rows = array();
$i = 0;
while($stmt->fetch()) {
$rows[$i] = array();
foreach($res as $k=>$v)
$rows[$i][$k] = $v;
$i++;
}
return $rows;
}
function tempParts($temp, $index) {
$parts = explode('.', number_format($temp, 1));
return $parts[$index];
}
?>
<html>
<head>
<title>Temperatures</title>
<link rel="stylesheet" type="text/css" href="./css/style.css" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="./amcharts/amcharts.js" type="text/javascript"></script>
<script type="text/javascript" src="./js/common.js"></script>
<script type="text/javascript">
var lineChartData = [
<?php foreach($last24hourValues as $row) { ?>
{
date: calcTime(<?php print strtotime($row['created_at']) * 1000; ?>, 2),
temp1: <?php print number_format($row['temp1'], 2); ?>,
temp2: <?php print number_format($row['temp2'], 2); ?>
},
<?php } ?>
];
function calcTime(unixTime, offset) {
// create Date object for current location
d = new Date(unixTime);
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
return nd;
}
function formatLabel(value, valueString, axis){
// let's say we dont' want minus sign next to negative numbers
if(value < 0)
{
valueString = valueString.substr(1);
}
// and we also want a letter C to be added next to all labels (you can do it with unit, but anyway)
valueString = valueString + "° C";
return valueString;
}
AmCharts.ready(function () {
var chart = new AmCharts.AmSerialChart();
chart.dataProvider = lineChartData;
chart.pathToImages = "../amcharts/images/";
chart.categoryField = "date";
chart.balloon.bulletSize = 5;
// sometimes we need to set margins manually
// autoMargins should be set to false in order chart to use custom margin values
chart.marginLeft = 0;
chart.marginBottom = 0;
chart.marginTop = 0;
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.parseDates = true; // as our data is date-based, we set parseDates to true
categoryAxis.minPeriod = "ss"; // our data is daily, so we set minPeriod to DD
categoryAxis.inside = true;
categoryAxis.gridAlpha = 0;
categoryAxis.tickLength = 0;
categoryAxis.axisAlpha = 0;
// value
var valueAxis = new AmCharts.ValueAxis();
valueAxis.dashLength = 1;
valueAxis.axisAlpha = 0;
//set label function which will format values
valueAxis.labelFunction = formatLabel;
chart.addValueAxis(valueAxis);
// GRAPH
var graph = new AmCharts.AmGraph();
graph.type = "line";
graph.valueField = "temp1";
graph.lineColor = "#5fb503";
graph.negativeLineColor = "#efcc26";
//graph.fillAlphas = 0.3; // setting fillAlphas to > 0 value makes it area graph
graph.bulletSize = 3; // bullet image should be a rectangle (width = height)
chart.addGraph(graph);
var graph = new AmCharts.AmGraph();
graph.type = "line";
graph.valueField = "temp2";
graph.lineColor = "#F2304D";
graph.negativeLineColor = "#3053F2";
//graph.fillAlphas = 0.3; // setting fillAlphas to > 0 value makes it area graph
graph.bulletSize = 3; // bullet image should be a rectangle (width = height)
chart.addGraph(graph);
// CURSOR
var chartCursor = new AmCharts.ChartCursor();
chartCursor.cursorPosition = "mouse";
chartCursor.categoryBalloonDateFormat = "JJ:NN, DD MMMM";
chart.addChartCursor(chartCursor);
// WRITE
chart.write("chartdiv");
});
</script>
</head>
<body>
<div class="content">
<div class="thermometers">
<div class="label">Sensor Binnen</div><div class="label">Sensor Buiten</div>
<div class="de">
<div class="den">
<div class="dene">
<div class="denem">
<div class="deneme">
<?php print tempParts($lastTemps['temp1'], 0); ?><span>.<?php print tempParts($lastTemps['temp1'], 1); ?></span><strong>°</strong>
</div>
</div>
</div>
</div>
</div>
<div class="de">
<div class="den">
<div class="dene">
<div class="denem">
<div class="deneme">
<?php print tempParts($lastTemps['temp2'], 0); ?><span>.<?php print tempParts($lastTemps['temp2'], 1); ?></span><strong>°</strong>
</div>
</div>
</div>
</div>
</div>
<div class="details">Gem laatste 24u<br /><?php print number_format($lastAVGTemps['temp1'], 1); ?>°</div><div class="details">Gem laatste 24u<br /><?php print number_format($lastAVGTemps['temp2'], 1); ?>°</div>
<div class="last-update"><?php print date('d-m-Y H:i:s', strtotime($lastTemps['created_at'])); ?></div>
</div>
<div id="chartdiv" style="width:100%; height:400px;"></div>
</div>
</body>
</html>