Skip to content

Commit

Permalink
time for some sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko authored and bewest committed Jun 21, 2015
1 parent 2918fd9 commit ec42912
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 46 deletions.
79 changes: 79 additions & 0 deletions bin/mm-glucose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

from decocare import commands, models
import json, argparse

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 find_records (self, page):
decoder = HistoryPage(page, self.pump.model)
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):
records = [ ]
for i in range(0, 10):
print "Next page ", i
pageHistory = self.download_page(i)
records.extend(self.find_records(pageHistory.data))
args.parsed_data.write(json.dumps(records))

def getRange(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.getRange()
print "yes"
print info
print vars(info)
print repr(info)
print "yes"


self.download_history(args)

if __name__ == '__main__':
app = DownloadHistory( )
app.run(None)

88 changes: 49 additions & 39 deletions bin/mm-history.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,66 +1,76 @@
#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

from decocare import commands
import json
from decocare import commands, models
import json, argparse

from decocare.history import HistoryPage
from decocare.helpers import cli

class LatestActivity (cli.CommandApp):
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):
kwds = dict(page=number)
page = self.exec_request(self.pump, commands.ReadHistoryData, args=kwds)
return page
return self.exec_request(self.pump, commands.ReadHistoryData, args=dict(page=number), render_decoded=False,render_hexdump=False)

def find_records (self, page, larger=None):
def find_records (self, page):
decoder = HistoryPage(page, self.pump.model)
records = decoder.decode( )
print "SINCE", self.since.isoformat( )

print "Found " , len(records), " records."
for record in records:
print " * found record", record['_type'], record.get('timestamp')
print " * should quit", record.get('timestamp') < self.since.isoformat( ), self.enough_history
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( ))
if record['timestamp'] < self.since.isoformat( ):
self.enough_history = True
if record['timestamp'] >= self.since.isoformat( ):
self.records.append(record)
return records

def download_history (self, args):
i = 0
print "find records since", self.since.isoformat( )
self.enough_history = False
self.records = [ ]
while not self.enough_history:
history = self.download_page(i)
remainder = self.find_records(history.data)
i = i + 1
results = self.records
print "```json"
args.parsed_data.write(json.dumps(results, indent=2))
print ''
print "```"
records = [ ]
for i in range(1, 38):
print "Next page ", i
pageHistory = self.download_page(i)
records.extend(self.find_records(pageHistory.data))
args.parsed_data.write(json.dumps(records))

def getRange(self):
range = self.exec_request(self.pump, commands.ReadCurPageNumber, render_decoded=False,render_hexdump=False)
return range.getData

def main (self, args):
self.delta = relativedelta.relativedelta(minutes=args.minutes)
self.report_settings(args)
if args.clock:
self.report_clock(args )
if args.status:
self.report_status(args)
if args.temp:
self.report_temp(args)
if args.basal:
self.report_basal(args)
if args.reservoir:
self.report_reservoir(args)
# Set Global variables..
self.timezone = args.timezone

info = self.getRange()
print "yes"
print info
print vars(info)
print repr(info)
print "yes"


self.download_history(args)

if __name__ == '__main__':
Expand Down
7 changes: 2 additions & 5 deletions decocare/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,7 @@ class ReadHistoryData(PumpCommand):
[3]
"""
__fields__ = PumpCommand.__fields__ + ['page']
_test_ok = bytearray([ 0x01, 0x00, 0xA7, 0x01, 0x20, 0x88, 0x50, 0x80,
0x01, 0x00, 0x02, 0x02, 0x00, 0x80, 0x9B, 0x03,
0x36, ])
_test_ok = bytearray([ 0x01, 0x00, 0xA7, 0x01, 0x20, 0x88, 0x50, 0x80, 0x01, 0x00, 0x02, 0x02, 0x00, 0x80, 0x9B, 0x03, 0x36, ])

page = None
def __init__(self, page=None, **kwds):
Expand Down Expand Up @@ -592,8 +590,7 @@ def getData(self):
log.info("XXX: READ cur page number:\n%s" % lib.hexdump(data))
if len(data) == 1:
return int(data[0])
result = dict(page= lib.BangLong(data[0:4]), glucose=data[5], isig=data[7])
return result
return dict(page= lib.BangLong(data[0:4]), glucose=data[5], isig=data[7])


class ReadRTC(PumpCommand):
Expand Down
5 changes: 3 additions & 2 deletions decocare/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,9 @@ def decode (self, larger=False):
for B in iter(lambda: bytearray(self.stream.read(2)), bytearray("")):
if B == bytearray( [ 0x00, 0x00 ] ):
if skipped:
last = records[-1]
last.update(appended=last.get('appended', [ ]) + skipped)
if len(records) > 0:
last = records[-1]
last.update(appended=last.get('appended', [ ]) + skipped)
# records.extend(skipped)
skipped = [ ]
break
Expand Down

0 comments on commit ec42912

Please sign in to comment.