From 0df4ae1d1a0cc854e65bca4d992d165134db6604 Mon Sep 17 00:00:00 2001
From: William Roberts <wroberts4@wisc.edu>
Date: Mon, 10 Jun 2019 09:45:57 -0500
Subject: [PATCH] Remove print statements and add exception clause back in

---
 aosstower/level_00/influxdb.py | 127 +++++++++++++++++----------------
 1 file changed, 65 insertions(+), 62 deletions(-)

diff --git a/aosstower/level_00/influxdb.py b/aosstower/level_00/influxdb.py
index 976c932..2350a38 100644
--- a/aosstower/level_00/influxdb.py
+++ b/aosstower/level_00/influxdb.py
@@ -185,68 +185,71 @@ def main():
         src = open(args.src, "r")
         record_gen = read_frames(src, tail=args.tail)
 
-    influx_gen = convert_to_influx_frame(record_gen, symbols, args.debug)
-    influx_gen = influxdb.grouper(influx_gen, args.bulk)
-    updater = Updater()
-    for record in influx_gen:
-        # lines = influxdb.frame_records(record, **station_tags)
-        # influxdb.insert(lines, host=args.host, port=args.port, dbname=args.dbname)
-        # Record is in a list of size 1, but want just the record.
-        avg = updater.rolling_average(record[0])
-        # Once every 5 minutes: 0 through 295 seconds inclusive.
-        if avg is not None:
-            # For timestamp, want YYYY-MM-DD+hh:mm:ss of last dataset that was averaged, rounded up to nearest minute.
-            timestamp = avg.index[-1].round('1T').isoformat('+')
-            wind_dir = avg['wind_dir'][-1]
-            wind_dir_2m = avg['wind_dir_2m'][-1]
-            # Converts from m/s to mph.
-            wind_speed = avg['wind_speed'][-1] * 2.23694
-            wind_speed_2m = avg['wind_speed_2m'][-1] * 2.23694
-            cur_gust = avg['cur_gust'][-1] * 2.23694
-            gust_10m = avg['gust_10m'][-1] * 2.23694
-            rel_hum = avg['rel_hum'][-1]
-            # Converts degrees Celsius to degrees Fahrenheit
-            air_temp = avg['air_temp'][-1] * 9. / 5. + 32.
-            # hpa to barometric pressure inches
-            pressure = avg['pressure'][-1] * 0.02952998016471232
-            # degrees Celcus to degrees Fahrenheit.
-            dewpoint = avg['dewpoint'][-1] * 9. / 5. + 32.
-            solar_flux = avg['solar_flux'][-1]
-            precip = avg['precip'][-1]
-            accum_precip = avg['accum_precip'][-1]
-            url = ('http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?'
-                   'ID={wu_id}&'
-                   'PASSWORD={wu_pw}&'
-                   'action=updateraw&'
-                   'dateutc={timestamp}&'
-                   'winddir={wind_dir}&'
-                   'winddir_avg2m={wind_dir_2m}&'
-                   'windspeedmph={wind_speed}&'
-                   'windspdmph_avg2m={wind_speed_2m}&'
-                   'windgustmph={cur_gust}&'
-                   'windgustmph_10m={gust_10m}&'
-                   'humidity={rel_hum}&'
-                   'tempf={air_temp}&'
-                   'baromin={pressure}&'
-                   'dewptf={dewpoint}&'
-                   'solarradiation={solar_flux}&'
-                   'rainin={precip}&'
-                   'dailyrainin={accum_precip}&'
-                   'softwaretype=SSEC-RIG').format(wu_id=args.wu_id, wu_pw=wu_pw,
-                                                   timestamp=timestamp, wind_dir=wind_dir, wind_dir_2m=wind_dir_2m,
-                                                   wind_speed=wind_speed, wind_speed_2m=wind_speed_2m,
-                                                   cur_gust=cur_gust, gust_10m=gust_10m, rel_hum=rel_hum,
-                                                   air_temp=air_temp, pressure=pressure, dewpoint=dewpoint,
-                                                   solar_flux=solar_flux, precip=precip, accum_precip=accum_precip)
-            print(url)
-            # if wu_pw and args.ldmp:
-            #     # TODO: CHECK FOR SUCCESS RESPONSE PING AFTER SENDING.
-            #     resp = requests.post(url)
-            #     if resp != 'success':
-            #         raise ValueError('Data not received.')
-
-        if args.sleep_interval:
-            time.sleep(args.sleep_interval)
+    try:
+        influx_gen = convert_to_influx_frame(record_gen, symbols, args.debug)
+        influx_gen = influxdb.grouper(influx_gen, args.bulk)
+        updater = Updater()
+        for record in influx_gen:
+            # lines = influxdb.frame_records(record, **station_tags)
+            # influxdb.insert(lines, host=args.host, port=args.port, dbname=args.dbname)
+            # Record is in a list of size 1, but want just the record.
+            avg = updater.rolling_average(record[0])
+            # Once every 5 minutes: 0 through 295 seconds inclusive.
+            if avg is not None:
+                # For timestamp, want YYYY-MM-DD+hh:mm:ss of last dataset that was averaged, rounded up to nearest minute.
+                timestamp = avg.index[-1].round('1T').isoformat('+')
+                wind_dir = avg['wind_dir'][-1]
+                wind_dir_2m = avg['wind_dir_2m'][-1]
+                # Converts from m/s to mph.
+                wind_speed = avg['wind_speed'][-1] * 2.23694
+                wind_speed_2m = avg['wind_speed_2m'][-1] * 2.23694
+                cur_gust = avg['cur_gust'][-1] * 2.23694
+                gust_10m = avg['gust_10m'][-1] * 2.23694
+                rel_hum = avg['rel_hum'][-1]
+                # Converts degrees Celsius to degrees Fahrenheit
+                air_temp = avg['air_temp'][-1] * 9. / 5. + 32.
+                # hpa to barometric pressure inches
+                pressure = avg['pressure'][-1] * 0.02952998016471232
+                # degrees Celcus to degrees Fahrenheit.
+                dewpoint = avg['dewpoint'][-1] * 9. / 5. + 32.
+                solar_flux = avg['solar_flux'][-1]
+                precip = avg['precip'][-1]
+                accum_precip = avg['accum_precip'][-1]
+                url = ('http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?'
+                       'ID={wu_id}&'
+                       'PASSWORD={wu_pw}&'
+                       'action=updateraw&'
+                       'dateutc={timestamp}&'
+                       'winddir={wind_dir}&'
+                       'winddir_avg2m={wind_dir_2m}&'
+                       'windspeedmph={wind_speed}&'
+                       'windspdmph_avg2m={wind_speed_2m}&'
+                       'windgustmph={cur_gust}&'
+                       'windgustmph_10m={gust_10m}&'
+                       'humidity={rel_hum}&'
+                       'tempf={air_temp}&'
+                       'baromin={pressure}&'
+                       'dewptf={dewpoint}&'
+                       'solarradiation={solar_flux}&'
+                       'rainin={precip}&'
+                       'dailyrainin={accum_precip}&'
+                       'softwaretype=SSEC-RIG').format(wu_id=args.wu_id, wu_pw=wu_pw,
+                                                       timestamp=timestamp, wind_dir=wind_dir, wind_dir_2m=wind_dir_2m,
+                                                       wind_speed=wind_speed, wind_speed_2m=wind_speed_2m,
+                                                       cur_gust=cur_gust, gust_10m=gust_10m, rel_hum=rel_hum,
+                                                       air_temp=air_temp, pressure=pressure, dewpoint=dewpoint,
+                                                       solar_flux=solar_flux, precip=precip, accum_precip=accum_precip)
+                if wu_pw and args.ldmp:
+                    # TODO: CHECK FOR SUCCESS RESPONSE PING AFTER SENDING.
+                    resp = requests.post(url)
+                    if resp != 'success':
+                        raise ValueError('Data not received.')
+
+            if args.sleep_interval:
+                time.sleep(args.sleep_interval)
+    except (RuntimeError, ValueError, KeyError, requests.RequestException):
+        if hasattr(record_gen, 'close'):
+            record_gen.close()
 
 
 if __name__ == "__main__":
-- 
GitLab