Skip to content

Commit bad50b9

Browse files
author
brentru
committed
remove dep for wifi, breaks initialization signature
1 parent 08df32e commit bad50b9

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

adafruit_minimqtt.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,33 @@
7373
const(0x04) : 'Connection Refused - Incorrect username/password',
7474
const(0x05) : 'Connection Refused - Unauthorized'}
7575

76+
_the_interface = None # pylint: disable=invalid-name
77+
_the_sock = None # pylint: disable=invalid-name
78+
7679
class MMQTTException(Exception):
7780
"""MiniMQTT Exception class."""
7881
# pylint: disable=unnecessary-pass
7982
#pass
8083

84+
def set_socket(sock, iface=None):
85+
"""Helper to set the global socket and optionally set the global network interface.
86+
:param sock: socket object.
87+
:param iface: internet interface object
88+
89+
"""
90+
global _the_sock # pylint: disable=invalid-name, global-statement
91+
_the_sock = sock
92+
if iface:
93+
global _the_interface # pylint: disable=invalid-name, global-statement
94+
_the_interface = iface
95+
_the_sock.set_interface(iface)
96+
print(_the_sock)
97+
98+
def unpretty_ip(ip): # pylint: disable=no-self-use, invalid-name
99+
"""Converts a dotted-quad string to a bytearray IP address"""
100+
octets = [int(x) for x in ip.split('.')]
101+
return bytes(octets)
102+
81103
class MQTT:
82104
"""MQTT Client for CircuitPython
83105
:param socket: Socket object for provided network interface
@@ -92,19 +114,13 @@ class MQTT:
92114
:param int keep_alive: KeepAlive interval between the broker and the MiniMQTT client.
93115
"""
94116
# pylint: disable=too-many-arguments,too-many-instance-attributes, not-callable, invalid-name, no-member
95-
def __init__(self, socket, broker, port=None, username=None,
96-
password=None, network_manager=None, client_id=None,
117+
def __init__(self, broker, port=None, username=None,
118+
password=None, client_id=None,
97119
is_ssl=True, log=False, keep_alive=60):
98-
# network management
99-
self._socket = socket
100-
network_manager_type = str(type(network_manager))
101-
if 'ESPSPI_WiFiManager' in network_manager_type:
102-
self._wifi = network_manager
103-
else:
104-
raise TypeError("This library requires a NetworkManager object.")
120+
self._sock = None
105121
# broker
106122
try: # set broker IP
107-
self.broker = self._wifi.esp.unpretty_ip(broker)
123+
self.broker = unpretty_ip(broker)
108124
except ValueError: # set broker URL
109125
self.broker = broker
110126
# port/ssl
@@ -187,11 +203,10 @@ def connect(self, clean_session=True):
187203
"""Initiates connection with the MQTT Broker.
188204
:param bool clean_session: Establishes a persistent session.
189205
"""
190-
self._set_interface()
191-
if self.logger is not None:
192-
self.logger.debug('Creating new socket')
193-
self._sock = self._socket.socket()
194-
self._sock.settimeout(10)
206+
global _the_interface # pylint: disable=global-statement, invalid-name
207+
global _the_sock # pylint: disable=global-statement, invalid-name
208+
209+
195210
if self.port == 8883:
196211
try:
197212
if self.logger is not None:
@@ -201,14 +216,15 @@ def connect(self, clean_session=True):
201216
raise MMQTTException("Invalid broker address defined.")
202217
else:
203218
if isinstance(self.broker, str):
204-
addr = self._socket.getaddrinfo(self.broker, self.port)[0][-1]
219+
addr = _the_sock.getaddrinfo(self.broker, self.port)[0]
205220
else:
206221
addr = (self.broker, self.port)
207222
try:
208223
if self.logger is not None:
209224
self.logger.debug('Attempting to establish insecure MQTT connection...')
210-
#self._sock.connect((self.broker, self.port), TCP_MODE)
211-
self._sock.connect(addr, TCP_MODE)
225+
self._sock = _the_sock.socket(addr[0], 0x21, addr[2])
226+
self._sock.settimeout(15)
227+
self._sock.connect(addr[-1], TCP_MODE)
212228
except RuntimeError as e:
213229
raise MMQTTException("Invalid broker address defined.", e)
214230

@@ -641,6 +657,7 @@ def _wait_for_msg(self, timeout=30):
641657
"""
642658
res = self._sock.recv(1)
643659
self._sock.settimeout(timeout)
660+
print("RES: ", res)
644661
if res in [None, b""]:
645662
return None
646663
if res == MQTT_PINGRESP:

0 commit comments

Comments
 (0)