Browse code

improve readibility

Gandolf authored on 06/20/2021 00:49:50
Showing 1 changed files
... ...
@@ -55,6 +55,9 @@ img.chart {
55 55
 
56 56
 # Define global constants
57 57
 
58
+# debug mode
59
+define("_DEBUG", false);
60
+
58 61
 # round robin database file
59 62
 define("_RRD_FILE", str_replace("public_html/power/power.php",
60 63
                                 "database/powerData.rrd",
... ...
@@ -67,8 +70,6 @@ define("_CHART_DIRECTORY", str_replace("power.php",
67 70
 define("_CHART_WIDTH", 600);
68 71
 # standard chart height in pixels
69 72
 define("_CHART_HEIGHT", 150);
70
-# debug mode
71
-define("_DEBUG", false);
72 73
 
73 74
 # Set error handling modes.
74 75
 error_reporting(E_ALL);
... ...
@@ -101,6 +102,30 @@ if ($beginDateEp > $endDateEp) {
101 102
           date('m / d / Y', $firstDP) . " and " . 
102 103
           date('m / d / Y', $lastDP) . ".</p>";
103 104
 } else {
105
+    generateCharts($beginDateEp, $endDateEp);
106
+    sendChartsToClient();
107
+}
108
+
109
+function sendChartsToClient() {
110
+    # Send html commands to client browser.
111
+    echo "<div class=\"chartContainer\">" .
112
+         "<img class=\"chart\" src=\"dynamic/custom_current.png\">" .
113
+         "</div>";
114
+    echo "<div class=\"chartContainer\">" .
115
+         "<img class=\"chart\" src=\"dynamic/custom_voltage.png\">" .
116
+         "</div>";
117
+    echo "<div class=\"chartContainer\">" .
118
+         "<img class=\"chart\" src=\"dynamic/custom_power.png\">" .
119
+         "</div>";
120
+    echo "<div class=\"chartContainer\">" .
121
+         "<img class=\"chart\" src=\"dynamic/custom_battemp.png\">" .
122
+         "</div>";
123
+    echo "<div class=\"chartContainer\">" .
124
+         "<img class=\"chart\" src=\"dynamic/custom_ambtemp.png\">" .
125
+         "</div>";
126
+}
127
+
128
+function generateCharts($beginDateEp, $endDateEp) {
104 129
     # Generate charts from validated user supplied dates.
105 130
     if (_DEBUG) {
106 131
         echo "<p>Date range: " . $beginDateEp . " thru " .
... ...
@@ -111,32 +136,16 @@ if ($beginDateEp > $endDateEp) {
111 136
                  0, 0, 2, false);
112 137
     createChart('custom_voltage', 'VOLT', 'Volts', 
113 138
                 'Voltage', $beginDateEp, $endDateEp,
114
-                 0, 0, 2, false);
139
+                 0, 0, 0, false);
115 140
     createChart('custom_power', 'PWR', 'Watts', 
116 141
                 'Power', $beginDateEp, $endDateEp,
117 142
                  0, 0, 2, false);
118 143
     createChart('custom_battemp', 'BTMP', 'deg\ F', 
119 144
                 'Battery\ Temperature', $beginDateEp, $endDateEp,
120
-                 0, 0, 2, false);
145
+                 0, 0, 0, false);
121 146
     createChart('custom_ambtemp', 'ATMP', 'deg\ F', 
122 147
                 'Ambient\ Temperature', $beginDateEp, $endDateEp,
123
-                 0, 0, 2, false);
124
-    # Send html commands to client browser.
125
-    echo "<div class=\"chartContainer\">" .
126
-         "<img class=\"chart\" src=\"dynamic/custom_current.png\">" .
127
-         "</div>";
128
-    echo "<div class=\"chartContainer\">" .
129
-         "<img class=\"chart\" src=\"dynamic/custom_voltage.png\">" .
130
-         "</div>";
131
-    echo "<div class=\"chartContainer\">" .
132
-         "<img class=\"chart\" src=\"dynamic/custom_power.png\">" .
133
-         "</div>";
134
-    echo "<div class=\"chartContainer\">" .
135
-         "<img class=\"chart\" src=\"dynamic/custom_battemp.png\">" .
136
-         "</div>";
137
-    echo "<div class=\"chartContainer\">" .
138
-         "<img class=\"chart\" src=\"dynamic/custom_ambtemp.png\">" .
139
-         "</div>";
148
+                 0, 0, 0, false);
140 149
 }
141 150
 
142 151
 function createChart($chartFile, $dataItem, $label, $title, $begin,
Browse code

added custom charts

Gandolf authored on 06/12/2021 06:29:52
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,222 @@
1
+<html>
2
+<!-- Courtsey ruler
3
+12345678901234567890123456789012345678901234567890123456789012345678901234567890
4
+-->
5
+<head>
6
+<style>
7
+p {
8
+    font: 14px ariel, sans serif;
9
+}
10
+#errorMsg {
11
+    font:bold 18px arial,sans-serif;
12
+    color:red;
13
+    text-align:center;
14
+}
15
+.chartContainer {
16
+    padding: 2px;
17
+}
18
+img.chart {
19
+    width:100%;
20
+}
21
+</style>
22
+</head>
23
+<body>
24
+
25
+<?php
26
+/*
27
+ Script: power.php
28
+
29
+ Description: This scripts generates on the server charts showing
30
+ power data spanning the period supplied by the user.  The script
31
+ does the following:
32
+    - converts user supplied dates to  epoch time
33
+    - gets the times of the first and last data point in the round
34
+      robin database (RRD)
35
+    - from above validates user supplied begin and end dates
36
+    - creates charts of the specified period
37
+
38
+ Copyright 2020 Jeff Owrey
39
+    This program is free software: you can redistribute it and/or modify
40
+    it under the terms of the GNU General Public License as published by
41
+    the Free Software Foundation, either version 3 of the License, or
42
+    (at your option) any later version.
43
+
44
+    This program is distributed in the hope that it will be useful,
45
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
46
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
+    GNU General Public License for more details.
48
+
49
+    You should have received a copy of the GNU General Public License
50
+    along with this program.  If not, see http://www.gnu.org/license.
51
+
52
+ Revision History
53
+   * v10 released 11 Jan 2021 by J L Owrey; first release
54
+*/
55
+
56
+# Define global constants
57
+
58
+# round robin database file
59
+define("_RRD_FILE", str_replace("public_html/power/power.php",
60
+                                "database/powerData.rrd",
61
+                                $_SERVER["SCRIPT_FILENAME"]));
62
+# charts html directory
63
+define("_CHART_DIRECTORY", str_replace("power.php",
64
+                                       "dynamic/",
65
+                                       $_SERVER["SCRIPT_FILENAME"]));
66
+# standard chart width in pixels
67
+define("_CHART_WIDTH", 600);
68
+# standard chart height in pixels
69
+define("_CHART_HEIGHT", 150);
70
+# debug mode
71
+define("_DEBUG", false);
72
+
73
+# Set error handling modes.
74
+error_reporting(E_ALL);
75
+
76
+# Get user supplied chart begin and end dates.
77
+$beginDate = $_POST["beginDate"];
78
+$endDate =  $_POST["endDate"];
79
+
80
+# Convert the user supplied dates to epoch time stamps.
81
+$beginDateEp = strtotime($beginDate);
82
+$endDateEp = strtotime($endDate);
83
+
84
+# Get the time stamp of the earliest data point in the RRD file.
85
+$cmd = sprintf("rrdtool first %s --rraindex 1", _RRD_FILE);
86
+$firstDP = shell_exec($cmd);
87
+
88
+# Get the time stamp of the latest data point in the RRD file.
89
+$cmd = sprintf("rrdtool last %s", _RRD_FILE);
90
+$lastDP = shell_exec($cmd);
91
+
92
+# Determine validity of user supplied dates.  User supplied begin
93
+# date must be less than user supplied end date.  Furthermore both
94
+# dates must be within the range of dates stored in the RRD.
95
+if ($beginDateEp > $endDateEp) {
96
+    echo "<p id=\"errorMsg\">" .
97
+         "End date must be after begin date.</p>";
98
+} elseif ($beginDateEp < $firstDP || $endDateEp > $lastDP) {
99
+    echo "<p id=\"errorMsg\">" .
100
+          "Date range must be between " .
101
+          date('m / d / Y', $firstDP) . " and " . 
102
+          date('m / d / Y', $lastDP) . ".</p>";
103
+} else {
104
+    # Generate charts from validated user supplied dates.
105
+    if (_DEBUG) {
106
+        echo "<p>Date range: " . $beginDateEp . " thru " .
107
+              $endDateEp . "</p>";
108
+    }
109
+    createChart('custom_current', 'CUR', 'Amps', 
110
+                'Current', $beginDateEp, $endDateEp,
111
+                 0, 0, 2, false);
112
+    createChart('custom_voltage', 'VOLT', 'Volts', 
113
+                'Voltage', $beginDateEp, $endDateEp,
114
+                 0, 0, 2, false);
115
+    createChart('custom_power', 'PWR', 'Watts', 
116
+                'Power', $beginDateEp, $endDateEp,
117
+                 0, 0, 2, false);
118
+    createChart('custom_battemp', 'BTMP', 'deg\ F', 
119
+                'Battery\ Temperature', $beginDateEp, $endDateEp,
120
+                 0, 0, 2, false);
121
+    createChart('custom_ambtemp', 'ATMP', 'deg\ F', 
122
+                'Ambient\ Temperature', $beginDateEp, $endDateEp,
123
+                 0, 0, 2, false);
124
+    # Send html commands to client browser.
125
+    echo "<div class=\"chartContainer\">" .
126
+         "<img class=\"chart\" src=\"dynamic/custom_current.png\">" .
127
+         "</div>";
128
+    echo "<div class=\"chartContainer\">" .
129
+         "<img class=\"chart\" src=\"dynamic/custom_voltage.png\">" .
130
+         "</div>";
131
+    echo "<div class=\"chartContainer\">" .
132
+         "<img class=\"chart\" src=\"dynamic/custom_power.png\">" .
133
+         "</div>";
134
+    echo "<div class=\"chartContainer\">" .
135
+         "<img class=\"chart\" src=\"dynamic/custom_battemp.png\">" .
136
+         "</div>";
137
+    echo "<div class=\"chartContainer\">" .
138
+         "<img class=\"chart\" src=\"dynamic/custom_ambtemp.png\">" .
139
+         "</div>";
140
+}
141
+
142
+function createChart($chartFile, $dataItem, $label, $title, $begin,
143
+                     $end, $lower, $upper, $addTrend, $autoScale) {
144
+    /*
145
+    Uses rrdtool to create a chart of specified aredn node data item.
146
+    Parameters:
147
+       fileName - name of the created chart file
148
+       dataItem - data item to be charted
149
+       label - string containing a label for the item to be charted
150
+       title - string containing a title for the chart
151
+       begin - beginning time of the chart data
152
+       end   - ending time of the data to be charted
153
+       lower - lower bound for chart ordinate #NOT USED
154
+       upper - upper bound for chart ordinate #NOT USED
155
+       addTrend - 0, show only chart data
156
+                  1, show only a trend line
157
+                  2, show a trend line and the chart data
158
+       autoScale - if True, then use vertical axis auto scaling
159
+           (lower and upper parameters are ignored), otherwise use
160
+           lower and upper parameters to set vertical axis scale
161
+    Returns: True if successful, False otherwise
162
+    */
163
+
164
+    # Define path on server to chart files.
165
+    $chartPath = _CHART_DIRECTORY . $chartFile . ".png";
166
+
167
+    # Format the rrdtool chart command.
168
+
169
+    # Set chart file name, start time, end time, height, and width.
170
+    $cmdfmt = "rrdtool graph %s -a PNG -s %s -e %s -w %s -h %s ";
171
+    $cmd = sprintf($cmdfmt, $chartPath, $begin, $end, _CHART_WIDTH,
172
+                   _CHART_HEIGHT);
173
+    $cmdfmt = "-l %s -u %s -r ";
174
+
175
+    # Set upper and lower ordinate bounds.
176
+    if ($lower < $upper) {
177
+        $cmd .= sprintf($cmdfmt, $lower, $upper);
178
+    } elseif ($autoScale) {
179
+        $cmd .= "-A ";
180
+    }
181
+    $cmd .= "-Y ";
182
+
183
+    # Set the chart ordinate label and chart title. 
184
+    $cmdfmt = "-v %s -t %s ";
185
+    $cmd .= sprintf($cmdfmt, $label, $title);
186
+   
187
+    # Define moving average window width.
188
+    $trendWindow = floor(($end - $begin) / 12);
189
+        
190
+    # Show the data, or a moving average trend line over
191
+    # the data, or both.
192
+    $cmdfmt = "DEF:dSeries=%s:%s:LAST ";
193
+    $cmd .= sprintf($cmdfmt, _RRD_FILE, $dataItem);
194
+    if ($addTrend == 0) {
195
+        $cmd .= "LINE1:dSeries#0400ff ";
196
+    } elseif ($addTrend == 1) {
197
+        $cmdfmt = "CDEF:smoothed=dSeries,%s,TREND LINE3:smoothed#006600 ";
198
+        $cmd .= sprintf($cmdfmt, $trendWindow);
199
+    } elseif ($addTrend == 2) {
200
+        $cmd .= "LINE1:dSeries#0400ff ";
201
+        $cmdfmt = "CDEF:smoothed=dSeries,%s,TREND LINE3:smoothed#006600 ";
202
+        $cmd .=  sprintf($cmdfmt, $trendWindow);
203
+    }
204
+     
205
+    # Execute the formatted rrdtool command in the shell. The rrdtool
206
+    # command will complete execution before the html image tags get
207
+    # sent to the browser.  This assures that the charts are available
208
+    # when the client browser executes the html code that loads the
209
+    # charts into the document displayed by the client browser.
210
+    if (_DEBUG) {
211
+        echo "<p>chart command:<br>" . $cmd . "</p>";
212
+    }
213
+    $result = shell_exec($cmd . " 2>&1");
214
+    if (_DEBUG) {
215
+        echo "<p>result:<br>" . $result . "</p>";
216
+    }
217
+}
218
+
219
+?>
220
+
221
+</body>
222
+</html>