File: example.py

package info (click to toggle)
py-nightscout 1.3.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188 kB
  • sloc: python: 635; makefile: 5; sh: 5
file content (71 lines) | stat: -rw-r--r-- 2,507 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import datetime

from aiohttp import ClientError, ClientConnectorError, ClientResponseError

import py_nightscout as nightscout

import pytz

NIGHTSCOUT_URL = 'https://your_nightscout_site.herokuapp.com'
API_SECRET = ''

async def main():
    """Example of library usage."""
    try:
        if API_SECRET:
                # To use authentication, use yout api secret:
                api = nightscout.Api(NIGHTSCOUT_URL, api_secret=API_SECRET)
        else:
            # You can use the api without authentication:
            api = nightscout.Api(NIGHTSCOUT_URL)
        status = await api.get_server_status()
    except ClientResponseError as error:
        raise RuntimeError("Received ClientResponseError") from error
    except (ClientError, ClientConnectorError, TimeoutError, OSError) as error:
        raise RuntimeError("Received client error or timeout") from error


    #### Glucose Values (SGVs) ####
    # Get last 10 entries:
    entries = await api.get_sgvs()
    print("SGV values in mg/dL:")
    print([entry.sgv for entry in entries])
    print("SGV values in mmol/L:")
    print([entry.sgv_mmol for entry in entries])

    # Specify time ranges:
    entries = await api.get_sgvs({'count':0, 'find[dateString][$gte]': '2017-03-07T01:10:26.000Z'})
    print("\nSGV values on timerange:")
    print([entry.sgv for entry in entries])

    ### Treatments ####
    # To fetch recent treatments (boluses, temp basals):
    treatments = await api.get_treatments()
    print("\nTreatments:")
    print([treatment.eventType for treatment in treatments])

    ### Profiles
    profile_definition_set = await api.get_profiles()
    profile_definition = profile_definition_set.get_profile_definition_active_at(datetime.datetime.now(tz=pytz.UTC))
    profile = profile_definition.get_default_profile()

    print("\nDuration of insulin action = %d" % profile.dia)

    five_thirty_pm = datetime.datetime(2017, 3, 24, 17, 30)
    five_thirty_pm = profile.timezone.localize(five_thirty_pm)
    print("\nScheduled basal rate at 5:30pm is = %f" % profile.basal.value_at_date(five_thirty_pm))

    ### Server Status

    server_status = await api.get_server_status()
    print("\nserver status: %s" % server_status.status)

    ### Device Status

    print("\nDevices:")
    devices_status = await api.get_latest_devices_status()
    for device_key in devices_status:
         print("\t%s battery: %d%%" % (device_key, devices_status[device_key].uploader.battery))
    
asyncio.run(main())