diff options
author | Ben West <bewest@gmail.com> | 2014-05-24 16:42:13 -0700 |
---|---|---|
committer | Ben West <bewest@gmail.com> | 2014-05-24 16:42:13 -0700 |
commit | 5d92368e5b10fb2d01ae25a2730e15cd05a84da6 (patch) | |
tree | 6f5264f423e8e5a9ddfd44f3222bc8ad215b6e2f /dexcom_reader/util.py | |
parent | 6b5ce2584b0fe2da991540a33c71b1f95cd8878a (diff) |
prep for installable module
Diffstat (limited to 'dexcom_reader/util.py')
-rw-r--r-- | dexcom_reader/util.py | 82 |
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 @@ | |||
1 | import constants | ||
2 | import datetime | ||
3 | import os | ||
4 | import platform | ||
5 | import plistlib | ||
6 | import re | ||
7 | import subprocess | ||
8 | |||
9 | |||
10 | def ReceiverTimeToTime(rtime): | ||
11 | return constants.BASE_TIME + datetime.timedelta(seconds=rtime) | ||
12 | |||
13 | |||
14 | def 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 | |||
32 | def 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 | |||
65 | def 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()) | ||