Browse code

revised 2016-12-23

fractalxaos authored on 12/23/2016 18:34:36
Showing 2 changed files
... ...
@@ -31,18 +31,24 @@
31 31
 # Revision History
32 32
 #   * v20 released 15 Sep 2015 by J L Owrey
33 33
 #
34
+
35
+import os
34 36
 import urllib2
35
-import time
36
-import calendar
37
+import sys   
37 38
 import subprocess
38
-import sys
39
-import os
40
-import json
41 39
 import multiprocessing
40
+import time
41
+import calendar
42
+
43
+_USER = os.environ['USER']
44
+
45
+   ### DEFAULT WEATHER STATION URL ###
46
+
47
+_DEFAULT_RADIATION_MONITOR_URL = "{your weather station url}"
48
+_DATA_FORWARDING_FILE = "/home/%s/public_html/radmon/dynamic/rad.dat" % _USER
42 49
 
43 50
     ### FILE AND FOLDER LOCATIONS ###
44 51
 
45
-_USER = os.environ['USER']
46 52
 _TMP_DIRECTORY = "/tmp/radmon" # folder for charts and output data file
47 53
 _RRD_FILE = "/home/%s/database/radmonData.rrd" % _USER # database that stores the data
48 54
 _OUTPUT_DATA_FILE = "/tmp/radmon/radmonData.js" # output file used by HTML docs
... ...
@@ -50,13 +56,12 @@ _OUTPUT_DATA_FILE = "/tmp/radmon/radmonData.js" # output file used by HTML docs
50 56
     ### GLOBAL CONSTANTS ###
51 57
 
52 58
 _DEFAULT_DATA_REQUEST_INTERVAL = 10 # interval between data requests to radiation monitor
53
-_CHART_UPDATE_INTERVAL = 300 # defines how often the charts get updated
59
+_CHART_UPDATE_INTERVAL = 300 # defines how often the charts get updated in seconds
54 60
 _DATABASE_UPDATE_INTERVAL = 30 # defines how often the database gets updated
55
-_HTTP_REQUEST_TIMEOUT = 5 # number seconds to wait for a response to HTTP request
56
-_MAX_RADIATION_MONITOR_OFFLINE_COUNT = 3 # max number of failed data requests allowed
61
+_HTTP_REQUEST_TIMEOUT = 3 # number seconds to wait for a response to HTTP request
62
+_MAX_RADIATION_MONITOR_OFFLINE_COUNT = 2 # max number of failed data requests allowed
57 63
 _CHART_WIDTH = 600
58 64
 _CHART_HEIGHT = 150
59
-_DEFAULT_RADIATION_MONITOR_URL = "{your radiation monitor url}"
60 65
 
61 66
    ### GLOBAL VARIABLES ###
62 67
 
... ...
@@ -126,7 +131,7 @@ def getRadiationData():
126 131
     global radiationMonitorOnline, radiationMonitorOfflineCount
127 132
 
128 133
     try:
129
-        conn = urllib2.urlopen(radiationMonitorUrl + "/rdata",
134
+        conn = urllib2.urlopen(radiationMonitorUrl,
130 135
                                timeout=_HTTP_REQUEST_TIMEOUT)
131 136
 
132 137
         # Format received data into a single string.
... ...
@@ -152,8 +157,9 @@ def getRadiationData():
152 157
         radiationMonitorOnline = True
153 158
 
154 159
     if debugOption:
155
-        print "http request successful"
156 160
         #print content
161
+        pass
162
+
157 163
     return content
158 164
 ##end def
159 165
 
... ...
@@ -236,6 +242,22 @@ def writeOutputDataFile(dData):
236 242
     return True
237 243
 ## end def
238 244
 
245
+def writeForwardingFile(sData):
246
+    """Write weather station response string to a forwarding file for use
247
+       by down stream servers that mirror this site.
248
+    """
249
+    # Write the string to the output data file for use by html documents.
250
+    try:
251
+        fc = open(_DATA_FORWARDING_FILE, "w")
252
+        fc.write(sData)
253
+        fc.close()
254
+    except Exception, exError:
255
+        print "%s writeOutputDataFile: %s" % (getTimeStamp(), exError)
256
+        return False
257
+
258
+    return True
259
+##end def
260
+
239 261
 def updateDatabase(dData):
240 262
     """
241 263
     Updates the rrdtool database by executing an rrdtool system command.
... ...
@@ -343,17 +365,17 @@ def generateGraphs():
343 365
     """
344 366
     autoScale = False
345 367
 
346
-    createGraph('radGraph1', 'CPM', 'counts\ per\ minute', 
368
+    createGraph('24hr_cpm', 'CPM', 'counts\ per\ minute', 
347 369
                 'CPM\ -\ Last\ 24\ Hours', 'end-1day', 0, 0, 2, autoScale)
348
-    createGraph('radGraph2', 'SvperHr', 'Sv\ per\ hour',
370
+    createGraph('24hr_svperhr', 'SvperHr', 'Sv\ per\ hour',
349 371
                 'Sv/Hr\ -\ Last\ 24\ Hours', 'end-1day', 0, 0, 2, autoScale)
350
-    createGraph('radGraph3', 'CPM', 'counts\ per\ minute',
372
+    createGraph('4wk_cpm', 'CPM', 'counts\ per\ minute',
351 373
                 'CPM\ -\ Last\ 4\ Weeks', 'end-4weeks', 0, 0, 2, autoScale)
352
-    createGraph('radGraph4', 'SvperHr', 'Sv\ per\ hour',
374
+    createGraph('4wk_svperhr', 'SvperHr', 'Sv\ per\ hour',
353 375
                 'Sv/Hr\ -\ Last\ 4\ Weeks', 'end-4weeks', 0, 0, 2, autoScale)
354
-    createGraph('radGraph5', 'CPM', 'counts\ per\ minute',
376
+    createGraph('12m_cpm', 'CPM', 'counts\ per\ minute',
355 377
                 'CPM\ -\ Past\ Year', 'end-12months', 0, 0, 2, autoScale)
356
-    createGraph('radGraph6', 'SvperHr', 'Sv\ per\ hour',
378
+    createGraph('12m_svperhr', 'SvperHr', 'Sv\ per\ hour',
357 379
                 'Sv/Hr\ -\ Past\ Year', 'end-12months', 0, 0, 2, autoScale)
358 380
 ##end def
359 381
 
... ...
@@ -439,7 +461,10 @@ def main():
439 461
 
440 462
             # If conversion successful, write data to output file.
441 463
             if result:
464
+                writeForwardingFile(sData)
442 465
                 writeOutputDataFile(dData)
466
+                if debugOption:
467
+                    print "http request successful"
443 468
 
444 469
                 # At the rrdtool database update interval, update the database.
445 470
                 if currentTime - lastDatabaseUpdateTime > _DATABASE_UPDATE_INTERVAL:   
... ...
@@ -200,16 +200,16 @@ function getRadmonGraphs() {
200 200
 
201 201
     switch(graphPeriod) {
202 202
         case 1:
203
-            cpmChart_g.src = "dynamic/radGraph1.png?ver=" + d.getTime();
204
-            uSvChart_g.src = "dynamic/radGraph2.png?ver=" + d.getTime();
203
+            cpmChart_g.src = "dynamic/24hr_cpm.png?ver=" + d.getTime();
204
+            uSvChart_g.src = "dynamic/24hr_svperhr.png?ver=" + d.getTime();
205 205
             break;
206 206
         case 2:
207
-            cpmChart_g.src = "dynamic/radGraph3.png?ver=" + d.getTime();
208
-            uSvChart_g.src = "dynamic/radGraph4.png?ver=" + d.getTime();
207
+            cpmChart_g.src = "dynamic/4wk_cpm.png?ver=" + d.getTime();
208
+            uSvChart_g.src = "dynamic/4wk_svperhr.png?ver=" + d.getTime();
209 209
             break;
210 210
        case 3:
211
-            cpmChart_g.src = "dynamic/radGraph5.png?ver=" + d.getTime();
212
-            uSvChart_g.src = "dynamic/radGraph6.png?ver=" + d.getTime();
211
+            cpmChart_g.src = "dynamic/12m_cpm.png?ver=" + d.getTime();
212
+            uSvChart_g.src = "dynamic/12m_svperhr.png?ver=" + d.getTime();
213 213
             break;
214 214
     }
215 215
 }