Browse code

minor revisions

Gandolf authored on 06/23/2021 00:43:14
Showing 1 changed files
... ...
@@ -59,7 +59,7 @@ _SERVER_MODE = "primary"
59 59
 
60 60
 # set url of the aredn node
61 61
 
62
-_DEFAULT_AREDN_NODE_URL = "http://localnode.local.mesh/cgi-bin/status"
62
+_DEFAULT_AREDN_NODE_URL = "{your AREDN mesh node url"
63 63
 
64 64
     ### FILE AND FOLDER LOCATIONS ###
65 65
 
... ...
@@ -92,8 +92,8 @@ _CHART_HEIGHT = 150
92 92
    ### GLOBAL VARIABLES ###
93 93
 
94 94
 # turn on or off of verbose debugging information
95
-debugOption = False
96
-verboseDebug = False
95
+verboseMode = False
96
+debugMode = False
97 97
 
98 98
 # The following two items are used for detecting system faults
99 99
 # and aredn node online or offline status.
... ...
@@ -174,19 +174,19 @@ def terminateAgentProcess(signal, frame):
174 174
 
175 175
   ###  PUBLIC METHODS  ###
176 176
 
177
-def getNodeData():
177
+def getNodeData(dData):
178 178
     """Send http request to aredn node.  The response from the
179 179
        node contains the node signal data as unformatted ascii text.
180 180
        Parameters: none
181
-       Returns: a string containing the node signal data if successful,
182
-                or None if not successful
181
+       Returns: True if successful,
182
+                or False if not successful
183 183
     """
184 184
     try:
185 185
         currentTime = time.time()
186 186
 
187 187
         response = urlopen(arednNodeUrl, timeout=_HTTP_REQUEST_TIMEOUT)
188 188
 
189
-        if debugOption:
189
+        if verboseMode:
190 190
             requestTime = time.time() - currentTime
191 191
             print("http request: %.4f seconds" % requestTime)
192 192
 
... ...
@@ -201,16 +201,18 @@ def getNodeData():
201 201
         # the device is down or unavailable over the network.  In
202 202
         # that case return None to the calling function.
203 203
         print("%s getNodeData: %s" % (getTimeStamp(), exError))
204
-        return None
204
+        return False
205 205
     ##end try
206 206
 
207
-    if verboseDebug:
207
+    if debugMode:
208 208
         print(content)
209
-   
210
-    return content
209
+
210
+    dData['content'] = content
211
+
212
+    return True
211 213
 ##end def
212 214
 
213
-def parseDataString(sData, dData):
215
+def parseDataString(dData):
214 216
     """Parse the node signal data JSON string from the aredn node
215 217
        into its component parts.  
216 218
        Parameters:
... ...
@@ -218,7 +220,7 @@ def parseDataString(sData, dData):
218 220
            dData - a dictionary object to contain the parsed data items
219 221
        Returns: True if successful, False otherwise
220 222
     """
221
- 
223
+    sData = dData.pop('content')
222 224
     try:
223 225
         strBeginSearch = '<nobr>Signal/Noise/Ratio</nobr></th>' \
224 226
                          '<td valign=middle><nobr><big><b>'
... ...
@@ -247,39 +249,6 @@ def parseDataString(sData, dData):
247 249
     return True
248 250
 ##end def
249 251
 
250
-def updateDatabase(dData):
251
-    """
252
-    Update the rrdtool database by executing an rrdtool system command.
253
-    Format the command using the data extracted from the aredn node
254
-    response.   
255
-    Parameters: dData - dictionary object containing data items to be
256
-                        written to the rr database file
257
-    Returns: True if successful, False otherwise
258
-    """
259
-    # Format the rrdtool update command.
260
-    strFmt = "rrdtool update %s %s:%s:%s:%s:%s:%s:%s:%s"
261
-    strCmd = strFmt % (_RRD_FILE, dData['time'], dData['signal'], \
262
-             dData['noise'], dData['snr'], '0', \
263
-             '0', '0', '0')
264
-
265
-    if verboseDebug:
266
-        print("%s" % strCmd) # DEBUG
267
-
268
-    # Run the command as a subprocess.
269
-    try:
270
-        subprocess.check_output(strCmd, shell=True,  \
271
-                             stderr=subprocess.STDOUT)
272
-    except subprocess.CalledProcessError as exError:
273
-        print("%s: rrdtool update failed: %s" % \
274
-                    (getTimeStamp(), exError.output))
275
-        return False
276
-
277
-    if debugOption and not verboseDebug:
278
-        print("database updated")
279
-
280
-    return True
281
-##end def
282
-
283 252
 def writeOutputFile(dData):
284 253
     """Write node data items to the output data file, formatted as 
285 254
        a Javascript file.  This file may then be accessed and used by
... ...
@@ -297,8 +266,8 @@ def writeOutputFile(dData):
297 266
                                 time.localtime(dData['time']) )
298 267
 
299 268
     # Format data into a JSON string.
269
+    jsData = json.loads("{}")
300 270
     try:
301
-        jsData = json.loads("{}")
302 271
         jsData.update({"date": lastUpdate})
303 272
         jsData.update({"chartUpdateInterval": chartUpdateInterval})
304 273
         jsData.update({"dataRequestInterval": dataRequestInterval})
... ...
@@ -308,7 +277,7 @@ def writeOutputFile(dData):
308 277
         print("%s writeOutputFile: %s" % (getTimeStamp(), exError))
309 278
         return False
310 279
 
311
-    if verboseDebug:
280
+    if debugMode:
312 281
         print(sData)
313 282
 
314 283
     try:
... ...
@@ -322,6 +291,39 @@ def writeOutputFile(dData):
322 291
     return True
323 292
 ## end def
324 293
 
294
+def updateDatabase(dData):
295
+    """
296
+    Update the rrdtool database by executing an rrdtool system command.
297
+    Format the command using the data extracted from the aredn node
298
+    response.   
299
+    Parameters: dData - dictionary object containing data items to be
300
+                        written to the rr database file
301
+    Returns: True if successful, False otherwise
302
+    """
303
+    # Format the rrdtool update command.
304
+    strFmt = "rrdtool update %s %s:%s:%s:%s:%s:%s:%s:%s"
305
+    strCmd = strFmt % (_RRD_FILE, dData['time'], dData['signal'], \
306
+             dData['noise'], dData['snr'], '0', \
307
+             '0', '0', '0')
308
+
309
+    if debugMode:
310
+        print("%s" % strCmd) # DEBUG
311
+
312
+    # Run the command as a subprocess.
313
+    try:
314
+        subprocess.check_output(strCmd, shell=True,  \
315
+                             stderr=subprocess.STDOUT)
316
+    except subprocess.CalledProcessError as exError:
317
+        print("%s: rrdtool update failed: %s" % \
318
+                    (getTimeStamp(), exError.output))
319
+        return False
320
+
321
+    if verboseMode and not debugMode:
322
+        print("database updated")
323
+
324
+    return True
325
+##end def
326
+
325 327
 def setNodeStatus(updateSuccess):
326 328
     """Detect if aredn node is offline or not available on
327 329
        the network. After a set number of attempts to get data
... ...
@@ -404,7 +406,7 @@ def createGraph(fileName, dataItem, gLabel, gTitle, gStart,
404 406
         strCmd += "CDEF:smoothed=dSeries,%s,TREND LINE2:smoothed#006600 " \
405 407
                   % trendWindow[gStart]
406 408
      
407
-    if verboseDebug:
409
+    if debugMode:
408 410
         print("%s" % strCmd) # DEBUG
409 411
     
410 412
     # Run the formatted rrdtool command as a subprocess.
... ...
@@ -416,7 +418,7 @@ def createGraph(fileName, dataItem, gLabel, gTitle, gStart,
416 418
         print("rrdtool graph failed: %s" % (exError.output))
417 419
         return False
418 420
 
419
-    if debugOption:
421
+    if verboseMode:
420 422
         print("rrdtool graph: %s\n" % result.decode('utf-8'), end='')
421 423
     return True
422 424
 
... ...
@@ -458,7 +460,7 @@ def generateGraphs():
458 460
     createGraph('12m_snr', 'SNR', 'dB', 
459 461
                 'SNR\ -\ Past\ Year', 'end-12months', 0, 0, 2, autoScale)
460 462
 
461
-    if debugOption:
463
+    if verboseMode:
462 464
         #print() # print a blank line to improve readability when in debug mode
463 465
         pass
464 466
 ##end def
... ...
@@ -471,16 +473,16 @@ def getCLarguments():
471 473
           -u sets the url of the aredn nodeing device
472 474
        Returns: nothing
473 475
     """
474
-    global debugOption, verboseDebug, dataRequestInterval, \
476
+    global verboseMode, debugMode, dataRequestInterval, \
475 477
            arednNodeUrl
476 478
 
477 479
     index = 1
478 480
     while index < len(sys.argv):
479
-        if sys.argv[index] == '-d':
480
-            debugOption = True
481
-        elif sys.argv[index] == '-v':
482
-            debugOption = True
483
-            verboseDebug = True
481
+        if sys.argv[index] == '-v':
482
+            verboseMode = True
483
+        elif sys.argv[index] == '-d':
484
+            verboseMode = True
485
+            debugMode = True
484 486
         elif sys.argv[index] == '-p':
485 487
             try:
486 488
                 dataRequestInterval = abs(int(sys.argv[index + 1]))
... ...
@@ -541,18 +543,13 @@ def main():
541 543
         if currentTime - lastDataRequestTime > dataRequestInterval:
542 544
             lastDataRequestTime = currentTime
543 545
             dData = {}
544
-            result = True
545 546
 
546 547
             # Get the data string from the device.
547
-            sData = getNodeData()
548
+            result = getNodeData(dData)
548 549
 
549
-            # If the first http request fails, try one more time.
550
-            if sData == None:
551
-                result = False
552
-            
553 550
             # If successful parse the data.
554 551
             if result:
555
-                result = parseDataString(sData, dData)
552
+                result = parseDataString(dData)
556 553
 
557 554
             # If parse successful, write data output data file.
558 555
             if result:
... ...
@@ -577,12 +574,11 @@ def main():
577 574
         # the next update interval.
578 575
 
579 576
         elapsedTime = time.time() - currentTime
580
-        if debugOption:
577
+        if verboseMode:
581 578
             if result:
582
-                print("%s update successful:" % getTimeStamp(), end='')
579
+                print("update successful: %s sec" % elapsedTime)
583 580
             else:
584
-                print("%s update failed:" % getTimeStamp(), end='')
585
-            print(" %6f seconds\n" % elapsedTime)
581
+                print("update failed: %s sec" % elapsedTime)
586 582
         remainingTime = dataRequestInterval - elapsedTime
587 583
         if remainingTime > 0.0:
588 584
             time.sleep(remainingTime)