mirror of
https://github.com/bckelley/tconnectsync.git
synced 2026-08-28 12:11:50 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71ceb94270 | ||
|
|
a3c7aa69a9 | ||
|
|
093a7f0627 | ||
|
|
e1921ccac7 | ||
|
|
52f7972153 | ||
|
|
2130099f70 | ||
|
|
4c6e05b2c1 | ||
|
|
cda01894dc | ||
|
|
7a18a5d4be | ||
|
|
4b8ddd0cec | ||
|
|
1efe532a2f | ||
|
|
8dcbee4280 | ||
|
|
c7aac1dca3 | ||
|
|
739c01f876 | ||
|
|
8ce8b42bfc | ||
|
|
c14298384a | ||
|
|
84067015e3 | ||
|
|
0b7e806a40 | ||
|
|
89b40dcecb | ||
|
|
539516aeae | ||
|
|
332f54e85e | ||
|
|
19f4f9363a | ||
|
|
d804510633 | ||
|
|
713d2ee79e | ||
|
|
e0bfab8759 | ||
|
|
209292dd21 | ||
|
|
1a9fb36b88 | ||
|
|
2e0120fdfc | ||
|
|
3911da7d37 | ||
|
|
7cfd486ac5 | ||
|
|
8c6719c6a6 | ||
|
|
d00d8e59dd | ||
|
|
63ca6eda43 |
@@ -0,0 +1,42 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Python package
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master, develop ]
|
||||
pull_request:
|
||||
branches: [ master, develop ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
python-version: [3.6, 3.7, 3.8, 3.9]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install flake8 pytest pipenv
|
||||
pipenv install --system
|
||||
- name: Run pipenv check
|
||||
run: |
|
||||
pipenv check
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
# stop the build if there are Python syntax errors or undefined names
|
||||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||||
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
|
||||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
pytest
|
||||
@@ -13,4 +13,5 @@ lxml = "*"
|
||||
python-dotenv = "*"
|
||||
|
||||
[scripts]
|
||||
tconnectsync = "python3 main.py"
|
||||
tconnectsync = "python3 main.py"
|
||||
test = "python3 -m unittest discover -vv"
|
||||
Generated
+46
-47
@@ -62,46 +62,45 @@
|
||||
},
|
||||
"lxml": {
|
||||
"hashes": [
|
||||
"sha256:0448576c148c129594d890265b1a83b9cd76fd1f0a6a04620753d9a6bcfd0a4d",
|
||||
"sha256:127f76864468d6630e1b453d3ffbbd04b024c674f55cf0a30dc2595137892d37",
|
||||
"sha256:1471cee35eba321827d7d53d104e7b8c593ea3ad376aa2df89533ce8e1b24a01",
|
||||
"sha256:2363c35637d2d9d6f26f60a208819e7eafc4305ce39dc1d5005eccc4593331c2",
|
||||
"sha256:2e5cc908fe43fe1aa299e58046ad66981131a66aea3129aac7770c37f590a644",
|
||||
"sha256:2e6fd1b8acd005bd71e6c94f30c055594bbd0aa02ef51a22bbfa961ab63b2d75",
|
||||
"sha256:366cb750140f221523fa062d641393092813b81e15d0e25d9f7c6025f910ee80",
|
||||
"sha256:42ebca24ba2a21065fb546f3e6bd0c58c3fe9ac298f3a320147029a4850f51a2",
|
||||
"sha256:4e751e77006da34643ab782e4a5cc21ea7b755551db202bc4d3a423b307db780",
|
||||
"sha256:4fb85c447e288df535b17ebdebf0ec1cf3a3f1a8eba7e79169f4f37af43c6b98",
|
||||
"sha256:50c348995b47b5a4e330362cf39fc503b4a43b14a91c34c83b955e1805c8e308",
|
||||
"sha256:535332fe9d00c3cd455bd3dd7d4bacab86e2d564bdf7606079160fa6251caacf",
|
||||
"sha256:535f067002b0fd1a4e5296a8f1bf88193080ff992a195e66964ef2a6cfec5388",
|
||||
"sha256:5be4a2e212bb6aa045e37f7d48e3e1e4b6fd259882ed5a00786f82e8c37ce77d",
|
||||
"sha256:60a20bfc3bd234d54d49c388950195d23a5583d4108e1a1d47c9eef8d8c042b3",
|
||||
"sha256:648914abafe67f11be7d93c1a546068f8eff3c5fa938e1f94509e4a5d682b2d8",
|
||||
"sha256:681d75e1a38a69f1e64ab82fe4b1ed3fd758717bed735fb9aeaa124143f051af",
|
||||
"sha256:68a5d77e440df94011214b7db907ec8f19e439507a70c958f750c18d88f995d2",
|
||||
"sha256:69a63f83e88138ab7642d8f61418cf3180a4d8cd13995df87725cb8b893e950e",
|
||||
"sha256:6e4183800f16f3679076dfa8abf2db3083919d7e30764a069fb66b2b9eff9939",
|
||||
"sha256:6fd8d5903c2e53f49e99359b063df27fdf7acb89a52b6a12494208bf61345a03",
|
||||
"sha256:791394449e98243839fa822a637177dd42a95f4883ad3dec2a0ce6ac99fb0a9d",
|
||||
"sha256:7a7669ff50f41225ca5d6ee0a1ec8413f3a0d8aa2b109f86d540887b7ec0d72a",
|
||||
"sha256:7e9eac1e526386df7c70ef253b792a0a12dd86d833b1d329e038c7a235dfceb5",
|
||||
"sha256:7ee8af0b9f7de635c61cdd5b8534b76c52cd03536f29f51151b377f76e214a1a",
|
||||
"sha256:8246f30ca34dc712ab07e51dc34fea883c00b7ccb0e614651e49da2c49a30711",
|
||||
"sha256:8c88b599e226994ad4db29d93bc149aa1aff3dc3a4355dd5757569ba78632bdf",
|
||||
"sha256:923963e989ffbceaa210ac37afc9b906acebe945d2723e9679b643513837b089",
|
||||
"sha256:94d55bd03d8671686e3f012577d9caa5421a07286dd351dfef64791cf7c6c505",
|
||||
"sha256:97db258793d193c7b62d4e2586c6ed98d51086e93f9a3af2b2034af01450a74b",
|
||||
"sha256:a9d6bc8642e2c67db33f1247a77c53476f3a166e09067c0474facb045756087f",
|
||||
"sha256:cd11c7e8d21af997ee8079037fff88f16fda188a9776eb4b81c7e4c9c0a7d7fc",
|
||||
"sha256:d8d3d4713f0c28bdc6c806a278d998546e8efc3498949e3ace6e117462ac0a5e",
|
||||
"sha256:e0bfe9bb028974a481410432dbe1b182e8191d5d40382e5b8ff39cdd2e5c5931",
|
||||
"sha256:f4822c0660c3754f1a41a655e37cb4dbbc9be3d35b125a37fab6f82d47674ebc",
|
||||
"sha256:f83d281bb2a6217cd806f4cf0ddded436790e66f393e124dfe9731f6b3fb9afe",
|
||||
"sha256:fc37870d6716b137e80d19241d0e2cff7a7643b925dfa49b4c8ebd1295eb506e"
|
||||
"sha256:079f3ae844f38982d156efce585bc540c16a926d4436712cf4baee0cce487a3d",
|
||||
"sha256:0fbcf5565ac01dff87cbfc0ff323515c823081c5777a9fc7703ff58388c258c3",
|
||||
"sha256:122fba10466c7bd4178b07dba427aa516286b846b2cbd6f6169141917283aae2",
|
||||
"sha256:1b7584d421d254ab86d4f0b13ec662a9014397678a7c4265a02a6d7c2b18a75f",
|
||||
"sha256:26e761ab5b07adf5f555ee82fb4bfc35bf93750499c6c7614bd64d12aaa67927",
|
||||
"sha256:289e9ca1a9287f08daaf796d96e06cb2bc2958891d7911ac7cae1c5f9e1e0ee3",
|
||||
"sha256:2a9d50e69aac3ebee695424f7dbd7b8c6d6eb7de2a2eb6b0f6c7db6aa41e02b7",
|
||||
"sha256:33bb934a044cf32157c12bfcfbb6649807da20aa92c062ef51903415c704704f",
|
||||
"sha256:3439c71103ef0e904ea0a1901611863e51f50b5cd5e8654a151740fde5e1cade",
|
||||
"sha256:39b78571b3b30645ac77b95f7c69d1bffc4cf8c3b157c435a34da72e78c82468",
|
||||
"sha256:4289728b5e2000a4ad4ab8da6e1db2e093c63c08bdc0414799ee776a3f78da4b",
|
||||
"sha256:4bff24dfeea62f2e56f5bab929b4428ae6caba2d1eea0c2d6eb618e30a71e6d4",
|
||||
"sha256:542d454665a3e277f76954418124d67516c5f88e51a900365ed54a9806122b83",
|
||||
"sha256:5a0a14e264069c03e46f926be0d8919f4105c1623d620e7ec0e612a2e9bf1c04",
|
||||
"sha256:66e575c62792c3f9ca47cb8b6fab9e35bab91360c783d1606f758761810c9791",
|
||||
"sha256:74f7d8d439b18fa4c385f3f5dfd11144bb87c1da034a466c5b5577d23a1d9b51",
|
||||
"sha256:7610b8c31688f0b1be0ef882889817939490a36d0ee880ea562a4e1399c447a1",
|
||||
"sha256:76fa7b1362d19f8fbd3e75fe2fb7c79359b0af8747e6f7141c338f0bee2f871a",
|
||||
"sha256:7728e05c35412ba36d3e9795ae8995e3c86958179c9770e65558ec3fdfd3724f",
|
||||
"sha256:8157dadbb09a34a6bd95a50690595e1fa0af1a99445e2744110e3dca7831c4ee",
|
||||
"sha256:820628b7b3135403540202e60551e741f9b6d3304371712521be939470b454ec",
|
||||
"sha256:884ab9b29feaca361f7f88d811b1eea9bfca36cf3da27768d28ad45c3ee6f969",
|
||||
"sha256:89b8b22a5ff72d89d48d0e62abb14340d9e99fd637d046c27b8b257a01ffbe28",
|
||||
"sha256:92e821e43ad382332eade6812e298dc9701c75fe289f2a2d39c7960b43d1e92a",
|
||||
"sha256:b007cbb845b28db4fb8b6a5cdcbf65bacb16a8bd328b53cbc0698688a68e1caa",
|
||||
"sha256:bc4313cbeb0e7a416a488d72f9680fffffc645f8a838bd2193809881c67dd106",
|
||||
"sha256:bccbfc27563652de7dc9bdc595cb25e90b59c5f8e23e806ed0fd623755b6565d",
|
||||
"sha256:c4f05c5a7c49d2fb70223d0d5bcfbe474cf928310ac9fa6a7c6dddc831d0b1d4",
|
||||
"sha256:ce256aaa50f6cc9a649c51be3cd4ff142d67295bfc4f490c9134d0f9f6d58ef0",
|
||||
"sha256:d2e35d7bf1c1ac8c538f88d26b396e73dd81440d59c1ef8522e1ea77b345ede4",
|
||||
"sha256:df7c53783a46febb0e70f6b05df2ba104610f2fb0d27023409734a3ecbb78fb2",
|
||||
"sha256:efac139c3f0bf4f0939f9375af4b02c5ad83a622de52d6dfa8e438e8e01d0eb0",
|
||||
"sha256:efd7a09678fd8b53117f6bae4fa3825e0a22b03ef0a932e070c0bdbb3a35e654",
|
||||
"sha256:f2380a6376dfa090227b663f9678150ef27543483055cc327555fb592c5967e2",
|
||||
"sha256:f8380c03e45cf09f8557bdaa41e1fa7c81f3ae22828e1db470ab2a6c96d8bc23",
|
||||
"sha256:f90ba11136bfdd25cae3951af8da2e95121c9b9b93727b1b896e3fa105b2f586"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==4.6.2"
|
||||
"version": "==4.6.3"
|
||||
},
|
||||
"python-dateutil": {
|
||||
"hashes": [
|
||||
@@ -113,11 +112,11 @@
|
||||
},
|
||||
"python-dotenv": {
|
||||
"hashes": [
|
||||
"sha256:0c8d1b80d1a1e91717ea7d526178e3882732420b03f08afea0406db6402e220e",
|
||||
"sha256:587825ed60b1711daea4832cf37524dfd404325b7db5e25ebe88c495c9f807a0"
|
||||
"sha256:471b782da0af10da1a80341e8438fca5fadeba2881c54360d5fd8d03d03a4f4a",
|
||||
"sha256:49782a97c9d641e8a09ae1d9af0856cc587c8d2474919342d5104d85be9890b2"
|
||||
],
|
||||
"index": "pypi",
|
||||
"version": "==0.15.0"
|
||||
"version": "==0.17.0"
|
||||
},
|
||||
"requests": {
|
||||
"hashes": [
|
||||
@@ -137,19 +136,19 @@
|
||||
},
|
||||
"soupsieve": {
|
||||
"hashes": [
|
||||
"sha256:407fa1e8eb3458d1b5614df51d9651a1180ea5fedf07feb46e45d7e25e6d6cdd",
|
||||
"sha256:d3a5ea5b350423f47d07639f74475afedad48cf41c0ad7a82ca13a3928af34f6"
|
||||
"sha256:052774848f448cf19c7e959adf5566904d525f33a3f8b6ba6f6f8f26ec7de0cc",
|
||||
"sha256:c2c1c2d44f158cdbddab7824a9af8c4f83c76b1e23e049479aa432feb6c4c23b"
|
||||
],
|
||||
"markers": "python_version >= '3.0'",
|
||||
"version": "==2.2"
|
||||
"version": "==2.2.1"
|
||||
},
|
||||
"urllib3": {
|
||||
"hashes": [
|
||||
"sha256:1b465e494e3e0d8939b50680403e3aedaa2bc434b7d5af64dfd3c958d7f5ae80",
|
||||
"sha256:de3eedaad74a2683334e282005cd8d7f22f4d55fa690a2a1020a416cb0a47e73"
|
||||
"sha256:2f4da4594db7e1e110a944bb1b551fdf4e6c136ad42e4234131391e21eb5b0df",
|
||||
"sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937"
|
||||
],
|
||||
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
|
||||
"version": "==1.26.3"
|
||||
"version": "==1.26.4"
|
||||
}
|
||||
},
|
||||
"develop": {}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# tconnectsync
|
||||
|
||||

|
||||
|
||||
Tconnectsync synchronizes data one-way from the Tandem Diabetes t:connect web/mobile application to Nightscout.
|
||||
|
||||
If you have a t:slim X2 pump with the companion t:connect mobile Android or iOS app, this will allow your pump bolus and basal data to be uploaded to [Nightscout](https://github.com/nightscout/cgm-remote-monitor) automatically. The t:connect Android app, by default, uploads pump data to Tandem's servers every hour, [but using this tool you can update the frequency to as low as every five minutes](https://github.com/jwoglom/tconnectpatcher)! This allows for nearly real-time (but not instantaneous) pump data updates, almost like your pump uploads data directly to Nightscout!
|
||||
@@ -12,13 +14,13 @@ At a high level, tconnectsync works by querying Tandem's undocumented APIs to re
|
||||
|
||||
This application utilizes three separate Tandem APIs for obtaining t:connect data, referenced here by the identifying part of their URLs:
|
||||
|
||||
* [**controliq**](https://github.com/jwoglom/tconnectsync/blob/master/api/controliq.py) - Contains Control:IQ related data, namely a timeline of all Basal events uploaded by the pump, separated by type (temp basals, algorithmically-updated basals, or profile-updated basals).
|
||||
* [**android**](https://github.com/jwoglom/tconnectsync/blob/master/api/android.py) - Used internally by the t:connect Android app, these API endpoints were discovered by reverse-engineering the Android app. Most of the API endpoints are used for uploading pump data, and tconnectsync uses one endpoint which returns the most recent event ID uploaded by the pump, so we know when more data has been uploaded.
|
||||
* [**tconnectws2**](https://github.com/jwoglom/tconnectsync/blob/master/api/ws2.py) - More legacy than the others, this seems to power the bulk of the main t:connect website. It is used to retrieve a CSV export of non-ControlIQ basal data, as well as bolus and IOB data. (I haven't found any mentions of bolus or IOB data in the Control:IQ-specific API.)
|
||||
* [**controliq**](https://github.com/jwoglom/tconnectsync/blob/master/tconnectsync/api/controliq.py) - Contains Control:IQ related data, namely a timeline of all Basal events uploaded by the pump, separated by type (temp basals, algorithmically-updated basals, or profile-updated basals).
|
||||
* [**android**](https://github.com/jwoglom/tconnectsync/blob/master/tconnectsync/api/android.py) - Used internally by the t:connect Android app, these API endpoints were discovered by reverse-engineering the Android app. Most of the API endpoints are used for uploading pump data, and tconnectsync uses one endpoint which returns the most recent event ID uploaded by the pump, so we know when more data has been uploaded.
|
||||
* [**tconnectws2**](https://github.com/jwoglom/tconnectsync/blob/master/tconnectsync/api/ws2.py) - More legacy than the others, this seems to power the bulk of the main t:connect website. It is used to retrieve a CSV export of non-ControlIQ basal data, as well as bolus and IOB data. (I haven't found any mentions of bolus or IOB data in the Control:IQ-specific API.)
|
||||
|
||||
## Setup
|
||||
|
||||
Create a file named `.env` containing configuration values. You should specify the following parameters:
|
||||
Create a file named `.env` containing configuration values inside the checked-out tconnectsync folder (the same folder as `main.py`). You should specify the following parameters:
|
||||
|
||||
```bash
|
||||
# Your credentials for t:connect
|
||||
@@ -36,9 +38,11 @@ NS_SECRET='apisecret'
|
||||
TIMEZONE_NAME='America/New_York'
|
||||
```
|
||||
|
||||
This file contains your t:connect username and password, Tandem pump serial number (which is utilized in API calls to t:connect), your Nightscout URL and secret token (for uploading data to Nightscout), and local timezone (the timezone used in t:connect).
|
||||
These values can alternatively be specified via environment variables.
|
||||
|
||||
I have only tested tconnectsync with a Tandem pump set in the US Eastern timezone. Tandem's (to us, undocumented) APIs are [a bit loose with timezones](https://github.com/jwoglom/tconnectsync/blob/d841c3811aeff3671d941a7d3ff4b80cce6a219e/parser.py#L16), so please let me know if you notice any timezone-related bugs.
|
||||
The .env file contains your t:connect username and password, Tandem pump serial number (which is utilized in API calls to t:connect), your Nightscout URL and secret token (for uploading data to Nightscout), and local timezone (the timezone used in t:connect).
|
||||
|
||||
I have only tested tconnectsync with a Tandem pump set in the US Eastern timezone. Tandem's (to us, undocumented) APIs are [a bit loose with timezones](https://github.com/jwoglom/tconnectsync/blob/master/tconnectsync/parser.py#L15), so please let me know if you notice any timezone-related bugs.
|
||||
|
||||
When you run the program with no arguments, it performs a single cycle of the following, and exits after completion:
|
||||
|
||||
@@ -59,7 +63,7 @@ If run with the `--auto-update` flag, then the application performs the followin
|
||||
You can run the application using Pipenv. Assuming you have only Python 3 and pip installed, install pipenv with `pip3 install pipenv`. Then install tconnectsync's dependencies with `pipenv install`, and you can launch the program with `pipenv run tconnectsync` (which, through an alias defined in `Pipfile`, runs ``pipenv run python3 main.py`).
|
||||
|
||||
```bash
|
||||
$ git clone https://github.com/jwoglom/tconnectsync
|
||||
$ git clone https://github.com/jwoglom/tconnectsync && cd tconnectsync
|
||||
$ pip3 install pipenv
|
||||
$ pipenv install
|
||||
$ pipenv run tconnectsync --help
|
||||
@@ -92,7 +96,7 @@ To download and run the `jwoglom/tconnectsync` prebuilt Docker image from [Docke
|
||||
|
||||
```bash
|
||||
$ docker pull jwoglom/tconnectsync:latest
|
||||
$ docker run tconnectsync --help
|
||||
$ docker run jwoglom/tconnectsync --help
|
||||
```
|
||||
|
||||
To instead build the image locally and launch the project:
|
||||
|
||||
@@ -2,275 +2,26 @@
|
||||
|
||||
import sys
|
||||
import datetime
|
||||
import json
|
||||
import hashlib
|
||||
import requests
|
||||
import arrow
|
||||
import argparse
|
||||
import time
|
||||
|
||||
from api import TConnectApi
|
||||
from api.common import ApiException
|
||||
from parser import TConnectEntry
|
||||
from nightscout import (
|
||||
NightscoutEntry,
|
||||
upload_nightscout,
|
||||
delete_nightscout,
|
||||
put_nightscout,
|
||||
last_uploaded_nightscout_entry,
|
||||
last_uploaded_nightscout_activity,
|
||||
BASAL_EVENTTYPE,
|
||||
BOLUS_EVENTTYPE,
|
||||
IOB_ACTIVITYTYPE
|
||||
)
|
||||
from tconnectsync.api import TConnectApi
|
||||
from tconnectsync.process import process_time_range
|
||||
from tconnectsync.autoupdate import process_auto_update
|
||||
from tconnectsync.check import check_login
|
||||
from tconnectsync.nightscout import NightscoutApi
|
||||
|
||||
try:
|
||||
from secret import (
|
||||
from tconnectsync.secret import (
|
||||
TCONNECT_EMAIL,
|
||||
TCONNECT_PASSWORD,
|
||||
PUMP_SERIAL_NUMBER,
|
||||
TIMEZONE_NAME
|
||||
NS_URL,
|
||||
NS_SECRET
|
||||
)
|
||||
except Exception:
|
||||
print('Unable to import secret.py')
|
||||
print('Unable to read secret.py')
|
||||
sys.exit(1)
|
||||
|
||||
"""
|
||||
Merges together input from the therapy timeline API into a digestable format of basal data.
|
||||
"""
|
||||
def process_ciq_basal_events(data):
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
suspensionEvents = {}
|
||||
for s in data["suspensionDeliveryEvents"]:
|
||||
entry = TConnectEntry.parse_suspension_entry(s)
|
||||
suspensionEvents[entry["time"]] = entry
|
||||
|
||||
basalEvents = []
|
||||
for b in data["basal"]["tempDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="tempDelivery"))
|
||||
|
||||
for b in data["basal"]["algorithmDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="algorithmDelivery"))
|
||||
|
||||
for b in data["basal"]["profileDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="profileDelivery"))
|
||||
|
||||
basalEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
|
||||
for i in basalEvents:
|
||||
if i["time"] in suspensionEvents:
|
||||
i["suspendReason"] = suspensionEvents[i["time"]]["suspendReason"]
|
||||
|
||||
return basalEvents
|
||||
|
||||
"""
|
||||
Processes basal data input from the therapy timeline CSV (which only exists for pre Control-IQ data) into a digestable format.
|
||||
"""
|
||||
def add_csv_basal_events(basalEvents, data):
|
||||
last_entry = None
|
||||
for row in data:
|
||||
entry = TConnectEntry.parse_csv_basal_entry(row)
|
||||
if last_entry:
|
||||
diff_mins = (arrow.get(entry["time"]) - arrow.get(last_entry["time"])).seconds // 60
|
||||
entry["duration_mins"] = diff_mins
|
||||
|
||||
basalEvents.append(entry)
|
||||
last_entry = entry
|
||||
|
||||
basalEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
return basalEvents
|
||||
|
||||
"""
|
||||
Given processed basal data, adds basal events to Nightscout.
|
||||
"""
|
||||
def ns_write_basal_events(basalEvents, pretend=False):
|
||||
last_upload = last_uploaded_nightscout_entry(BASAL_EVENTTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout basal upload:", last_upload_time)
|
||||
|
||||
add_count = 0
|
||||
for event in basalEvents:
|
||||
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"]))
|
||||
|
||||
reason = event["delivery_type"]
|
||||
if "suspendReason" in reason:
|
||||
reason += " (" + reason["suspendReason"] + ")"
|
||||
|
||||
entry = NightscoutEntry.basal(
|
||||
value=event["basal_rate"],
|
||||
duration_mins=event["duration_mins"],
|
||||
created_at=event["time"],
|
||||
reason=reason
|
||||
)
|
||||
|
||||
add_count += 1
|
||||
|
||||
print(" Processing basal:", event, "entry:", entry)
|
||||
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)
|
||||
|
||||
return add_count
|
||||
|
||||
"""
|
||||
Given bolus data input from the therapy timeline CSV, converts it into a digestable format.
|
||||
"""
|
||||
def process_bolus_events(bolusdata):
|
||||
bolusEvents = []
|
||||
|
||||
for b in bolusdata:
|
||||
parsed = TConnectEntry.parse_bolus_entry(b)
|
||||
if parsed["completion"] != "Completed":
|
||||
if parsed["insulin"] and float(parsed["insulin"]) > 0:
|
||||
# Count non-completed bolus if any insulin was delivered (vs. the amount of insulin requested)
|
||||
parsed["description"] += " (%s)" % parsed["completion"]
|
||||
else:
|
||||
print("Skipping non-completed bolus data:", b, "parsed:", parsed)
|
||||
continue
|
||||
bolusEvents.append(parsed)
|
||||
|
||||
bolusEvents.sort(key=lambda event: arrow.get(event["completion_time"] if not event["extended_bolus"] else event["bolex_start_time"]))
|
||||
|
||||
return bolusEvents
|
||||
|
||||
"""
|
||||
Given processed bolus data, adds bolus events to Nightscout.
|
||||
"""
|
||||
def ns_write_bolus_events(bolusEvents, pretend=False):
|
||||
last_upload = last_uploaded_nightscout_entry(BOLUS_EVENTTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout bolus upload:", last_upload_time)
|
||||
|
||||
add_count = 0
|
||||
for event in bolusEvents:
|
||||
if last_upload_time and arrow.get(event["completion_time"]) <= last_upload_time:
|
||||
if pretend:
|
||||
print("Skipping basal event before last upload time:", event)
|
||||
continue
|
||||
|
||||
entry = NightscoutEntry.bolus(
|
||||
bolus=event["insulin"],
|
||||
carbs=event["carbs"],
|
||||
created_at=event["completion_time"] if not event["extended_bolus"] else event["bolex_start_time"],
|
||||
notes="{}{}{}".format(event["description"], " (Override)" if event["user_override"] == "1" else "", " (Extended)" if event["extended_bolus"] == "1" else "")
|
||||
)
|
||||
|
||||
add_count += 1
|
||||
|
||||
print(" Processing bolus:", event, "entry:", entry)
|
||||
if not pretend:
|
||||
upload_nightscout(entry)
|
||||
|
||||
return add_count
|
||||
|
||||
"""
|
||||
Given IOB data input from the therapy timeline CSV, converts it into a digestable format.
|
||||
"""
|
||||
def process_iob_events(iobdata):
|
||||
iobEvents = []
|
||||
for d in iobdata:
|
||||
iobEvents.append(TConnectEntry.parse_iob_entry(d))
|
||||
|
||||
iobEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
|
||||
return iobEvents
|
||||
|
||||
"""
|
||||
Given processed IOB data, creates a single Nightscout activity definition to store IOB.
|
||||
"""
|
||||
def ns_write_iob_events(iobEvents, pretend=False):
|
||||
last_upload = last_uploaded_nightscout_activity(IOB_ACTIVITYTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout iob upload:", last_upload_time)
|
||||
|
||||
if not iobEvents or len(iobEvents) == 0:
|
||||
print("No IOB events: skipping")
|
||||
return 0
|
||||
|
||||
event = iobEvents[-1]
|
||||
if last_upload_time and arrow.get(event["time"]) <= last_upload_time:
|
||||
print(" Skipping already uploaded iob event:", event)
|
||||
return 0
|
||||
|
||||
entry = NightscoutEntry.iob(
|
||||
iob=event["iob"],
|
||||
created_at=event["time"]
|
||||
)
|
||||
|
||||
print(" Processing iob:", event, "entry:", entry)
|
||||
if not pretend:
|
||||
upload_nightscout(entry, entity='activity')
|
||||
|
||||
# Delete the previous activity
|
||||
if last_upload and '_id' in last_upload:
|
||||
print(" Deleting old iob entry:", last_upload)
|
||||
if not pretend:
|
||||
delete_nightscout('activity/{}'.format(last_upload['_id']))
|
||||
|
||||
return 1
|
||||
|
||||
def process_time_range(tconnect, time_start, time_end, pretend):
|
||||
print("Downloading t:connect ControlIQ data")
|
||||
try:
|
||||
ciqBasalData = tconnect.controliq.therapy_timeline(time_start, time_end)
|
||||
except ApiException as e:
|
||||
# The ControlIQ API returns a 404 if the user did not have a ControlIQ enabled
|
||||
# device in the time range which is queried. Since it launched in early 2020,
|
||||
# ignore 404's before February.
|
||||
if e.status_code == 404 and time_start.date() < datetime.date(2020, 2, 1):
|
||||
print("Ignoring HTTP 404 for ControlIQ API request before Feb 2020")
|
||||
ciqBasalData = None
|
||||
else:
|
||||
raise e
|
||||
|
||||
print("Downloading t:connect CSV data")
|
||||
csvdata = tconnect.ws2.therapy_timeline_csv(time_start, time_end)
|
||||
|
||||
readingData = csvdata["readingData"]
|
||||
iobData = csvdata["iobData"]
|
||||
csvBasalData = csvdata["basalData"]
|
||||
bolusData = csvdata["bolusData"]
|
||||
|
||||
if readingData and len(readingData) > 0:
|
||||
print("Last CGM reading from t:connect:", readingData[-1]['EventDateTime'] if 'EventDateTime' in readingData[-1] else readingData)
|
||||
|
||||
added = 0
|
||||
|
||||
basalEvents = process_ciq_basal_events(ciqBasalData)
|
||||
if csvBasalData:
|
||||
add_csv_basal_events(basalEvents, csvBasalData)
|
||||
|
||||
added += ns_write_basal_events(basalEvents, pretend=pretend)
|
||||
|
||||
|
||||
bolusEvents = process_bolus_events(bolusData)
|
||||
added += ns_write_bolus_events(bolusEvents, pretend=pretend)
|
||||
|
||||
iobEvents = process_iob_events(iobData)
|
||||
added += ns_write_iob_events(iobEvents, pretend=pretend)
|
||||
|
||||
return added
|
||||
|
||||
def parse_args():
|
||||
parser = argparse.ArgumentParser(description="Syncs bolus, basal, and IOB data from Tandem Diabetes t:connect to Nightscout.")
|
||||
@@ -279,15 +30,13 @@ def parse_args():
|
||||
parser.add_argument('--end-date', dest='end_date', type=str, default=None, help='The newest date to process data until (inclusive). Must be specified with --start-date.')
|
||||
parser.add_argument('--days', dest='days', type=int, default=1, help='The number of days of t:connect data to read in. Cannot be used with --from-date and --until-date.')
|
||||
parser.add_argument('--auto-update', dest='auto_update', action='store_const', const=True, default=False, help='If set, continuously checks for updates from t:connect and syncs with Nightscout.')
|
||||
parser.add_argument('--check-login', dest='check_login', action='store_const', const=True, default=False, help='If set, checks that the provided t:connect credentials can be used to log in.')
|
||||
|
||||
return parser.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')
|
||||
|
||||
@@ -303,52 +52,17 @@ def main():
|
||||
|
||||
tconnect = TConnectApi(TCONNECT_EMAIL, TCONNECT_PASSWORD)
|
||||
|
||||
nightscout = NightscoutApi(NS_URL, NS_SECRET)
|
||||
|
||||
if args.check_login:
|
||||
return check_login(tconnect, time_start, time_end)
|
||||
|
||||
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, nightscout, time_start, time_end, args.pretend)
|
||||
else:
|
||||
print("Processing data between", time_start, "and", time_end)
|
||||
added = process_time_range(tconnect, time_start, time_end, args.pretend)
|
||||
print("Processing data between", time_start, "and", time_end, "(PRETEND)" if args.pretend else "")
|
||||
added = process_time_range(tconnect, nightscout, time_start, time_end, args.pretend)
|
||||
print("Added", added, "items")
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -1,92 +0,0 @@
|
||||
import sys
|
||||
import requests
|
||||
import hashlib
|
||||
import time
|
||||
import urllib.parse
|
||||
|
||||
try:
|
||||
from secret import NS_URL, NS_SECRET, TIMEZONE_NAME
|
||||
except Exception:
|
||||
print('Unable to import Nightscout secrets from secret.py')
|
||||
sys.exit(1)
|
||||
|
||||
ENTERED_BY = "Pump (tconnectsync)"
|
||||
BASAL_EVENTTYPE = "Temp Basal"
|
||||
BOLUS_EVENTTYPE = "Combo Bolus"
|
||||
IOB_ACTIVITYTYPE = "tconnect_iob"
|
||||
|
||||
class NightscoutEntry:
|
||||
@staticmethod
|
||||
def basal(value, duration_mins, created_at, reason=""):
|
||||
return {
|
||||
"eventType": BASAL_EVENTTYPE,
|
||||
"reason": reason,
|
||||
"duration": float(duration_mins) if duration_mins else None,
|
||||
"absolute": float(value),
|
||||
"created_at": created_at,
|
||||
"carbs": None,
|
||||
"insulin": None,
|
||||
"enteredBy": ENTERED_BY
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def bolus(bolus, carbs, created_at, notes=""):
|
||||
return {
|
||||
"eventType": BOLUS_EVENTTYPE,
|
||||
"created_at": created_at,
|
||||
"carbs": carbs,
|
||||
"insulin": bolus,
|
||||
"notes": notes,
|
||||
"enteredBy": ENTERED_BY,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def iob(iob, created_at):
|
||||
return {
|
||||
"activityType": IOB_ACTIVITYTYPE,
|
||||
"iob": iob,
|
||||
"created_at": created_at,
|
||||
"enteredBy": ENTERED_BY
|
||||
}
|
||||
|
||||
def upload_nightscout(ns_format, entity='treatments'):
|
||||
upload = requests.post(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 upload status:", upload.status_code, upload.text)
|
||||
|
||||
def delete_nightscout(entity):
|
||||
upload = requests.delete(NS_URL + 'api/v1/' + entity + '?api_secret=' + NS_SECRET, json={}, headers={
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest()
|
||||
})
|
||||
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()
|
||||
})
|
||||
j = latest.json()
|
||||
if j and len(j) > 0:
|
||||
return j[0]
|
||||
return None
|
||||
|
||||
def last_uploaded_nightscout_activity(activityType):
|
||||
latest = requests.get(NS_URL + 'api/v1/activity?find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[activityType]=' + urllib.parse.quote(activityType) + '&ts=' + str(time.time()), headers={
|
||||
'api-secret': hashlib.sha1(NS_SECRET.encode()).hexdigest()
|
||||
})
|
||||
j = latest.json()
|
||||
if j and len(j) > 0:
|
||||
return j[0]
|
||||
return None
|
||||
@@ -1,26 +0,0 @@
|
||||
import os, sys
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
TCONNECT_EMAIL = os.environ.get('TCONNECT_EMAIL', 'email@email.com')
|
||||
TCONNECT_PASSWORD = os.environ.get('TCONNECT_PASSWORD', 'password')
|
||||
|
||||
try:
|
||||
PUMP_SERIAL_NUMBER = int(os.environ.get('PUMP_SERIAL_NUMBER', '11111111'))
|
||||
except ValueError as e:
|
||||
print("Error: PUMP_SERIAL_NUMBER must be a number.")
|
||||
print("Current value: {}".format(PUMP_SERIAL_NUMBER))
|
||||
sys.exit(1)
|
||||
|
||||
NS_URL = os.environ.get('NS_URL', 'https://yournightscouturl/')
|
||||
NS_SECRET = os.environ.get('NS_SECRET', 'apisecret')
|
||||
|
||||
TIMEZONE_NAME = os.environ.get('TIMEZONE_NAME', 'America/New_York')
|
||||
|
||||
_config = ['TCONNECT_EMAIL', 'TCONNECT_PASSWORD', 'PUMP_SERIAL_NUMBER',
|
||||
'NS_URL', 'NS_SECRET', 'TIMEZONE_NAME']
|
||||
|
||||
if __name__ == '__main__':
|
||||
for k in locals():
|
||||
print("{}={}".format(k, locals().get(k)))
|
||||
@@ -18,7 +18,7 @@ class TConnectApi:
|
||||
|
||||
@property
|
||||
def controliq(self):
|
||||
if self._ciq:
|
||||
if self._ciq and not self._ciq.needs_relogin():
|
||||
return self._ciq
|
||||
|
||||
self._ciq = ControlIQApi(self.email, self.password)
|
||||
@@ -10,6 +10,11 @@ from bs4 import BeautifulSoup
|
||||
|
||||
from .common import ApiException, ApiLoginException
|
||||
|
||||
"""
|
||||
The AndroidApi class contains methods which are queried in the t:connect
|
||||
Android application. These methods are a part of the tdc API which require
|
||||
Android specific credentials.
|
||||
"""
|
||||
class AndroidApi:
|
||||
BASE_URL = 'https://tdcservices.tandemdiabetes.com/'
|
||||
OAUTH_TOKEN_PATH = 'cloud/oauth2/token'
|
||||
@@ -52,13 +57,15 @@ class AndroidApi:
|
||||
|
||||
self.accessToken = j["accessToken"]
|
||||
self.accessTokenExpiresAt = j["accessTokenExpiresAt"]
|
||||
# NOTE: the refresh token is currently unused, instead a new access
|
||||
# token is obtained from scratch by re-logging in when it expires.
|
||||
self.refreshToken = j["refreshToken"]
|
||||
self.refreshTokenExpiresAt = j["refreshTokenExpiresAt"]
|
||||
self.userId = j["user"]["id"]
|
||||
self.patientObjectId = j["user"]["patientObjectId"]
|
||||
|
||||
def needs_relogin(self):
|
||||
diff = (arrow.get(self.refreshTokenExpiresAt) - arrow.get())
|
||||
diff = (arrow.get(self.accessTokenExpiresAt) - arrow.get())
|
||||
return (diff.seconds <= 5 * 60)
|
||||
|
||||
def api_headers(self):
|
||||
@@ -84,3 +91,37 @@ class AndroidApi:
|
||||
"""
|
||||
def last_event_uploaded(self, pump_serial_number):
|
||||
return self.get('cloud/upload/getlasteventuploaded?sn=%d' % pump_serial_number)
|
||||
|
||||
"""
|
||||
Returns user login information about a tconnect account.
|
||||
{'firstName': <string>, 'lastName': <string>, 'birthDate': 'YYYY-MM-DDT00:00:00.000Z',
|
||||
'emailAddress': <string>, 'secretQuestion': <string>, 'secretAnswer': <string>,
|
||||
'secretQuestionId': <integer>}
|
||||
"""
|
||||
def patient_info(self):
|
||||
return self.get('cloud/account/patient_info')
|
||||
|
||||
|
||||
# TODO: these methods are used in the web app, not the Android app,
|
||||
# but support the same auth tokens and are on this domain. They should
|
||||
# be moved to a new Api class.
|
||||
|
||||
"""
|
||||
Returns BG and pump threshold values.
|
||||
{'targetBGHigh': <integer>, 'targetBGLow': <integer>, 'hypoThreshold': <integer>,
|
||||
'hyperThreshold': <integer>, 'siteChangeThreshold': <integer>,
|
||||
'cartridgeChangeThreshold': <integer>, 'tubingChangeThreshold': <integer>}
|
||||
"""
|
||||
def therapy_thresholds(self):
|
||||
return self.get('cloud/usersettings/api/therapythresholds?userId=%s' % self.userId)
|
||||
|
||||
"""
|
||||
Returns therapy-related user information about a tconnect account.
|
||||
{'userID': <string>, 'targetBgHigh': <integer>, 'targetBgLow': <integer>,
|
||||
'hypoThreshold': <integer>, 'hyperThreshold': <integer>,
|
||||
'dateOfBirth': 'YYYY-MM-DDT00:00:00', 'age': <integer>,
|
||||
'patientFullName': <string>, 'caregiverDateOfBirth': <string>,
|
||||
'hasCGM': <bool>, 'hasBASALIQ': <bool>, 'hasControlIQ': <bool>}
|
||||
"""
|
||||
def user_profile(self):
|
||||
return self.get('cloud/usersettings/api/UserProfile?userId=%s' % self.userId)
|
||||
@@ -1,6 +1,8 @@
|
||||
import requests
|
||||
import urllib
|
||||
import datetime
|
||||
import arrow
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from .common import parse_date, base_headers, ApiException, ApiLoginException
|
||||
@@ -46,6 +48,10 @@ class ControlIQApi:
|
||||
self.accessTokenExpiresAt = req.cookies['accessTokenExpiresAt']
|
||||
return True
|
||||
|
||||
def needs_relogin(self):
|
||||
diff = (arrow.get(self.accessTokenExpiresAt) - arrow.get())
|
||||
return (diff.seconds <= 5 * 60)
|
||||
|
||||
def api_headers(self):
|
||||
if not self.accessToken:
|
||||
raise Exception('No access token provided')
|
||||
@@ -57,6 +63,9 @@ class ControlIQApi:
|
||||
raise ApiException(r.status_code, "ControlIQ API HTTP %s response: %s" % (str(r.status_code), r.text))
|
||||
return r.json()
|
||||
|
||||
"""
|
||||
Returns detailed basal event information and reasons for delivery suspension.
|
||||
"""
|
||||
def therapy_timeline(self, start=None, end=None):
|
||||
startDate = parse_date(start)
|
||||
endDate = parse_date(end)
|
||||
@@ -65,3 +74,19 @@ class ControlIQApi:
|
||||
"startDate": startDate,
|
||||
"endDate": endDate
|
||||
})
|
||||
|
||||
"""
|
||||
Returns a summary of pump and cgm activity.
|
||||
{'averageReading': <integer>, 'timeInUseMinutes': <integer>, 'controlIqSetToOffMinutes': <integer>,
|
||||
'cgmInactiveMinutes': <integer>, 'pumpInactiveMinutes': <integer>, 'averageDailySleepMinutes': <integer>,
|
||||
'weeklyExerciseEvents': <integer>, 'timeInUsePercent': <integer>, 'controlIqOffPercent': <integer>,
|
||||
'cgmInactivePercent': <integer>, 'pumpInactivePercent': <integer>, 'totalDays': <integer>}
|
||||
"""
|
||||
def dashboard_summary(self, start, end):
|
||||
startDate = parse_date(start)
|
||||
endDate = parse_date(end)
|
||||
|
||||
return self.get('summary/users/%s' % (self.userGuid), {
|
||||
"startDate": startDate,
|
||||
"endDate": endDate
|
||||
})
|
||||
@@ -18,6 +18,18 @@ class WS2Api:
|
||||
raise ApiException(r.status_code, "WS2 API HTTP %s response: %s" % (str(r.status_code), r.text))
|
||||
return r.text
|
||||
|
||||
def get_jsonp(self, endpoint):
|
||||
r = requests.get(self.BASE_URL + endpoint, {'callback': 'cb'}, headers=base_headers())
|
||||
if r.status_code != 200:
|
||||
raise ApiException(r.status_code, "WS2 API HTTP %s response: %s" % (str(r.status_code), r.text))
|
||||
|
||||
t = r.text.strip()
|
||||
if t.startswith('cb('):
|
||||
t = t[3:]
|
||||
if t.endswith(')'):
|
||||
t = t[:-1]
|
||||
|
||||
return t
|
||||
|
||||
def _split_empty_sections(self, text):
|
||||
sections = [[]]
|
||||
@@ -74,3 +86,12 @@ class WS2Api:
|
||||
"basalData": self._csv_to_dict(basalData),
|
||||
"bolusData": self._csv_to_dict(bolusData)
|
||||
}
|
||||
|
||||
"""
|
||||
Returns info on BasalIQ in JSONP format.
|
||||
"""
|
||||
def basaliqtech(self, start=None, end=None):
|
||||
startDate = parse_date(start)
|
||||
endDate = parse_date(end)
|
||||
|
||||
return self.get_jsonp('basaliqtech/%s/%s/%s' % (self.userGuid, startDate, endDate))
|
||||
@@ -0,0 +1,61 @@
|
||||
import time
|
||||
|
||||
from .process import process_time_range
|
||||
from .secret import (
|
||||
PUMP_SERIAL_NUMBER,
|
||||
AUTOUPDATE_DEFAULT_SLEEP_SECONDS,
|
||||
AUTOUPDATE_MAX_SLEEP_SECONDS,
|
||||
AUTOUPDATE_USE_FIXED_SLEEP
|
||||
)
|
||||
|
||||
"""
|
||||
Performs the auto-update functionality. Runs indefinitely in a loop
|
||||
until stopped (ctrl+c).
|
||||
"""
|
||||
def process_auto_update(tconnect, nightscout, 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, nightscout, 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 = AUTOUPDATE_DEFAULT_SLEEP_SECONDS
|
||||
if AUTOUPDATE_USE_FIXED_SLEEP != 1:
|
||||
if len(time_diffs) > 10:
|
||||
time_diffs = time_diffs[1:]
|
||||
|
||||
if len(time_diffs) > 2:
|
||||
sleep_secs = sum(time_diffs) / len(time_diffs)
|
||||
|
||||
if sleep_secs > AUTOUPDATE_MAX_SLEEP_SECONDS:
|
||||
sleep_secs = AUTOUPDATE_MAX_SLEEP_SECONDS
|
||||
|
||||
# Sleep for a rolling average of time between updates
|
||||
print('Sleeping for', sleep_secs, 'sec')
|
||||
time.sleep(sleep_secs)
|
||||
@@ -0,0 +1,58 @@
|
||||
from .nightscout import NightscoutApi
|
||||
|
||||
"""
|
||||
Attempts to authenticate with each t:connect API,
|
||||
and returns the output of a sample API call from each.
|
||||
Also attempts to connect to the Nightscout API.
|
||||
"""
|
||||
def check_login(tconnect, time_start, time_end):
|
||||
errors = 0
|
||||
|
||||
print("Logging in to t:connect ControlIQ API...")
|
||||
try:
|
||||
summary = tconnect.controliq.dashboard_summary(time_start, time_end)
|
||||
print("ControlIQ dashboard summary: %s" % summary)
|
||||
except Exception as e:
|
||||
print("Error occurred querying ControlIQ API: %s" % e)
|
||||
errors += 1
|
||||
|
||||
print("\nLogging in to t:connect WS2 API...")
|
||||
try:
|
||||
summary = tconnect.ws2.basaliqtech(time_start, time_end)
|
||||
print("WS2 basaliq status: %s" % summary)
|
||||
except Exception as e:
|
||||
print("Error occurred querying WS2 API: %s" % e)
|
||||
errors += 1
|
||||
|
||||
print("\nLogging in to t:connect Android API...")
|
||||
try:
|
||||
summary = tconnect.android.user_profile()
|
||||
print("Android user profile: %s" % summary)
|
||||
|
||||
from .secret import PUMP_SERIAL_NUMBER
|
||||
|
||||
event = tconnect.android.last_event_uploaded(PUMP_SERIAL_NUMBER)
|
||||
print("\nAndroid last uploaded event: %s" % event)
|
||||
except ImportError:
|
||||
print("Error: Unable to load config file.")
|
||||
except Exception as e:
|
||||
print("Error occurred querying Android API: %s" % e)
|
||||
errors += 1
|
||||
|
||||
print("\nLogging in to Nightscout...")
|
||||
try:
|
||||
from .secret import NS_URL, NS_SECRET
|
||||
|
||||
status = NightscoutApi(NS_URL, NS_SECRET).api_status()
|
||||
print("\nNightscout status: %s" % status)
|
||||
except ImportError:
|
||||
print("Error: Unable to load config file.")
|
||||
except Exception as e:
|
||||
print("Error occurred querying Nightscout API: %s" % e)
|
||||
errors += 1
|
||||
|
||||
if errors == 0:
|
||||
print("\nNo API errors returned!")
|
||||
else:
|
||||
print("\nAPI errors occurred. Please check the errors above.")
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import sys
|
||||
import requests
|
||||
import hashlib
|
||||
import time
|
||||
import urllib.parse
|
||||
|
||||
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)
|
||||
|
||||
class NightscoutApi:
|
||||
def __init__(self, url, secret):
|
||||
self.url = url
|
||||
self.secret = secret
|
||||
|
||||
|
||||
def upload_entry(self, ns_format, entity='treatments'):
|
||||
r = requests.post(urljoin(self.url, 'api/v1/' + entity + '?api_secret=' + self.secret), json=ns_format, headers={
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if r.status_code != 200:
|
||||
raise ApiException(r.status_code, "Nightscout upload response: %s" % r.text)
|
||||
|
||||
def delete_entry(self, entity):
|
||||
r = requests.delete(urljoin(self.url, 'api/v1/' + entity + '?api_secret=' + self.secret), json={}, headers={
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if r.status_code != 200:
|
||||
raise ApiException(r.status_code, "Nightscout delete response: %s" % r.text)
|
||||
|
||||
def put_entry(self, ns_format, entity):
|
||||
r = requests.put(urljoin(self.url, 'api/v1/' + entity + '?api_secret=' + self.secret), json=ns_format, headers={
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
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={
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if latest.status_code != 200:
|
||||
raise ApiException(latest.status_code, "Nightscout treatments response: %s" % latest.text)
|
||||
|
||||
j = latest.json()
|
||||
if j and len(j) > 0:
|
||||
return j[0]
|
||||
return None
|
||||
|
||||
def last_uploaded_activity(self, activityType):
|
||||
latest = requests.get(urljoin(self.url, 'api/v1/activity?find[enteredBy]=' + urllib.parse.quote(ENTERED_BY) + '&find[activityType]=' + urllib.parse.quote(activityType) + '&ts=' + str(time.time())), headers={
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if latest.status_code != 200:
|
||||
raise ApiException(latest.status_code, "Nightscout activity response: %s" % latest.text)
|
||||
|
||||
j = latest.json()
|
||||
if j and len(j) > 0:
|
||||
return j[0]
|
||||
return None
|
||||
|
||||
"""
|
||||
Returns general status information about the Nightscout server.
|
||||
"""
|
||||
def api_status(self):
|
||||
status = requests.get(urljoin(self.url, 'api/v1/status.json'), headers={
|
||||
'api-secret': hashlib.sha1(self.secret.encode()).hexdigest()
|
||||
})
|
||||
if status.status_code != 200:
|
||||
raise Exception('HTTP error status code (%d) from Nightscout: %s' % (status.status_code, status.text))
|
||||
return status.json()
|
||||
@@ -0,0 +1,41 @@
|
||||
ENTERED_BY = "Pump (tconnectsync)"
|
||||
BASAL_EVENTTYPE = "Temp Basal"
|
||||
BOLUS_EVENTTYPE = "Combo Bolus"
|
||||
IOB_ACTIVITYTYPE = "tconnect_iob"
|
||||
|
||||
"""
|
||||
Conversion methods for parsing data into Nightscout objects.
|
||||
"""
|
||||
class NightscoutEntry:
|
||||
@staticmethod
|
||||
def basal(value, duration_mins, created_at, reason=""):
|
||||
return {
|
||||
"eventType": BASAL_EVENTTYPE,
|
||||
"reason": reason,
|
||||
"duration": float(duration_mins) if duration_mins else None,
|
||||
"absolute": float(value),
|
||||
"created_at": created_at,
|
||||
"carbs": None,
|
||||
"insulin": None,
|
||||
"enteredBy": ENTERED_BY
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def bolus(bolus, carbs, created_at, notes=""):
|
||||
return {
|
||||
"eventType": BOLUS_EVENTTYPE,
|
||||
"created_at": created_at,
|
||||
"carbs": int(carbs),
|
||||
"insulin": float(bolus),
|
||||
"notes": notes,
|
||||
"enteredBy": ENTERED_BY,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def iob(iob, created_at):
|
||||
return {
|
||||
"activityType": IOB_ACTIVITYTYPE,
|
||||
"iob": iob,
|
||||
"created_at": created_at,
|
||||
"enteredBy": ENTERED_BY
|
||||
}
|
||||
@@ -2,11 +2,15 @@ import sys
|
||||
import arrow
|
||||
|
||||
try:
|
||||
from secret import TIMEZONE_NAME
|
||||
from ..secret import TIMEZONE_NAME
|
||||
except Exception:
|
||||
print('Unable to import parser secrets from secret.py')
|
||||
sys.exit(1)
|
||||
|
||||
"""
|
||||
Conversion methods for parsing raw t:connect data into
|
||||
a more digestable format, which is used internally.
|
||||
"""
|
||||
class TConnectEntry:
|
||||
BASAL_EVENTS = { 0: "Suspension", 1: "Profile", 2: "TempRate", 3: "Algorithm" }
|
||||
ACTIVITY_EVENTS = { 1: "Sleep", 2: "Exercise", 3: "AutoBolus", 4: "CarbOnly" }
|
||||
@@ -0,0 +1,62 @@
|
||||
from datetime import datetime
|
||||
|
||||
from .api.common import ApiException
|
||||
from .sync.basal import (
|
||||
process_ciq_basal_events,
|
||||
add_csv_basal_events,
|
||||
ns_write_basal_events
|
||||
)
|
||||
from .sync.bolus import (
|
||||
process_bolus_events,
|
||||
ns_write_bolus_events
|
||||
)
|
||||
from .sync.iob import (
|
||||
process_iob_events,
|
||||
ns_write_iob_events
|
||||
)
|
||||
|
||||
"""
|
||||
Given a TConnectApi object and start/end range, performs a single
|
||||
cycle of synchronizing data within the time range.
|
||||
If pretend is true, then doesn't actually write data to Nightscout.
|
||||
"""
|
||||
def process_time_range(tconnect, nightscout, time_start, time_end, pretend):
|
||||
print("Downloading t:connect ControlIQ data")
|
||||
try:
|
||||
ciqTherapyTimelineData = tconnect.controliq.therapy_timeline(time_start, time_end)
|
||||
except ApiException as e:
|
||||
# The ControlIQ API returns a 404 if the user did not have a ControlIQ enabled
|
||||
# device in the time range which is queried. Since it launched in early 2020,
|
||||
# ignore 404's before February.
|
||||
if e.status_code == 404 and time_start.date() < datetime.date(2020, 2, 1):
|
||||
print("Ignoring HTTP 404 for ControlIQ API request before Feb 2020")
|
||||
ciqTherapyTimelineData = None
|
||||
else:
|
||||
raise e
|
||||
|
||||
print("Downloading t:connect CSV data")
|
||||
csvdata = tconnect.ws2.therapy_timeline_csv(time_start, time_end)
|
||||
|
||||
readingData = csvdata["readingData"]
|
||||
iobData = csvdata["iobData"]
|
||||
csvBasalData = csvdata["basalData"]
|
||||
bolusData = csvdata["bolusData"]
|
||||
|
||||
if readingData and len(readingData) > 0:
|
||||
print("Last CGM reading from t:connect:", readingData[-1]['EventDateTime'] if 'EventDateTime' in readingData[-1] else readingData)
|
||||
|
||||
added = 0
|
||||
|
||||
basalEvents = process_ciq_basal_events(ciqTherapyTimelineData)
|
||||
if csvBasalData:
|
||||
add_csv_basal_events(basalEvents, csvBasalData)
|
||||
|
||||
added += ns_write_basal_events(nightscout, basalEvents, pretend=pretend)
|
||||
|
||||
bolusEvents = process_bolus_events(bolusData)
|
||||
added += ns_write_bolus_events(nightscout, bolusEvents, pretend=pretend)
|
||||
|
||||
iobEvents = process_iob_events(iobData)
|
||||
added += ns_write_iob_events(nightscout, iobEvents, pretend=pretend)
|
||||
|
||||
return added
|
||||
@@ -0,0 +1,41 @@
|
||||
import os, sys
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
def get(*args):
|
||||
return os.environ.get(*args)
|
||||
|
||||
def get_number(name, default):
|
||||
val = get(name, default)
|
||||
try:
|
||||
return int(val)
|
||||
except ValueError:
|
||||
print("Error: %s must be a number." % name)
|
||||
print("Current value: %s" % val)
|
||||
sys.exit(1)
|
||||
|
||||
TCONNECT_EMAIL = get('TCONNECT_EMAIL', 'email@email.com')
|
||||
TCONNECT_PASSWORD = get('TCONNECT_PASSWORD', 'password')
|
||||
|
||||
PUMP_SERIAL_NUMBER = get_number('PUMP_SERIAL_NUMBER', '11111111')
|
||||
|
||||
NS_URL = get('NS_URL', 'https://yournightscouturl/')
|
||||
NS_SECRET = get('NS_SECRET', 'apisecret')
|
||||
|
||||
TIMEZONE_NAME = get('TIMEZONE_NAME', 'America/New_York')
|
||||
|
||||
# Optional configuration
|
||||
|
||||
AUTOUPDATE_DEFAULT_SLEEP_SECONDS = get_number('AUTOUPDATE_DEFAULT_SLEEP_SECONDS', '60')
|
||||
AUTOUPDATE_MAX_SLEEP_SECONDS = get_number('AUTOUPDATE_MAX_SLEEP_SECONDS', '600')
|
||||
AUTOUPDATE_USE_FIXED_SLEEP = get_number('AUTOUPDATE_USE_FIXED_SLEEP', '0')
|
||||
|
||||
_config = ['TCONNECT_EMAIL', 'TCONNECT_PASSWORD', 'PUMP_SERIAL_NUMBER',
|
||||
'NS_URL', 'NS_SECRET', 'TIMEZONE_NAME',
|
||||
'AUTOUPDATE_DEFAULT_SLEEP_SECONDS', 'AUTOUPDATE_MAX_SLEEP_SECONDS',
|
||||
'AUTOUPDATE_USE_FIXED_SLEEP']
|
||||
|
||||
if __name__ == '__main__':
|
||||
for k in locals():
|
||||
print("{} = {}".format(k, locals().get(k)))
|
||||
@@ -0,0 +1,109 @@
|
||||
import arrow
|
||||
|
||||
from ..parser.nightscout import (
|
||||
BASAL_EVENTTYPE,
|
||||
NightscoutEntry
|
||||
)
|
||||
from ..parser.tconnect import TConnectEntry
|
||||
|
||||
|
||||
"""
|
||||
Merges together input from the therapy timeline API
|
||||
into a digestable format of basal data.
|
||||
"""
|
||||
def process_ciq_basal_events(data):
|
||||
if data is None:
|
||||
return []
|
||||
|
||||
suspensionEvents = {}
|
||||
for s in data["suspensionDeliveryEvents"]:
|
||||
entry = TConnectEntry.parse_suspension_entry(s)
|
||||
suspensionEvents[entry["time"]] = entry
|
||||
|
||||
basalEvents = []
|
||||
for b in data["basal"]["tempDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="tempDelivery"))
|
||||
|
||||
for b in data["basal"]["algorithmDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="algorithmDelivery"))
|
||||
|
||||
for b in data["basal"]["profileDeliveryEvents"]:
|
||||
basalEvents.append(TConnectEntry.parse_ciq_basal_entry(b, delivery_type="profileDelivery"))
|
||||
|
||||
basalEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
|
||||
for i in basalEvents:
|
||||
if i["time"] in suspensionEvents:
|
||||
i["suspendReason"] = suspensionEvents[i["time"]]["suspendReason"]
|
||||
|
||||
return basalEvents
|
||||
|
||||
"""
|
||||
Processes basal data input from the therapy timeline CSV (which only
|
||||
exists for pre Control-IQ data) into a digestable format.
|
||||
"""
|
||||
def add_csv_basal_events(basalEvents, data):
|
||||
last_entry = {}
|
||||
for row in data:
|
||||
entry = TConnectEntry.parse_csv_basal_entry(row)
|
||||
if last_entry:
|
||||
diff_mins = (arrow.get(entry["time"]) - arrow.get(last_entry["time"])).seconds // 60
|
||||
entry["duration_mins"] = diff_mins
|
||||
|
||||
basalEvents.append(entry)
|
||||
last_entry = entry
|
||||
|
||||
basalEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
return basalEvents
|
||||
|
||||
"""
|
||||
Given processed basal data, adds basal events to Nightscout.
|
||||
"""
|
||||
def ns_write_basal_events(nightscout, basalEvents, pretend=False):
|
||||
last_upload = nightscout.last_uploaded_entry(BASAL_EVENTTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout basal upload:", last_upload_time)
|
||||
|
||||
add_count = 0
|
||||
for event in basalEvents:
|
||||
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"]))
|
||||
|
||||
# If the timestamps are identical, and the duration is identical,
|
||||
# then don't upload a duplicate entry of what we already have.
|
||||
if not recent_needs_update:
|
||||
continue
|
||||
|
||||
reason = event["delivery_type"]
|
||||
if "suspendReason" in reason:
|
||||
reason += " (" + reason["suspendReason"] + ")"
|
||||
|
||||
entry = NightscoutEntry.basal(
|
||||
value=event["basal_rate"],
|
||||
duration_mins=event["duration_mins"],
|
||||
created_at=event["time"],
|
||||
reason=reason
|
||||
)
|
||||
|
||||
add_count += 1
|
||||
|
||||
print(" Processing basal:", event, "entry:", entry)
|
||||
if recent_needs_update:
|
||||
print("Replacing last uploaded entry:", last_upload)
|
||||
if not pretend:
|
||||
entry['_id'] = last_upload['_id']
|
||||
nightscout.put_entry(entry, entity='treatments')
|
||||
elif not pretend:
|
||||
nightscout.upload_entry(entry)
|
||||
|
||||
return add_count
|
||||
@@ -0,0 +1,60 @@
|
||||
import arrow
|
||||
|
||||
from ..parser.nightscout import (
|
||||
BOLUS_EVENTTYPE,
|
||||
NightscoutEntry
|
||||
)
|
||||
from ..parser.tconnect import TConnectEntry
|
||||
|
||||
"""
|
||||
Given bolus data input from the therapy timeline CSV, converts it into a digestable format.
|
||||
"""
|
||||
def process_bolus_events(bolusdata):
|
||||
bolusEvents = []
|
||||
|
||||
for b in bolusdata:
|
||||
parsed = TConnectEntry.parse_bolus_entry(b)
|
||||
if parsed["completion"] != "Completed":
|
||||
if parsed["insulin"] and float(parsed["insulin"]) > 0:
|
||||
# Count non-completed bolus if any insulin was delivered (vs. the amount of insulin requested)
|
||||
parsed["description"] += " (%s)" % parsed["completion"]
|
||||
else:
|
||||
print("Skipping non-completed bolus data:", b, "parsed:", parsed)
|
||||
continue
|
||||
bolusEvents.append(parsed)
|
||||
|
||||
bolusEvents.sort(key=lambda event: arrow.get(event["completion_time"] if not event["extended_bolus"] else event["bolex_start_time"]))
|
||||
|
||||
return bolusEvents
|
||||
|
||||
"""
|
||||
Given processed bolus data, adds bolus events to Nightscout.
|
||||
"""
|
||||
def ns_write_bolus_events(nightscout, bolusEvents, pretend=False):
|
||||
last_upload = nightscout.last_uploaded_entry(BOLUS_EVENTTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout bolus upload:", last_upload_time)
|
||||
|
||||
add_count = 0
|
||||
for event in bolusEvents:
|
||||
if last_upload_time and arrow.get(event["completion_time"]) <= last_upload_time:
|
||||
if pretend:
|
||||
print("Skipping basal event before last upload time:", event)
|
||||
continue
|
||||
|
||||
entry = NightscoutEntry.bolus(
|
||||
bolus=event["insulin"],
|
||||
carbs=event["carbs"],
|
||||
created_at=event["completion_time"] if not event["extended_bolus"] else event["bolex_start_time"],
|
||||
notes="{}{}{}".format(event["description"], " (Override)" if event["user_override"] == "1" else "", " (Extended)" if event["extended_bolus"] == "1" else "")
|
||||
)
|
||||
|
||||
add_count += 1
|
||||
|
||||
print(" Processing bolus:", event, "entry:", entry)
|
||||
if not pretend:
|
||||
nightscout.upload_entry(entry)
|
||||
|
||||
return add_count
|
||||
@@ -0,0 +1,55 @@
|
||||
import arrow
|
||||
|
||||
from ..parser.nightscout import (
|
||||
IOB_ACTIVITYTYPE,
|
||||
NightscoutEntry
|
||||
)
|
||||
from ..parser.tconnect import TConnectEntry
|
||||
|
||||
"""
|
||||
Given IOB data input from the therapy timeline CSV, converts it into a digestable format.
|
||||
"""
|
||||
def process_iob_events(iobdata):
|
||||
iobEvents = []
|
||||
for d in iobdata:
|
||||
iobEvents.append(TConnectEntry.parse_iob_entry(d))
|
||||
|
||||
iobEvents.sort(key=lambda x: arrow.get(x["time"]))
|
||||
|
||||
return iobEvents
|
||||
|
||||
"""
|
||||
Given processed IOB data, creates a single Nightscout activity definition to store IOB.
|
||||
"""
|
||||
def ns_write_iob_events(nightscout, iobEvents, pretend=False):
|
||||
last_upload = nightscout.last_uploaded_activity(IOB_ACTIVITYTYPE)
|
||||
last_upload_time = None
|
||||
if last_upload:
|
||||
last_upload_time = arrow.get(last_upload["created_at"])
|
||||
print("Last Nightscout iob upload:", last_upload_time)
|
||||
|
||||
if not iobEvents or len(iobEvents) == 0:
|
||||
print("No IOB events: skipping")
|
||||
return 0
|
||||
|
||||
event = iobEvents[-1]
|
||||
if last_upload_time and arrow.get(event["time"]) <= last_upload_time:
|
||||
print(" Skipping already uploaded iob event:", event)
|
||||
return 0
|
||||
|
||||
entry = NightscoutEntry.iob(
|
||||
iob=event["iob"],
|
||||
created_at=event["time"]
|
||||
)
|
||||
|
||||
print(" Processing iob:", event, "entry:", entry)
|
||||
if not pretend:
|
||||
nightscout.upload_entry(entry, entity='activity')
|
||||
|
||||
# Delete the previous activity
|
||||
if last_upload and '_id' in last_upload:
|
||||
print(" Deleting old iob entry:", last_upload)
|
||||
if not pretend:
|
||||
nightscout.delete_entry('activity/{}'.format(last_upload['_id']))
|
||||
|
||||
return 1
|
||||
@@ -0,0 +1,46 @@
|
||||
import tconnectsync.api
|
||||
|
||||
class ControlIQApi(tconnectsync.api.controliq.ControlIQApi):
|
||||
def __init__(self):
|
||||
self.BASE_URL = 'invalid://'
|
||||
self.LOGIN_URL = 'invalid://'
|
||||
|
||||
def login(self, email, password):
|
||||
raise NotImplementedError
|
||||
|
||||
def needs_relogin(self):
|
||||
return False
|
||||
|
||||
def get(self, endpoint, query):
|
||||
raise NotImplementedError
|
||||
|
||||
class WS2Api(tconnectsync.api.ws2.WS2Api):
|
||||
def __init__(self):
|
||||
self.BASE_URL = 'invalid://'
|
||||
|
||||
def get(self, endpoint, query):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_jsonp(self, endpoint):
|
||||
raise NotImplementedError
|
||||
|
||||
class AndroidApi(tconnectsync.api.android.AndroidApi):
|
||||
def __init__(self):
|
||||
self.BASE_URL = 'invalid://'
|
||||
|
||||
def login(self, email, password):
|
||||
raise NotImplementedError
|
||||
|
||||
def needs_relogin(self):
|
||||
return False
|
||||
|
||||
def get(self, endpoint, query={}, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
class TConnectApi(tconnectsync.api.TConnectApi):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
_ciq = ControlIQApi()
|
||||
_ws2 = WS2Api()
|
||||
_android = AndroidApi()
|
||||
@@ -0,0 +1,31 @@
|
||||
import collections
|
||||
|
||||
import tconnectsync.nightscout
|
||||
|
||||
class NightscoutApi(tconnectsync.nightscout.NightscoutApi):
|
||||
def __init__(self):
|
||||
self.url = 'invalid://'
|
||||
self.secret = 'invalid'
|
||||
|
||||
self.uploaded_entries = collections.defaultdict(list)
|
||||
self.deleted_entries = collections.defaultdict(list)
|
||||
self.put_entries = collections.defaultdict(list)
|
||||
|
||||
def upload_entry(self, ns_format, entity='treatments'):
|
||||
self.uploaded_entries[entity].append(ns_format)
|
||||
|
||||
def delete_entry(self, ns_format, entity):
|
||||
self.deleted_entries[entity].append(ns_format)
|
||||
|
||||
def put_entry(self, ns_format, entity):
|
||||
self.put_entries[entity].append(ns_format)
|
||||
|
||||
def last_uploaded_entry(self, eventType):
|
||||
raise NotImplementedError
|
||||
|
||||
def last_uploaded_activity(self, activityType):
|
||||
raise NotImplementedError
|
||||
|
||||
def api_status(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
from tconnectsync.parser.nightscout import NightscoutEntry
|
||||
|
||||
class TestNightscoutEntry(unittest.TestCase):
|
||||
def test_basal(self):
|
||||
self.assertEqual(
|
||||
NightscoutEntry.basal(
|
||||
value=1.05,
|
||||
duration_mins=30,
|
||||
created_at="2021-03-16 00:25:21-04:00"),
|
||||
{
|
||||
"eventType": "Temp Basal",
|
||||
"reason": "",
|
||||
"duration": 30,
|
||||
"absolute": 1.05,
|
||||
"created_at": "2021-03-16 00:25:21-04:00",
|
||||
"carbs": None,
|
||||
"insulin": None,
|
||||
"enteredBy": "Pump (tconnectsync)"
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
NightscoutEntry.basal(
|
||||
value=0.95,
|
||||
duration_mins=5,
|
||||
created_at="2021-03-16 12:25:21-04:00",
|
||||
reason="Correction"),
|
||||
{
|
||||
"eventType": "Temp Basal",
|
||||
"reason": "Correction",
|
||||
"duration": 5,
|
||||
"absolute": 0.95,
|
||||
"created_at": "2021-03-16 12:25:21-04:00",
|
||||
"carbs": None,
|
||||
"insulin": None,
|
||||
"enteredBy": "Pump (tconnectsync)"
|
||||
}
|
||||
)
|
||||
|
||||
def test_bolus(self):
|
||||
self.assertEqual(
|
||||
NightscoutEntry.bolus(
|
||||
bolus=7.5,
|
||||
carbs=45,
|
||||
created_at="2021-03-16 00:25:21-04:00"),
|
||||
{
|
||||
"eventType": "Combo Bolus",
|
||||
"created_at": "2021-03-16 00:25:21-04:00",
|
||||
"carbs": 45,
|
||||
"insulin": 7.5,
|
||||
"notes": "",
|
||||
"enteredBy": "Pump (tconnectsync)"
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
NightscoutEntry.bolus(
|
||||
bolus=0.5,
|
||||
carbs=5,
|
||||
created_at="2021-03-16 12:25:21-04:00"),
|
||||
{
|
||||
"eventType": "Combo Bolus",
|
||||
"created_at": "2021-03-16 12:25:21-04:00",
|
||||
"carbs": 5,
|
||||
"insulin": 0.5,
|
||||
"notes": "",
|
||||
"enteredBy": "Pump (tconnectsync)"
|
||||
}
|
||||
)
|
||||
|
||||
def test_iob(self):
|
||||
self.assertEqual(
|
||||
NightscoutEntry.iob(
|
||||
iob=2.05,
|
||||
created_at="2021-03-16 00:25:21-04:00"),
|
||||
{
|
||||
"activityType": "tconnect_iob",
|
||||
"iob": 2.05,
|
||||
"created_at": "2021-03-16 00:25:21-04:00",
|
||||
"enteredBy": "Pump (tconnectsync)"
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
from tconnectsync.parser.tconnect import TConnectEntry
|
||||
|
||||
class TestTConnectEntryBasal(unittest.TestCase):
|
||||
def test_parse_ciq_basal_entry(self):
|
||||
self.assertEqual(
|
||||
TConnectEntry.parse_ciq_basal_entry({
|
||||
"y": 0.8,
|
||||
"duration": 1221,
|
||||
"x": 1615878000
|
||||
}),
|
||||
{
|
||||
"time": "2021-03-16 00:00:00-04:00",
|
||||
"delivery_type": "",
|
||||
"duration_mins": 1221/60,
|
||||
"basal_rate": 0.8,
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
TConnectEntry.parse_ciq_basal_entry({
|
||||
"y": 0.797,
|
||||
"duration": 300,
|
||||
"x": 1615879521
|
||||
}, delivery_type="algorithmDelivery"),
|
||||
{
|
||||
"time": "2021-03-16 00:25:21-04:00",
|
||||
"delivery_type": "algorithmDelivery",
|
||||
"duration_mins": 5,
|
||||
"basal_rate": 0.797,
|
||||
}
|
||||
)
|
||||
|
||||
class TestTConnectEntryBolus(unittest.TestCase):
|
||||
entryStdCorrection = {
|
||||
"Type": "Bolus",
|
||||
"Description": "Standard/Correction",
|
||||
"BG": "141",
|
||||
"IOB": "",
|
||||
"BolusRequestID": "7001.000",
|
||||
"BolusCompletionID": "7001.000",
|
||||
"CompletionDateTime": "2021-04-01T12:58:26",
|
||||
"InsulinDelivered": "13.53",
|
||||
"FoodDelivered": "12.50",
|
||||
"CorrectionDelivered": "1.03",
|
||||
"CompletionStatusID": "3",
|
||||
"CompletionStatusDesc": "Completed",
|
||||
"BolusIsComplete": "1",
|
||||
"BolexCompletionID": "",
|
||||
"BolexSize": "",
|
||||
"BolexStartDateTime": "",
|
||||
"BolexCompletionDateTime": "",
|
||||
"BolexInsulinDelivered": "",
|
||||
"BolexIOB": "",
|
||||
"BolexCompletionStatusID": "",
|
||||
"BolexCompletionStatusDesc": "",
|
||||
"ExtendedBolusIsComplete": "",
|
||||
"EventDateTime": "2021-04-01T12:53:36",
|
||||
"RequestDateTime": "2021-04-01T12:53:36",
|
||||
"BolusType": "Carb",
|
||||
"BolusRequestOptions": "Standard/Correction",
|
||||
"StandardPercent": "100.00",
|
||||
"Duration": "0",
|
||||
"CarbSize": "75",
|
||||
"UserOverride": "0",
|
||||
"TargetBG": "110",
|
||||
"CorrectionFactor": "30.00",
|
||||
"FoodBolusSize": "12.50",
|
||||
"CorrectionBolusSize": "1.03",
|
||||
"ActualTotalBolusRequested": "13.53",
|
||||
"IsQuickBolus": "0",
|
||||
"EventHistoryReportEventDesc": "0",
|
||||
"EventHistoryReportDetails": "Correction & Food Bolus",
|
||||
"NoteID": "CF 1:30 - Carb Ratio 1:6 - Target BG 110",
|
||||
"IndexID": "0",
|
||||
"Note": "1181649"
|
||||
}
|
||||
def test_parse_bolus_entry_std_correction(self):
|
||||
self.assertEqual(
|
||||
TConnectEntry.parse_bolus_entry(self.entryStdCorrection),
|
||||
{
|
||||
"description": "Standard/Correction",
|
||||
"complete": "1",
|
||||
"completion": "Completed",
|
||||
"request_time": "2021-04-01 12:53:36-04:00",
|
||||
"completion_time": "2021-04-01 12:58:26-04:00",
|
||||
"insulin": "13.53",
|
||||
"carbs": "75",
|
||||
"user_override": "0",
|
||||
"extended_bolus": "",
|
||||
"bolex_completion_time": None,
|
||||
"bolex_start_time": None
|
||||
})
|
||||
|
||||
entryStd = {
|
||||
"Type": "Bolus",
|
||||
"Description": "Standard",
|
||||
"BG": "159",
|
||||
"IOB": "2.13",
|
||||
"BolusRequestID": "7007.000",
|
||||
"BolusCompletionID": "7007.000",
|
||||
"CompletionDateTime": "2021-04-01T23:23:17",
|
||||
"InsulinDelivered": "1.25",
|
||||
"FoodDelivered": "0.00",
|
||||
"CorrectionDelivered": "0.00",
|
||||
"CompletionStatusID": "3",
|
||||
"CompletionStatusDesc": "Completed",
|
||||
"BolusIsComplete": "1",
|
||||
"BolexCompletionID": "",
|
||||
"BolexSize": "",
|
||||
"BolexStartDateTime": "",
|
||||
"BolexCompletionDateTime": "",
|
||||
"BolexInsulinDelivered": "",
|
||||
"BolexIOB": "",
|
||||
"BolexCompletionStatusID": "",
|
||||
"BolexCompletionStatusDesc": "",
|
||||
"ExtendedBolusIsComplete": "",
|
||||
"EventDateTime": "2021-04-01T23:21:58",
|
||||
"RequestDateTime": "2021-04-01T23:21:58",
|
||||
"BolusType": "Carb",
|
||||
"BolusRequestOptions": "Standard",
|
||||
"StandardPercent": "100.00",
|
||||
"Duration": "0",
|
||||
"CarbSize": "0",
|
||||
"UserOverride": "1",
|
||||
"TargetBG": "110",
|
||||
"CorrectionFactor": "30.00",
|
||||
"FoodBolusSize": "0.00",
|
||||
"CorrectionBolusSize": "0.00",
|
||||
"ActualTotalBolusRequested": "1.25",
|
||||
"IsQuickBolus": "0",
|
||||
"EventHistoryReportEventDesc": "0",
|
||||
"EventHistoryReportDetails": "Food Bolus",
|
||||
"NoteID": "CF 1:30 - Carb Ratio 1:6 - Target BG 110 | Override: Pump calculated Bolus = 0.0 units",
|
||||
"IndexID": "0",
|
||||
"Note": "1182867"
|
||||
}
|
||||
def test_parse_bolus_entry_std(self):
|
||||
self.assertEqual(
|
||||
TConnectEntry.parse_bolus_entry(self.entryStd),
|
||||
{
|
||||
"description": "Standard",
|
||||
"complete": "1",
|
||||
"completion": "Completed",
|
||||
"request_time": "2021-04-01 23:21:58-04:00",
|
||||
"completion_time": "2021-04-01 23:23:17-04:00",
|
||||
"insulin": "1.25",
|
||||
"carbs": "0",
|
||||
"user_override": "1",
|
||||
"extended_bolus": "",
|
||||
"bolex_completion_time": None,
|
||||
"bolex_start_time": None
|
||||
})
|
||||
|
||||
entryStdAutomatic = {
|
||||
"Type": "Bolus",
|
||||
"Description": "Automatic Bolus/Correction",
|
||||
"BG": "",
|
||||
"IOB": "3.24",
|
||||
"BolusRequestID": "7010.000",
|
||||
"BolusCompletionID": "7010.000",
|
||||
"CompletionDateTime": "2021-04-02T01:00:47",
|
||||
"InsulinDelivered": "1.70",
|
||||
"FoodDelivered": "0.00",
|
||||
"CorrectionDelivered": "1.70",
|
||||
"CompletionStatusID": "3",
|
||||
"CompletionStatusDesc": "Completed",
|
||||
"BolusIsComplete": "1",
|
||||
"BolexCompletionID": "",
|
||||
"BolexSize": "",
|
||||
"BolexStartDateTime": "",
|
||||
"BolexCompletionDateTime": "",
|
||||
"BolexInsulinDelivered": "",
|
||||
"BolexIOB": "",
|
||||
"BolexCompletionStatusID": "",
|
||||
"BolexCompletionStatusDesc": "",
|
||||
"ExtendedBolusIsComplete": "",
|
||||
"EventDateTime": "2021-04-02T00:59:13",
|
||||
"RequestDateTime": "2021-04-02T00:59:13",
|
||||
"BolusType": "Automatic Correction",
|
||||
"BolusRequestOptions": "Automatic Bolus/Correction",
|
||||
"StandardPercent": "100.00",
|
||||
"Duration": "0",
|
||||
"CarbSize": "0",
|
||||
"UserOverride": "0",
|
||||
"TargetBG": "160",
|
||||
"CorrectionFactor": "30.00",
|
||||
"FoodBolusSize": "0.00",
|
||||
"CorrectionBolusSize": "1.70",
|
||||
"ActualTotalBolusRequested": "1.70",
|
||||
"IsQuickBolus": "0",
|
||||
"EventHistoryReportEventDesc": "0",
|
||||
"EventHistoryReportDetails": "Correction Bolus",
|
||||
"NoteID": "CF 1:30 - Carb Ratio 1:0 - Target BG 160",
|
||||
"IndexID": "0",
|
||||
"Note": "1183132"
|
||||
}
|
||||
def test_parse_bolus_entry_std_automatic(self):
|
||||
self.assertEqual(
|
||||
TConnectEntry.parse_bolus_entry(self.entryStdAutomatic),
|
||||
{
|
||||
"description": "Automatic Bolus/Correction",
|
||||
"complete": "1",
|
||||
"completion": "Completed",
|
||||
"request_time": "2021-04-02 00:59:13-04:00",
|
||||
"completion_time": "2021-04-02 01:00:47-04:00",
|
||||
"insulin": "1.70",
|
||||
"carbs": "0",
|
||||
"user_override": "0",
|
||||
"extended_bolus": "",
|
||||
"bolex_completion_time": None,
|
||||
"bolex_start_time": None
|
||||
})
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
from tconnectsync.sync.basal import process_ciq_basal_events
|
||||
from tconnectsync.parser.tconnect import TConnectEntry
|
||||
|
||||
class TestBasalSync(unittest.TestCase):
|
||||
base = {
|
||||
"basal": {
|
||||
"profileRates": [],
|
||||
"tempDeliveryEvents": [],
|
||||
"algorithmDeliveryEvents": [],
|
||||
"profileDeliveryEvents": []
|
||||
},
|
||||
"events": [],
|
||||
"suspensionDeliveryEvents": [],
|
||||
"softwareUpdates": [],
|
||||
"pumpFeatures": []
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def get_example_ciq_basal_events():
|
||||
data = TestBasalSync.base.copy()
|
||||
data["basal"]["tempDeliveryEvents"] = [
|
||||
{
|
||||
"y": 0.8,
|
||||
"duration": 1221,
|
||||
"x": 1615878000
|
||||
}
|
||||
]
|
||||
data["basal"]["algorithmDeliveryEvents"] = [
|
||||
{
|
||||
"y": 0.797,
|
||||
"duration": 300,
|
||||
"x": 1615879521
|
||||
},
|
||||
{
|
||||
"y": 0,
|
||||
"duration": 2693,
|
||||
"x": 1615879821
|
||||
},
|
||||
]
|
||||
data["basal"]["profileDeliveryEvents"] = [
|
||||
{
|
||||
"y": 0.799,
|
||||
"duration": 300,
|
||||
"x": 1615879221
|
||||
}
|
||||
]
|
||||
|
||||
data["suspensionDeliveryEvents"] = [
|
||||
{
|
||||
"suspendReason": "control-iq",
|
||||
"continuation": None,
|
||||
"x": 1615879821
|
||||
},
|
||||
]
|
||||
|
||||
return data
|
||||
|
||||
def test_process_ciq_basal_events(self):
|
||||
data = TestBasalSync.get_example_ciq_basal_events()
|
||||
|
||||
basalEvents = process_ciq_basal_events(data)
|
||||
self.assertEqual(len(basalEvents), 4)
|
||||
|
||||
self.assertEqual(basalEvents[0], TConnectEntry.parse_ciq_basal_entry(
|
||||
data["basal"]["tempDeliveryEvents"][0], delivery_type="tempDelivery"))
|
||||
|
||||
self.assertEqual(basalEvents[1], TConnectEntry.parse_ciq_basal_entry(
|
||||
data["basal"]["profileDeliveryEvents"][0], delivery_type="profileDelivery"))
|
||||
|
||||
self.assertEqual(basalEvents[2], TConnectEntry.parse_ciq_basal_entry(
|
||||
data["basal"]["algorithmDeliveryEvents"][0], delivery_type="algorithmDelivery"))
|
||||
|
||||
self.assertEqual(basalEvents[3], {
|
||||
"suspendReason": "control-iq",
|
||||
**TConnectEntry.parse_ciq_basal_entry(
|
||||
data["basal"]["algorithmDeliveryEvents"][1],
|
||||
delivery_type="algorithmDelivery")
|
||||
})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
|
||||
from tconnectsync.sync.bolus import process_bolus_events
|
||||
from tconnectsync.parser.tconnect import TConnectEntry
|
||||
|
||||
from ..parser.test_tconnect import TestTConnectEntryBolus
|
||||
|
||||
class TestBolusSync(unittest.TestCase):
|
||||
|
||||
@staticmethod
|
||||
def get_example_csv_bolus_events():
|
||||
return [
|
||||
TestTConnectEntryBolus.entryStdCorrection,
|
||||
TestTConnectEntryBolus.entryStd,
|
||||
TestTConnectEntryBolus.entryStdAutomatic
|
||||
]
|
||||
|
||||
def test_process_bolus_events(self):
|
||||
bolusData = TestBolusSync.get_example_csv_bolus_events()
|
||||
|
||||
bolusEvents = process_bolus_events(bolusData)
|
||||
self.assertEqual(len(bolusEvents), 3)
|
||||
|
||||
self.assertListEqual(bolusEvents, [
|
||||
TConnectEntry.parse_bolus_entry(bolusData[0]),
|
||||
TConnectEntry.parse_bolus_entry(bolusData[1]),
|
||||
TConnectEntry.parse_bolus_entry(bolusData[2])
|
||||
])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -0,0 +1,198 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import unittest
|
||||
import datetime
|
||||
import pprint
|
||||
|
||||
from tconnectsync.process import process_time_range
|
||||
from tconnectsync.parser.nightscout import NightscoutEntry
|
||||
|
||||
from .api.fake import TConnectApi
|
||||
from .nightscout_fake import NightscoutApi
|
||||
from .sync.test_basal import TestBasalSync
|
||||
from .sync.test_bolus import TestBolusSync
|
||||
|
||||
class TestProcessTimeRange(unittest.TestCase):
|
||||
maxDiff = None
|
||||
|
||||
def stub_therapy_timeline(self, time_start, time_end):
|
||||
pass
|
||||
|
||||
def stub_therapy_timeline_csv(self, time_start, time_end):
|
||||
return {
|
||||
"readingData": [],
|
||||
"iobData": [],
|
||||
"basalData": [],
|
||||
"bolusData": []
|
||||
}
|
||||
|
||||
def stub_last_uploaded_entry(self, event_type):
|
||||
return None
|
||||
|
||||
def stub_last_uploaded_activity(self, activity_type):
|
||||
return None
|
||||
|
||||
"""No data in Nightscout. Uploads all basal data from tconnect."""
|
||||
def test_new_ciq_basal_data(self):
|
||||
tconnect = TConnectApi()
|
||||
|
||||
start = datetime.datetime(2021, 4, 20, 12, 0)
|
||||
end = datetime.datetime(2021, 4, 21, 12, 0)
|
||||
|
||||
def fake_therapy_timeline(time_start, time_end):
|
||||
self.assertEqual(time_start, start)
|
||||
self.assertEqual(time_end, end)
|
||||
|
||||
return TestBasalSync.get_example_ciq_basal_events()
|
||||
|
||||
tconnect.controliq.therapy_timeline = fake_therapy_timeline
|
||||
tconnect.ws2.therapy_timeline_csv = self.stub_therapy_timeline_csv
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
nightscout.last_uploaded_entry = self.stub_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
|
||||
process_time_range(tconnect, nightscout, start, end, pretend=False)
|
||||
|
||||
self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 4)
|
||||
self.assertDictEqual(dict(nightscout.uploaded_entries), {
|
||||
"treatments": [
|
||||
NightscoutEntry.basal(0.8, 20.35, "2021-03-16 00:00:00-04:00", reason="tempDelivery"),
|
||||
NightscoutEntry.basal(0.799, 5.0, "2021-03-16 00:20:21-04:00", reason="profileDelivery"),
|
||||
NightscoutEntry.basal(0.797, 5.0, "2021-03-16 00:25:21-04:00", reason="algorithmDelivery"),
|
||||
NightscoutEntry.basal(0, 2693/60, "2021-03-16 00:30:21-04:00", reason="algorithmDelivery")
|
||||
]})
|
||||
self.assertDictEqual(nightscout.put_entries, {})
|
||||
self.assertDictEqual(nightscout.deleted_entries, {})
|
||||
|
||||
|
||||
"""Two basal entries in Nightscout. Two new basal entries in tconnect."""
|
||||
def test_partial_ciq_basal_data(self):
|
||||
tconnect = TConnectApi()
|
||||
|
||||
start = datetime.datetime(2021, 4, 20, 12, 0)
|
||||
end = datetime.datetime(2021, 4, 21, 12, 0)
|
||||
|
||||
def fake_therapy_timeline(time_start, time_end):
|
||||
self.assertEqual(time_start, start)
|
||||
self.assertEqual(time_end, end)
|
||||
|
||||
return TestBasalSync.get_example_ciq_basal_events()
|
||||
|
||||
tconnect.controliq.therapy_timeline = fake_therapy_timeline
|
||||
tconnect.ws2.therapy_timeline_csv = self.stub_therapy_timeline_csv
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
if event_type == "Temp Basal":
|
||||
return {
|
||||
"created_at": "2021-03-16 00:20:21-04:00",
|
||||
"duration": 5
|
||||
}
|
||||
|
||||
nightscout.last_uploaded_entry = fake_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
|
||||
process_time_range(tconnect, nightscout, start, end, pretend=False)
|
||||
|
||||
self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 2)
|
||||
self.assertDictEqual(dict(nightscout.uploaded_entries), {
|
||||
"treatments": [
|
||||
NightscoutEntry.basal(0.797, 5.0, "2021-03-16 00:25:21-04:00", reason="algorithmDelivery"),
|
||||
NightscoutEntry.basal(0, 2693/60, "2021-03-16 00:30:21-04:00", reason="algorithmDelivery")
|
||||
]})
|
||||
self.assertDictEqual(nightscout.put_entries, {})
|
||||
self.assertDictEqual(nightscout.deleted_entries, {})
|
||||
|
||||
|
||||
"""
|
||||
Two basal entries in Nightscout, the latter which needs to be updated
|
||||
with a longer duration. Two entirely new entries in tconnect."""
|
||||
def test_with_updated_duration_ciq_basal_data(self):
|
||||
tconnect = TConnectApi()
|
||||
|
||||
start = datetime.datetime(2021, 4, 20, 12, 0)
|
||||
end = datetime.datetime(2021, 4, 21, 12, 0)
|
||||
|
||||
def fake_therapy_timeline(time_start, time_end):
|
||||
self.assertEqual(time_start, start)
|
||||
self.assertEqual(time_end, end)
|
||||
|
||||
return TestBasalSync.get_example_ciq_basal_events()
|
||||
|
||||
tconnect.controliq.therapy_timeline = fake_therapy_timeline
|
||||
tconnect.ws2.therapy_timeline_csv = self.stub_therapy_timeline_csv
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
def fake_last_uploaded_entry(event_type):
|
||||
if event_type == "Temp Basal":
|
||||
return {
|
||||
"created_at": "2021-03-16 00:20:21-04:00",
|
||||
"duration": 3,
|
||||
"_id": "nightscout_id"
|
||||
}
|
||||
|
||||
nightscout.last_uploaded_entry = fake_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
|
||||
process_time_range(tconnect, nightscout, start, end, pretend=False)
|
||||
|
||||
self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 2)
|
||||
self.assertDictEqual(nightscout.uploaded_entries, {
|
||||
"treatments": [
|
||||
NightscoutEntry.basal(0.797, 5.0, "2021-03-16 00:25:21-04:00", reason="algorithmDelivery"),
|
||||
NightscoutEntry.basal(0, 2693/60, "2021-03-16 00:30:21-04:00", reason="algorithmDelivery")
|
||||
]})
|
||||
self.assertEqual(len(nightscout.put_entries["treatments"]), 1)
|
||||
self.assertDictEqual(dict(nightscout.put_entries), {
|
||||
"treatments": [
|
||||
{
|
||||
"_id": "nightscout_id",
|
||||
**NightscoutEntry.basal(0.799, 5.0, "2021-03-16 00:20:21-04:00", reason="profileDelivery")
|
||||
}
|
||||
]
|
||||
})
|
||||
self.assertDictEqual(nightscout.deleted_entries, {})
|
||||
|
||||
"""No data in Nightscout. Uploads all bolus data from tconnect."""
|
||||
def test_new_ciq_bolus_data(self):
|
||||
tconnect = TConnectApi()
|
||||
|
||||
start = datetime.datetime(2021, 4, 20, 12, 0)
|
||||
end = datetime.datetime(2021, 4, 21, 12, 0)
|
||||
|
||||
tconnect.controliq.therapy_timeline = self.stub_therapy_timeline
|
||||
|
||||
def fake_therapy_timeline_csv(time_start, time_end):
|
||||
return {
|
||||
**self.stub_therapy_timeline_csv(time_start, time_end),
|
||||
"bolusData": TestBolusSync.get_example_csv_bolus_events(),
|
||||
}
|
||||
|
||||
tconnect.ws2.therapy_timeline_csv = fake_therapy_timeline_csv
|
||||
|
||||
nightscout = NightscoutApi()
|
||||
|
||||
nightscout.last_uploaded_entry = self.stub_last_uploaded_entry
|
||||
nightscout.last_uploaded_activity = self.stub_last_uploaded_activity
|
||||
|
||||
process_time_range(tconnect, nightscout, start, end, pretend=False)
|
||||
|
||||
pprint.pprint(nightscout.uploaded_entries)
|
||||
self.assertEqual(len(nightscout.uploaded_entries["treatments"]), 3)
|
||||
self.assertDictEqual(dict(nightscout.uploaded_entries), {
|
||||
"treatments": [
|
||||
NightscoutEntry.bolus(13.53, 75, "2021-04-01 12:58:26-04:00", notes="Standard/Correction"),
|
||||
NightscoutEntry.bolus(1.25, 0, "2021-04-01 23:23:17-04:00", notes="Standard (Override)"),
|
||||
NightscoutEntry.bolus(1.7, 0, "2021-04-02 01:00:47-04:00", notes="Automatic Bolus/Correction"),
|
||||
]})
|
||||
self.assertDictEqual(nightscout.put_entries, {})
|
||||
self.assertDictEqual(nightscout.deleted_entries, {})
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user