Skip to content

Instantly share code, notes, and snippets.

@bewest
Last active August 29, 2015 14:01
Show Gist options
  • Save bewest/d168a91847346b6b613b to your computer and use it in GitHub Desktop.
Save bewest/d168a91847346b6b613b to your computer and use it in GitHub Desktop.
quick way to test nightscout PC?
import os, sys
import dexcom_reader
import time
from dexcom_reader import readdata
from pymongo import MongoClient
def get_client (uri):
client = MongoClient(uri)
return client
class Uploader (object):
def __init__ (self, opts):
self.opts = opts
self.mongo = get_client(self.opts.get('uri'))
if (self.opts.get('mongo_only', False)):
db = self.mongo.get_default_database( )
collection = db[self.opts.get('collection')]
print "Mongo works"
sys.exit(0)
def get_parse (self, parser):
return parser
def get_pages (self, pages):
records = [ ]
for x in range(*pages):
records.extend(self.device.ReadDatabasePage('EGV_DATA', x))
return records
def get_dexcom (self):
Dexcom = readdata.Dexcom
usb = Dexcom.FindDevice( )
if not usb:
sys.stderr.write("I could not find Dexcom G4 Receiver.\n")
sys.exit(1)
self.device = dexcom = Dexcom(usb)
return dexcom
def get_range (self):
pages = self.device.ReadDatabasePageRange('EGV_DATA')
print "Available PAGES", pages
# begin = pages[0]
# end = begin + 1
end = pages[1]
begin = end - 1
return [begin, end]
def run (self):
dev = self.get_dexcom( )
pages = self.get_range( )
print "Finding %s pages" % pages
records = self.get_pages(pages)
records = self.reformat(records)
print "Uploading %s records" % len(records)
self.upload_to_cloud(records)
print "Done"
def reformat (self, old):
records = [ ]
for egv in old:
rec = dict(device='dexcom'
, date=time.mktime(egv.display_time.timetuple( )) * 1000
, dateString=egv.display_time.isoformat( )
, sgv=egv.full_glucose)
records.append(rec)
return records
def upload_to_cloud (self, records):
db = self.mongo.get_default_database( )
collection = db[self.opts.get('collection')]
if (self.opts.get('mongo_only', False)):
print "Mongo works"
sys.exit(0)
print "Inserting %s into %s on %s" % (len(records), self.opts.get('db'), self.opts.get('collection'))
collection.insert(records)
if __name__ == '__main__':
uri = sys.argv[1:2].pop( ) or os.environ.get('MONGO_URI')
collection = sys.argv[2:3].pop( ) or os.environ.get('MONGO_COLLECTION')
only_test_mongo = os.environ.get('ONLY_TEST_MONGO', False)
if uri and collection:
print uri
parts = uri.split('/')
db = parts.pop( )
# uri = '/'.join(parts)
opts = dict(uri=uri, db=db, collection=collection, mongo_only=only_test_mongo)
app = Uploader(opts)
app.run( )
else:
print "usage: dexcom-to-mongo.py [mongo url] [collection]"
#!/bin/bash
# install python package management system
curl -s https://raw.githubusercontent.com/bewest/decoding-carelink/master/ez_setup.py | sudo python
# use it to install dexcom_reader package
sudo easy_install pyserial pymongo
sudo easy_install https://github.com/bewest/dexcom_reader/tarball/master
# fetch our script
uploader_script="https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/1261e2e13ac0f354cc675be101b3d3d96d470160/dexcom-to-mongo.py"
curl -o dexcom-to-mongo.py $uploader_script
echo "Downloaded and installed"
@bewest
Copy link
Author

bewest commented May 25, 2014

Ordinarily people should not do this, but this can be useful for a quick way to try things with people you trust.

curl https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/b7098df62a3b0b2300a7748acb1e67caf12f57a6/install.sh | bash

Will ask for your password and install the dependencies to run the python script above, which should write into mongo.

Once it's done, try:

python dexcom-to-mongo.py [mongodb://...] [collection]

Replace with your mongo connection uri and collection. Eg, python dexcom-to-mongo.py mongodb://aa:bb@mongo:8888/myDB myCollection, but with your values.

@bewest
Copy link
Author

bewest commented May 25, 2014

New install instructions:

curl https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/5255b04a3b1e75c8e0e1d8f87bce16858581781d/install.sh | bash

Then, same thing:

python dexcom-to-mongo.py mongo-connection-uri mongo-collection

@bewest
Copy link
Author

bewest commented May 25, 2014

New instructions:
(Noticed and fixed a bug).
If running the first time there are two steps in the terminal app:

# download/install things, will ask for password
curl  https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/afb6299f173401dd9e22238022b2634e79703412/install.sh | bash
# try connecting dexcom to the cloud
# use mongo://connection/uri and collection from MongoLab quckstart setup
python dexcom-to-mongo.py mongo://connection/uri collection

Replacing the values from the startup.

If it works it will say how many records it is inserting and then Done at the end. Afterwards, your nightscout cgm-monitor should have a few data points showing.

@bewest
Copy link
Author

bewest commented May 25, 2014

Ok,
To install things first time ever, do this:

curl https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/afb6299f173401dd9e22238022b2634e79703412/install.sh | bash

To update your existing script:

# fetch our script
uploader_script="https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/1261e2e13ac0f354cc675be101b3d3d96d470160/dexcom-to-mongo.py"
curl -o dexcom-to-mongo.py $uploader_script

Then run it:

python dexcom-to-mongo.py mongodb://connection/uri collection

@bewest
Copy link
Author

bewest commented May 29, 2014

Update script:

curl https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/8d9ea032b2a2c1bd77dc3e4c0e02b8dbca111822/dexcom-to-mongo.py -o dexcom-to-mongo.py

Then run with:

python decom-to-mongo.py mongodb://connection/uri collection

@bewest
Copy link
Author

bewest commented May 31, 2014

Update:

# update script
curl https://gist.githubusercontent.com/bewest/d168a91847346b6b613b/raw/f05ffd9ef19262de9a4fcb1660564c8386279234/dexcom-to-mongo.py -o dexcom-to-mongo.py

Then run it the same way:

# run the script with mongodb:// connection and collection
python decom-to-mongo.py mongodb://connection/uri collection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment