Skip to content

Commit

Permalink
Merge pull request #7 from elfman03/master
Browse files Browse the repository at this point in the history
add set_socketRetryLimit(integer)
  • Loading branch information
jasonacox authored Nov 24, 2020
2 parents 26191eb + 1627ab0 commit c5cb95e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Classes
set_version(version) # 3.1 [default] or 3.3
set_socketPersistent(False/True) # False [default] or True
set_socketNODELAY(False/True) # False or True [default]
set_socketRetryLimit(integer) # retry count limit [default 5]
set_dpsUsed(dpsUsed) # set data points (DPs)
set_retry(retry=True) # retry if response payload is truncated
set_status(on, switch=1) # Set status of the device to 'on' or 'off' (bool)
Expand Down
27 changes: 24 additions & 3 deletions tinytuya/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
set_version(version) # 3.1 [default] or 3.3
set_socketPersistent(False/True) # False [default] or True
set_socketNODELAY(False/True) # False or True [default]
set_socketRetryLimit(integer) # retry count limit [default 5]
set_dpsUsed(dpsUsed) # set data points (DPs)
set_retry(retry=True) # retry if response payload is truncated
set_status(on, switch=1) # Set status of the device to 'on' or 'off' (bool)
Expand Down Expand Up @@ -275,6 +276,7 @@ def __init__(self, dev_id, address, local_key="", dev_type="default", connection
self.socket = None
self.socketPersistent = False
self.socketNODELAY = True
self.socketRetryLimit = 5

def __del__(self):
# In case we have a lingering socket connection, close it
Expand Down Expand Up @@ -304,6 +306,7 @@ def _send_receive(self, payload):
payload(bytes): Data to send.
"""
success=False
retries=0
while not success:
# make sure I have a socket (may already exist)
self._get_socket(False)
Expand All @@ -320,11 +323,26 @@ def _send_receive(self, payload):
self.socket.close()
self.socket=None
except:
#print('Exception with low level TinyTuya socket!!! will retry!!!')
log.exception('Exception with low level TinyTuya socket!!! will retry!!!')
retries=retries+1
log.debug('Exception with low level TinyTuya socket!!! retry '+str(retries)+'/'+str(self.socketRetryLimit))
#print('Exception with low level TinyTuya socket!!! retry '+str(retries)+'/'+str(self.socketRetryLimit))
#
# if we exceed the limit of retries then lets get out of here by reraising to a higher power
#
if(retries>self.socketRetryLimit):
if(self.socket!=None):
self.socket.close()
self.socket=None
log.exception('Exceeded tinytuya retry limit ('+str(self.socketRetryLimit)+')')
#
# goodbye
#
raise
# retry: wait a bit, toss old socket and get new one
time.sleep(0.1)
# toss old socket and get new one
self._get_socket(True)
# except
# while
return data

def set_version(self, version):
Expand All @@ -336,6 +354,9 @@ def set_socketPersistent(self, persist):
def set_socketNODELAY(self, nodelay):
self.socketNODELAY = nodelay

def set_socketRetryLimit(self, limit):
self.socketRetryLimit = limit

def set_dpsUsed(self, dpsUsed):
self.dpsUsed = dpsUsed

Expand Down

0 comments on commit c5cb95e

Please sign in to comment.