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

Commit

Permalink
Merge pull request #5 from hmn/master
Browse files Browse the repository at this point in the history
Added timeout handling
  • Loading branch information
TheRealLink authored Apr 19, 2017
2 parents 193529e + 381fa92 commit 60ae0d2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 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")
37 changes: 31 additions & 6 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 @@ -19,7 +22,7 @@ def __init__(self, id, message):


class WebOsClient(object):
def __init__(self, ip, key_file_path=None):
def __init__(self, ip, key_file_path=None, timeout_connect=2):
"""Initialize the client."""
self.ip = ip
self.port = 3000
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 = timeout_connect

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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup

setup(name='pylgtv',
version='0.1.4',
version='0.1.5',
description='Library to control webOS based LG Tv devices',
url='https://github.com/TheRealLink/pylgtv',
author='Dennis Karpienski',
Expand Down

0 comments on commit 60ae0d2

Please sign in to comment.