Skip to content

Commit

Permalink
Lucas "BT reconnect" changes
Browse files Browse the repository at this point in the history
serial_read one place update
  • Loading branch information
LocutusOfPenguin committed Dec 12, 2017
1 parent a5fa349 commit 5d6b3bf
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions dgt/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,23 @@ def _process_board_message(self, message_id: int, message: tuple, message_length
else: # Default
logging.warning('message not handled [%s]', DgtMsg(message_id))

def _read_serial(self, bytes_toread=1):
try:
return self.serial.read(bytes_toread)
except SerialException:
pass
except TypeError:
pass
except struct.error: # can happen, when plugin board-cable again
pass
except AttributeError: # serial is None (race condition)
pass
return b''

def _read_board_message(self, head: bytes):
message = ()
header_len = 3
header = head + self.serial.read(header_len - 1)
header = head + self._read_serial(header_len - 1)
try:
header = struct.unpack('>BBB', header)
except struct.error:
Expand All @@ -391,7 +404,7 @@ def _read_board_message(self, head: bytes):
bytes_toread = 0x1f00
now = time.time()
while bytes_toread > 0:
ee_moves = self.serial.read(bytes_toread)
ee_moves = self._read_serial(bytes_toread)
logging.info('EE_MOVES 0x%x bytes read', len(ee_moves))
bytes_toread -= len(ee_moves)
if time.time() - now > 20:
Expand All @@ -410,7 +423,7 @@ def _read_board_message(self, head: bytes):
return message

while counter:
byte = self.serial.read(1)
byte = self._read_serial()
if byte:
data = struct.unpack('>B', byte)
counter -= 1
Expand All @@ -432,7 +445,7 @@ def _process_incoming_board_forever(self):
try:
byte = None
if self.serial:
byte = self.serial.read(1)
byte = self._read_serial()
else:
self._setup_serial_port()
if self.serial:
Expand All @@ -453,6 +466,8 @@ def _process_incoming_board_forever(self):
pass
except struct.error: # can happen, when plugin board-cable again
pass
except AttributeError: # serial is None (race condition)
pass

def ask_battery_status(self):
"""Ask the BT board for the battery status."""
Expand Down Expand Up @@ -489,6 +504,7 @@ def _open_bluetooth(self):
if path.exists('/dev/rfcomm123'):
logging.debug('BT releasing /dev/rfcomm123')
subprocess.call(['rfcomm', 'release', '123'])
subprocess.call(['cat', '/dev/rfcomm123']) # Lucas
self.bt_current_device = -1
self.bt_mac_list = []
self.bt_name_list = []
Expand Down Expand Up @@ -553,7 +569,7 @@ def _open_bluetooth(self):
self.bt_current_device -= 1
logging.debug('BT pairing failed, unknown device')
elif ('DGT_BT_' in self.bt_line or 'PCS-REVII' in self.bt_line) and \
('NEW' in self.bt_line or 'CHG' in self.bt_line) and 'DEL' not in self.bt_line:
('NEW' in self.bt_line or 'CHG' in self.bt_line) and 'Device' in self.bt_line:
# New e-Board found add to list
try:
if not self.bt_line.split()[3] in self.bt_mac_list:
Expand Down

0 comments on commit 5d6b3bf

Please sign in to comment.