dswd / OctoPrint-PSUControl-Tapo

Adds TPLink Smart Plug support to OctoPrint-PSUControl as a sub-plugin
GNU Affero General Public License v3.0
7 stars 1 forks source link

Decreased timeout in self.session.post() causes HTTP Timeout error #3

Closed eataiwo closed 1 year ago

eataiwo commented 2 years ago

I recently installed the PSU Control and PSU Tapo plugin. Initially, I couldn't get the plugin to work with my plug, so I copied the code onto my laptop and started playing around with the code to see any errors, as I am not sure if Octoprint logs these. On investigation, I got the error below. If I change the timeout for self.session.post() on line 55 of tapo.py back to 2 seconds rather than 0.5, the script runs fine, and I can control the smart plug. As this prevents the control of Tapo devices with no apparent error the other issue #2 where control is not working might also be solved.

`Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "", line 3, in raise_from File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 440, in _make_request httplib_response = conn.getresponse() File "/usr/lib/python3.10/http/client.py", line 1374, in getresponse response.begin() File "/usr/lib/python3.10/http/client.py", line 318, in begin version, status, reason = self._read_status() File "/usr/lib/python3.10/http/client.py", line 279, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib/python3.10/socket.py", line 705, in readinto return self._sock.recv_into(b) TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send resp = conn.urlopen( File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 755, in urlopen retries = retries.increment( File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 532, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/lib/python3/dist-packages/six.py", line 719, in reraise raise value File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 447, in _make_request self._raise_timeout(err=e, url=url, timeout_value=read_timeout) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 336, in _raise_timeout raise ReadTimeoutError( urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='192.168.68.104', port=80): Read timed out. (read timeout=0.5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 247, in device_type = smartplug.get_type() File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 165, in get_type return self._get_device_info()["model"] File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 159, in _get_device_info return self._request("get_device_info") File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 73, in _request self._initialize() File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 136, in _initialize result = self._request_raw("handshake", { File "/home/tj/Applications/PycharmProjects/farscope_group_project/psu_control.py", line 57, in _request_raw resp = self.session.post(url, json=payload, timeout=0.5) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 590, in post return self.request('POST', url, data=data, json=json, kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 542, in request resp = self.send(prep, send_kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "/usr/lib/python3/dist-packages/requests/adapters.py", line 529, in send raise ReadTimeout(e, request=request) requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.68.104', port=80): Read timed out. (read timeout=0.5)`

georgeav commented 1 year ago

Thank you ! This worked for me as well. Before:

2022-10-09 20:54:12,111 - octoprint.plugins.psucontrol - INFO - Switching PSU On
2022-10-09 20:54:12,712 - octoprint.plugins.psucontrol - ERROR - Error while executing callback <bound method PSUControl_Tapo.turn_psu_on of <octoprint_psucontrol_tapo.PSUControl_Tapo object at 0xb2242ef0
>>
...
  File "/home/pi/oprint/lib/python3.7/site-packages/requests/adapters.py", line 578, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='192.168.86.27', port=80): Read timed out. (read timeout=0.5)

After bumping the timeout back to 2s:

2022-10-09 21:04:45,756 - octoprint.plugins.psucontrol - INFO - Switching PSU On
2022-10-09 21:04:51,276 - octoprint.plugins.psucontrol - INFO - Switching PSU Off
2022-10-09 21:04:51,465 - octoprint.util.comm - INFO - Changing monitoring state from "Operational" to "Offline"
dswd commented 1 year ago

I reverted the timeout