예제 #1
0
            cert_regs = ssl.CERT_NONE
        else:
            cert_regs = ssl.CERT_REQUIRED

        mqc.tls_set(ca_certs=args.mqtt_cacerts,
                    certfile=None,
                    keyfile=None,
                    cert_reqs=cert_regs,
                    tls_version=tls_version)

        if args.mqtt_insecure:
            mqc.tls_insecure_set(True)

#Setup HomeAssistant
    if (addToHass):
        adder = addToHomeAssistant.HassConnector(mqc, globaltopic, verbosity)
        adder.addAll(referenceList)

#Main Loop
modbus_connected = False
while control.runLoop:
    if not modbus_connected:
        print("Connecting to MODBUS...")
        modbus_connected = master.connect()
        if modbus_connected:
            if verbosity >= 2:
                print("MODBUS connected successfully")
        else:
            if verbosity >= 1:
                print("MODBUS connection error, trying again...")
        p.mqtt_client.tls_set(ca_certs=args.mqtt_cacerts, certfile= None, keyfile=None, cert_reqs=cert_regs, tls_version=tls_version)

        if args.mqtt_insecure:
            p.mqtt_client.tls_insecure_set(True)

    # attempt initial connection
    if not p.mqtt_client.initial_connection_attempted:
       try:
            print("Connecting to MQTT Broker: " + args.mqtt_host + ":" + str(mqtt_port) + "...")
            p.mqtt_client.connect(args.mqtt_host, mqtt_port, 60)
            p.mqtt_client.initial_connection_attempted = True #Once we have connected the mqc loop will take care of reconnections.
            p.mqtt_client.loop_start()
            #Setup HomeAssistant
            if(addToHass):
                adder=addToHomeAssistant.HassConnector(p.mqtt_client,globaltopic,verbosity>=1)
                adder.addAll(referenceList)
            if verbosity >= 1:
                print("MQTT Loop started")
       except:
            if verbosity>=1:
              print("Socket Error connecting to MQTT broker: " + args.mqtt_host + ":" + str(mqtt_port) + ", check LAN/Internet connection, trying again...")

    #mqc=mqtt.Client(client_id=clientid)
    # mqc.on_connect=connecthandler
    # mqc.on_message=messagehandler
    # mqc.on_disconnect=disconnecthandler
    # mqc.on_log= loghandler
    # mqc.will_set(globaltopic+"connected","False",qos=2,retain=True)
    # mqc.initial_connection_attempted = False
    # mqc.initial_connection_made = False