Skip to content

Commit

Permalink
check changes for last seen event id
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoglom committed Oct 1, 2024
1 parent 4af54bf commit ef51502
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 7 additions & 2 deletions tconnectsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ def main(*args, **kwargs):

if args.auto_update:
u = TandemSourceAutoupdate(secret)
sys.exit(u.process(tconnect, nightscout, time_start, time_end, args.pretend, features=args.features))
changed, last_event_id = u.process(tconnect, nightscout, time_start, time_end, args.pretend, features=args.features)

# return exit code 0 if processed events
sys.exit(0 if changed>0 else 1)
else:
tconnectDevice = TandemSourceChooseDevice(secret, tconnect).choose()
added = TandemSourceProcessTimeRange(tconnect, nightscout, tconnectDevice, pretend=args.pretend, features=args.features).process(time_start, time_end)
sys.exit(added)

# return exit code 0 if processed events
sys.exit(0 if added>0 else 1)

8 changes: 6 additions & 2 deletions tconnectsync/sync/tandemsource/autoupdate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, secret):
self.last_max_date_with_events = None
self.last_event_time = 0
self.last_attempt_time = 0
self.last_event_index = None
self.time_diffs_between_attempts = []
self.time_diffs_between_updates = []

Expand All @@ -41,14 +42,15 @@ def process(self, tconnect, nightscout, time_start, time_end, pretend, features=

tconnectDevice = ChooseDevice(self.secret, tconnect).choose()

event_id = None
cur_max_date_with_events = arrow.get(tconnectDevice['maxDateWithEvents'])
if not self.last_max_date_with_events or cur_max_date_with_events > self.cur_max_date_with_events:
logger.info('New reported tandemsource data. (cur_max_date: %s last_max_date: %s)' % (cur_max_date_with_events, self.last_max_date_with_events))

if pretend:
logger.info('Would update now if not in pretend mode')
else:
added = ProcessTimeRange(tconnect, nightscout, tconnectDevice, pretend, features=features).process(time_start, time_end)
added, event_id = ProcessTimeRange(tconnect, nightscout, tconnectDevice, pretend, features=features).process(time_start, time_end)
logger.info('Added %d items from ProcessTimeRange' % added)

# Track the time it took to find a new event between runs,
Expand All @@ -59,8 +61,10 @@ def process(self, tconnect, nightscout, time_start, time_end, pretend, features=
logger.debug('Updating tracking of time since last update: %s' % self.time_diffs_between_updates)

# Mark the last event index uploaded from the pump and timestamp
if event_id:
self.last_event_index = event_id
self.last_event_time = now
self.last_max_date_with_events = cur_max_date_with_events
self.last_event_time = now
self.last_attempt_time = now
self.time_diffs_between_attempts = []
else:
Expand Down
8 changes: 6 additions & 2 deletions tconnectsync/sync/tandemsource/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ def process(self, time_start, time_end):

events_first_time = None
events_last_time = None
last_event_id = None # aka seqnum
for_eventclass = collections.defaultdict(list)
for event in events:
if not events_first_time:
events_first_time = event.eventTimestamp
if not events_last_time:
events_last_time = event.eventTimestamp
if not last_event_id:
last_event_id = event.eventId
events_first_time = min(events_first_time, event.eventTimestamp)
events_last_time = max(events_last_time, event.eventTimestamp)
last_event_id = max(event.eventId, last_event_id)

clazz = EventClass.for_event(event)
if clazz:
Expand Down Expand Up @@ -91,6 +95,6 @@ def process(self, time_start, time_end):
else:
logger.info("Skipping %s, is not enabled from features %s" % (updater_class.__name__, self.features))

logger.info("Processed %d events" % processed_count)
return processed_count
logger.info("Processed %d events. Last event ID seen: %d" % (processed_count, last_event_id))
return processed_count, last_event_id

0 comments on commit ef51502

Please sign in to comment.