Skip to content

Instantly share code, notes, and snippets.

@mgranberry
Last active August 29, 2015 14:17
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 mgranberry/09772479be4398267845 to your computer and use it in GitHub Desktop.
Save mgranberry/09772479be4398267845 to your computer and use it in GitHub Desktop.
Upload data from a DexCom G4 CGM into nightscout using dexcom_reader and pymongo
#!/usr/bin/env python
# replace MONGO_URI with your own nightscout mongo URI
MONGO_URI = 'mongodb://nightscout.example.com:1234/'
import dexcom_reader.readdata
import time
from serial.serialutil import SerialException
from datetime import datetime, date
from pymongo import MongoClient
mongo_client = MongoClient(MONGO_URI)
db = mongo_client.nightscout
def unix_time(dt):
epoch = datetime.fromtimestamp(0) #NOT utc (ugh)
delta = dt - epoch
return delta.total_seconds()
def unix_time_millis(dt):
return long(unix_time(dt) * 1000) - (3600000 if time.localtime().tm_isdst else 0)
name_map = {None: 'NONE', 'DOUBLE_UP':'DoubleUp', 'SINGLE_UP':'SingleUp',
'45_UP':'FortyFiveUp', 'FLAT':'Flat', '45_DOWN':'FortyFiveDown',
'SINGLE_DOWN':'SingleDown', 'DOUBLE_DOWN':'DoubleDown',
'NOT_COMPUTABLE':'NOT COMPUTABLE', 'OUT_OF_RANGE':'OUT OF RANGE'}
old_last_time = datetime.fromtimestamp(0)
old_last = 0
start_time = datetime.now()
while True:
try:
dd = dexcom_reader.readdata.Dexcom.FindDevice()
dr = dexcom_reader.readdata.Dexcom(dd)
records = dr.ReadRecords('EGV_DATA')
last = 0
for record in records:
if not record.is_special:
old_last = last
for_mongo = {'device':'dexcom', 'date':unix_time_millis(record.display_time), 'dateString':str(record.display_time), 'type':'sgv', 'sgv':record.glucose, 'direction':name_map[record.trend_arrow]}
last_time, last = record.display_time, record.glucose
if old_last_time < last_time:
if (last_time > start_time):
db.entries.insert(for_mongo)
print last_time, last, '\033]0;%s %+d\007'%(last,last - old_last)
if old_last_time != last_time:
old_last_time = last_time
old_last = last
except Exception as e:
print (e)
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment