use float duration, replace most recent entry if it has a longer duration than before

This commit is contained in:
James Woglom
2020-09-29 01:21:03 -04:00
parent 33edcc9d44
commit 5cc3f8574b
2 changed files with 24 additions and 3 deletions
+15 -2
View File
@@ -14,6 +14,7 @@ from nightscout import (
NightscoutEntry,
upload_nightscout,
delete_nightscout,
put_nightscout,
last_uploaded_nightscout_entry,
last_uploaded_nightscout_activity,
BASAL_EVENTTYPE,
@@ -85,19 +86,31 @@ def ns_write_basal_events(basalEvents, pretend=False):
print("Last Nightscout basal upload:", last_upload_time)
for event in basalEvents:
if last_upload_time and arrow.get(event["time"]) <= last_upload_time:
if last_upload_time and arrow.get(event["time"]) < last_upload_time:
if pretend:
print("Skipping basal event before last upload time:", event)
continue
recent_needs_update = False
if last_upload_time and arrow.get(event["time"]) == last_upload_time:
# If this entry has the same time as the most recent upload, but
# has newer info, then delete and recreate it.
recent_needs_update = (round(last_upload["duration"]) < round(event["duration_mins"]))
entry = NightscoutEntry.basal(
value=event["basal_rate"],
duration_mins=event["duration_mins"],
created_at=event["time"],
reason=event["delivery_type"]
)
print(" Processing basal:", event, "entry:", entry)
if not pretend:
if recent_needs_update:
print("Replacing last uploaded entry:", last_upload)
if not pretend:
entry['_id'] = last_upload['_id']
put_nightscout(entry, entity='treatments')
elif not pretend:
upload_nightscout(entry)
"""
+9 -1
View File
@@ -21,7 +21,7 @@ class NightscoutEntry:
return {
"eventType": BASAL_EVENTTYPE,
"reason": reason,
"duration": int(round(duration_mins)) if duration_mins else None,
"duration": float(duration_mins) if duration_mins else None,
"absolute": float(value),
"created_at": created_at,
"carbs": None,
@@ -65,6 +65,14 @@ def delete_nightscout(entity):
})
print("Nightscout delete status:", upload.status_code, upload.text)
def put_nightscout(ns_format, entity):
upload = requests.put(NS_URL + 'api/v1/' + entity + '?api_secret=' + NS_SECRET, json=ns_format, headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest()
})
print("Nightscout put status:", upload.status_code, upload.text)
def last_uploaded_nightscout_entry(eventType):
latest = requests.get(NS_URL + 'api/v1/treatments?count=1&find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[eventType]=' + urllib.parse.quote(eventType) + '&ts=' + str(time.time()), headers={
'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest()