-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmm-glucose.py
executable file
·69 lines (53 loc) · 2.23 KB
/
mm-glucose.py
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
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
from decocare import commands, models, cgm
import json, argparse, sys
from decocare.history import HistoryPage
from decocare.helpers import cli
from dateutil.parser import parse
from dateutil.tz import gettz
class DownloadHistory (cli.CommandApp):
"""%(prog)s - Grab history
"""
def customize_parser (self, parser):
parser.add_argument('--model',
# type=get_model,
choices=models.known.keys( ))
parser.add_argument('--timezone',
default=gettz( ),
type=gettz,
help="Timezone to use")
parser.add_argument('--parser-out',
dest="parsed_data",
default='-',
type=argparse.FileType('w'),
help="Put history json in this file")
return parser
def download_page (self, number):
return self.exec_request(self.pump, commands.ReadGlucoseHistory, args=dict(page=number), render_decoded=False,render_hexdump=False)
def download_history (self, args, pageRange):
records = [ ]
for i in range(1 + pageRange['page'] - pageRange['isig'], pageRange['page']):
print "Next page ", i
try:
pageRaw = self.download_page(i).data
pageResult = cgm.PagedData.Data(pageRaw)
records.extend(pageResult.decode())
except:
print "Unexpected error when downloading cgm-page ", i, " from pump:", sys.exc_info()[0]
recordsJson = json.dumps(records);
args.parsed_data.write(recordsJson)
handle = open('glucose.json', 'wb')
handle.write(recordsJson)
handle.close( )
def getPagesRange(self):
range = self.exec_request(self.pump, commands.ReadCurGlucosePageNumber, render_decoded=False,render_hexdump=False)
return range.getData( )
def main (self, args):
# Set Global variables..
self.timezone = args.timezone
info = self.getPagesRange()
self.download_history(args, info)
if __name__ == '__main__':
app = DownloadHistory( )
app.run(None)