summaryrefslogtreecommitdiff
path: root/dexcom_reader/util.py
diff options
context:
space:
mode:
authorBen West <bewest@gmail.com>2014-05-24 16:42:13 -0700
committerBen West <bewest@gmail.com>2014-05-24 16:42:13 -0700
commit5d92368e5b10fb2d01ae25a2730e15cd05a84da6 (patch)
tree6f5264f423e8e5a9ddfd44f3222bc8ad215b6e2f /dexcom_reader/util.py
parent6b5ce2584b0fe2da991540a33c71b1f95cd8878a (diff)
prep for installable module
Diffstat (limited to 'dexcom_reader/util.py')
-rw-r--r--dexcom_reader/util.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/dexcom_reader/util.py b/dexcom_reader/util.py
new file mode 100644
index 0000000..c719092
--- /dev/null
+++ b/dexcom_reader/util.py
@@ -0,0 +1,82 @@
1import constants
2import datetime
3import os
4import platform
5import plistlib
6import re
7import subprocess
8
9
10def ReceiverTimeToTime(rtime):
11 return constants.BASE_TIME + datetime.timedelta(seconds=rtime)
12
13
14def linux_find_usbserial(vendor, product):
15 DEV_REGEX = re.compile('^tty(USB|ACM)[0-9]+$')
16 for usb_dev_root in os.listdir('/sys/bus/usb/devices'):
17 device_name = os.path.join('/sys/bus/usb/devices', usb_dev_root)
18 if not os.path.exists(os.path.join(device_name, 'idVendor')):
19 continue
20 idv = open(os.path.join(device_name, 'idVendor')).read().strip()
21 if idv != vendor:
22 continue
23 idp = open(os.path.join(device_name, 'idProduct')).read().strip()
24 if idp != product:
25 continue
26 for root, dirs, files in os.walk(device_name):
27 for option in dirs + files:
28 if DEV_REGEX.match(option):
29 return os.path.join('/dev', option)
30
31
32def osx_find_usbserial(vendor, product):
33 def recur(v):
34 if hasattr(v, '__iter__') and 'idVendor' in v and 'idProduct' in v:
35 if v['idVendor'] == vendor and v['idProduct'] == product:
36 tmp = v
37 while True:
38 if 'IODialinDevice' not in tmp and 'IORegistryEntryChildren' in tmp:
39 tmp = tmp['IORegistryEntryChildren']
40 elif 'IODialinDevice' in tmp:
41 return tmp['IODialinDevice']
42 else:
43 break
44
45 if type(v) == list:
46 for x in v:
47 out = recur(x)
48 if out is not None:
49 return out
50 elif type(v) == dict or issubclass(type(v), dict):
51 for x in v.values():
52 out = recur(x)
53 if out is not None:
54 return out
55
56 sp = subprocess.Popen(['/usr/sbin/ioreg', '-k', 'IODialinDevice',
57 '-r', '-t', '-l', '-a', '-x'],
58 stdout=subprocess.PIPE,
59 stdin=subprocess.PIPE, stderr=subprocess.PIPE)
60 stdout, _ = sp.communicate()
61 plist = plistlib.readPlistFromString(stdout)
62 return recur(plist)
63
64
65def find_usbserial(vendor, product):
66 """Find the tty device for a given usbserial devices identifiers.
67
68 Args:
69 vendor: (int) something like 0x0000
70 product: (int) something like 0x0000
71
72 Returns:
73 String, like /dev/ttyACM0 or /dev/tty.usb...
74 """
75 if platform.system() == 'Linux':
76 vendor, product = [('%04x' % (x)).strip() for x in (vendor, product)]
77 return linux_find_usbserial(vendor, product)
78 elif platform.system() == 'Darwin':
79 return osx_find_usbserial(vendor, product)
80 else:
81 raise NotImplementedError('Cannot find serial ports on %s'
82 % platform.system())