Beispiel #1
0
    def connect(self):
        """
        Connect the client to IBM Watson IoT Platform using the underlying Paho MQTT client
        
        # Raises
        ConnectionException: If there is a problem establishing the connection.
        """
        self.logger.debug(
            "Connecting... (address = %s, port = %s, clientId = %s, username = %s)"
            % (self.address, self.port, self.clientId, self.username))
        try:
            self.connectEvent.clear()
            self.logger.debug(
                "Connecting with clientId %s to host %s on port %s with keepAlive set to %s"
                % (self.clientId, self.address, self.port, self.keepAlive))
            self.client.connect(self.address,
                                port=self.port,
                                keepalive=self.keepAlive)
            self.client.loop_start()
            if not self.connectEvent.wait(timeout=60):
                self.client.loop_stop()
                self._logAndRaiseException(
                    ConnectionException(
                        "Operation timed out connecting to IBM Watson IoT Platform: %s"
                        % (self.address)))

        except socket.error as serr:
            self.client.loop_stop()
            self._logAndRaiseException(
                ConnectionException(
                    "Failed to connect to IBM Watson IoT Platform: %s - %s" %
                    (self.address, str(serr))))
Beispiel #2
0
    def _onConnect(self, mqttc, userdata, flags, rc):
        """
        Called when the broker responds to our connection request.

        The value of rc determines success or not:
            0: Connection successful
            1: Connection refused - incorrect protocol version
            2: Connection refused - invalid client identifier
            3: Connection refused - server unavailable
            4: Connection refused - bad username or password
            5: Connection refused - not authorised
            6-255: Currently unused.
        """
        if rc == 0:
            self.connectEvent.set()
            self.logger.info("Connected successfully: %s" % (self.clientId))

            # Restoring previous subscriptions
            with self._subLock:
                if len(self._subscriptions) > 0:
                    for subscription in self._subscriptions:
                        # We use the underlying mqttclient subscribe method rather than _subscribe because we are
                        # claiming a lock on the subscriptions list and do not want anything else to modify it,
                        # which that method does
                        (result, mid) = self.client.subscribe(
                            subscription,
                            qos=self._subscriptions[subscription])
                        if result != paho.MQTT_ERR_SUCCESS:
                            self._logAndRaiseException(
                                ConnectionException(
                                    "Unable to subscribe to %s" %
                                    subscription))
                    self.logger.debug("Restored %s previous subscriptions" %
                                      len(self._subscriptions))
        elif rc == 1:
            self._logAndRaiseException(
                ConnectionException("Incorrect protocol version"))
        elif rc == 2:
            self._logAndRaiseException(
                ConnectionException("Invalid client identifier"))
        elif rc == 3:
            self._logAndRaiseException(
                ConnectionException("Server unavailable"))
        elif rc == 4:
            self._logAndRaiseException(
                ConnectionException("Bad username or password: (%s, %s)" %
                                    (self.username, self.password)))
        elif rc == 5:
            self._logAndRaiseException(
                ConnectionException(
                    "Not authorized: s (%s, %s, %s)" %
                    (self.clientId, self.username, self.password)))
        else:
            self._logAndRaiseException(
                ConnectionException("Unexpected connection failure: %s" %
                                    (rc)))