From ac4e43df8c7a6ec11e2fe1e1ec89368005faec81 Mon Sep 17 00:00:00 2001 From: Kong Wai Weng Date: Sat, 24 Jul 2021 22:05:47 +0800 Subject: [PATCH 1/4] Fixed a few bugs as follow: 1. In socket_receive, return once we've received all the data instead of waiting for timeout. 2. In nslookup, the returned IP address should not contain double quotes. --- adafruit_espatcontrol/adafruit_espatcontrol.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/adafruit_espatcontrol/adafruit_espatcontrol.py b/adafruit_espatcontrol/adafruit_espatcontrol.py index 7c0f7c8..42b4389 100644 --- a/adafruit_espatcontrol/adafruit_espatcontrol.py +++ b/adafruit_espatcontrol/adafruit_espatcontrol.py @@ -283,6 +283,7 @@ def socket_receive(self, timeout=5): bundle.append(self._ipdpacket[0:i]) gc.collect() i = incoming_bytes = 0 + break # We've received all the data. Don't wait until timeout. else: # no data waiting self.hw_flow(True) # start the floooow totalsize = sum([len(x) for x in bundle]) @@ -408,7 +409,7 @@ def nslookup(self, host): reply = self.at_response('AT+CIPDOMAIN="%s"' % host.strip('"'), timeout=3) for line in reply.split(b"\r\n"): if line and line.startswith(b"+CIPDOMAIN:"): - return str(line[11:], "utf-8") + return str(line[11:], "utf-8").strip('"') raise RuntimeError("Couldn't find IP address") # *************************** AP SETUP **************************** From 0fc5e2418a3b8d6a7f3bd9c6198ea6f35d5f75f3 Mon Sep 17 00:00:00 2001 From: Kong Wai Weng Date: Sat, 24 Jul 2021 22:08:59 +0800 Subject: [PATCH 2/4] 1. In connect(), we should determine the conntype base on port if it's not specified instead of leave it as None. 2. In recv(), we should get the data from socket_receive() for the first time when the buffer is empty. --- adafruit_espatcontrol/adafruit_espatcontrol_socket.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py index 8a49438..b9187df 100644 --- a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py +++ b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py @@ -47,6 +47,14 @@ def connect(self, address, conntype=None): """Connect the socket to the 'address' (which should be dotted quad IP). 'conntype' is an extra that may indicate SSL or not, depending on the underlying interface""" host, port = address + + # Determine the conntype from port if not specified. + if conntype == None: + if port == 80: + conntype = "TCP" + elif port == 443: + conntype = "SSL" + if not _the_interface.socket_connect( conntype, host, port, keepalive=10, retries=3 ): @@ -74,6 +82,8 @@ def recv(self, num=0): ret = self._buffer + _the_interface.socket_receive(timeout=self._timeout) self._buffer = b"" else: + if (self._buffer == b""): + self._buffer = self._buffer + _the_interface.socket_receive(timeout=self._timeout) ret = self._buffer[:num] self._buffer = self._buffer[num:] return ret From afdc6ae6898f5e424f4ccebcea5c59c0afcd2796 Mon Sep 17 00:00:00 2001 From: Kong Wai Weng Date: Sat, 24 Jul 2021 22:49:43 +0800 Subject: [PATCH 3/4] Use Singleton-comparison instead (conntype is None). --- adafruit_espatcontrol/adafruit_espatcontrol_socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py index b9187df..b5c36c7 100644 --- a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py +++ b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py @@ -49,7 +49,7 @@ def connect(self, address, conntype=None): host, port = address # Determine the conntype from port if not specified. - if conntype == None: + if conntype is None: if port == 80: conntype = "TCP" elif port == 443: From e1cd639812b8c41b4d74a664efb5701721fde4ad Mon Sep 17 00:00:00 2001 From: Kong Wai Weng Date: Sat, 24 Jul 2021 23:11:27 +0800 Subject: [PATCH 4/4] Reformatted by black. --- adafruit_espatcontrol/adafruit_espatcontrol_socket.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py index b5c36c7..1761191 100644 --- a/adafruit_espatcontrol/adafruit_espatcontrol_socket.py +++ b/adafruit_espatcontrol/adafruit_espatcontrol_socket.py @@ -47,14 +47,14 @@ def connect(self, address, conntype=None): """Connect the socket to the 'address' (which should be dotted quad IP). 'conntype' is an extra that may indicate SSL or not, depending on the underlying interface""" host, port = address - + # Determine the conntype from port if not specified. if conntype is None: if port == 80: conntype = "TCP" elif port == 443: conntype = "SSL" - + if not _the_interface.socket_connect( conntype, host, port, keepalive=10, retries=3 ): @@ -82,8 +82,10 @@ def recv(self, num=0): ret = self._buffer + _the_interface.socket_receive(timeout=self._timeout) self._buffer = b"" else: - if (self._buffer == b""): - self._buffer = self._buffer + _the_interface.socket_receive(timeout=self._timeout) + if self._buffer == b"": + self._buffer = self._buffer + _the_interface.socket_receive( + timeout=self._timeout + ) ret = self._buffer[:num] self._buffer = self._buffer[num:] return ret