-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmm-history.py
executable file
·82 lines (63 loc) · 2.64 KB
/
mm-history.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
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
from decocare import commands, models
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.ReadHistoryData, args=dict(page=number), render_decoded=False,render_hexdump=False)
def find_records (self, page):
records = decoder.decode( )
print "Found " , len(records), " records."
for record in records:
print " * found record", record.get('timestamp'), record['_type']
if record.get('timestamp'):
dt = parse(record['timestamp'])
dt = dt.replace(tzinfo=self.timezone)
record.update(timestamp=dt.isoformat( ))
return records
def download_history (self, args, nrPages):
records = [ ]
for i in range(1, nrPages):
print "Next page ", i
try:
pageRaw = self.download_page(i)
pageResult = HistoryPage(pageRaw.data, self.pump.model)
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('history.json', 'wb')
handle.write(recordsJson)
handle.close( )
def getNumberOfPages(self):
range = self.exec_request(self.pump, commands.ReadCurPageNumber, render_decoded=False,render_hexdump=False)
return range.getData()
def main (self, args):
# Set Global variables..
self.timezone = args.timezone
nrPages = self.getNumberOfPages()
self.download_history(args, nrPages)
if __name__ == '__main__':
app = DownloadHistory( )
app.run(None)