Skip to content

Commit 54a5559

Browse files
Paul JenningsPaul Jennings
authored andcommitted
Added randomized backoff/retry scheme to try to help with failed requests, since the CT50 doesn't seem to handle requests in quick succession very well.
Also set the default API to CT50 for now, since it is the only one at the moment and it saves a request to not check the thermostat version.
1 parent db0090d commit 54a5559

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

TStat.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import httplib
5050
import urllib
5151
import logging
52+
import random
53+
import time
5254

5355
# For Python < 2.6, this json module:
5456
# http://pypi.python.org/pypi/python-json
@@ -86,6 +88,7 @@ def __init__(self, address, cacheExpiry=5, api=None, logger=None, logLevel=None)
8688
if api is None:
8789
self.api = API()
8890
self.api = getAPI(self.getModel())
91+
time.sleep(2)
8992
else:
9093
self.api = api
9194

@@ -135,9 +138,15 @@ def _post(self, key, value):
135138
l.debug("Will send params: %s" % params)
136139

137140
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
138-
conn = self._getConn()
139-
conn.request("POST", location, params, headers)
140-
response = conn.getresponse()
141+
response = None
142+
count = 0
143+
while response is None and count < 5:
144+
conn = self._getConn()
145+
conn.request("POST", location, params, headers)
146+
response = conn.getresponse()
147+
if response is None:
148+
time.sleep(count*random.randint(0, 3))
149+
count = count + 1
141150
if response.status != 200:
142151
l.error("Error %s while trying to set '%s' with '%s'" % (response.status, location, params))
143152
continue
@@ -199,9 +208,15 @@ def _get(self, key, raw=False):
199208
else:
200209
for getter in entry.getters:
201210
# Either data was not cached or cache was expired
202-
conn = self._getConn()
203-
conn.request("GET", getter[0])
204-
response = conn.getresponse()
211+
response = None
212+
count = 0
213+
while response is None and count < 5:
214+
conn = self._getConn()
215+
conn.request("GET", getter[0])
216+
response = conn.getresponse()
217+
if response is None:
218+
time.sleep(count*random.randint(0, 10))
219+
count = count + 1
205220
if response.status != 200:
206221
l.warning("Request for '%s' failed (error %s)" % (getter[0], response.status))
207222
response = None
@@ -357,7 +372,7 @@ def setCloudMode(self, value):
357372
def main():
358373
import sys
359374
addr = sys.argv[1]
360-
t = TStat(addr)
375+
t = TStat(addr, api=API_CT50v109())
361376
for cmd in sys.argv[2:]:
362377
result = eval("t.%s(raw=True)" % cmd)
363378
#print "%s: %s" % (cmd, result)

0 commit comments

Comments
 (0)