aboutsummaryrefslogtreecommitdiff
path: root/parrot_zik
diff options
context:
space:
mode:
authorMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-07-03 13:40:07 +0200
committerMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-07-03 13:40:37 +0200
commit3d455057798db862b5e1fb510d2f4aceaf71515a (patch)
treee9c1409b600376e9bab5ee9350f27d5792fb89d5 /parrot_zik
parent6eba736eb54ebecef14c4bd2b03df29b82f16f4b (diff)
downloadpyParrotZikTCP-3d455057798db862b5e1fb510d2f4aceaf71515a.tar.xz
pyParrotZikTCP-3d455057798db862b5e1fb510d2f4aceaf71515a.zip
Refactor.
Diffstat (limited to 'parrot_zik')
-rw-r--r--parrot_zik/bluetooth_paired_devices.py80
1 files changed, 47 insertions, 33 deletions
diff --git a/parrot_zik/bluetooth_paired_devices.py b/parrot_zik/bluetooth_paired_devices.py
index 0a40481..905819f 100644
--- a/parrot_zik/bluetooth_paired_devices.py
+++ b/parrot_zik/bluetooth_paired_devices.py
@@ -18,49 +18,63 @@ p = re.compile('90:03:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2
'A0:14:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}')
-def get_parrot_zik_mac_bluez():
- pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE,
- stderr=STDOUT)
- try:
- stdout, stderr = pipe.communicate()
- except dbus.exceptions.DBusException:
- pass
- else:
- bluetooth_on = int(stdout)
- if bluetooth_on == 1:
- pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE,
- stderr=STDOUT)
- try:
- stdout, stderr = pipe.communicate()
- except dbus.exceptions.DBusException:
- pass
- else:
- res = p.findall(stdout)
- if len(res) > 0:
- return res[0]
- else:
- raise DeviceNotConnected
+class BluetoothDeviceManager(object):
+ def is_bluetooth_on(self):
+ raise NotImplementedError
+
+ def get_mac(self):
+ raise NotImplementedError
+
+
+class BluezBluetoothDeviceManager(BluetoothDeviceManager):
+ def is_bluetooth_on(self):
+ pipe = Popen(['bluez-test-adapter', 'powered'], stdout=PIPE, stdin=PIPE,
+ stderr=STDOUT)
+ try:
+ stdout, stderr = pipe.communicate()
+ except dbus.exceptions.DBusException:
+ pass
+ else:
+ return bool(stdout.strip())
+
+ def get_mac(self):
+ pipe = Popen(['bluez-test-device', 'list'], stdout=PIPE, stdin=PIPE,
+ stderr=STDOUT)
+ try:
+ stdout, stderr = pipe.communicate()
+ except dbus.exceptions.DBusException:
+ pass
else:
- raise BluetoothIsNotOn
+ res = p.findall(stdout)
+ if len(res) > 0:
+ return res[0]
+ else:
+ raise DeviceNotConnected
-def get_parrot_zik_mac_bluetoothcmd():
- pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
- res = pipe.communicate("exit")
- if len(res) > 0 and res[0]:
- match = p.search(res[0])
- if match:
- return match.group(0)
+class BluetoothCmdDeviceManager(BluetoothDeviceManager):
+ def is_bluetooth_on(self):
+ return True
- raise DeviceNotConnected
+ def get_mac(self):
+ pipe = Popen(['bluetoothctl'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
+ res = pipe.communicate("exit")
+ if len(res) > 0 and res[0]:
+ match = p.search(res[0])
+ if match:
+ return match.group(0)
+ raise DeviceNotConnected
def get_parrot_zik_mac_linux():
+ bluez_manager = BluezBluetoothDeviceManager()
try:
- return get_parrot_zik_mac_bluez()
+ bluez_manager.is_bluetooth_on()
+ return bluez_manager.get_mac()
except OSError as e:
if e.errno == 2:
- return get_parrot_zik_mac_bluetoothcmd()
+ bluetoothcmd_manager = BluetoothCmdDeviceManager()
+ return bluetoothcmd_manager.get_mac()
def get_parrot_zik_mac_darwin():