Browse code

minor revision 20180306

fractalxaos authored on 03/07/2018 23:54:56
Showing 1 changed files
1 1
old mode 100755
2 2
new mode 100644
... ...
@@ -1,6 +1,6 @@
1 1
 #!/usr/bin/python -u
2
-## The -u option above turns off block buffering of python output. This 
3
-## assures that each error message gets individually printed to the log file.
2
+# The -u option above turns off block buffering of python output. This 
3
+# assures that each error message gets individually printed to the log file.
4 4
 #
5 5
 # Module: radmonAgent.py
6 6
 #
... ...
@@ -48,12 +48,12 @@ import calendar
48 48
 
49 49
 _USER = os.environ['USER']
50 50
 
51
-   ### DEFAULT WEATHER STATION URL ###
51
+   ### DEFAULT RADIATION MONITOR URL ###
52 52
 
53 53
 # ip address of radiation monitoring device
54
-_DEFAULT_RADIATION_MONITOR_URL = '{your radmon device url}'
54
+_DEFAULT_RADIATION_MONITOR_URL = "{your radiation device url}"
55 55
 # url if this is a mirror server
56
-_PRIMARY_SERVER_URL = '{your primary server weather data url}'
56
+_PRIMARY_SERVER_URL = "{your primary server radiation data url}"
57 57
 
58 58
     ### FILE AND FOLDER LOCATIONS ###
59 59
 
... ...
@@ -61,12 +61,12 @@ _PRIMARY_SERVER_URL = '{your primary server weather data url}'
61 61
 _DOCROOT_PATH = "/home/%s/public_html/radmon/" % _USER
62 62
 # folder for charts and output data file
63 63
 _CHARTS_DIRECTORY = _DOCROOT_PATH + "dynamic/"
64
- # database that stores weather data
65
-_RRD_FILE = "/home/%s/database/radmonData.rrd" % _USER
66 64
 # location of data input file
67 65
 _INPUT_DATA_FILE = _DOCROOT_PATH + "dynamic/radmonInputData.dat"
68 66
 # location of data output file
69 67
 _OUTPUT_DATA_FILE = _DOCROOT_PATH + "dynamic/radmonOutputData.js"
68
+# database that stores weather data
69
+_RRD_FILE = "/home/%s/database/radmonData.rrd" % _USER
70 70
 
71 71
     ### GLOBAL CONSTANTS ###
72 72
 # interval in seconds between data requests to radiation monitor
... ...
@@ -153,10 +153,12 @@ def getRadiationData():
153 153
 
154 154
     if _MIRROR_SERVER:
155 155
         sUrl = _PRIMARY_SERVER_URL
156
-    elif remoteDeviceReset:
157
-        sUrl = radiationMonitorUrl + "/reset"
158 156
     else:
159
-        sUrl = radiationMonitorUrl + "/rdata"
157
+        sUrl = radiationMonitorUrl
158
+        if remoteDeviceReset:
159
+            sUrl += "/reset"
160
+        else:
161
+            sUrl += "/rdata"
160 162
 
161 163
     try:
162 164
         conn = urllib2.urlopen(sUrl, timeout=_HTTP_REQUEST_TIMEOUT)
... ...
@@ -207,8 +209,7 @@ def parseDataString(sData, dData):
207 209
     dData['status'] = 'online'
208 210
 
209 211
     if len(dData) != 6:
210
-        print "%s parse failed: corrupted data string: %s" % \
211
-               (getTimeStamp(), sData)
212
+        print "%s parse failed: corrupted data string" % getTimeStamp()
212 213
         return False;
213 214
 
214 215
     return True
... ...
@@ -224,19 +225,18 @@ def convertData(dData):
224 225
     result = True
225 226
  
226 227
     try:
227
-
228
-        # Uncomment below to use timestamp from radiation monitoring device
229
-        # otherwise the requesting server (this) will generate the
230
-        # timestamp. Allowing the server to generate the timestamp
231
-        # prevents timestamp errors due to the radiation monitoring device
232
-        # failing to synchronize with a NTP time server.
233
-
234
-        #dData['UTC'] = time.time()
235
-
236
-        ## Convert UTC from radiation monitoring device to local time.
228
+        # Convert the UTC timestamp provided by the radiation monitoring
229
+        # device to epoch local time in seconds.
237 230
         ts_utc = time.strptime(dData['UTC'], "%H:%M:%S %m/%d/%Y")
238
-        local_sec = calendar.timegm(ts_utc)
239
-        dData['UTC'] = local_sec
231
+        epoch_local_sec = calendar.timegm(ts_utc)
232
+        dData['ELT'] = epoch_local_sec
233
+
234
+        # Uncomment the code line below to use a timestamp generated by the
235
+        # requesting server (this) instead of the timestamp provided by the
236
+        # radiation monitoring device.  Using the server generated timestamp
237
+        # prevents errors that occur when the radiation monitoring device
238
+        # fails to synchronize with a valid NTP time server.
239
+        #dData['ELT'] = time.time()
240 240
         
241 241
         dData['Mode'] = dData['Mode'].lower()
242 242
  
... ...
@@ -247,7 +247,7 @@ def convertData(dData):
247 247
         dData['CPS'] = int(dData.pop('CPS'))
248 248
 
249 249
     except Exception, exError:
250
-        print "%s convertData: %s" % (getTimeStamp(), exError)
250
+        print "%s convert data failed: %s" % (getTimeStamp(), exError)
251 251
         result = False
252 252
 
253 253
     return result
... ...
@@ -262,13 +262,17 @@ def writeOutputDataFile(dData):
262 262
        Returns true if successful, false otherwise.
263 263
     """
264 264
     # Set date to current time and data
265
-    dData['date'] = getTimeStamp()
265
+    dData['date'] = time.strftime("%m/%d/%Y %T", time.localtime(dData['ELT']))
266 266
 
267 267
     # Format the weather data as string using java script object notation.
268 268
     sData = '[{'
269
-    for key in dData:
270
-        sData += "\"%s\":\"%s\"," % (key, dData[key])
271
-    sData = sData[:-1] + '}]'
269
+    sData += "\"date\":\"%s\"," % dData['date']
270
+    sData += "\"CPM\":\"%s\"," % dData['CPM']
271
+    sData += "\"CPS\":\"%s\"," % dData['CPS']
272
+    sData += "\"uSvPerHr\":\"%s\"," % dData['uSvPerHr']
273
+    sData += "\"Mode\":\"%s\"," % dData['Mode']
274
+    sData += "\"status\":\"%s\"," % dData['status']
275
+    sData = sData[:-1] + '}]\n'
272 276
 
273 277
     # Write the string to the output data file for use by html documents.
274 278
     try:
... ...
@@ -318,7 +322,7 @@ def updateDatabase(dData):
318 322
 
319 323
     # Create the rrdtool update command.
320 324
     strCmd = "rrdtool update %s %s:%s:%s" % \
321
-                       (_RRD_FILE, dData['UTC'], dData['CPM'], SvPerHr)
325
+                       (_RRD_FILE, dData['ELT'], dData['CPM'], SvPerHr)
322 326
     if debugOption and False:
323 327
         print "%s" % strCmd # DEBUG
324 328