Skip to content

Commit

Permalink
improve login() and login retry
Browse files Browse the repository at this point in the history
  • Loading branch information
ksanislo committed Dec 23, 2018
1 parent a72ed0f commit ca188c6
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions tcc-exporter
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import urllib.request
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.error import HTTPError

VERSION = '0.8.3'
VERSION = '0.8.4'
PREFIX = 'https://mytotalconnectcomfort.com/'
devices = list()

Expand Down Expand Up @@ -54,6 +54,7 @@ class Client(object):
if '/portal/Account/LogOff' in decoded:
log('INFO', 'TCC portal login successful.')
self._backoff = 0
return True
else:
log('WARNING', 'TCC portal login failed.')
if (self._backoff < self._min_backoff):
Expand All @@ -63,10 +64,13 @@ class Client(object):
else:
self._backoff = self._backoff * 2
log('INFO', 'Set login retry delay to {0} seconds.'.format(self._backoff))
return False
except Exception as e:
log('ERROR', '{0} {1}: {2!r}'.format(inspect.currentframe().f_code.co_name, type(e).__name__, e.args))
return False
else:
log('INFO', 'Login retry delayed another {0} seconds.'.format(int(self._last_login + self._backoff - utc_seconds)))
return False

def _request(self, path, data={}, headers={}):
if isinstance(data, str):
Expand Down Expand Up @@ -96,14 +100,15 @@ class Client(object):
def _request_data(self, path, data={}, headers={}):
data = self._request(path, data, headers)
if isinstance(data, int) and (data == 401): # Login failure, lets try to login again.
log('INFO', 'Retrying Client.login()')
self.login()
data = self._request(path, data, headers)
if self.login():
data = self._request(path, data, headers)
if isinstance(data, int):
return data # Another failure, just return the code.
reader = codecs.getreader(data.headers.get_content_charset())
try:
return json.load(reader(data))
retval = json.load(reader(data))
self._backoff = 0 # We got decodable JSON, reset the backoff.
return retval
except json.JSONDecodeError as e:
log('INFO', 'TCC portal returned invalid data.')
return 502
Expand Down

0 comments on commit ca188c6

Please sign in to comment.