Skip to content

Commit 2b52e34

Browse files
committed
add setter for conntype-attribute
1 parent c7b9ae9 commit 2b52e34

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
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 == self.TYPE_TCP or conntype == 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"""

0 commit comments

Comments
 (0)