Skip to content

Commit

Permalink
Stability updates
Browse files Browse the repository at this point in the history
Keep alive exception handling, avoid flooding the commands queue.
  • Loading branch information
deiger authored Sep 13, 2019
1 parent e3c264e commit cc3bb94
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions hisense.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def unpad(data: bytes):
class KeepAliveThread(threading.Thread):
"""Thread to preiodically generate keep-alive requests."""

_INTERVAL = 10.0
_KEEP_ALIVE_INTERVAL = 10.0

def __init__(self):
self.run_lock = threading.Condition()
Expand Down Expand Up @@ -370,18 +370,21 @@ def run(self) -> None:
with self.run_lock:
conn = None
while True:
if not conn:
conn = HTTPConnection(_parsed_args.ip)
method = 'POST'
else:
method = 'PUT'
logging.debug('%s /local_reg.json %s', method, json.dumps(self._json))
conn.request(method, '/local_reg.json', json.dumps(self._json), self._headers)
resp = conn.getresponse()
if resp.status != 202:
logging.error('Recieved invalid response for local_reg: %r', resp)
try:
if not conn:
conn = HTTPConnection(_parsed_args.ip, timeout=1)
method = 'POST'
else:
method = 'PUT'
logging.debug('%s /local_reg.json %s', method, json.dumps(self._json))
conn.request(method, '/local_reg.json', json.dumps(self._json), self._headers)
resp = conn.getresponse()
if resp.status != 202:
logging.error('Recieved invalid response for local_reg: %r', resp)
except:
logging.exception('Failed to send local_reg keep alive to the AC.')
self._json['local_reg']['notify'] = int(
_data.commands_queue.qsize() > 0 or self.run_lock.wait(self._INTERVAL))
_data.commands_queue.qsize() > 0 or self.run_lock.wait(self._KEEP_ALIVE_INTERVAL))


class QueryStatusThread(threading.Thread):
Expand All @@ -391,14 +394,19 @@ class QueryStatusThread(threading.Thread):
to the keep alive, so this is just a belt and suspenders.
"""

_INTERVAL = 600.0
_STATUS_UPDATE_INTERVAL = 600.0
_WAIT_FOR_EMPTY_QUEUE = 10.0

def __init__(self):
self._next_command_id = 0
super(QueryStatusThread, self).__init__(name='Query Status thread')

def run(self) -> None:
while True:
# In case the AC is stuck, and not fetching commands, avoid flooding
# the queue with status updates.
while _data.commands_queue.qsize() > 10:
time.sleep(self._WAIT_FOR_EMPTY_QUEUE)
for data_field in fields(Properties):
command = {
'cmds': [{
Expand All @@ -416,7 +424,7 @@ def run(self) -> None:
if _keep_alive:
with _keep_alive.run_lock:
_keep_alive.run_lock.notify()
time.sleep(self._INTERVAL)
time.sleep(self._STATUS_UPDATE_INTERVAL)


class HTTPRequestHandler(BaseHTTPRequestHandler):
Expand Down

0 comments on commit cc3bb94

Please sign in to comment.