(self, config=None)
Create a new instance of the MQTTHandler class
Create a new instance of the MQTTHandler class
Create a new instance of the MQTTHandler class
def __init__(self, config=None):
        """
        Create a new instance of the MQTTHandler class
        """
        # Initialize Handler
        Handler.__init__(self, config)

        # Initialize Data
        self.mqttc = None
        self.hostname = get_hostname(self.config)
        self.client_id = "%s_%s" % (self.hostname, os.getpid())

        # Initialize Options
        self.host = self.config.get('host', 'localhost')
        self.port = 0
        self.qos = int(self.config.get('qos', 0))
        self.prefix = self.config.get('prefix', "")
        self.tls = self.config.get('tls', False)
        self.timestamp = 0
        try:
            self.timestamp = self.config['timestamp']
            if not self.timestamp:
                self.timestamp = 1
            else:
                self.timestamp = 0
        except:
            self.timestamp = 1

        if not mosquitto:
            self.log.error('mosquitto import failed. Handler disabled')
            self.enabled = False
            return

        # Initialize
        self.mqttc = mosquitto.Mosquitto(self.client_id, clean_session=True)

        if not self.tls:
            self.port = int(self.config.get('port', 1883))
        else:
            # Set up TLS if requested

            self.port = int(self.config.get('port', 8883))

            self.cafile = self.config.get('cafile', None)
            self.certfile = self.config.get('certfile', None)
            self.keyfile = self.config.get('keyfile', None)

            if None in [self.cafile, self.certfile, self.keyfile]:
                self.log.error("MQTTHandler: TLS configuration missing.")
                return

            try:
                self.mqttc.tls_set(
                    self.cafile,
                    certfile=self.certfile,
                    keyfile=self.keyfile,
                    cert_reqs=ssl.CERT_REQUIRED,
                    tls_version=3,
                    ciphers=None)
            except:
                self.log.error("MQTTHandler: Cannot set up TLS " +
                               "configuration. Files missing?")

        self.mqttc.will_set("clients/diamond/%s" % (self.hostname),
                            payload="Adios!", qos=0, retain=False)
        self.mqttc.connect(self.host, self.port, 60)

        self.mqttc.on_disconnect = self._disconnect
def __init__ ( self , config = None ) : # Initialize Handler Handler . __init__ ( self , config ) # Initialize Data self . mqttc = None self . hostname = get_hostname ( self . config ) self . client_id = "%s_%s" % ( self . hostname , os . getpid ( ) ) # Initialize Options self . host = self . config . get ( 'host' , 'localhost' ) self . port = 0 self . qos = int ( self . config . get ( 'qos' , 0 ) ) self . prefix = self . config . get ( 'prefix' , "" ) self . tls = self . config . get ( 'tls' , False ) self . timestamp = 0 try : self . timestamp = self . config [ 'timestamp' ] if not self . timestamp : self . timestamp = 1 else : self . timestamp = 0 except : self . timestamp = 1 if not mosquitto : self . log . error ( 'mosquitto import failed. Handler disabled' ) self . enabled = False return # Initialize self . mqttc = mosquitto . Mosquitto ( self . client_id , clean_session = True ) if not self . tls : self . port = int ( self . config . get ( 'port' , 1883 ) ) else : # Set up TLS if requested self . port = int ( self . config . get ( 'port' , 8883 ) ) self . cafile = self . config . get ( 'cafile' , None ) self . certfile = self . config . get ( 'certfile' , None ) self . keyfile = self . config . get ( 'keyfile' , None ) if None in [ self . cafile , self . certfile , self . keyfile ] : self . log . error ( "MQTTHandler: TLS configuration missing." ) return try : self . mqttc . tls_set ( self . cafile , certfile = self . certfile , keyfile = self . keyfile , cert_reqs = ssl . CERT_REQUIRED , tls_version = 3 , ciphers = None ) except : self . log . error ( "MQTTHandler: Cannot set up TLS " + "configuration. Files missing?" ) self . mqttc . will_set ( "clients/diamond/%s" % ( self . hostname ) , payload = "Adios!" , qos = 0 , retain = False ) self . mqttc . connect ( self . host , self . port , 60 ) self . mqttc . on_disconnect = self . _disconnect