mirror of
https://github.com/bckelley/tconnectsync.git
synced 2026-08-24 03:34:12 -05:00
refactor: move auto-update code to separate file
This commit is contained in:
@@ -2,30 +2,23 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
|
||||||
import hashlib
|
|
||||||
import requests
|
|
||||||
import arrow
|
import arrow
|
||||||
import argparse
|
import argparse
|
||||||
import time
|
|
||||||
|
|
||||||
from tconnectsync.api import TConnectApi
|
from tconnectsync.api import TConnectApi
|
||||||
from tconnectsync.api.common import ApiException
|
|
||||||
from tconnectsync.process import process_time_range
|
from tconnectsync.process import process_time_range
|
||||||
|
from tconnectsync.autoupdate import process_auto_update
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tconnectsync.secret import (
|
from tconnectsync.secret import (
|
||||||
TCONNECT_EMAIL,
|
TCONNECT_EMAIL,
|
||||||
TCONNECT_PASSWORD,
|
TCONNECT_PASSWORD
|
||||||
PUMP_SERIAL_NUMBER,
|
|
||||||
TIMEZONE_NAME
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
print('Unable to import secret.py')
|
print('Unable to read secret.py')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
parser = argparse.ArgumentParser(description="Syncs bolus, basal, and IOB data from Tandem Diabetes t:connect to Nightscout.")
|
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.')
|
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():
|
def main():
|
||||||
args = parse_args()
|
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):
|
if args.auto_update and (args.start_date or args.end_date):
|
||||||
raise Exception('Auto-update cannot be used with start/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)
|
tconnect = TConnectApi(TCONNECT_EMAIL, TCONNECT_PASSWORD)
|
||||||
|
|
||||||
if args.auto_update:
|
if args.auto_update:
|
||||||
# Read from android api, find exact interval to cut down on API calls
|
print("Starting auto-update between", time_start, "and", time_end, "(PRETEND)" if args.pretend else "")
|
||||||
# Refresh API token. If failure, die, have wrapper script re-run.
|
process_auto_update(tconnect, time_start, time_end, args.pretend)
|
||||||
|
|
||||||
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)
|
|
||||||
else:
|
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)
|
added = process_time_range(tconnect, time_start, time_end, args.pretend)
|
||||||
print("Added", added, "items")
|
print("Added", added, "items")
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user