refactor: move auto-update code to separate file

This commit is contained in:
James Woglom
2021-03-18 01:04:24 -04:00
parent 8c6719c6a6
commit 7cfd486ac5
2 changed files with 58 additions and 56 deletions
+6 -56
View File
@@ -2,30 +2,23 @@
import sys
import datetime
import json
import hashlib
import requests
import arrow
import argparse
import time
from tconnectsync.api import TConnectApi
from tconnectsync.api.common import ApiException
from tconnectsync.process import process_time_range
from tconnectsync.autoupdate import process_auto_update
try:
from tconnectsync.secret import (
TCONNECT_EMAIL,
TCONNECT_PASSWORD,
PUMP_SERIAL_NUMBER,
TIMEZONE_NAME
TCONNECT_PASSWORD
)
except Exception:
print('Unable to import secret.py')
print('Unable to read secret.py')
sys.exit(1)
def parse_args():
parser = argparse.ArgumentParser(description="Syncs bolus, basal, and IOB data from Tandem Diabetes t:connect to Nightscout.")
parser.add_argument('--pretend', dest='pretend', action='store_const', const=True, default=False, help='Pretend mode: do not upload any data to Nightscout.')
@@ -39,9 +32,6 @@ def parse_args():
def main():
args = parse_args()
if args.pretend:
print("Pretend mode: will not write to Nightscout")
if args.auto_update and (args.start_date or args.end_date):
raise Exception('Auto-update cannot be used with start/end date')
@@ -58,50 +48,10 @@ def main():
tconnect = TConnectApi(TCONNECT_EMAIL, TCONNECT_PASSWORD)
if args.auto_update:
# Read from android api, find exact interval to cut down on API calls
# Refresh API token. If failure, die, have wrapper script re-run.
last_event_index = None
last_event_time = None
time_diffs = []
while True:
last_event = tconnect.android.last_event_uploaded(PUMP_SERIAL_NUMBER)
if not last_event_index or last_event['maxPumpEventIndex'] > last_event_index:
now = time.time()
print('New event index:', last_event['maxPumpEventIndex'], 'last:', last_event_index)
if args.pretend:
print('Would update now')
else:
added = process_time_range(tconnect, time_start, time_end, args.pretend)
print('Added', added, 'items')
if last_event_index:
time_diffs.append(now - last_event_time)
print('Time diffs:', time_diffs)
last_event_index = last_event['maxPumpEventIndex']
last_event_time = now
else:
print('No event index change:', last_event['maxPumpEventIndex'])
if len(time_diffs) > 2:
print('Sleeping 60 seconds after unexpected no index change')
time.sleep(60)
continue
sleep_secs = 60
if len(time_diffs) > 10:
time_diffs = time_diffs[1:]
if len(time_diffs) > 2:
sleep_secs = sum(time_diffs) / len(time_diffs)
# Sleep for a rolling average of time between updates
print('Sleeping for', sleep_secs, 'sec')
time.sleep(sleep_secs)
print("Starting auto-update between", time_start, "and", time_end, "(PRETEND)" if args.pretend else "")
process_auto_update(tconnect, time_start, time_end, args.pretend)
else:
print("Processing data between", time_start, "and", time_end)
print("Processing data between", time_start, "and", time_end, "(PRETEND)" if args.pretend else "")
added = process_time_range(tconnect, time_start, time_end, args.pretend)
print("Added", added, "items")
+52
View File
@@ -0,0 +1,52 @@
import time
from .process import process_time_range
from .secret import PUMP_SERIAL_NUMBER
"""
Performs the auto-update functionality. Runs indefinitely in a loop
until stopped (ctrl+c).
"""
def process_auto_update(tconnect, time_start, time_end, pretend):
# Read from android api, find exact interval to cut down on API calls
# Refresh API token. If failure, die, have wrapper script re-run.
last_event_index = None
last_event_time = None
time_diffs = []
while True:
last_event = tconnect.android.last_event_uploaded(PUMP_SERIAL_NUMBER)
if not last_event_index or last_event['maxPumpEventIndex'] > last_event_index:
now = time.time()
print('New event index:', last_event['maxPumpEventIndex'], 'last:', last_event_index)
if pretend:
print('Would update now')
else:
added = process_time_range(tconnect, time_start, time_end, pretend)
print('Added', added, 'items')
if last_event_index:
time_diffs.append(now - last_event_time)
print('Time diffs:', time_diffs)
last_event_index = last_event['maxPumpEventIndex']
last_event_time = now
else:
print('No event index change:', last_event['maxPumpEventIndex'])
if len(time_diffs) > 2:
print('Sleeping 60 seconds after unexpected no index change')
time.sleep(60)
continue
sleep_secs = 60
if len(time_diffs) > 10:
time_diffs = time_diffs[1:]
if len(time_diffs) > 2:
sleep_secs = sum(time_diffs) / len(time_diffs)
# Sleep for a rolling average of time between updates
print('Sleeping for', sleep_secs, 'sec')
time.sleep(sleep_secs)