aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-06-15 01:10:40 +0200
committerMarek Siarkowicz <mareksiarkowicz@gmail.com>2015-06-15 01:20:11 +0200
commitffa76dd958282735c1b39793351a075a3d1a64ac (patch)
tree99593c6c6e09a6228c4a7172f773779e77f7e6f3
parent21cf625f6970c001c9f14bdbc603a104e544b9e1 (diff)
downloadpyParrotZikTCP-ffa76dd958282735c1b39793351a075a3d1a64ac.tar.xz
pyParrotZikTCP-ffa76dd958282735c1b39793351a075a3d1a64ac.zip
Refactor.
-rw-r--r--ParrotZik.py52
-rwxr-xr-xParrotZikTray6
-rw-r--r--bluetooth_paired_devices.py (renamed from BluetoothPairedDevices.py)53
-rw-r--r--resource_manager.py3
4 files changed, 55 insertions, 59 deletions
diff --git a/ParrotZik.py b/ParrotZik.py
index 8e4b31b..52414b5 100644
--- a/ParrotZik.py
+++ b/ParrotZik.py
@@ -1,58 +1,6 @@
-import sys
-
-from resource_manager import GenericResourceManager
from resource_manager import Version1ResourceManager
from resource_manager import Version2ResourceManager
-if sys.platform == "darwin":
- import lightblue
-else:
- import bluetooth
-
-
-def connect(addr=None):
- uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb",
- "8B6814D3-6CE7-4498-9700-9312C1711F63"]
-
- if sys.platform == "darwin":
- service_matches = lightblue.findservices(
- name="Parrot RFcomm service", addr=addr)
- else:
- for uuid in uuids:
- service_matches = bluetooth.find_service(uuid=uuid,
- address=addr)
- if service_matches:
- break
-
- if len(service_matches) == 0:
- print "Failed to find Parrot Zik RFCOMM service"
- return GenericResourceManager(None)
-
- if sys.platform == "darwin":
- first_match = service_matches[0]
- port = first_match[1]
- name = first_match[2]
- host = first_match[0]
- else:
- first_match = service_matches[0]
- port = first_match["port"]
- name = first_match["name"]
- host = first_match["host"]
-
- print "Connecting to \"%s\" on %s" % (name, host)
-
- if sys.platform == "darwin":
- sock = lightblue.socket()
- else:
- sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
-
- sock.connect((host, port))
-
- sock.send('\x00\x03\x00')
- data = sock.recv(1024)
- return GenericResourceManager(sock)
-
-
class BatteryStates:
CHARGED = 'charged'
diff --git a/ParrotZikTray b/ParrotZikTray
index 6850a3f..e43ac90 100755
--- a/ParrotZikTray
+++ b/ParrotZikTray
@@ -3,12 +3,12 @@ import functools
import gtk
-import BluetoothPairedDevices
+import bluetooth_paired_devices
from ParrotZik import BatteryStates
from ParrotZik import ParrotZikVersion1
from ParrotZik import ParrotZikVersion2
from ParrotZik import NoiseControlTypes
-from ParrotZik import connect
+from bluetooth_paired_devices import connect
from ParrotZik import Rooms
from SysIndicator import MenuItem
from SysIndicator import Menu
@@ -67,7 +67,7 @@ class ParrotZikIndicator(SysIndicator):
else:
self.reconnect.stop()
else:
- mac = BluetoothPairedDevices.ParrotZikMac()
+ mac = bluetooth_paired_devices.get_parrot_zik_mac()
if mac:
self.info_item.set_label("Connecting")
resource_manager = connect(mac)
diff --git a/BluetoothPairedDevices.py b/bluetooth_paired_devices.py
index aa5b056..139ca3d 100644
--- a/BluetoothPairedDevices.py
+++ b/bluetooth_paired_devices.py
@@ -2,13 +2,18 @@ import sys
import re
import os
+from resource_manager import GenericResourceManager
+
if sys.platform == "darwin":
from binplist import binplist
-elif sys.platform == "win32":
- import _winreg
+ import lightblue
+else:
+ import bluetooth
+ if sys.platform == "win32":
+ import _winreg
-def ParrotZikMac():
+def get_parrot_zik_mac():
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}')
if sys.platform == "linux2":
@@ -43,3 +48,45 @@ def ParrotZikMac():
except EnvironmentError:
pass
+
+
+def connect(addr=None):
+ if sys.platform == "darwin":
+ service_matches = lightblue.findservices(
+ name="Parrot RFcomm service", addr=addr)
+ else:
+ uuids = ["0ef0f502-f0ee-46c9-986c-54ed027807fb",
+ "8B6814D3-6CE7-4498-9700-9312C1711F63"]
+ service_matches = []
+ for uuid in uuids:
+ service_matches = bluetooth.find_service(uuid=uuid, address=addr)
+ if service_matches:
+ break
+
+ if len(service_matches) == 0:
+ print "Failed to find Parrot Zik RFCOMM service"
+ return GenericResourceManager(None)
+
+ if sys.platform == "darwin":
+ first_match = service_matches[0]
+ port = first_match[1]
+ name = first_match[2]
+ host = first_match[0]
+ else:
+ first_match = service_matches[0]
+ port = first_match["port"]
+ name = first_match["name"]
+ host = first_match["host"]
+
+ print "Connecting to \"%s\" on %s" % (name, host)
+
+ if sys.platform == "darwin":
+ sock = lightblue.socket()
+ else:
+ sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
+
+ sock.connect((host, port))
+
+ sock.send('\x00\x03\x00')
+ sock.recv(1024)
+ return GenericResourceManager(sock)
diff --git a/resource_manager.py b/resource_manager.py
index f0703ac..ddd39fb 100644
--- a/resource_manager.py
+++ b/resource_manager.py
@@ -1,9 +1,11 @@
from operator import itemgetter
import sys
+
from BeautifulSoup import BeautifulSoup
from message import Message
+
class ResourceManagerBase(object):
resources = [
]
@@ -137,4 +139,3 @@ class Version2ResourceManager(ResourceManagerBase):
'/api/audio/noise_control/enabled': ['get', 'set'],
'/api/audio/track/metadata': ['get'],
}
-