forked from fechu/PiTemp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
197 lines (165 loc) · 5.68 KB
/
index.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
<?php
// Get average functions and all that stuff
include 'lib/functions.php';
// Load the configuration
$config = getConfig();
// Load the temperature.log file
$temperatureFile = file_get_contents('temperature.log');
// Regex to capture format: 2013-07-22T21:30:01+0200;46.5
$regex = "/^((?:[0-9]{2,4}-?){3})T((?:[0-9]{2}:?){3}).*?;([0-9.]*$)/im";
preg_match_all($regex, $temperatureFile, $result);
// Prepare the values
$values = $result[3];
$floatValues = array();
foreach ($values as $value) {
$floatValue = floatval($value);
// Conversation to farenheit?
if ($config['unit'] == 'f') {
$floatValue = $floatValue * 9 / 5 + 32; // Conversion to Farenheit
}
$floatValues[] = $floatValue;
}
$valuesLast24Hours = array_slice($floatValues, -96);
$valuesLastWeek = array_slice($floatValues, -96*7);
// Merge date and time part together
$labels = array_map(function($a, $b){
$dateString = $a . ' ' . $b . ' UTC';
return strtotime($dateString) * 1000;
}, $result[1], $result[2]);
// Create Points for Flot charting library
$points = array_map(function($a, $b){
return array($a, $b);
}, $labels, $floatValues);
// Calculate average and min/max values
$average = average($floatValues);
$lowest = getMin($floatValues);
$highest = getMax($floatValues);
$average24Hours = average($valuesLast24Hours);
$lowest24Hours = getMin($valuesLast24Hours);
$highest24Hours = getMax($valuesLast24Hours);
$averageWeek = average($valuesLastWeek);
$lowestWeek = getMin($valuesLastWeek);
$highestWeek = getMax($valuesLastWeek);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Raspberry Pi Monitor</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<meta http-equiv="refresh" content="900">
<!-- Le styles -->
<link href="css/bootstrap.css" rel="stylesheet">
<style>
body {
padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
}
</style>
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">Raspberry Pi</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li class="active"><a href="#">Temperature</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<h3>Last 24 Hours</h3>
<div id="day" style="height:300px" class="span9"></div>
<div class="span2 well">
<p>Average: <strong class="pull-right"><?php echo $average24Hours;?> C°</strong></p>
<p>Lowest: <strong class="pull-right"><?php echo $lowest24Hours?> C°</strong></p>
<p>Highest: <strong class="pull-right"><?php echo $highest24Hours?> C°</strong></p>
</div>
</div>
<div class="row">
<h3>Last Week</h3>
<div id="week" style="height:300px" class="span9"></div>
<div class="span2 well">
<p>Average: <strong class="pull-right"><?php echo $averageWeek;?> C°</strong></p>
<p>Lowest: <strong class="pull-right"><?php echo $lowestWeek?> C°</strong></p>
<p>Highest: <strong class="pull-right"><?php echo $highestWeek?> C°</strong></p>
</div>
</div>
<div class="row">
<h3>Overall</h3>
<div id="overall" style="height:300px" class="span9"></div>
<div class="span2 well">
<p>Average: <strong class="pull-right"><?php echo $average;?> C°</strong></p>
<p>Lowest: <strong class="pull-right"><?php echo $lowest?> C°</strong></p>
<p>Highest: <strong class="pull-right"><?php echo $highest?> C°</strong></p>
</div>
</div>
</div> <!-- /container -->
<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.flot.min.js"></script>
<script src="js/jquery.flot.time.min.js""></script>
<script src="js/jquery.flot.tooltip.min.js""></script>
<?php
// Prepare the data
$pointsDay = array_slice($points, -96);
$pointsWeek = array_slice($points, -672);
?>
<script type="text/javascript">
$(document).ready(function() {
var options = {
xaxis: {
mode: "time",
format: "%d.%m.Y %H:%M"
},
yaxis: {
tickFormatter: function(value) {
return value.toFixed(1) + ' <?php echo strtoupper($config['unit'])?>°';
}
},
grid: {
hoverable: true,
clickable: true,
},
tooltip: true,
tooltipOpts: {
content: "%y"
}
};
var dataDay = [{
color: "rgb(212,62, 48)",
data: <?php echo json_encode($pointsDay);?>
}];
$.plot($("#day"), dataDay, options);
var dataWeek = [{
color: "rgb(212,62, 48)",
data: <?php echo json_encode($pointsWeek);?>
}];
$.plot($("#week"), dataWeek, options);
var dataOverall = [{
color: "rgb(212,62, 48)",
data: <?php echo json_encode($points);?>
}];
$.plot($("#overall"), dataOverall, options);
});
</script>
</body>
</html>