Skip to content

Commit 3b06a6a

Browse files
committed
Merge branch 'main' into enterprise
2 parents 3934aac + a4101ac commit 3b06a6a

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

adafruit_espatcontrol/adafruit_espatcontrol.py

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

257-
def socket_connect(
257+
def socket_connect( # pylint: disable=too-many-branches
258258
self,
259259
conntype: str,
260260
remote: str,
@@ -265,7 +265,31 @@ def socket_connect(
265265
) -> bool:
266266
"""Open a socket. conntype can be TYPE_TCP, TYPE_UDP, or TYPE_SSL. Remote
267267
can be an IP address or DNS (we'll do the lookup for you. Remote port
268-
is integer port on other side. We can't set the local port"""
268+
is integer port on other side. We can't set the local port.
269+
270+
Note that this method is usually called by the requests-package, which
271+
does not know anything about conntype. So it is mandatory to set
272+
the conntype manually before calling this method if the conntype-parameter
273+
is not provided.
274+
275+
If requests are done using ESPAT_WiFiManager, the conntype is set there
276+
depending on the protocol (http/https)."""
277+
278+
# if caller does not provide conntype, use default conntype from
279+
# object if set, otherwise fall back to old buggy logic
280+
if not conntype and self._conntype:
281+
conntype = self._conntype
282+
elif not conntype:
283+
# old buggy code from espatcontrol_socket
284+
# added here for compatibility with old code
285+
if remote_port == 80:
286+
conntype = self.TYPE_TCP
287+
elif remote_port == 443:
288+
conntype = self.TYPE_SSL
289+
# to cater for MQTT over TCP
290+
elif remote_port == 1883:
291+
conntype = self.TYPE_TCP
292+
269293
# lets just do one connection at a time for now
270294
if conntype == self.TYPE_UDP:
271295
# always disconnect for TYPE_UDP
@@ -295,7 +319,7 @@ def socket_connect(
295319
replies = self.at_response(cmd, timeout=10, retries=retries).split(b"\r\n")
296320
for reply in replies:
297321
if reply == b"CONNECT" and (
298-
(conntype == self.TYPE_TCP or conntype == self.TYPE_SSL)
322+
conntype in (self.TYPE_TCP, self.TYPE_SSL)
299323
and self.status == self.STATUS_SOCKETOPEN
300324
or conntype == self.TYPE_UDP
301325
):
@@ -626,6 +650,16 @@ def mode(self, mode: int) -> None:
626650
raise RuntimeError("Invalid Mode")
627651
self.at_response("AT+CWMODE=%d" % mode, timeout=3)
628652

653+
@property
654+
def conntype(self) -> Union[str, None]:
655+
"""The configured connection-type"""
656+
return self._conntype
657+
658+
@conntype.setter
659+
def conntype(self, conntype: str) -> None:
660+
"""set connection-type for subsequent socket_connect()"""
661+
self._conntype = conntype
662+
629663
@property
630664
def local_ip(self) -> Union[str, None]:
631665
"""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 & 6 deletions
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

@@ -87,6 +87,14 @@ def connect(self, timeout: int = 15, retries: int = 3) -> None:
8787
print("Failed to connect\n", error)
8888
raise
8989

90+
def set_conntype(self, url: str) -> None:
91+
"""set the connection-type according to protocol"""
92+
self._esp.conntype = (
93+
ESP_ATcontrol.TYPE_SSL
94+
if url.startswith("https")
95+
else ESP_ATcontrol.TYPE_TCP
96+
)
97+
9098
def disconnect(self) -> None:
9199
"""
92100
Disconnect the Wifi from the AP if any
@@ -108,7 +116,7 @@ def get(self, url: str, **kw: Any) -> requests.Response:
108116
if not self._esp.is_connected:
109117
self.connect()
110118
self.pixel_status((0, 0, 100))
111-
requests.set_socket(socket, self._esp)
119+
self.set_conntype(url)
112120
return_val = requests.get(url, **kw)
113121
self.pixel_status(0)
114122
return return_val
@@ -132,7 +140,7 @@ def post(self, url: str, **kw: Any) -> requests.Response:
132140
print("post(): not connected, trying to connect")
133141
self.connect()
134142
self.pixel_status((0, 0, 100))
135-
requests.set_socket(socket, self._esp)
143+
self.set_conntype(url)
136144
return_val = requests.post(url, **kw)
137145
self.pixel_status(0)
138146

@@ -153,7 +161,7 @@ def put(self, url: str, **kw: Any) -> requests.Response:
153161
if not self._esp.is_connected:
154162
self.connect()
155163
self.pixel_status((0, 0, 100))
156-
requests.set_socket(socket, self._esp)
164+
self.set_conntype(url)
157165
return_val = requests.put(url, **kw)
158166
self.pixel_status(0)
159167
return return_val
@@ -173,7 +181,7 @@ def patch(self, url: str, **kw: Any) -> requests.Response:
173181
if not self._esp.is_connected:
174182
self.connect()
175183
self.pixel_status((0, 0, 100))
176-
requests.set_socket(socket, self._esp)
184+
self.set_conntype(url)
177185
return_val = requests.patch(url, **kw)
178186
self.pixel_status(0)
179187
return return_val
@@ -193,7 +201,7 @@ def delete(self, url: str, **kw: Any) -> requests.Response:
193201
if not self._esp.is_connected:
194202
self.connect()
195203
self.pixel_status((0, 0, 100))
196-
requests.set_socket(socket, self._esp)
204+
self.set_conntype(url)
197205
return_val = requests.delete(url, **kw)
198206
self.pixel_status(0)
199207
return return_val

0 commit comments

Comments
 (0)