Skip to content

Instantly share code, notes, and snippets.

@mddub mddub/summarize.py Secret
Last active Feb 29, 2016

Embed
What would you like to do?
import json
import os
import re
import sys
from datetime import datetime
def try_json(filename):
try:
return json.loads(open(filename).read())
except IOError:
return None
temp_basal = try_json('monitor/temp_basal.json')
suggested = try_json('enact/suggested.json')
enacted = try_json('enact/enacted.json')
last_output = open(sys.argv[1]).read().strip()
openaps_success, cgm_success, preflight_success, suggest_success, enact_success = [a == '1' for a in sys.argv[2:]]
if suggest_success:
time = datetime.fromtimestamp(os.stat('enact/suggested.json').st_mtime).strftime("%I:%M%P")[:-1]
else:
time = datetime.now().strftime("%I:%M%P")[:-1]
if time.startswith('0'):
time = time[1:]
out = time
if suggest_success:
iob_json = try_json('monitor/iob.json')
if iob_json:
iob = round(json.loads(open('monitor/iob.json').read())['iob'], 1)
out += ' %su' % iob
if enact_success:
out += ' %sx%s' % (round(enacted['rate'], 1), enacted['duration'])
elif suggest_success:
if temp_basal and temp_basal.get('duration', 0) > 0:
out += ' %sx%s' % (round(temp_basal['rate'], 1), temp_basal['duration'])
else:
out += ''
else:
if not openaps_success:
out += ' oa'
elif not cgm_success:
out += ' cgm'
elif not preflight_success:
out += ' pf'
elif not suggest_success:
out += ' pump'
else:
out += ' ?'
last_successful_time = '?'
persisted_time = re.search('\[(.+)\]$', last_output)
recent_time = re.search('^(\d+:\d\d(a|p))', last_output)
if persisted_time:
last_successful_time = persisted_time.group(1)
elif recent_time:
last_successful_time = recent_time.group(1)
out += ' [%s]' % last_successful_time
print out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.