Browse code

revisions

Jeff Owrey authored on 02/07/2016 04:35:37
Showing 2 changed files
... ...
@@ -42,8 +42,9 @@ import multiprocessing
42 42
 
43 43
     ### FILE AND FOLDER LOCATIONS ###
44 44
 
45
+_USER = os.environ['USER']
45 46
 _TMP_DIRECTORY = "/tmp/radmon" # folder for charts and output data file
46
-_RRD_FILE = "/home/{your user id}/database/radmonData.rrd"  # database that stores the data
47
+_RRD_FILE = "/home/%s/database/radmonData.rrd" % _USER # database that stores the data
47 48
 _OUTPUT_DATA_FILE = "/tmp/radmon/radmonData.js" # output file used by HTML docs
48 49
 
49 50
     ### GLOBAL CONSTANTS ###
... ...
@@ -52,11 +53,13 @@ _DEFAULT_WEB_DATA_UPDATE_INTERVAL = 10
52 53
 _CHART_UPDATE_INTERVAL = 60 # defines how often the charts get updated
53 54
 _DATABASE_UPDATE_INTERVAL = 30 # defines how often the database gets updated
54 55
 _HTTP_REQUEST_TIMEOUT = 5 # number seconds to wait for a response to HTTP request
56
+_CHART_WIDTH = 600
57
+_CHART_HEIGHT = 150
55 58
 
56 59
    ### GLOBAL VARIABLES ###
57 60
 
58 61
 webUpdateInterval = _DEFAULT_WEB_DATA_UPDATE_INTERVAL  # web update frequency
59
-deviceUrl = "{your URL}"  # radiation monitor network address
62
+deviceUrl = "http://73.157.139.23:4371"  # radiation monitor network address
60 63
 deviceOnline = True
61 64
 debugOption = False
62 65
 
... ...
@@ -211,7 +214,7 @@ def writeOutputDataFile(dData):
211 214
         print "%s writeOutputDataFile: %s" % (getTimeStamp(), exError)
212 215
         return False
213 216
 
214
-    if debugOption:
217
+    if debugOption and 0:
215 218
         print sData
216 219
 
217 220
     return True
... ...
@@ -247,7 +250,7 @@ def updateDatabase(dData):
247 250
     return True
248 251
 ##end def
249 252
 
250
-def createGraph(fileName, dataItem, gTitle, gStart):
253
+def createGraph(fileName, dataItem, gLabel, gTitle, gStart, lower, upper, addTrend):
251 254
     """Uses rrdtool to create a graph of specified weather data item.
252 255
        Parameters:
253 256
            fileName - name of graph image file
... ...
@@ -257,30 +260,52 @@ def createGraph(fileName, dataItem, gTitle, gStart):
257 260
        Returns true if successful, false otherwise.
258 261
     """
259 262
     gPath = _TMP_DIRECTORY + '/' + fileName + ".png"
260
-
261
-    # Create the rrdtool graph command.
262
-    strFmt = ("rrdtool graph %s -a PNG -s %s -w 600 -h 150 "
263
-                           ##  "-l 50 -u 110 -r "
264
-                               "-v %s -t %s "
265
-                               "DEF:%s=%s:%s:AVERAGE "
266
-                               "LINE2:%s\#0400ff:")         
267
-    strCmd = strFmt % (gPath, gStart, dataItem, gTitle, dataItem, \
268
-                       _RRD_FILE, dataItem, dataItem)
263
+    trendWindow = { 'end-1day': 7200,
264
+                    'end-4weeks': 172800,
265
+                    'end-12months': 604800 }
266
+ 
267
+    # Format the rrdtool graph command.
268
+
269
+    # Set chart start time, height, and width.
270
+    strCmd = "rrdtool graph %s -a PNG -s %s -e now -w %s -h %s " \
271
+             % (gPath, gStart, _CHART_WIDTH, _CHART_HEIGHT)
272
+   
273
+    # Set the range of the chart ordinate dataum.
274
+    if lower < upper:
275
+        strCmd  +=  "-l %s -u %s " % (lower, upper)
276
+    else:
277
+        #strCmd += "-A -Y "
278
+        strCmd += "-Y "
279
+
280
+    # Set the chart ordinate label and chart title. 
281
+    strCmd += "-v %s -t %s " % (gLabel, gTitle)
282
+
283
+    # Show the data, or a moving average trend line over
284
+    # the data, or both.
285
+    strCmd += "DEF:%s=%s:%s:AVERAGE " % (dataItem, _RRD_FILE, dataItem)
286
+
287
+    if addTrend == 0 or addTrend == 2:
288
+        strCmd += "LINE1:%s\#0400ff " % (dataItem)
289
+    if addTrend == 1 or addTrend == 2:
290
+        strCmd += "CDEF:smoothed=%s,%s,TREND LINE1:smoothed#ff0000" \
291
+                  % (dataItem, trendWindow[gStart])
292
+       
269 293
     if debugOption:
270 294
         print "%s\n" % strCmd # DEBUG
271 295
     
272
-    # Run the command as a subprocess.
296
+    # Run the formatted rrdtool command as a subprocess.
273 297
     try:
274
-        result = subprocess.check_output(strCmd, stderr=subprocess.STDOUT, \
298
+        result = subprocess.check_output(strCmd, \
299
+                     stderr=subprocess.STDOUT,   \
275 300
                      shell=True)
276 301
     except subprocess.CalledProcessError, exError:
277
-        print "rdtool graph failed: %s" % (exError.output)
302
+        print "rrdtool graph failed: %s" % (exError.output)
278 303
         return False
279 304
 
280 305
     if debugOption:
281 306
         print "rrdtool graph: %s" % result
282
-
283 307
     return True
308
+
284 309
 ##end def
285 310
 
286 311
 def getCLarguments():
... ...
@@ -318,12 +343,12 @@ def generateGraphs():
318 343
        Parameters: none
319 344
        Returns nothing.
320 345
     """
321
-    createGraph('radGraph1', 'CPM', "'CPM - Last 24 Hours'", 'end-1day')
322
-    createGraph('radGraph2', 'SvperHr', "'Sv/Hr - Last 24 Hours'", 'end-1day')
323
-    createGraph('radGraph3', 'CPM', "'CPM - Last 4 Weeks'", 'end-4weeks')
324
-    createGraph('radGraph4', 'SvperHr', "'Sv/Hr - Last 4 Weeks'", 'end-4weeks')
325
-    createGraph('radGraph5', 'CPM', "'CPM - Past Year'", 'end-12months')
326
-    createGraph('radGraph6', 'SvperHr', "'Sv/Hr - Past Year'", 'end-12months')
346
+    createGraph('radGraph1', 'CPM', "'counts per minute'", "'CPM - Last 24 Hours'", 'end-1day', 0, 0, 2)
347
+    createGraph('radGraph2', 'SvperHr', "'Sv per hour'", "'Sv/Hr - Last 24 Hours'", 'end-1day', 0, 0, 2)
348
+    createGraph('radGraph3', 'CPM', "'counts per minute'", "'CPM - Last 4 Weeks'", 'end-4weeks', 0, 0, 2)
349
+    createGraph('radGraph4', 'SvperHr', "'Sv per hour'", "'Sv/Hr - Last 4 Weeks'", 'end-4weeks', 0, 0, 2)
350
+    createGraph('radGraph5', 'CPM', "'counts per minute'", "'CPM - Past Year'", 'end-12months', 0, 0, 2)
351
+    createGraph('radGraph6', 'SvperHr', "'Sv per hour'", "'Sv/Hr - Past Year'", 'end-12months', 0, 0, 2)
327 352
 ##end def
328 353
 
329 354
 def main():
... ...
@@ -13,7 +13,7 @@ h4 {
13 13
     font: bold 16px arial, sans-serif;
14 14
 }
15 15
 .mainContainer {
16
-    width: 800px;
16
+    width: 750px;
17 17
     text-align: center;
18 18
     margin: auto;
19 19
 }
... ...
@@ -50,6 +50,31 @@ img.chart {
50 50
     text-align: left;
51 51
     padding: 10px;
52 52
 }
53
+span.chartNav {
54
+    margin: auto;
55
+}
56
+ul.chartNav {
57
+    list-style-type: none;
58
+    margin: 10px;
59
+    padding: 0;
60
+    overflow: hidden;
61
+    background-color: #bbb;
62
+    text-align: center;
63
+}
64
+li.chartNav {
65
+    display: inline-block;
66
+    font: bold 18px arial, sans-serif;
67
+    color: black;
68
+}
69
+text.chartNav:hover {
70
+    background-color: #333;
71
+    cursor: pointer;
72
+    color: white;
73
+}
74
+text.chartNav {
75
+    display: inline-block;
76
+    padding: 8px 12px;
77
+}
53 78
 </style>
54 79
 </head>
55 80
 
... ...
@@ -94,30 +119,22 @@ Mode:
94 119
 </div>
95 120
 </div>
96 121
 
122
+<span class="chartNav">
123
+<ul class="chartNav">
124
+<li class="chartNav">Select charts:</li>
125
+<li class="chartNav"><text class="chartNav" onclick="show_charts(1)">24 hours</text></li>
126
+<li class="chartNav"><text class="chartNav" onclick="show_charts(2)">4 weeks</text></li>
127
+<li class="chartNav"><text class="chartNav" onclick="show_charts(3)">12 months</text></li>
128
+</ul>
129
+</span>
97 130
 <br>
98 131
 
99 132
 <div class="chartContainer">
100
-<img class="chart" id="1-day-cpm">
133
+<img class="chart" id="cpmChart">
101 134
 </div>
102 135
 
103 136
 <div class="chartContainer">
104
-<img class="chart" id="1-day-uSv">
105
-</div>
106
-
107
-<div class="chartContainer">
108
-<img class="chart" id="4-week-cpm">
109
-</div>
110
-
111
-<div class="chartContainer">
112
-<img class="chart" id="4-week-uSv">
113
-</div>
114
-
115
-<div class="chartContainer">
116
-<img class="chart" id="1-year-cpm">
117
-</div>
118
-
119
-<div class="chartContainer">
120
-<img class="chart" id="1-year-uSv">
137
+<img class="chart" id="uSvChart">
121 138
 </div>
122 139
 
123 140
 <div class="notes">
... ...
@@ -140,12 +157,12 @@ Radiation Dose Chart</a> by Randall Monroe.</li>
140 157
 /* Globals */
141 158
 var radmonDataUrl = "dynamic/radmonData.js";
142 159
 var httpRequest = new XMLHttpRequest();
160
+var graphPeriod;
143 161
 
144 162
 function main() {
145 163
     getRadmonData();
146
-    getRadmonGraphs();
164
+    show_charts(1);
147 165
     setInterval(getRadmonData, 10000);
148
-    setInterval(getRadmonGraphs, 60000);
149 166
 }
150 167
 
151 168
 function getRadmonData() {
... ...
@@ -159,14 +176,34 @@ function getRadmonData() {
159 176
     httpRequest.send();
160 177
 }
161 178
 
179
+function show_charts(n) {
180
+    graphPeriod = n;
181
+    getRadmonGraphs();   
182
+    setInterval(getRadmonGraphs, 60000);
183
+}
184
+
162 185
 function getRadmonGraphs() {
163 186
     var d = new Date;
164
-    document.getElementById("1-day-cpm").src = "dynamic/radGraph1.png?ver=" + d.getTime();
165
-    document.getElementById("1-day-uSv").src = "dynamic/radGraph2.png?ver=" + d.getTime();
166
-    document.getElementById("4-week-cpm").src = "dynamic/radGraph3.png?ver=" + d.getTime();
167
-    document.getElementById("4-week-uSv").src = "dynamic/radGraph4.png?ver=" + d.getTime();
168
-    document.getElementById("1-year-cpm").src = "dynamic/radGraph5.png?ver=" + d.getTime();
169
-    document.getElementById("1-year-uSv").src = "dynamic/radGraph6.png?ver=" + d.getTime();
187
+    switch(graphPeriod) {
188
+        case 1:
189
+            document.getElementById("cpmChart").src =
190
+                "dynamic/radGraph1.png?ver=" + d.getTime();
191
+            document.getElementById("uSvChart").src = 
192
+                "dynamic/radGraph2.png?ver=" + d.getTime();
193
+            break;
194
+        case 2:
195
+            document.getElementById("cpmChart").src = 
196
+                "dynamic/radGraph3.png?ver=" + d.getTime();
197
+            document.getElementById("uSvChart").src = 
198
+                "dynamic/radGraph4.png?ver=" + d.getTime();
199
+            break;
200
+       case 3:
201
+            document.getElementById("cpmChart").src = 
202
+                "dynamic/radGraph5.png?ver=" + d.getTime();
203
+            document.getElementById("uSvChart").src = 
204
+                "dynamic/radGraph6.png?ver=" + d.getTime();
205
+            break;
206
+    }
170 207
 }
171 208
 
172 209
 function displayData(dataItem) {