summaryrefslogtreecommitdiff
path: root/dexcom_reader
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2017-01-26 16:16:08 +0000
committerAndrew Cady <d@jerkface.net>2017-01-26 16:16:08 +0000
commitc60a203b350272c08d7bcc0d31bce43e7cd825db (patch)
tree3aa71151a2c1d11539627d8948090b99c2ed2853 /dexcom_reader
parente1bb96f519c0492319167700673313aa1f61595c (diff)
dexcom_dumper: improve JSON serialization format
normal & special EGV_DATA values are now distinguished with a tag
Diffstat (limited to 'dexcom_reader')
-rw-r--r--dexcom_reader/dexcom_dumper.py64
1 files changed, 49 insertions, 15 deletions
diff --git a/dexcom_reader/dexcom_dumper.py b/dexcom_reader/dexcom_dumper.py
index 8a0c0d6..2694b83 100644
--- a/dexcom_reader/dexcom_dumper.py
+++ b/dexcom_reader/dexcom_dumper.py
@@ -129,17 +129,18 @@ def sleep_verbose(n):
129 sleep(n) 129 sleep(n)
130 130
131def POST(path, json_str): 131def POST(path, json_str):
132 resp = None 132 msg = None
133 try: 133 try:
134 import requests 134 import requests
135 resp = requests.post(HOST + path, data=json_str, 135 resp = requests.post(HOST + path, data=json_str,
136 headers={'Content-type': 'application/json'}) 136 headers={'Content-type': 'application/json'})
137 msg = resp.text
137 resp.raise_for_status() 138 resp.raise_for_status()
138 return True 139 return True
139 except: 140 except requests.exceptions.HTTPError as e:
140 print_verbose(sys.exc_info()) 141 print_verbose(e)
141 if resp: 142 if msg:
142 print_verbose(resp.text) 143 print_verbose(msg)
143 return False 144 return False
144 145
145def send_ping(now): 146def send_ping(now):
@@ -149,7 +150,8 @@ def send_ping(now):
149def print_cgm_bg(now, r): 150def print_cgm_bg(now, r):
150 if HOST: 151 if HOST:
151 r.trend_arrow.replace('45_', 'DIAGONAL_', 1) 152 r.trend_arrow.replace('45_', 'DIAGONAL_', 1)
152 POST('/bgevent', toJSON(r)) 153 if not r.is_special:
154 POST('/egv_data', toJSON(r))
153 send_ping(now) 155 send_ping(now)
154 return 156 return
155 elif JSON: 157 elif JSON:
@@ -181,10 +183,35 @@ def poll():
181def since(when, rectype='EGV_DATA'): 183def since(when, rectype='EGV_DATA'):
182 return reversed(list(takewhile(lambda r: r.system_time > when, dr.iter_records(rectype)))) 184 return reversed(list(takewhile(lambda r: r.system_time > when, dr.iter_records(rectype))))
183 185
186def parsetime(s):
187 return datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ")
188
184def test(): 189def test():
185 now = dr.ReadSystemTime() 190 now = dr.ReadSystemTime()
186 POST('/ping', toJSON(now)) 191 POST('/ping', toJSON(now))
187 192
193 import requests
194 rectype = 'EGV_DATA'
195 resp = requests.get(HOST + '/egv_data/1')
196 resp.raise_for_status()
197 rs = None
198 if len(resp.json()):
199 when = parsetime(resp.json()[0]['contents']['system_time'])
200 print when
201 rs = list(since(when, rectype))
202 else:
203 #rs = list(reversed(list(dr.iter_records(rectype))))
204 now = dr.ReadSystemTime()
205 when = now - timedelta(days=5)
206 rs = list(since(when, rectype))
207
208 print len(rs)
209 print POST('/egv_datas', toJSON(rs))
210
211def test0():
212 now = dr.ReadSystemTime()
213 POST('/ping', toJSON(now))
214
188 for t in parseable_record_types(): 215 for t in parseable_record_types():
189 rs = dr.ReadRecords(t, 1) 216 rs = dr.ReadRecords(t, 1)
190 if not rs: 217 if not rs:
@@ -193,30 +220,37 @@ def test():
193 print toJSON([t,r]) 220 print toJSON([t,r])
194 221
195class JSON_Time(json.JSONEncoder): 222class JSON_Time(json.JSONEncoder):
196 def default(self, o): 223 def default(self, o):
197 if isinstance(o, datetime): 224 if isinstance(o, datetime):
198 if o.tzinfo is None: 225 if o.tzinfo is None:
199 from pytz import UTC 226 from pytz import UTC
200 return o.replace(tzinfo=UTC).isoformat() 227 return o.replace(tzinfo=UTC).isoformat()
201 else: 228 else:
202 return o.isoformat() 229 return o.isoformat()
203 230
204 return json.JSONEncoder.default(self, o) 231 return json.JSONEncoder.default(self, o)
205 232
206class JSON_CGM(JSON_Time): 233class JSON_CGM(JSON_Time):
207 def default(self, o): 234 def default(self, o):
208 if isinstance(o, EGVRecord) and o.is_special: 235
236 if isinstance(o, EGVRecord) and o.is_special:
209 op={} 237 op={}
210 for k in o.BASE_FIELDS + ['glucose_special_meaning']: 238 for k in o.BASE_FIELDS + ['glucose_special_meaning']:
211 op[k] = getattr(o, k) 239 op[k] = getattr(o, k)
212 return op 240 return {'tag': 'BGE_Special', 'contents': op}
213 elif isinstance(o, GenericTimestampedRecord): 241
242 elif isinstance(o, GenericTimestampedRecord):
214 op={} 243 op={}
215 for k in o.BASE_FIELDS + o.FIELDS: 244 for k in o.BASE_FIELDS + o.FIELDS:
216 op[k] = getattr(o, k) 245 op[k] = getattr(o, k)
217 return op
218 246
219 return JSON_Time.default(self, o) 247 if isinstance(o, EGVRecord):
248 return {'tag': 'BGE_Normal', 'contents': op}
249 else:
250 return op
251
252 else:
253 return JSON_Time.default(self, o)
220 254
221def toJSON(o): 255def toJSON(o):
222 return json.dumps(o, cls=JSON_CGM) 256 return json.dumps(o, cls=JSON_CGM)