add reconnect feature
This commit is contained in:
parent
279caf49b5
commit
adeabcae00
|
@ -35,6 +35,10 @@ class Controller:
|
|||
displayL = None
|
||||
displayR = None
|
||||
|
||||
basetopic = None
|
||||
mqtthost = None
|
||||
mqttport = None
|
||||
|
||||
|
||||
def __init__(self, base_topic, mqtt_host, mqtt_port = 1883):
|
||||
|
||||
|
@ -73,18 +77,28 @@ class Controller:
|
|||
self.displayL.brightness = 1.0
|
||||
self.displayR.brightness = 1.0
|
||||
|
||||
self.basetopic = basetopic
|
||||
self.mqtthost = mqtt_host
|
||||
self.mqttport = mqtt_port
|
||||
|
||||
connectToMQTT()
|
||||
|
||||
def connectToMQTT(self):
|
||||
print("trying to connect to mqtt")
|
||||
self.mqtt = mqtt.Client()
|
||||
self.mqtt.on_connect = self.on_connect
|
||||
self.mqtt.on_message = self.on_message
|
||||
self.mqtt.on_disconnect = self.on_disconnect
|
||||
self.mqtt.connect(mqtt_host, mqtt_port)
|
||||
self.topic = base_topic
|
||||
self.mqtt.connect(self.mqtthost, self.mqttport)
|
||||
self.topic = self.basetopic
|
||||
|
||||
|
||||
|
||||
|
||||
def on_disconnect(self, client, userdata, rc):
|
||||
print("MQTT disconnected")
|
||||
self.mode = Mode.STATIC
|
||||
self.text = "- - - -"
|
||||
self.connected = False
|
||||
|
||||
def on_message(self, client, userdata, message):
|
||||
|
@ -162,6 +176,10 @@ class Controller:
|
|||
self.mqtt.publish(self.topic + "/" + "$hostname", socket.gethostname() , 1 )
|
||||
self.mqtt.publish(self.topic + "/" + "$localip", self.get_ip() , 1 )
|
||||
|
||||
#clear display
|
||||
self.mode = Mode.STATIC
|
||||
self.text = ""
|
||||
|
||||
|
||||
def get_ip(self):
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
@ -242,9 +260,15 @@ class Controller:
|
|||
run = True
|
||||
|
||||
while run:
|
||||
|
||||
if !self.connected:
|
||||
if (time.time()>self.last_reconnectTry+30):
|
||||
connectToMQTT()
|
||||
self.last_reconnectTry=time.time()
|
||||
self.mqtt.loop(0.1) #with block timeout
|
||||
|
||||
|
||||
|
||||
if self.displayL is not None and self.displayR is not None: #displays initialized
|
||||
|
||||
if self.mode == Mode.STATIC or self.mode == Mode.ONCE:
|
||||
|
|
Loading…
Reference in New Issue