Saturday, October 16, 2010

How to copy Accu-Chek Smart Pix data

The Accu-Chek Smart Pix device can be used to read data from a number of blood glucose meters (e.g. Accu-Chek Aviva, Nano, etc.). Since the device simply acts as a USB drive, it's also possible to copy the generated content.

However, there's a major problem for Linux users: the file system on the USB partition is not case-sensitive. That means opening the HTML files directly on the drive works, but after copying to a case-sensitive file system (such as EXT3/4 on Linux), the links inside the files won't work anymore.

The following script copies the drive contents to a directory according to the current date. Afterwards, a small Python script is invoked to fix both the file names themselves and the links. It also reduces the file size a bit by converting some images to more space-efficient formats.

copy.sh:

#!/bin/bash

folder=`date +%y-%m-%d`

cp -r /media/SMART_PIX/REPORT ${folder}
chmod -R +w ${folder}
cd ${folder}

# Rename file names and contents to lower case.
../lowercase.py

# Remove some superfluous stuff.
rm -r img/rd*.gif img/scanning.gif img/*.png

# Don't use bitmaps.
for f in `find -name "*.bmp"`; do g=${f/.bmp/.gif}; convert $f $g; rm $f; ln -s `basename $g` $f; done

# Add index file.
ln -s _review.htm index.html

lowercase.py:

#!/usr/bin/env python

import os
import re

def rename_lower(dirpath, name):
upper_path = os.path.join(dirpath, name)
lower_path = os.path.join(dirpath, name.lower())
os.rename(upper_path, lower_path)
return lower_path

pattern = re.compile('"([\w/.]+?)\.([\w/.]+?)"')

for dirpath, dirnames, filenames in os.walk('.', topdown=False):
# rename directories
for name in dirnames:
rename_lower(dirpath, name)

# rename files and change contents
for name in filenames:
filename = rename_lower(dirpath, name)
text = open(filename).read()
replaced = pattern.sub(lambda m: m.group(0).lower(), text)
open(filename, 'w').write(replaced)

Don't forget to make both files executable:

chmod +x copy.sh lowercase.py

Afterwards, simply run copy.sh inside the directory where you want the copy to be stored.

Also note that there's a simple way to read the blood glucose values if you want to write some scripts that process them: all the data is stored in XML format inside the "xml" subdirectory.