From b7e8dceef18a898a3d89ad3f44a91c8e3e53e70b Mon Sep 17 00:00:00 2001 From: James Woglom Date: Mon, 15 Mar 2021 20:42:13 -0400 Subject: [PATCH] Add dockerfile and instructions in README --- Dockerfile | 38 +++++++++++++++++++++ Pipfile | 3 ++ README.md | 96 ++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..367de74 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +FROM python:3.9-slim as base + +# The following is adapted from: +# https://sourcery.ai/blog/python-docker/ + +# Setup env +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONFAULTHANDLER 1 + +FROM base AS python-deps + +# Install pipenv and compilation dependencies +RUN pip install pipenv +RUN apt-get update && apt-get install -y --no-install-recommends gcc + +# Install python dependencies in /.venv +COPY Pipfile . +COPY Pipfile.lock . +RUN PIPENV_VENV_IN_PROJECT=1 pipenv install --deploy + +FROM base AS runtime + +# Copy virtualenv from python-deps stage +COPY --from=python-deps /.venv /.venv +ENV PATH="/.venv/bin:$PATH" + +# Create and switch to a new user +RUN useradd --create-home appuser +WORKDIR /home/appuser +USER appuser + +# Install application into container +COPY . . + +# Run the application +ENTRYPOINT ["python3", "-u", "main.py"] \ No newline at end of file diff --git a/Pipfile b/Pipfile index 8522024..5656904 100644 --- a/Pipfile +++ b/Pipfile @@ -11,3 +11,6 @@ bs4 = "*" arrow = "*" lxml = "*" python-dotenv = "*" + +[scripts] +tconnectsync = "python3 main.py" \ No newline at end of file diff --git a/README.md b/README.md index 3f7c1e3..65d97af 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ If you have a t:slim X2 pump with the companion t:connect mobile Android or iOS At a high level, tconnectsync works by querying Tandem's undocumented APIs to receive basal and bolus data from t:connect, and then uploads that data as treatment objects to Nightscout. It contains features for checking for new Tandem pump data continuously, and updating that data along with the pump's reported IOB value to Nightscout whenever there is new data. +**To get started,** read the setup instructions below and choose whether to run the application via **Pipenv** or **Docker**. + ## Tandem APIs This application utilizes three separate Tandem APIs for obtaining t:connect data, referenced here by the identifying part of their URLs: @@ -16,26 +18,28 @@ This application utilizes three separate Tandem APIs for obtaining t:connect dat ## Setup -Create a file named `secret.py` containing configuration values. See `sample.py.example`: +Create a file named `.env` containing configuration values. You should specify the following parameters: -```python -TCONNECT_EMAIL = 'email@email.com' -TCONNECT_PASSWORD = 'password' +```bash +# Your credentials for t:connect +TCONNECT_EMAIL=email@email.com +TCONNECT_PASSWORD=password -PUMP_SERIAL_NUMBER = 11111111 +# Your pump's serial number (numeric) +PUMP_SERIAL_NUMBER=11111111 -NS_URL = 'https://yournightscouturl/' -NS_SECRET = 'apisecret' +# URL and API secret for Nightscout +NS_URL=https://yournightscouturl/ +NS_SECRET=apisecret -TIMEZONE_NAME = 'America/New_York' +# Current timezone of the pump +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). 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. -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 python3 main.py`. - When you run the program with no arguments, it performs a single cycle of the following, and exits after completion: * Queries for basal information via the t:connect ControlIQ API. @@ -50,10 +54,51 @@ If run with the `--auto-update` flag, then the application performs the followin * Queries an API endpoint used only by the t:connect mobile app which returns an internal event ID, corresponding to the most recent event published by the mobile app. * Whenever the internal event ID changes (denoting that the mobile app uploaded new data to synchronize), perform all of the above mentioned steps to synchronize data. -### Running with Cron -To configure tconnectsync to run at a periodic interval (i.e. every 15 minutes), you can just invoke main.py with no arguments via cron. If using Pipenv or a virtualenv, make sure that you either prefix the call to main.py with `pipenv run` or source the `bin/activate` file within the virtualenv, so that the proper dependencies are loaded. If not using any kind of virtualenv, you can instead just install the necessary dependencies as specified inside Pipfile globally. +### Running with Pipenv -### Running with Supervisord +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 +$ pip3 install pipenv +$ pipenv install +$ pipenv run tconnectsync --help +usage: main.py [-h] [--pretend] [--start-date START_DATE] [--end-date END_DATE] + [--days DAYS] [--auto-update] + +Syncs bolus, basal, and IOB data from Tandem Diabetes t:connect to Nightscout. + +optional arguments: + -h, --help show this help message and exit + --pretend Pretend mode: do not upload any data to Nightscout. + --start-date START_DATE + The oldest date to process data from. Must be specified with + --end-date. + --end-date END_DATE The newest date to process data until (inclusive). Must be + specified with --start-date. + --days DAYS The number of days of t:connect data to read in. Cannot be + used with --from-date and --until-date. + --auto-update If set, continuously checks for updates from t:connect and + syncs with Nightscout. +``` + +You can now continue to either the **Running with Cron** or **Running with Supervisord** sections. + +### Running with Docker + +First, [ensure that you have Docker running and installed](https://docs.docker.com/get-started/#download-and-install-docker). + +To build and launch the project: + +```bash +$ git clone https://github.com/jwoglom/tconnectsync +$ docker build -t tconnectsync . +$ docker run tconnectsync --help +``` + +You can now continue to either the **Running with Cron** or **Running with Supervisord** sections. + +### Running with Supervisord (recommended) To instead configure tconnectsync to run continuously in the background using its `--auto-update` feature, you can use a tool such as Supervisord. Here is an example `tconnectsync.conf` which you can place inside `/etc/supervisor/conf.d`: ``` @@ -70,7 +115,7 @@ autorestart=true An example `run.sh` which launches tconnectsync within its pipenv-configured virtual environment: -``` +```bash #!/bin/bash PIPENV=/home/$(whoami)/.local/bin/pipenv @@ -82,6 +127,29 @@ cd /path/to/tconnectsync exec python3 -u main.py --auto-update ``` +An example `run.sh` which uses Docker: + +```bash +#!/bin/bash + +docker build -t tconnectsync +docker run tconnectsync --auto-update +``` + +### Running with Cron +To configure tconnectsync to run at a periodic interval (i.e. every 15 minutes), you can just invoke main.py with no arguments via cron. + +If using Pipenv or a virtualenv, make sure that you either prefix the call to main.py with `pipenv run` or source the `bin/activate` file within the virtualenv, so that the proper dependencies are loaded. If not using any kind of virtualenv, you can instead just install the necessary dependencies as specified inside Pipfile globally. + +An example configuration in `/etc/crontab` which runs every 15 minutes: + +```bash +# m h dom mon dow user command +0,15,30,45 * * * * root /path/to/tconnectsync/run.sh +``` + +You can use one of the same `run.sh` files mentioned above in the Supervisord example, but remove the `--auto-update` flag since you are handling the functionality for running the script periodically yourself. + ## Backfilling t:connect Data To backfill existing t:connect data in to Nightscout, you can use the `--start-date` and `--end-date` options. For example, the following will upload all t:connect data between January 1st and March 1st, 2020 to Nightscout: