From 5a270755eb539065fe1e626358418a65fde1d1e8 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Fri, 3 Feb 2017 15:44:27 +0000 Subject: dexcom_dumper: cache http remote record This avoids polling the http server every 10 seconds while the dexcom receiver is disconnected. --- dexcom_reader/dexcom_dumper.py | 26 +++++++++++++++++++++----- 1 file 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(): if (str(sys.exc_info()[0]) == ""): print_verbose('Error: could not connect to remote host.') else: + print_verbose('Exception: %s' % str(sys.exc_info()[0])) traceback.print_exc() dexcom_reconnect() sleep_verbose(10) @@ -247,22 +248,37 @@ def parsetime(s): # server restarts, etc.; it should send back the value it got in the # query to the server, so that the server can verify that the updates # are consecutive. -def remote_update(rectype): +REMOTE_HAS = {} +def check_remote_has(rectype): + global REMOTE_HAS + + if REMOTE_HAS.get(rectype, None) is not None: + return REMOTE_HAS[rectype] + requests = import_requests() - url_path = '/' + rectype - resp = requests.get(HOST + url_path + '/1') + resp = requests.get(HOST + '/' + rectype + '/1') resp.raise_for_status() + when = None if len(resp.json()): when = parsetime(resp.json()[0]['system_time']) - #print_verbose("Latest record on server: %s" % when.isoformat()) + print_verbose("Latest %s record on server: %s" % (rectype, when.isoformat())) + + REMOTE_HAS[rectype] = when + return when + +def remote_update(rectype): + global REMOTE_HAS + when = check_remote_has(rectype) (rs, r) = since_and_first(when, rectype) + connected(True) if len(rs): + REMOTE_HAS[rectype] = None print_verbose("Sending %d %s record%s... " % (len(rs), rectype, '' if len(rs) == 1 else 's'), newline=False) - result = POST(url_path, toJSON(rs)) + result = POST('/' + rectype, toJSON(rs)) print_verbose("done. (Result: %s)" % result) return (len(rs) if result else None, r) else: -- cgit v1.2.3