Skip to content

Commit

Permalink
Update command line usage and added configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
fg1 committed Jan 11, 2015
1 parent 2e54067 commit e1ef3b1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.pyc
*.pyo
*.db
BLEHeartRateLogger.conf
4 changes: 4 additions & 0 deletions BLEHeartRateLogger.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[config]
m = 00:11:22:33:44:55
o = hrm_data.db
b = 1
60 changes: 50 additions & 10 deletions BLEHeartRateLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import sqlite3
import pexpect
import argparse

import ConfigParser

logging.basicConfig(format="%(asctime)-15s %(message)s")
log = logging.getLogger("BLEHeartRateLogger")
Expand All @@ -35,11 +35,32 @@ def parse_args():
Command line argument parsing
"""
parser = argparse.ArgumentParser(description="Bluetooth heart rate monitor data logger")
parser.add_argument("-b", type=str, help="MAC address of BLE device (default: auto-discovery)")
parser.add_argument("-B", action='store_true', help="Check battery level")
parser.add_argument("-g", type=str, help="gatttool path (default: system available)", default="gatttool")
parser.add_argument("-o", type=str, help="Output filename of the database (default: none)")
parser.add_argument("-V", action='store_true', help="Verbose output")
parser.add_argument("-m", metavar='MAC', type=str, help="MAC address of BLE device (default: auto-discovery)")
parser.add_argument("-b", action='store_true', help="Check battery level")
parser.add_argument("-g", metavar='PATH', type=str, help="gatttool path (default: system available)", default="gatttool")
parser.add_argument("-o", metavar='FILE', type=str, help="Output filename of the database (default: none)")
parser.add_argument("-v", action='store_true', help="Verbose output")

confpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "BLEHeartRateLogger.conf")
if os.path.exists(confpath):

config = ConfigParser.SafeConfigParser()
config.read([confpath])
config = dict(config.items("config"))

# We compare here the configuration given in the config file with the
# configuration of the parser.
args = vars(parser.parse_args([]))
err = False
for key in config.iterkeys():
if key not in args:
log.error("Configuration file error: invalid key '" + key + "'.")
err = True
if err:
sys.exit(1)

parser.set_defaults(**config)

return parser.parse_args()


Expand Down Expand Up @@ -135,14 +156,20 @@ def get_ble_hr_mac():
log.info("Trying to find a BLE device")
hci = pexpect.spawn("hcitool lescan")
try:
hci.expect("([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", timeout=10)
hci.expect("([0-9A-F]{2}[:-]){5}([0-9A-F]{2})", timeout=20)
addr = hci.match.group(0)
hci.close()
break

except pexpect.TIMEOUT:
time.sleep(60)
time.sleep(20)
continue

except KeyboardInterrupt:
log.info("Received keyboard interrupt. Quitting cleanly.")
hci.close()
return None

# We wait for the 'hcitool lescan' to finish
time.sleep(1)
return addr
Expand All @@ -164,6 +191,9 @@ def main(addr=None, sqlfile=None, gatttool="gatttool", check_battery=False):
if addr is None:
# In case no address has been provided, we scan to find any BLE devices
addr = get_ble_hr_mac()
if addr == None:
sq.close()
return

retry = True
while retry:
Expand All @@ -175,10 +205,19 @@ def main(addr=None, sqlfile=None, gatttool="gatttool", check_battery=False):
gt.sendline("connect")

try:
gt.expect("Connection successful.", timeout=10)
gt.expect(r"\[LE\]>", timeout=10)
gt.expect("Connection successful.", timeout=30)
gt.expect(r"\[LE\]>", timeout=30)

except pexpect.TIMEOUT:
continue

except KeyboardInterrupt:
log.info("Received keyboard interrupt. Quitting cleanly.")
retry = False
break
break

if not retry:
break

log.info("Connected to " + addr)
Expand Down Expand Up @@ -219,6 +258,7 @@ def main(addr=None, sqlfile=None, gatttool="gatttool", check_battery=False):
# If the timer expires, it means that we have lost the
# connection with the HR monitor
log.warn("Connection lost with " + addr + ". Reconnecting.")
sq.commit()
gt.sendline("quit")
try:
gt.wait()
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ To quit the tool, simply Ctrl-C.

Command line options:
```
usage: BLEHeartRateLogger.py [-h] [-b B] [-B] [-g G] [-o O] [-V]
usage: BLEHeartRateLogger.py [-h] [-m MAC] [-b] [-g PATH] [-o FILE] [-v]
Bluetooth heart rate monitor data logger
optional arguments:
-h, --help show this help message and exit
-b B MAC address of BLE device (default: auto-discovery)
-B Check battery level
-g G gatttool path (default: system available)
-o O Output filename of the database (default: none)
-V Verbose output
-m MAC MAC address of BLE device (default: auto-discovery)
-b Check battery level
-g PATH gatttool path (default: system available)
-o FILE Output filename of the database (default: none)
-v Verbose output
```


Expand Down

0 comments on commit e1ef3b1

Please sign in to comment.