@@ -224,23 +224,20 @@ def connect(self, clean_session=True):
224
224
self .broker , port = self .broker .split (":" , 1 )
225
225
port = int (port )
226
226
227
+ addr = _the_sock .getaddrinfo (self .broker , self .port )[0 ]
228
+ self ._sock = _the_sock .socket (addr [0 ], _the_sock .SOCK_STREAM , addr [2 ])
229
+ self ._sock .settimeout (15 )
227
230
if self .port == 8883 :
228
231
try :
229
232
if self .logger is not None :
230
233
self .logger .debug ('Attempting to establish secure MQTT connection...' )
231
- self ._sock .connect (( self . broker , self . port ) , TLS_MODE )
232
- except RuntimeError :
233
- raise MMQTTException ("Invalid broker address defined." )
234
+ self ._sock .connect (addr [ - 1 ] , TLS_MODE )
235
+ except RuntimeError as e :
236
+ raise MMQTTException ("Invalid broker address defined." , e )
234
237
else :
235
- if isinstance (self .broker , str ):
236
- addr = _the_sock .getaddrinfo (self .broker , self .port )[0 ]
237
- else :
238
- addr = (self .broker , 0x21 , self .port )
239
238
try :
240
239
if self .logger is not None :
241
240
self .logger .debug ('Attempting to establish insecure MQTT connection...' )
242
- self ._sock = _the_sock .socket (addr [0 ], 0x21 , addr [2 ])
243
- self ._sock .settimeout (15 )
244
241
self ._sock .connect (addr [- 1 ], TCP_MODE )
245
242
except RuntimeError as e :
246
243
raise MMQTTException ("Invalid broker address defined." , e )
@@ -571,49 +568,6 @@ def unsubscribe(self, topic):
571
568
self ._subscribed_topics .remove (t )
572
569
return
573
570
574
- @property
575
- def is_wifi_connected (self ):
576
- """Returns if the ESP module is connected to
577
- an access point, resets module if False"""
578
- if self ._wifi :
579
- return self ._wifi .esp .is_connected
580
- raise MMQTTException ("MiniMQTT Client does not use a WiFi NetworkManager." )
581
-
582
- # pylint: disable=line-too-long, protected-access
583
- @property
584
- def is_sock_connected (self ):
585
- """Returns if the socket is connected."""
586
- return self .is_wifi_connected and self ._sock and self ._wifi .esp .socket_connected (self ._sock ._socknum )
587
-
588
- def reconnect_socket (self ):
589
- """Re-establishes the socket's connection with the MQTT broker.
590
- """
591
- try :
592
- if self .logger is not None :
593
- self .logger .debug ("Attempting to reconnect with MQTT Broker..." )
594
- self .reconnect ()
595
- except RuntimeError as err :
596
- if self .logger is not None :
597
- self .logger .debug ('Failed to reconnect with MQTT Broker, retrying...' , err )
598
- time .sleep (1 )
599
- self .reconnect_socket ()
600
-
601
- def reconnect_wifi (self ):
602
- """Reconnects to WiFi Access Point and socket, if disconnected.
603
- """
604
- while not self .is_wifi_connected :
605
- try :
606
- if self .logger is not None :
607
- self .logger .debug ('Connecting to WiFi AP...' )
608
- self ._wifi .connect ()
609
- except (RuntimeError , ValueError ):
610
- if self .logger is not None :
611
- self .logger .debug ('Failed to reset WiFi module, retrying...' )
612
- time .sleep (1 )
613
- # we just reconnected, is the socket still connected?
614
- if not self .is_sock_connected :
615
- self .reconnect_socket ()
616
-
617
571
def reconnect (self , resub_topics = True ):
618
572
"""Attempts to reconnect to the MQTT broker.
619
573
:param bool resub_topics: Resubscribe to previously subscribed topics.
@@ -634,27 +588,19 @@ def loop_forever(self):
634
588
"""Starts a blocking message loop. Use this
635
589
method if you want to run a program forever.
636
590
Code below a call to this method will NOT execute.
637
- Network reconnection is handled within this call.
591
+
592
+ NOTE: Network reconnection is not handled within this call and
593
+ must be handled by your code for each interface.
638
594
639
595
"""
640
596
while True :
641
- # Check WiFi and socket status
642
- if self .is_sock_connected :
643
- try :
644
- self .loop ()
645
- except (RuntimeError , ValueError ):
646
- if self ._wifi :
647
- # Reconnect the WiFi module and the socket
648
- self .reconnect_wifi ()
649
- continue
597
+ if self ._sock .connected :
598
+ self .loop ()
650
599
651
600
def loop (self ):
652
601
"""Non-blocking message loop. Use this method to
653
602
check incoming subscription messages.
654
603
655
- This method does NOT handle networking or
656
- network hardware management, use loop_forever
657
- or handle in code instead.
658
604
"""
659
605
if self ._timestamp == 0 :
660
606
self ._timestamp = time .monotonic ()
@@ -674,7 +620,6 @@ def _wait_for_msg(self, timeout=30):
674
620
"""
675
621
res = self ._sock .recv (1 )
676
622
self ._sock .settimeout (timeout )
677
- print ("RES: " , res )
678
623
if res in [None , b"" ]:
679
624
return None
680
625
if res == MQTT_PINGRESP :
0 commit comments