Skip to content

Instantly share code, notes, and snippets.

@mddub
Last active February 29, 2016 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mddub/26d0b0dd06d0fab16225 to your computer and use it in GitHub Desktop.
Save mddub/26d0b0dd06d0fab16225 to your computer and use it in GitHub Desktop.
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