Browse code

minor revision

Gandolf authored on 07/06/2021 21:04:25
Showing 4 changed files
... ...
@@ -70,8 +70,9 @@ class ina260:
70 70
 
71 71
         if self.debugMode:
72 72
             data = self.getInfo()
73
+            print(self)
73 74
             print("manufacturer ID: %s %s\n"\
74
-                  "configuration register: %s %s\n" % data)
75
+                  "INA260 configuration register: %s %s\n" % data)
75 76
     ## end def
76 77
 
77 78
     def getInfo(self):
... ...
@@ -31,6 +31,8 @@
31 31
 #   * v10 released 01 June 2021 by J L Owrey; first release
32 32
 #   * v11 released 02 July 2021 by J L Owrey; improved sensor fault
33 33
 #     handling; improved code readability
34
+#   * v12 released 06 July 2021 by J L Owrey; improved debug mode
35
+#     handling; debug mode state now passed to sensor object constructors
34 36
 #
35 37
 #2345678901234567890123456789012345678901234567890123456789012345678901234567890
36 38
 
... ...
@@ -103,11 +105,6 @@ failedUpdateCount = 0
103 105
 # sensor status
104 106
 deviceOnline = False
105 107
 
106
-# Create sensor objects.  This also initialzes each sensor.
107
-power = ina260.ina260(_PWR_SENSOR_ADDR, _BUS_NUMBER)
108
-battemp = tmp102.tmp102(_BAT_TMP_SENSOR_ADDR, _BUS_NUMBER)
109
-ambtemp = tmp102.tmp102(_AMB_TMP_SENSOR_ADDR, _BUS_NUMBER)
110
-
111 108
   ###  PRIVATE METHODS  ###
112 109
 
113 110
 def getTimeStamp():
... ...
@@ -170,22 +167,23 @@ def terminateAgentProcess(signal, frame):
170 167
 
171 168
   ###  PUBLIC METHODS  ###
172 169
 
173
-def getSensorData(dData):
170
+def getSensorData(dSensors, dData):
174 171
     """
175 172
     Poll sensors for data. Store the data in a dictionary object for
176 173
     use by other subroutines.  The dictionary object passed in should
177 174
     an empty dictionary, i.e., dData = { }.
178 175
     Parameters: dData - a dictionary object to contain the sensor data
176
+                dSensors - a dictionary containing sensor objects
179 177
     Returns: True if successful, False otherwise
180 178
     """
181 179
     dData["time"] = getTimeStamp()
182 180
  
183 181
     try:
184
-        dData["current"] = power.getCurrent()
185
-        dData["voltage"] = power.getVoltage()
186
-        dData["power"] = power.getPower()
187
-        dData["battemp"] = battemp.getTempF()
188
-        dData["ambtemp"] = ambtemp.getTempF()
182
+        dData["current"] = dSensors['power'].getCurrent()
183
+        dData["voltage"] = dSensors['power'].getVoltage()
184
+        dData["power"] = dSensors['power'].getPower()
185
+        dData["battemp"] = dSensors['battemp'].getTempF()
186
+        dData["ambtemp"] = dSensors['ambtemp'].getTempF()
189 187
     except Exception as exError:
190 188
         print("%s sensor error: %s" % (getTimeStamp(), exError))
191 189
         return False
... ...
@@ -517,8 +515,18 @@ def main():
517 515
               'use createPowerRrd script to ' \
518 516
               'create rrdtool database\n')
519 517
         exit(1)
520
- 
521
-    ## main loop
518
+
519
+    # Create sensor objects.  This also initializes each sensor.
520
+    dSensors = {}
521
+    dSensors['power'] = ina260.ina260(_PWR_SENSOR_ADDR, _BUS_NUMBER,
522
+                            debug=debugMode)
523
+    dSensors['battemp'] = tmp102.tmp102(_BAT_TMP_SENSOR_ADDR, _BUS_NUMBER,
524
+                            debug=debugMode)
525
+    dSensors['ambtemp'] = tmp102.tmp102(_AMB_TMP_SENSOR_ADDR, _BUS_NUMBER,
526
+                            debug=debugMode)
527
+
528
+    ### MAIN LOOP ###
529
+
522 530
     while True:
523 531
 
524 532
         currentTime = time.time() # get current time in seconds
... ...
@@ -530,7 +538,7 @@ def main():
530 538
             dData = {}
531 539
 
532 540
             # Get the data from the sensors.
533
-            result =getSensorData(dData)
541
+            result =getSensorData(dSensors, dData)
534 542
  
535 543
             # If get data successful, write data to data files.
536 544
             if result:
... ...
@@ -71,6 +71,7 @@ class tmp102:
71 71
         if self.debugMode:
72 72
             # Read the TMP102 configuration register.
73 73
             data = self.getInfo()
74
+            print(self)
74 75
             print("configuration register: %s %s\n" % data)
75 76
     ## end def
76 77
 
... ...
@@ -324,6 +324,7 @@ var chartPeriod = 1;
324 324
 var objBlink;
325 325
 
326 326
 function main() {
327
+    httpRequest.timeout = 3000;
327 328
 
328 329
     if (location.hostname.match(/.local/g) == null) {
329 330
         customSelector.style.visibility = "hidden";
... ...
@@ -338,6 +339,7 @@ function main() {
338 339
             displayOfflineStatus();
339 340
         }
340 341
     };
342
+
341 343
     httpRequest.ontimeout = function(e) {
342 344
         displayOfflineStatus();
343 345
     };
... ...
@@ -350,8 +352,7 @@ function main() {
350 352
 }
351 353
 
352 354
 function getSensorData() {
353
-    httpRequest.open("GET", SENSOR_DATA_URL, true);
354
-    httpRequest.timeout = 3000;
355
+    httpRequest.open("POST", SENSOR_DATA_URL, true);
355 356
     httpRequest.send();
356 357
 }
357 358