Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
- added logging
Browse files Browse the repository at this point in the history
- added timeout handler for connection to TV, to fix error exceptions from OS when TV is off
- updated the example code to show errors
  • Loading branch information
hmn committed Apr 15, 2017
1 parent 193529e commit d50eb36
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
16 changes: 12 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from pylgtv import WebOsClient

webos_client = WebOsClient('192.168.0.112')
#webos_client.launch_app('netflix')
import sys
import logging

for app in webos_client.get_apps():
print(app)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)

try:
webos_client = WebOsClient('192.168.0.112')
#webos_client.launch_app('netflix')

for app in webos_client.get_apps():
print(app)
except:
print("Error connecting to TV")
35 changes: 30 additions & 5 deletions pylgtv/webos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import json
import os
import websockets
import logging

logger = logging.getLogger(__name__)

from .endpoints import *

Expand All @@ -28,6 +31,7 @@ def __init__(self, ip, key_file_path=None):
self.web_socket = None
self.command_count = 0
self.last_response = None
self.timeout_connect = 2

self.load_key_file()

Expand All @@ -49,12 +53,15 @@ def load_key_file(self):
key_file_path = self._get_key_file_path()
key_dict = {}

logger.debug('load keyfile from %s', key_file_path);

if os.path.isfile(key_file_path):
with open(key_file_path, 'r') as f:
raw_data = f.read()
if raw_data:
key_dict = json.loads(raw_data)

logger.debug('getting client_key for %s from %s', self.ip, key_file_path);
if self.ip in key_dict:
self.client_key = key_dict[self.ip]

Expand All @@ -68,6 +75,8 @@ def save_key_file(self):
else:
key_file_path = self._get_key_file_path()

logger.debug('save keyfile to %s', key_file_path);

with open(key_file_path, 'w+') as f:
raw_data = f.read()
key_dict = {}
Expand Down Expand Up @@ -109,8 +118,16 @@ def is_registered(self):
@asyncio.coroutine
def _register(self):
"""Register wrapper."""
websocket = yield from websockets.connect(
"ws://{}:{}".format(self.ip, self.port))
logger.debug('register on %s', "ws://{}:{}".format(self.ip, self.port));
try:
websocket = yield from websockets.connect(
"ws://{}:{}".format(self.ip, self.port), timeout=self.timeout_connect)

except:
logger.error('register failed to connect to %s', "ws://{}:{}".format(self.ip, self.port));
return False

logger.info('register websocket connected to %s', "ws://{}:{}".format(self.ip, self.port));

try:
yield from self._send_register_payload(websocket)
Expand All @@ -127,8 +144,16 @@ def register(self):
@asyncio.coroutine
def _command(self, msg):
"""Send a command to the tv."""
websocket = yield from websockets.connect(
"ws://{}:{}".format(self.ip, self.port))
logger.debug('send command to %s', "ws://{}:{}".format(self.ip, self.port));
try:
websocket = yield from websockets.connect(
"ws://{}:{}".format(self.ip, self.port), timeout=self.timeout_connect)

except:
logger.error('command failed to connect to %s', "ws://{}:{}".format(self.ip, self.port));
return False

logger.info('command websocket connected to %s', "ws://{}:{}".format(self.ip, self.port));

try:
yield from self._send_register_payload(websocket)
Expand Down Expand Up @@ -162,7 +187,7 @@ def command(self, request_type, uri, payload):
self.last_response = None
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._command(message))
loop.run_until_complete(asyncio.wait_for(self._command(message), self.timeout_connect))

def request(self, uri, payload=None):
"""Send a request."""
Expand Down

0 comments on commit d50eb36

Please sign in to comment.