Skip to content

Commit

Permalink
Merge pull request #2 from pebble/improval-readability
Browse files Browse the repository at this point in the history
Improved readability and interface timeout. Fixed a buggy log in pebble.py read function.
  • Loading branch information
PaulMcInnis committed Mar 27, 2013
2 parents 3caec95 + 5a59afd commit 7dfb73e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
33 changes: 16 additions & 17 deletions pebble/LightBlueSerial.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
logging.basicConfig(format='[%(levelname)-8s] %(message)s')
log.setLevel(logging.DEBUG)

class BTPebbleError(Exception):
class LightBlueSerialError(Exception):
def __init__(self, id, message):
self._id = id
self._message = message
Expand All @@ -21,7 +21,9 @@ def __str__(self):
return "%s ID:(%s) on LightBlue API" % (self._message, self._id)

class LightBlueSerial(object):
def __init__(self, id, should_pair, debug_protocol=True, connection_process_timeout=60):
""" a wrapper for LightBlue that provides Serial-style read, write and close"""

def __init__(self, id, should_pair, debug_protocol=False, connection_process_timeout=60):

self.mac_address = id
self.debug_protocol = debug_protocol
Expand All @@ -36,25 +38,22 @@ def __init__(self, id, should_pair, debug_protocol=True, connection_process_time
self.bt_message_recd = multiprocessing.Event()
self.bt_connected = multiprocessing.Event()

self.bt_socket_proc = Process(target=self.LightBlueProcess)
self.bt_socket_proc = Process(target=self.run)
self.bt_socket_proc.daemon = True
self.bt_socket_proc.start()

# wait for a successful connection from child process before returning to main process
self.bt_connected.wait(connection_process_timeout)
if not self.bt_connected.is_set():
raise BTPebbleError(id, "Connection timed out, LightBlueProcess was provided %d seconds to complete connecting" % connection_process_timeout)

#def __del__(self):
# self.close()
raise LightBlueSerialError(id, "Connection timed out, LightBlueProcess was provided %d seconds to complete connecting" % connection_process_timeout)

def write(self, message):
""" send a message to the LightBlue processs"""
try:
self.send_queue.put(message)
self.bt_message_sent.wait()
except (IOError, EOFError):
raise BTPebbleError(self.mac_address, "Failed to access queue, disconnecting")
raise LightBlueSerialError(self.mac_address, "Failed to access write queue, disconnecting")
self.close()


Expand All @@ -66,14 +65,14 @@ def read(self):
except Queue.Empty:
return (None, None)
except (IOError, EOFError):
raise BTPebbleError(self.mac_address, "Failed to access queue, disconnecting")
raise LightBlueSerialError(self.mac_address, "Failed to access read queue, disconnecting")
self.close()

def close(self):
""" close the LightBlue connection process"""
self.bt_teardown.set()

def LightBlueProcess(self):
def run(self):
""" create bluetooth process paired to mac_address, must be run as a process"""
from lightblue import pair, unpair, socket as lb_socket, finddevices, selectdevice

Expand All @@ -84,17 +83,17 @@ def autodetect(self):
# we have the friendly name, let's get the full mac address
log.warn("Going to get full address for device %s, ensure device is broadcasting." % self.mac_address)
# scan for active devices
devices = finddevices(length=4)
devices = finddevices(timeout=8)

for device in devices:
if re.search(r'Pebble ' + self.mac_address, device[1], re.IGNORECASE):
log.debug("Found Pebble: " + device[1])
log.debug("Found Pebble: %s @ %s" % (device[1], device[0]))
list_of_pebbles.append(device)

if len(list_of_pebbles) is 1:
return list_of_pebbles[0][0]
else:
raise BTPebbleError(self.mac_address, "Failed to find Pebble")
raise LightBlueSerialError(self.mac_address, "Failed to find Pebble")
else:
# no pebble id was provided... give them the GUI selector
try:
Expand All @@ -115,7 +114,7 @@ def autodetect(self):
self._bts.connect((self.mac_address, 1)) # pebble uses RFCOMM port 1
self._bts.setblocking(False)
except:
raise BTPebbleError(self.mac_address, "Failed to connect to Pebble")
raise LightBlueSerialError(self.mac_address, "Failed to connect to Pebble")

if self.debug_protocol:
log.debug("Connection established to " + self.mac_address)
Expand All @@ -137,7 +136,7 @@ def autodetect(self):
self.bt_teardown.set()
e = "Queue Error while sending data"

# if anything is recieved relay it back
# if anything is received relay it back
rec_data = None
try:
rec_data = self._bts.recv(4)
Expand All @@ -159,5 +158,5 @@ def autodetect(self):
self.bt_message_recd.set()

# just let it die silent whenever the parent dies and it throws an EOFERROR
if e is not None and not self.debug_protocol:
raise BTPebbleError(self.mac_address, "LightBlue polling loop closed due to " + e)
if e is not None and self.debug_protocol:
raise LightBlueSerialError(self.mac_address, "LightBlue polling loop closed due to " + e)
4 changes: 3 additions & 1 deletion pebble/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,10 @@ def _send_message(self, endpoint, data, callback = None):

def _recv_message(self):
if self.using_lightblue:
resp, endpoint = self._ser.read()
endpoint, resp = self._ser.read()
data = ''
if resp is None:
return (None, None)
else:
data = self._ser.read(4)
if len(data) == 0:
Expand Down

0 comments on commit 7dfb73e

Please sign in to comment.