Increase logging for websockets

This commit is contained in:
croneter 2017-09-06 14:14:42 +02:00
parent 6dfed36dbe
commit 81084ea479

View file

@ -84,7 +84,8 @@ class WebSocket(Thread):
# No worries if read timed out # No worries if read timed out
pass pass
except websocket.WebSocketConnectionClosedException: except websocket.WebSocketConnectionClosedException:
log.info("Connection closed, (re)connecting") log.info("%s: connection closed, (re)connecting"
% self.__class__.__name__)
uri, sslopt = self.getUri() uri, sslopt = self.getUri()
try: try:
# Low timeout - let's us shut this thread down! # Low timeout - let's us shut this thread down!
@ -95,7 +96,7 @@ class WebSocket(Thread):
enable_multithread=True) enable_multithread=True)
except IOError: except IOError:
# Server is probably offline # Server is probably offline
log.info("Error connecting") log.info("%s: Error connecting" % self.__class__.__name__)
self.ws = None self.ws = None
counter += 1 counter += 1
if counter > 3: if counter > 3:
@ -103,33 +104,41 @@ class WebSocket(Thread):
self.IOError_response() self.IOError_response()
sleep(1000) sleep(1000)
except websocket.WebSocketTimeoutException: except websocket.WebSocketTimeoutException:
log.info("timeout while connecting, trying again") log.info("%s: Timeout while connecting, trying again"
% self.__class__.__name__)
self.ws = None self.ws = None
sleep(1000) sleep(1000)
except websocket.WebSocketException as e: except websocket.WebSocketException as e:
log.info('WebSocketException: %s' % e) log.info('%s: WebSocketException: %s'
% (self.__class__.__name__, e))
if 'Handshake Status 401' in e.args: if 'Handshake Status 401' in e.args:
handshake_counter += 1 handshake_counter += 1
if handshake_counter >= 5: if handshake_counter >= 5:
log.info('Error in handshake detected. Stopping ' log.info('%s: Error in handshake detected. '
'%s now' % self.__class__.__name__) 'Stopping now'
% self.__class__.__name__)
break break
self.ws = None self.ws = None
sleep(1000) sleep(1000)
except Exception as e: except Exception as e:
log.error("Unknown exception encountered in connecting: %s" log.error('%s: Unknown exception encountered when '
% e) 'connecting: %s' % (self.__class__.__name__, e))
import traceback import traceback
log.error("Traceback:\n%s" % traceback.format_exc()) log.error("%s: Traceback:\n%s"
% (self.__class__.__name__,
traceback.format_exc()))
self.ws = None self.ws = None
sleep(1000) sleep(1000)
else: else:
counter = 0 counter = 0
handshake_counter = 0 handshake_counter = 0
except Exception as e: except Exception as e:
log.error("Unknown exception encountered: %s" % e) log.error("%s: Unknown exception encountered: %s"
% (self.__class__.__name__, e))
import traceback import traceback
log.error("Traceback:\n%s" % traceback.format_exc()) log.error("%s: Traceback:\n%s"
% (self.__class__.__name__,
traceback.format_exc()))
try: try:
self.ws.shutdown() self.ws.shutdown()
except: except:
@ -171,7 +180,8 @@ class PMS_Websocket(WebSocket):
sslopt = {} sslopt = {}
if settings('sslverify') == "false": if settings('sslverify') == "false":
sslopt["cert_reqs"] = CERT_NONE sslopt["cert_reqs"] = CERT_NONE
log.debug("Uri: %s, sslopt: %s" % (uri, sslopt)) log.debug("%s: Uri: %s, sslopt: %s"
% (self.__class__.__name__, uri, sslopt))
return uri, sslopt return uri, sslopt
def process(self, opcode, message): def process(self, opcode, message):
@ -181,20 +191,24 @@ class PMS_Websocket(WebSocket):
try: try:
message = loads(message) message = loads(message)
except Exception as ex: except Exception as ex:
log.error('Error decoding message from websocket: %s' % ex) log.error('%s: Error decoding message from websocket: %s'
% (self.__class__.__name__, ex))
log.error(message) log.error(message)
return False return False
try: try:
message = message['NotificationContainer'] message = message['NotificationContainer']
except KeyError: except KeyError:
log.error('Could not parse PMS message: %s' % message) log.error('%s: Could not parse PMS message: %s'
% (self.__class__.__name__, message))
return False return False
# Triage # Triage
typus = message.get('type') typus = message.get('type')
if typus is None: if typus is None:
log.error('No message type, dropping message: %s' % message) log.error('%s: No message type, dropping message: %s'
% (self.__class__.__name__, message))
return False return False
log.debug('Received message from PMS server: %s' % message) log.debug('%s: Received message from PMS server: %s'
% (self.__class__.__name__, message))
# Drop everything we're not interested in # Drop everything we're not interested in
if typus not in ('playing', 'timeline'): if typus not in ('playing', 'timeline'):
return True return True
@ -224,27 +238,32 @@ class Alexa_Websocket(WebSocket):
% (state.PLEX_USER_ID, % (state.PLEX_USER_ID,
self.plex_client_Id, state.PLEX_TOKEN)) self.plex_client_Id, state.PLEX_TOKEN))
sslopt = {} sslopt = {}
log.debug("Uri: %s, sslopt: %s" % (uri, sslopt)) log.debug("%s: Uri: %s, sslopt: %s"
% (self.__class__.__name__, uri, sslopt))
return uri, sslopt return uri, sslopt
def process(self, opcode, message): def process(self, opcode, message):
if opcode not in self.opcode_data: if opcode not in self.opcode_data:
return False return False
log.debug('Received the following message from Alexa:') log.debug('%s: Received the following message from Alexa:'
log.debug(message) % self.__class__.__name__)
log.debug('%s: %s' % (self.__class__.__name__, message))
try: try:
message = etree.fromstring(message) message = etree.fromstring(message)
except Exception as ex: except Exception as ex:
log.error('Error decoding message from Alexa: %s' % ex) log.error('%s: Error decoding message from Alexa: %s'
% (self.__class__.__name__, ex))
return False return False
try: try:
if message.attrib['command'] == 'processRemoteControlCommand': if message.attrib['command'] == 'processRemoteControlCommand':
message = message[0] message = message[0]
else: else:
log.error('Unknown Alexa message received') log.error('%s: Unknown Alexa message received'
% self.__class__.__name__)
return False return False
except: except:
log.error('Could not parse Alexa message') log.error('%s: Could not parse Alexa message'
% self.__class__.__name__)
return False return False
process_command(message.attrib['path'][1:], process_command(message.attrib['path'][1:],
message.attrib, message.attrib,