Browse code

minor revision

Gandolf authored on 07/09/2021 23:37:24
Showing 1 changed files
... ...
@@ -36,6 +36,8 @@
36 36
 #   * v23 released 16 Nov 2018 by J L Owrey: improved fault handling
37 37
 #         and data conversion
38 38
 #   * v24 released 14 Jun 2021 by J L Owrey; minor revisions
39
+#   * v25 released 9 Jul 2021 by J L Owrey; improved handling of
40
+#         monitor status function
39 41
 #
40 42
 #2345678901234567890123456789012345678901234567890123456789012345678901234567890
41 43
 
... ...
@@ -58,7 +60,7 @@ _USE_RADMON_TIMESTAMP = True
58 60
    ### DEFAULT RADIATION MONITOR URL ###
59 61
 
60 62
 _DEFAULT_RADIATION_MONITOR_URL = \
61
-    "{your radiation monitor url}"
63
+    "http://192.168.1.24"
62 64
 
63 65
     ### FILE AND FOLDER LOCATIONS ###
64 66
 
... ...
@@ -100,7 +102,7 @@ debugMode = False
100 102
 # count of failed attempts to get data from radiation monitor
101 103
 failedUpdateCount = 0
102 104
 # detected status of radiation monitor device
103
-radmonOnline = True
105
+radmonOnline = False
104 106
 
105 107
 # status of reset command to radiation monitor
106 108
 remoteDeviceReset = False
... ...
@@ -147,11 +149,9 @@ def terminateAgentProcess(signal, frame):
147 149
            signal, frame - dummy parameters
148 150
        Returns: nothing
149 151
     """
150
-    # Inform downstream clients by removing output data file.
151
-    if os.path.exists(_OUTPUT_DATA_FILE):
152
-       os.remove(_OUTPUT_DATA_FILE)
153 152
     print('%s terminating radmon agent process' % \
154 153
               (getTimeStamp()))
154
+    setStatusToOffline()
155 155
     sys.exit(0)
156 156
 ##end def
157 157
 
... ...
@@ -174,12 +174,8 @@ def getRadiationData(dData):
174 174
 
175 175
     try:
176 176
         currentTime = time.time()
177
-
178 177
         response = urlopen(sUrl, timeout=_HTTP_REQUEST_TIMEOUT)
179
-
180
-        if verboseMode:
181
-            requestTime = time.time() - currentTime
182
-            print("http request: %.4f seconds" % requestTime)
178
+        requestTime = time.time() - currentTime
183 179
 
184 180
         content = response.read().decode('utf-8')
185 181
         content = content.replace('\n', '')
... ...
@@ -198,9 +194,10 @@ def getRadiationData(dData):
198 194
 
199 195
     if debugMode:
200 196
         print(content)
197
+    if verboseMode:
198
+        print("http request successful: %.4f sec" % requestTime)
201 199
     
202 200
     dData['content'] = content
203
-
204 201
     return True
205 202
 ##end def
206 203
 
... ...
@@ -221,17 +218,19 @@ def parseDataString(dData):
221 218
         print("%s parseDataString: %s" % (getTimeStamp(), exError))
222 219
         return False
223 220
 
221
+    # Verfy the expected number of data items have been received.
222
+    if len(lData) != 5:
223
+        print("%s parse failed: corrupted data string" % getTimeStamp())
224
+        return False;
225
+
224 226
     # Load the parsed data into a dictionary for easy access.
225 227
     for item in lData:
226 228
         if "=" in item:
227 229
             dData[item.split('=')[0]] = item.split('=')[1]
230
+
228 231
     # Add status to dictionary object
229 232
     dData['status'] = 'online'
230
-
231
-    # Verfy the expected number of data items have been received.
232
-    if len(dData) != 6:
233
-        print("%s parse failed: corrupted data string" % getTimeStamp())
234
-        return False;
233
+    dData['serverMode'] = _SERVER_MODE
235 234
 
236 235
     return True
237 236
 ##end def
... ...
@@ -278,18 +277,11 @@ def writeOutputFile(dData):
278 277
                    to the output data file
279 278
        Returns: True if successful, False otherwise
280 279
     """
281
-    # Create temporary copy of output data dictionary
282
-    # and remove unnecessary items.
283
-    dTemp = dict(dData)
284
-    dTemp.pop('ELT')
285
-    dTemp.pop('UTC')
286
-
287 280
     # Format the radmon data as string using java script object notation.
288 281
     jsData = json.loads("{}")
289 282
     try:
290
-        for key in dTemp:
291
-            jsData.update({key:dTemp[key]})
292
-        jsData.update({"serverMode":"%s" % _SERVER_MODE })
283
+        for key in dData:
284
+            jsData.update({key:dData[key]})
293 285
         sData = "[%s]" % json.dumps(jsData)
294 286
     except Exception as exError:
295 287
         print("%s writeOutputFile: %s" % (getTimeStamp(), exError))
... ...
@@ -310,6 +302,35 @@ def writeOutputFile(dData):
310 302
     return True
311 303
 ## end def
312 304
 
305
+def setRadmonStatus(updateSuccess):
306
+    """Detect if radiation monitor is offline or not available on
307
+       the network. After a set number of attempts to get data
308
+       from the monitor set a flag that the radmon is offline.
309
+       Parameters:
310
+           updateSuccess - a boolean that is True if data request
311
+                           successful, False otherwise
312
+       Returns: nothing
313
+    """
314
+    global failedUpdateCount, radmonOnline
315
+
316
+    if updateSuccess:
317
+        failedUpdateCount = 0
318
+        # Set status and send a message to the log if the device
319
+        # previously offline and is now online.
320
+        if not radmonOnline:
321
+            print('%s radiation monitor online' % getTimeStamp())
322
+            radmonOnline = True
323
+        return
324
+    elif failedUpdateCount == _MAX_FAILED_DATA_REQUESTS - 1:
325
+        # Max number of failed data requests, so set
326
+        # device status to offline.
327
+        setStatusToOffline()
328
+    ## end if
329
+    failedUpdateCount += 1
330
+##end def
331
+
332
+    ### DATABASE FUNCTIONS ###
333
+
313 334
 def updateDatabase(dData):
314 335
     """
315 336
     Update the rrdtool database by executing an rrdtool system command.
... ...
@@ -343,40 +364,11 @@ def updateDatabase(dData):
343 364
         return False
344 365
 
345 366
     if verboseMode and not debugMode:
346
-        print("update database")
367
+        print("database update successful")
347 368
 
348 369
     return True
349 370
 ##end def
350 371
 
351
-def setRadmonStatus(updateSuccess):
352
-    """Detect if radiation monitor is offline or not available on
353
-       the network. After a set number of attempts to get data
354
-       from the monitor set a flag that the radmon is offline.
355
-       Parameters:
356
-           updateSuccess - a boolean that is True if data request
357
-                           successful, False otherwise
358
-       Returns: nothing
359
-    """
360
-    global failedUpdateCount, radmonOnline
361
-
362
-    if updateSuccess:
363
-        failedUpdateCount = 0
364
-        # Set status and send a message to the log if the radmon was
365
-        # previously offline and is now online.
366
-        if not radmonOnline:
367
-            print('%s radiation monitor online' % getTimeStamp())
368
-            radmonOnline = True
369
-    else:
370
-        # The last attempt failed, so update the failed attempts
371
-        # count.
372
-        failedUpdateCount += 1
373
-
374
-    if failedUpdateCount >= _MAX_FAILED_DATA_REQUESTS:
375
-        # Max number of failed data requests, so set
376
-        # monitor status to offline.
377
-        setStatusToOffline()
378
-##end def
379
-
380 372
 def createGraph(fileName, dataItem, gLabel, gTitle, gStart,
381 373
                 lower, upper, addTrend, autoScale):
382 374
     """Uses rrdtool to create a graph of specified radmon data item.
... ...
@@ -514,6 +506,7 @@ def main():
514 506
     signal.signal(signal.SIGTERM, terminateAgentProcess)
515 507
     signal.signal(signal.SIGINT, terminateAgentProcess)
516 508
 
509
+    print('===================')
517 510
     print('%s starting up radmon agent process' % \
518 511
                   (getTimeStamp()))
519 512