diff options
author | Andrew Cady <d@jerkface.net> | 2017-02-03 15:44:27 +0000 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2017-02-05 14:46:44 +0000 |
commit | 5a270755eb539065fe1e626358418a65fde1d1e8 (patch) | |
tree | e40cd49409da2f51d86149f28855ed0194a1392d | |
parent | a36c0dea9079a8268b30249fdc5bb1d04ce97110 (diff) |
dexcom_dumper: cache http remote record
This avoids polling the http server every 10 seconds while the dexcom
receiver is disconnected.
-rw-r--r-- | dexcom_reader/dexcom_dumper.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/dexcom_reader/dexcom_dumper.py b/dexcom_reader/dexcom_dumper.py index 3ac21c0..10317e1 100644 --- a/dexcom_reader/dexcom_dumper.py +++ b/dexcom_reader/dexcom_dumper.py | |||
@@ -183,6 +183,7 @@ def poll(): | |||
183 | if (str(sys.exc_info()[0]) == "<class 'requests.exceptions.ConnectionError'>"): | 183 | if (str(sys.exc_info()[0]) == "<class 'requests.exceptions.ConnectionError'>"): |
184 | print_verbose('Error: could not connect to remote host.') | 184 | print_verbose('Error: could not connect to remote host.') |
185 | else: | 185 | else: |
186 | print_verbose('Exception: %s' % str(sys.exc_info()[0])) | ||
186 | traceback.print_exc() | 187 | traceback.print_exc() |
187 | dexcom_reconnect() | 188 | dexcom_reconnect() |
188 | sleep_verbose(10) | 189 | sleep_verbose(10) |
@@ -247,22 +248,37 @@ def parsetime(s): | |||
247 | # server restarts, etc.; it should send back the value it got in the | 248 | # server restarts, etc.; it should send back the value it got in the |
248 | # query to the server, so that the server can verify that the updates | 249 | # query to the server, so that the server can verify that the updates |
249 | # are consecutive. | 250 | # are consecutive. |
250 | def remote_update(rectype): | 251 | REMOTE_HAS = {} |
252 | def check_remote_has(rectype): | ||
253 | global REMOTE_HAS | ||
254 | |||
255 | if REMOTE_HAS.get(rectype, None) is not None: | ||
256 | return REMOTE_HAS[rectype] | ||
257 | |||
251 | requests = import_requests() | 258 | requests = import_requests() |
252 | url_path = '/' + rectype | ||
253 | 259 | ||
254 | resp = requests.get(HOST + url_path + '/1') | 260 | resp = requests.get(HOST + '/' + rectype + '/1') |
255 | resp.raise_for_status() | 261 | resp.raise_for_status() |
262 | |||
256 | when = None | 263 | when = None |
257 | if len(resp.json()): | 264 | if len(resp.json()): |
258 | when = parsetime(resp.json()[0]['system_time']) | 265 | when = parsetime(resp.json()[0]['system_time']) |
259 | #print_verbose("Latest record on server: %s" % when.isoformat()) | 266 | print_verbose("Latest %s record on server: %s" % (rectype, when.isoformat())) |
267 | |||
268 | REMOTE_HAS[rectype] = when | ||
269 | return when | ||
270 | |||
271 | def remote_update(rectype): | ||
272 | global REMOTE_HAS | ||
273 | when = check_remote_has(rectype) | ||
260 | 274 | ||
261 | (rs, r) = since_and_first(when, rectype) | 275 | (rs, r) = since_and_first(when, rectype) |
276 | |||
262 | connected(True) | 277 | connected(True) |
263 | if len(rs): | 278 | if len(rs): |
279 | REMOTE_HAS[rectype] = None | ||
264 | print_verbose("Sending %d %s record%s... " % (len(rs), rectype, '' if len(rs) == 1 else 's'), newline=False) | 280 | print_verbose("Sending %d %s record%s... " % (len(rs), rectype, '' if len(rs) == 1 else 's'), newline=False) |
265 | result = POST(url_path, toJSON(rs)) | 281 | result = POST('/' + rectype, toJSON(rs)) |
266 | print_verbose("done. (Result: %s)" % result) | 282 | print_verbose("done. (Result: %s)" % result) |
267 | return (len(rs) if result else None, r) | 283 | return (len(rs) if result else None, r) |
268 | else: | 284 | else: |