Skip to content

Commit a4101ac

Browse files
authored
Merge pull request #66 from bablokb/fix-conntype
Fix conntype
2 parents 42644e6 + 861cbf3 commit a4101ac

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

adafruit_espatcontrol/adafruit_espatcontrol.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def cipmux(self) -> int:
180180
return int(reply[8:])
181181
raise RuntimeError("Bad response to CIPMUX?")
182182

183-
def socket_connect(
183+
def socket_connect( # pylint: disable=too-many-branches
184184
self,
185185
conntype: str,
186186
remote: str,
@@ -191,7 +191,31 @@ def socket_connect(
191191
) -> bool:
192192
"""Open a socket. conntype can be TYPE_TCP, TYPE_UDP, or TYPE_SSL. Remote
193193
can be an IP address or DNS (we'll do the lookup for you. Remote port
194-
is integer port on other side. We can't set the local port"""
194+
is integer port on other side. We can't set the local port.
195+
196+
Note that this method is usually called by the requests-package, which
197+
does not know anything about conntype. So it is mandatory to set
198+
the conntype manually before calling this method if the conntype-parameter
199+
is not provided.
200+
201+
If requests are done using ESPAT_WiFiManager, the conntype is set there
202+
depending on the protocol (http/https)."""
203+
204+
# if caller does not provide conntype, use default conntype from
205+
# object if set, otherwise fall back to old buggy logic
206+
if not conntype and self._conntype:
207+
conntype = self._conntype
208+
elif not conntype:
209+
# old buggy code from espatcontrol_socket
210+
# added here for compatibility with old code
211+
if remote_port == 80:
212+
conntype = self.TYPE_TCP
213+
elif remote_port == 443:
214+
conntype = self.TYPE_SSL
215+
# to cater for MQTT over TCP
216+
elif remote_port == 1883:
217+
conntype = self.TYPE_TCP
218+
195219
# lets just do one connection at a time for now
196220
if conntype == self.TYPE_UDP:
197221
# always disconnect for TYPE_UDP
@@ -219,7 +243,7 @@ def socket_connect(
219243
replies = self.at_response(cmd, timeout=10, retries=retries).split(b"\r\n")
220244
for reply in replies:
221245
if reply == b"CONNECT" and (
222-
conntype == self.TYPE_TCP
246+
conntype in (self.TYPE_TCP, self.TYPE_SSL)
223247
and self.status == self.STATUS_SOCKETOPEN
224248
or conntype == self.TYPE_UDP
225249
):
@@ -415,6 +439,16 @@ def mode(self, mode: int) -> None:
415439
raise RuntimeError("Invalid Mode")
416440
self.at_response("AT+CWMODE=%d" % mode, timeout=3)
417441

442+
@property
443+
def conntype(self) -> Union[str, None]:
444+
"""The configured connection-type"""
445+
return self._conntype
446+
447+
@conntype.setter
448+
def conntype(self, conntype: str) -> None:
449+
"""set connection-type for subsequent socket_connect()"""
450+
self._conntype = conntype
451+
418452
@property
419453
def local_ip(self) -> Union[str, None]:
420454
"""Our local IP address as a dotted-quad string"""

adafruit_espatcontrol/adafruit_espatcontrol_socket.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,6 @@ def connect(self, address: Tuple[str, int], conntype: Optional[str] = None) -> N
6767
is an extra that may indicate SSL or not, depending on the underlying interface"""
6868
host, port = address
6969

70-
# Determine the conntype from port if not specified.
71-
if conntype is None:
72-
if port == 80:
73-
conntype = "TCP"
74-
elif port == 443:
75-
conntype = "SSL"
76-
# to cater for MQTT over TCP
77-
elif port == 1883:
78-
conntype = "TCP"
79-
8070
if not _the_interface.socket_connect(
8171
conntype, host, port, keepalive=10, retries=3
8272
):

adafruit_espatcontrol/adafruit_espatcontrol_wifimanager.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515

1616
import adafruit_requests as requests
1717
import adafruit_espatcontrol.adafruit_espatcontrol_socket as socket
18+
from adafruit_espatcontrol.adafruit_espatcontrol import ESP_ATcontrol
1819

1920
try:
2021
from typing import Dict, Any, Optional, Union, Tuple
2122
from circuitpython_typing.led import FillBasedLED
22-
from adafruit_espatcontrol.adafruit_espatcontrol import ESP_ATcontrol
2323
except ImportError:
2424
pass
2525

@@ -74,6 +74,14 @@ def connect(self, timeout: int = 15, retries: int = 3) -> None:
7474
print("Failed to connect\n", error)
7575
raise
7676

77+
def set_conntype(self, url: str) -> None:
78+
"""set the connection-type according to protocol"""
79+
self._esp.conntype = (
80+
ESP_ATcontrol.TYPE_SSL
81+
if url.startswith("https")
82+
else ESP_ATcontrol.TYPE_TCP
83+
)
84+
7785
def get(self, url: str, **kw: Any) -> requests.Response:
7886
"""
7987
Pass the Get request to requests and update Status NeoPixel
@@ -89,6 +97,7 @@ def get(self, url: str, **kw: Any) -> requests.Response:
8997
if not self._esp.is_connected:
9098
self.connect()
9199
self.pixel_status((0, 0, 100))
100+
self.set_conntype(url)
92101
return_val = requests.get(url, **kw)
93102
self.pixel_status(0)
94103
return return_val
@@ -108,6 +117,7 @@ def post(self, url: str, **kw: Any) -> requests.Response:
108117
if not self._esp.is_connected:
109118
self.connect()
110119
self.pixel_status((0, 0, 100))
120+
self.set_conntype(url)
111121
return_val = requests.post(url, **kw)
112122
return return_val
113123

@@ -126,6 +136,7 @@ def put(self, url: str, **kw: Any) -> requests.Response:
126136
if not self._esp.is_connected:
127137
self.connect()
128138
self.pixel_status((0, 0, 100))
139+
self.set_conntype(url)
129140
return_val = requests.put(url, **kw)
130141
self.pixel_status(0)
131142
return return_val
@@ -145,6 +156,7 @@ def patch(self, url: str, **kw: Any) -> requests.Response:
145156
if not self._esp.is_connected:
146157
self.connect()
147158
self.pixel_status((0, 0, 100))
159+
self.set_conntype(url)
148160
return_val = requests.patch(url, **kw)
149161
self.pixel_status(0)
150162
return return_val
@@ -164,6 +176,7 @@ def delete(self, url: str, **kw: Any) -> requests.Response:
164176
if not self._esp.is_connected:
165177
self.connect()
166178
self.pixel_status((0, 0, 100))
179+
self.set_conntype(url)
167180
return_val = requests.delete(url, **kw)
168181
self.pixel_status(0)
169182
return return_val

0 commit comments

Comments
 (0)