-
Notifications
You must be signed in to change notification settings - Fork 4
/
meter-stats.php
executable file
·135 lines (119 loc) · 5.18 KB
/
meter-stats.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
<?php
include "./meterParams.php";
if(isset($_POST['id'])) {
$mId = $_POST['id'];
}
// connect to db
include "./dbConnect.php"; // $link = mysqli_connect("hostname", "utility_mon", "password", "UtilityMon"); ...
$mPrimaryKey = "";
$mPrimaryKey2 = "";
// most recent data
$mostRecentQ = $link->query("SELECT mPrimaryKey, mTime, mTotalConsumption, mConsumed from UtilityMeter WHERE mPrimaryKey = (SELECT max(mPrimaryKey) FROM UtilityMeter WHERE mId = $mId);");
// second most recent
$nextRecentQ = $link->query("SELECT mPrimaryKey, mTime, mTotalConsumption, mConsumed from UtilityMeter WHERE mId = $mId order by mPrimaryKey desc LIMIT 1,1;");
// very first data point
$firstQ = $link->query("SELECT mTime, mTotalConsumption from UtilityMeter WHERE mPrimaryKey = (SELECT min(mPrimaryKey) FROM UtilityMeter WHERE mId = $mId);");
// average of all data points
$avgUseQ = $link->query("SELECT mType, avg(mConsumed), count(mPrimaryKey) FROM UtilityMeter WHERE mId = $mId;");
if(!$mostRecentQ || !$avgUseQ || !$firstQ || !$nextRecentQ) {
die("Query Failed: ".$link->error);
}
// Fetch Queries:
// get avg (incremental) use calculated by mysql
$row=$avgUseQ->fetch_array(MYSQLI_ASSOC);
$mType = $row['mType'];
$avgUse = $row['avg(mConsumed)']; // average incremental use
$pktsRecv = $row['count(mPrimaryKey)']; // get count here because we already have the full list (length)
// Get most recent data
$row=$mostRecentQ->fetch_array(MYSQLI_ASSOC);
$mPrimaryKey = $row['mPrimaryKey'];
$mConsumed = $row['mConsumed'];
$lastReading = $row['mTotalConsumption'];
$lastTime = $row['mTime'];
$lastTimeDateFormat = date('Y-m-d H:i:s', $lastTime);
// get next most recent
$row=$nextRecentQ->fetch_array(MYSQLI_ASSOC);
$nextMConsumed = $row['mConsumed'];
// check if it's set; if it isn't (only one data point) make it equal to the only available value
if ($nextMConsumed == 0) {
$nextMConsumed = $mConsumed;
}
// get first use so we can calculate regular average
$row=$firstQ->fetch_array(MYSQLI_ASSOC);
$firstReading = $row['mTotalConsumption'];
$firstTime = $row['mTime'];
// calculate values
$totalTimeDiff = $lastTime - $firstTime;
// prevent math errors if 0
if ($totalTimeDiff == 0) {
$totalTimeDiff = 1;
}
$totalReadingDiff = $lastReading - $firstReading;
$consumedDiff = $mConsumed - $nextMConsumed;
if ($consumedDiff > 0) {
$diff = "increase";
} elseif ($consumedDiff < 0) {
$diff = "decrease";
} elseif ($consumedDiff == 0) {
$diff = "noChange";
}
echo "<div id='customer'>";
echo "Customer ID: <span id='mId'>$mId</span>";
// determine types
$mTypeStr = "";
if(in_array($mType, $electricMeterTypes)) {
$mTypeStr = "Electric";
$totalPowerDiff = $totalReadingDiff; // Wh
$mConsumed = sprintf("%.2f", $mConsumed);
$avgUse = sprintf("%.2f", $avgUse);
$totalWatts = sprintf("%.2f", $totalPowerDiff * 3600 / $totalTimeDiff);
echo "<br/>$mTypeStr Meter ($mType)";
echo "</div>"; // end customer id
echo "<div id='meterContent'>";
echo "<div id='utilityData'> <span class='$diff bold'>Current Use: <span id='mConsumed'>$mConsumed</span> Watts</span>";
echo "<br/>Avg Incremental Use: $avgUse Watts";
echo "<br/>Avg Use: $totalWatts Watts";
echo "</div>"; // end utilityData id
echo "<div id='sdrData'>";
echo "<br/>$pktsRecv Data Points";
echo "<br/>Last Data Point Collected: $lastTimeDateFormat";
echo "</div>"; // end sdrData id
echo "</div>"; // end meterContent id
} elseif(in_array($mType, $gasMeterTypes)) {
$mTypeStr = "Gas";
$totalGasDiff = $totalReadingDiff;
$mConsumed = sprintf("%.4f", $mConsumed);
$avgUse = sprintf("%.4f", $avgUse);
$totalGas = sprintf("%.4f", $totalGasDiff / $totalTimeDiff); // Cubic feet / sec ??
echo "<br/>$mTypeStr Meter ($mType)";
echo "</div>"; // end customer id
echo "<div id='meterContent'>";
echo "<div id='utilityData'> <span class='$diff bold'>Current Use: <span id='mConsumed'>$mConsumed</span> Cubic Feet / Sec</span>";
echo "<br/> Avg Incremental Use: $avgUse Cubic Feet / Sec";
echo "<br/> Avg Use: $totalGas Cubic Feet / Sec";
echo "</div>"; // end utilityData id
echo "<div id='sdrData'>";
echo "<br/>$pktsRecv Data Points";
echo "<br/>Last Data Point Collected: $lastTimeDateFormat";
echo "</div>"; // end sdrData id
echo "</div>"; // end meterContent id
} elseif(in_array($mType, $waterMeterTypes)) {
$mTypeStr = "Water";
$totalWaterDiff = $totalReadingDiff;
$mConsumed = sprintf("%.4f", $mConsumed);
$avgUse = sprintf("%.4f", $avgUse);
$totalWater = sprintf("%.4f", $totalWaterDiff / $totalTimeDiff); // Cubic feet / sec ??
echo "<br/>$mTypeStr Meter ($mType)";
echo "</div>"; // end customer id
echo "<div id='meterContent'>";
echo "<div id='utilityData'> <span class='$diff bold'>Current Use: <span id='mConsumed'>$mConsumed</span> Cubic Feet / Sec</span>";
echo "<br/>Avg Incremental Use: $avgUse Cubic Feet / Sec";
echo "<br/>Avg Use: $totalWater Cubic Feet / Sec";
echo "</div>"; // end utilityData id
echo "<div id='sdrData'>";
echo "<br/>$pktsRecv Data Points";
echo "<br/>Last Data Point Collected: $lastTimeDateFormat";
echo "</div>"; // end sdrData id
echo "</div>"; // end meterContent id
}
?>