mirror of
https://github.com/bckelley/tconnectsync.git
synced 2026-08-24 11:44:12 -05:00
propagate -- but do not use -- time_start and time_end in last_uploaded_entry
This commit is contained in:
@@ -124,6 +124,8 @@ class WS2Api:
|
||||
- "manual"
|
||||
- "previous"
|
||||
- "alarm"
|
||||
|
||||
The start date includes all events on the given day, and stops at 00:00 midnight on the end day.
|
||||
|
||||
{"BasalSuspension":[{"EventDateTime":"/Date(EPOCH_MS-0000)/", "SuspendReason": "reason"}]}
|
||||
"""
|
||||
|
||||
@@ -1,19 +1,33 @@
|
||||
import sys
|
||||
import datetime
|
||||
import requests
|
||||
import hashlib
|
||||
import time
|
||||
import urllib.parse
|
||||
import arrow
|
||||
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from .api.common import ApiException
|
||||
from .parser.nightscout import ENTERED_BY
|
||||
|
||||
# try:
|
||||
# from .secret import NS_URL, NS_SECRET
|
||||
# except Exception:
|
||||
# print('Unable to import Nightscout secrets from secret.py')
|
||||
# sys.exit(1)
|
||||
def format_datetime(date, day_delta=0):
|
||||
d = arrow.get(date)
|
||||
if day_delta:
|
||||
d += datetime.timedelta(days=day_delta)
|
||||
|
||||
return d.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
def time_range(field_name, start_time, end_time):
|
||||
arg = ''
|
||||
if start_time:
|
||||
# $gte on a date means that the entire day is included starting at 00:00
|
||||
arg += '&find[%s][$gte]=%s' % (field_name, format_datetime(start_time))
|
||||
if end_time:
|
||||
# $lte on a date refers to 00:00 midnight on that day, aka anything from
|
||||
# that day is EXCLUDED. so we increment by one day.
|
||||
arg += '&find[%s][$lte]=%s' % (field_name, format_datetime(end_time))
|
||||
return arg
|
||||
|
||||
|
||||
class NightscoutApi:
|
||||
def __init__(self, url, secret):
|
||||
@@ -48,8 +62,10 @@ class NightscoutApi:
|
||||
if r.status_code != 200:
|
||||
raise ApiException(r.status_code, "Nightscout put response: %s" % r.text)
|
||||
|
||||
def last_uploaded_entry(self, eventType):
|
||||
latest = requests.get(urljoin(self.url, 'api/v1/treatments?count=1&find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[eventType]=' + urllib.parse.quote(eventType) + '&ts=' + str(time.time())), headers={
|
||||
def last_uploaded_entry(self, eventType, time_start=None, time_end=None):
|
||||
#dateFilter = time_range('created_at', time_start, time_end)
|
||||
dateFilter = ''
|
||||
latest = requests.get(urljoin(self.url, 'api/v1/treatments?count=1&find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[eventType]=' + urllib.parse.quote(eventType) + dateFilter + '&ts=' + str(time.time())), headers={
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if latest.status_code != 200:
|
||||
|
||||
@@ -86,7 +86,7 @@ def process_time_range(tconnect, nightscout, time_start, time_end, pretend, feat
|
||||
else:
|
||||
logger.debug("No CSV basal data found")
|
||||
|
||||
added += ns_write_basal_events(nightscout, basalEvents, pretend=pretend)
|
||||
added += ns_write_basal_events(nightscout, basalEvents, pretend=pretend, time_start=time_start, time_end=time_end)
|
||||
|
||||
if PUMP_EVENTS in features:
|
||||
pumpEvents = process_ciq_activity_events(ciqTherapyTimelineData)
|
||||
|
||||
@@ -104,9 +104,9 @@ def add_csv_basal_events(basalEvents, data):
|
||||
"""
|
||||
Given processed basal data, adds basal events to Nightscout.
|
||||
"""
|
||||
def ns_write_basal_events(nightscout, basalEvents, pretend=False):
|
||||
def ns_write_basal_events(nightscout, basalEvents, pretend=False, time_start=None, time_end=None):
|
||||
logger.debug("ns_write_basal_events: querying for last uploaded entry")
|
||||
last_upload = nightscout.last_uploaded_entry(BASAL_EVENTTYPE)
|
||||
last_upload = nightscout.last_uploaded_entry(BASAL_EVENTTYPE, time_start=time_start, time_end=time_end)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
|
||||
@@ -57,9 +57,9 @@ def guess_bolus_bg_type(bg, created_at, cgmEvents):
|
||||
"""
|
||||
Given processed bolus data, adds bolus events to Nightscout.
|
||||
"""
|
||||
def ns_write_bolus_events(nightscout, bolusEvents, pretend=False, include_bg=False, reading_events=None):
|
||||
def ns_write_bolus_events(nightscout, bolusEvents, pretend=False, include_bg=False, reading_events=None, time_start=None, time_end=None):
|
||||
logger.debug("ns_write_bolus_events: querying for last uploaded entry")
|
||||
last_upload = nightscout.last_uploaded_entry(BOLUS_EVENTTYPE)
|
||||
last_upload = nightscout.last_uploaded_entry(BOLUS_EVENTTYPE, time_start=time_start, time_end=time_end)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
|
||||
@@ -161,13 +161,13 @@ def ns_write_activity_events(nightscout, activityEvents, pretend=False):
|
||||
ACTIVITY_EVENTTYPE,
|
||||
pretend=pretend)
|
||||
|
||||
def _ns_write_pump_events(nightscout, events, buildNsEventFunc, eventType, pretend=False):
|
||||
def _ns_write_pump_events(nightscout, events, buildNsEventFunc, eventType, pretend=False, time_start=None, time_end=None):
|
||||
if len(events) == 0:
|
||||
logger.debug("No %s events to process" % eventType)
|
||||
return 0
|
||||
|
||||
logger.debug("ns_write_pump_events: querying for last %s" % eventType)
|
||||
last_upload = nightscout.last_uploaded_entry(eventType)
|
||||
last_upload = nightscout.last_uploaded_entry(eventType, time_start=time_start, time_end=time_end)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
|
||||
@@ -20,7 +20,7 @@ class NightscoutApi(tconnectsync.nightscout.NightscoutApi):
|
||||
def put_entry(self, ns_format, entity):
|
||||
self.put_entries[entity].append(ns_format)
|
||||
|
||||
def last_uploaded_entry(self, eventType):
|
||||
def last_uploaded_entry(self, eventType, start_date=None, end_date=None):
|
||||
raise NotImplementedError
|
||||
|
||||
def last_uploaded_activity(self, activityType):
|
||||
|
||||
@@ -32,7 +32,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
def stub_ws2_basalsuspension(self, time_start, time_end):
|
||||
return {"BasalSuspension": []}
|
||||
|
||||
def stub_last_uploaded_entry(self, event_type):
|
||||
def stub_last_uploaded_entry(self, event_type, **kwargs):
|
||||
return None
|
||||
|
||||
def stub_last_uploaded_activity(self, activity_type):
|
||||
@@ -120,7 +120,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
def fake_last_uploaded_entry(event_type, **kwargs):
|
||||
if event_type == "Temp Basal":
|
||||
return {
|
||||
"created_at": "2021-03-16 00:20:21-04:00",
|
||||
@@ -163,7 +163,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
def fake_last_uploaded_entry(event_type, **kwargs):
|
||||
if event_type == "Temp Basal":
|
||||
return {
|
||||
"created_at": "2021-03-16 00:20:21-04:00",
|
||||
@@ -506,7 +506,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
def fake_last_uploaded_entry(event_type, **kwargs):
|
||||
if event_type == "Sleep":
|
||||
return {
|
||||
"created_at": "2021-05-02 14:46:40-04:00",
|
||||
@@ -519,7 +519,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
"duration": 20,
|
||||
"_id": "exercise"
|
||||
}
|
||||
return self.stub_last_uploaded_entry()
|
||||
return self.stub_last_uploaded_entry(**kwargs)
|
||||
|
||||
nightscout.last_uploaded_entry = fake_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
@@ -628,7 +628,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
def fake_last_uploaded_entry(event_type, **kwargs):
|
||||
if event_type == "Site Change":
|
||||
return {
|
||||
"created_at": "2021-12-04 16:18:10-05:00"
|
||||
@@ -637,7 +637,7 @@ class TestProcessTimeRange(unittest.TestCase):
|
||||
return {
|
||||
"created_at": "2021-12-04 16:07:32-05:00"
|
||||
}
|
||||
return self.stub_last_uploaded_entry()
|
||||
return self.stub_last_uploaded_entry(**kwargs)
|
||||
|
||||
nightscout.last_uploaded_entry = fake_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
|
||||
Reference in New Issue
Block a user