From e1bb96f519c0492319167700673313aa1f61595c Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Thu, 26 Jan 2017 14:09:16 +0000 Subject: dexcom_dumper: fix EGVRecord JSON output when .is_special==True --- dexcom_reader/dexcom_dumper.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/dexcom_reader/dexcom_dumper.py b/dexcom_reader/dexcom_dumper.py index c531dfc..8a0c0d6 100644 --- a/dexcom_reader/dexcom_dumper.py +++ b/dexcom_reader/dexcom_dumper.py @@ -2,13 +2,13 @@ import constants import readdata import sys import json -import requests -from pytz import UTC +# import requests # As this takes SEVEN SECONDS, it's delayed until needed from sys import stdout, stderr from datetime import timedelta, datetime from time import sleep from itertools import islice, takewhile -from database_records import GenericTimestampedRecord +from database_records import GenericTimestampedRecord, EGVRecord +# from pytz import UTC # Since the dependency is external, don't import unless needed from optparse import OptionParser @@ -39,6 +39,8 @@ HOST = options.host if command is 'dump_cgm' and options.num_records is None and not options.hours: options.hours = 2 +if options.num_records <= 0: + options.num_records = None def get_dexcom_reader(): if options.g5: @@ -127,15 +129,17 @@ def sleep_verbose(n): sleep(n) def POST(path, json_str): + resp = None try: + import requests resp = requests.post(HOST + path, data=json_str, - headers={'Content-type': - 'application/json'}) - print_verbose(resp.text) + headers={'Content-type': 'application/json'}) resp.raise_for_status() return True except: print_verbose(sys.exc_info()) + if resp: + print_verbose(resp.text) return False def send_ping(now): @@ -192,6 +196,7 @@ class JSON_Time(json.JSONEncoder): def default(self, o): if isinstance(o, datetime): if o.tzinfo is None: + from pytz import UTC return o.replace(tzinfo=UTC).isoformat() else: return o.isoformat() @@ -200,7 +205,12 @@ class JSON_Time(json.JSONEncoder): class JSON_CGM(JSON_Time): def default(self, o): - if isinstance(o, GenericTimestampedRecord): + if isinstance(o, EGVRecord) and o.is_special: + op={} + for k in o.BASE_FIELDS + ['glucose_special_meaning']: + op[k] = getattr(o, k) + return op + elif isinstance(o, GenericTimestampedRecord): op={} for k in o.BASE_FIELDS + o.FIELDS: op[k] = getattr(o, k) -- cgit v1.2.3