diff options
author | Andrew Cady <d@jerkface.net> | 2017-01-25 14:31:09 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2017-01-25 14:31:09 -0500 |
commit | 1f3de244a446b3bde7232d265da30971d1992455 (patch) | |
tree | 54b748d4559a778fbc5fd613cf5897615c51128a | |
parent | afb78e8933997db2d9fc257c1466d04d1f6e93d4 (diff) |
dexcom_dumper: submit polled CGM data via HTTP
Each reading is sent as JSON via an HTTP POST request to the host
specified via '--http' and to the paths '/bgevent' and '/ping'.
Currently, this is blocking and it does not retry after errors.
-rw-r--r-- | dexcom_reader/dexcom_dumper.py | 41 |
1 files changed, 34 insertions, 7 deletions
diff --git a/dexcom_reader/dexcom_dumper.py b/dexcom_reader/dexcom_dumper.py index bba4d17..e279433 100644 --- a/dexcom_reader/dexcom_dumper.py +++ b/dexcom_reader/dexcom_dumper.py | |||
@@ -2,6 +2,7 @@ import constants | |||
2 | import readdata | 2 | import readdata |
3 | import sys | 3 | import sys |
4 | import json | 4 | import json |
5 | import requests | ||
5 | from sys import stdout, stderr | 6 | from sys import stdout, stderr |
6 | from datetime import timedelta, datetime | 7 | from datetime import timedelta, datetime |
7 | from time import sleep | 8 | from time import sleep |
@@ -22,6 +23,7 @@ parser.add_option("-n", type="int", dest="num_records", default=DEFAULT_PAGE_COU | |||
22 | parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="verbosity (currently for debugging)") | 23 | parser.add_option("-v", "--verbose", action="store_true", dest="verbose", help="verbosity (currently for debugging)") |
23 | parser.add_option("-H", "--human", action="store_true", dest="human", help="print human-readable times") | 24 | parser.add_option("-H", "--human", action="store_true", dest="human", help="print human-readable times") |
24 | parser.add_option("-j", "--json", action="store_true", dest="json", help="print JSON output") | 25 | parser.add_option("-j", "--json", action="store_true", dest="json", help="print JSON output") |
26 | parser.add_option("--http", type="string", dest="host", help="submit via HTTP") | ||
25 | 27 | ||
26 | (options, args) = parser.parse_args() | 28 | (options, args) = parser.parse_args() |
27 | 29 | ||
@@ -29,6 +31,7 @@ command = options.command or "dump_cgm" | |||
29 | VERBOSE = options.verbose | 31 | VERBOSE = options.verbose |
30 | HUMAN = options.human | 32 | HUMAN = options.human |
31 | JSON = options.json | 33 | JSON = options.json |
34 | HOST = options.host | ||
32 | 35 | ||
33 | def get_dexcom_reader(): | 36 | def get_dexcom_reader(): |
34 | if options.g5: | 37 | if options.g5: |
@@ -111,15 +114,36 @@ def sleep_verbose(n): | |||
111 | print_verbose('sleep(%d)' % n) | 114 | print_verbose('sleep(%d)' % n) |
112 | sleep(n) | 115 | sleep(n) |
113 | 116 | ||
117 | def POST(path, json_str): | ||
118 | try: | ||
119 | resp = requests.post(HOST + path, data=json_str, | ||
120 | headers={'Content-type': | ||
121 | 'application/json'}) | ||
122 | return True | ||
123 | except: | ||
124 | return False | ||
125 | |||
126 | def strftime_JSON(t): | ||
127 | return t.strftime('%FT%T.0Z'); | ||
128 | |||
129 | def send_ping(now): | ||
130 | if HOST: | ||
131 | POST('/ping', json.dumps(strftime_JSON(now))) | ||
132 | |||
114 | def print_cgm_bg(now, r): | 133 | def print_cgm_bg(now, r): |
115 | if JSON: | 134 | if HOST or JSON: |
116 | print json.dumps({ | 135 | json_str = json.dumps({ |
117 | 'current_time': now.strftime('%s'), | 136 | 'bgeEventTime': strftime_JSON(r.system_time), |
118 | 'event_time': r.system_time.strftime('%s'), | 137 | 'bgeGlucose': r.glucose, |
119 | 'glucose': r.glucose, | 138 | 'bgeTrendArrow': r.trend_arrow.replace('45_', 'DIAGONAL_', 1), |
120 | 'trend_arrow': r.trend_arrow, | 139 | 'bgeDisplayOnly': r.display_only |
121 | 'display_only': r.display_only | ||
122 | }) | 140 | }) |
141 | if HOST: | ||
142 | POST('/bgevent', json_str) | ||
143 | send_ping(now) | ||
144 | return | ||
145 | if JSON: | ||
146 | print json_str | ||
123 | else: | 147 | else: |
124 | print '%s: %s %s' % (format_times(now, r.system_time), r.glucose, r.trend_arrow) | 148 | print '%s: %s %s' % (format_times(now, r.system_time), r.glucose, r.trend_arrow) |
125 | stdout.flush() | 149 | stdout.flush() |
@@ -130,6 +154,9 @@ def poll(): | |||
130 | r = read_recent_egv_data() | 154 | r = read_recent_egv_data() |
131 | if r is None: | 155 | if r is None: |
132 | connected(True) | 156 | connected(True) |
157 | if HOST: | ||
158 | now = dr.ReadSystemTime() | ||
159 | send_ping(now) | ||
133 | sleep_verbose(10) | 160 | sleep_verbose(10) |
134 | elif r is False: | 161 | elif r is False: |
135 | connected(False) | 162 | connected(False) |