Beispiel #1
0
    def __init__(self, options, logHandlers=None):
        self._options = options

        if self._options['org'] == None:
            raise ConfigurationException("Missing required property: org")
        if self._options['type'] == None:
            raise ConfigurationException("Missing required property: type")
        if self._options['id'] == None:
            raise ConfigurationException("Missing required property: id")

        if self._options['org'] != "quickstart":
            if self._options['auth-method'] == None:
                raise ConfigurationException(
                    "Missing required property: auth-method")

            if (self._options['auth-method'] == "token"):
                if self._options['auth-token'] == None:
                    raise ConfigurationException(
                        "Missing required property for token based authentication: auth-token"
                    )
            else:
                raise UnsupportedAuthenticationMethod(options['authMethod'])
        self._options['subscriptionList'] = {}

        AbstractClient.__init__(self,
                                organization=options['org'],
                                clientId="g:" + options['org'] + ":" +
                                options['type'] + ":" + options['id'],
                                username="******" if
                                (options['auth-method'] == "token") else None,
                                password=options['auth-token'],
                                logHandlers=logHandlers)

        # Add handler for commands if not connected to QuickStart
        if self._options['org'] != "quickstart":
            gatewayCommandTopic = "iot-2/type/" + options[
                'type'] + "/id/" + options['id'] + "/cmd/+/fmt/json"
            messageNotificationTopic = "iot-2/type/" + options[
                'type'] + "/id/" + options['id'] + "/notify"
            #localTopic = "iot-2/type/iotsample-raspberrypi2/id/89898889/cmd/greeting/fmt/json"
            self.client.message_callback_add(gatewayCommandTopic,
                                             self.__onCommand)
            self.client.message_callback_add("iot-2/type/+/id/+/cmd/+/fmt/+",
                                             self.__onDeviceCommand)
            self.client.message_callback_add(messageNotificationTopic,
                                             self.__onMessageNotification)

        self.subscriptionsAcknowledged = threading.Event()

        # Initialize user supplied callback
        self.commandCallback = None
        self.deviceCommandCallback = None
        self.notificationCallback = None
        self.client.on_connect = self.on_connect
        self.setMessageEncoderModule('json', jsonCodec)
        self.setMessageEncoderModule('json-iotf', jsonIotfCodec)
Beispiel #2
0
	def __init__(self, options, logHandlers=None):
		self._options = options

		### DEFAULTS ###
		if "domain" not in self._options:
			# Default to the domain for the public cloud offering
			self._options['domain'] = "internetofthings.ibmcloud.com"
		if "clean-session" not in self._options:
			self._options['clean-session'] = "true"

		### REQUIRED ###
		if self._options['org'] == None:
			raise ConfigurationException("Missing required property: org")
		if self._options['type'] == None:
			raise ConfigurationException("Missing required property: type")
		if self._options['id'] == None:
			raise ConfigurationException("Missing required property: id")

		if self._options['org'] != "quickstart":
			if self._options['auth-method'] == None:
				raise ConfigurationException("Missing required property: auth-method")

			if (self._options['auth-method'] == "token"):
				if self._options['auth-token'] == None:
					raise ConfigurationException("Missing required property for token based authentication: auth-token")
			else:
				raise UnsupportedAuthenticationMethod(options['authMethod'])


		AbstractClient.__init__(
			self,
			domain = self._options['domain'],
			organization = self._options['org'],
			clientId = "d:" + self._options['org'] + ":" + self._options['type'] + ":" + self._options['id'],
			username = "******" if (self._options['auth-method'] == "token") else None,
			password = self._options['auth-token'],
			logHandlers = logHandlers,
			cleanSession = self._options['clean-session']
		)

		# Add handler for commands if not connected to QuickStart
		if self._options['org'] != "quickstart":
			self.client.message_callback_add("iot-2/cmd/+/fmt/+", self.__onCommand)

		self.subscriptionsAcknowledged = threading.Event()

		# Initialize user supplied callback
		self.commandCallback = None

		self.client.on_connect = self.on_connect

		self.setMessageEncoderModule('json', jsonCodec)
		self.setMessageEncoderModule('json-iotf', jsonIotfCodec)
Beispiel #3
0
    def __init__(self, options, logHandlers=None):
        self._options = options

        if self._options['org'] == None:
            raise ConfigurationException("Missing required property: org")
        if self._options['type'] == None:
            raise ConfigurationException("Missing required property: type")
        if self._options['id'] == None:
            raise ConfigurationException("Missing required property: id")

        if self._options['org'] != "quickstart":
            if self._options['auth-method'] == None:
                raise ConfigurationException(
                    "Missing required property: auth-method")

            if (self._options['auth-method'] == "token"):
                if self._options['auth-token'] == None:
                    raise ConfigurationException(
                        "Missing required property for token based authentication: auth-token"
                    )
            else:
                raise UnsupportedAuthenticationMethod(options['authMethod'])

        # Include staging
        self._options[
            'staging'] = options['staging'] if 'staging' in options else None

        AbstractClient.__init__(self,
                                organization=options['org'],
                                clientId="d:" + options['org'] + ":" +
                                options['type'] + ":" + options['id'],
                                username="******" if
                                (options['auth-method'] == "token") else None,
                                password=options['auth-token'],
                                logHandlers=logHandlers,
                                staging=options['staging'])

        # Add handler for commands if not connected to QuickStart
        if self._options['org'] != "quickstart":
            self.client.message_callback_add("iot-2/cmd/+/fmt/+",
                                             self.__onCommand)

        self.subscriptionsAcknowledged = threading.Event()

        # Initialize user supplied callback
        self.commandCallback = None

        self.client.on_connect = self.on_connect

        self.setMessageEncoderModule('json', jsonCodec)
        self.setMessageEncoderModule('json-iotf', jsonIotfCodec)
Beispiel #4
0
    def __init__(self, options, logHandlers=None):
        self._options = options

        ### DEFAULTS ###
        if "domain" not in self._options:
            # Default to the domain for the public cloud offering
            self._options['domain'] = "internetofthings.ibmcloud.com"
        if "clean-session" not in self._options:
            self._options['clean-session'] = "true"

        ### REQUIRED ###
        if self._options['org'] == None:
            raise ConfigurationException("Missing required property: org")
        if self._options['type'] == None:
            raise ConfigurationException("Missing required property: type")
        if self._options['id'] == None:
            raise ConfigurationException("Missing required property: id")

        if self._options['org'] != "quickstart":
            if self._options['auth-method'] == None:
                raise ConfigurationException(
                    "Missing required property: auth-method")

            if (self._options['auth-method'] == "token"):
                if self._options['auth-token'] == None:
                    raise ConfigurationException(
                        "Missing required property for token based authentication: auth-token"
                    )
            else:
                raise UnsupportedAuthenticationMethod(options['authMethod'])

        HttpAbstractClient.__init__(self,
                                    clientId="d:" + self._options['org'] +
                                    ":" + self._options['type'] + ":" +
                                    self._options['id'],
                                    logHandlers=logHandlers)

        self.setMessageEncoderModule('json', jsonCodec)
        self.setMessageEncoderModule('xml', xmlCodec)
Beispiel #5
0
    def __init__(self, options, logHandlers=None):
        self._options = options

        #Defaults
        if "domain" not in self._options:
            # Default to the domain for the public cloud offering
            self._options['domain'] = "internetofthings.ibmcloud.com"

        if "org" not in self._options:
            # Default to the quickstart ode
            self._options['org'] = "quickstart"

        if "clean-session" not in self._options:
            self._options['clean-session'] = "true"

        if "port" not in self._options and self._options["org"] != "quickstart":
            self._options["port"] = 8883

        if self._options["org"] == "quickstart":
            self._options["port"] = 1883

        #Check for any missing required properties
        if self._options['org'] == None:
            raise ConfigurationException("Missing required property: org")
        if self._options['type'] == None:
            raise ConfigurationException("Missing required property: type")
        if self._options['id'] == None:
            raise ConfigurationException("Missing required property: id")

        if self._options['org'] != "quickstart":
            if self._options['auth-method'] == None:
                raise ConfigurationException(
                    "Missing required property: auth-method")

            if (self._options['auth-method'] == "token"):
                if self._options['auth-token'] == None:
                    raise ConfigurationException(
                        "Missing required property for token based authentication: auth-token"
                    )
            else:
                raise UnsupportedAuthenticationMethod(options['authMethod'])
        self._options['subscriptionList'] = {}

        self.COMMAND_TOPIC = "iot-2/type/" + self._options[
            'type'] + "/id/" + self._options['id'] + "/cmd/+/fmt/+"

        AbstractClient.__init__(
            self,
            domain=self._options['domain'],
            organization=self._options['org'],
            clientId="g:" + self._options['org'] + ":" +
            self._options['type'] + ":" + self._options['id'],
            username="******" if
            (self._options['auth-method'] == "token") else None,
            password=self._options['auth-token'],
            logHandlers=logHandlers,
            port=self._options['port'])

        # Add handler for commands if not connected to QuickStart
        if self._options['org'] != "quickstart":
            gatewayCommandTopic = "iot-2/type/" + options[
                'type'] + "/id/" + options['id'] + "/cmd/+/fmt/json"
            messageNotificationTopic = "iot-2/type/" + options[
                'type'] + "/id/" + options['id'] + "/notify"
            #localTopic = "iot-2/type/iotsample-raspberrypi2/id/89898889/cmd/greeting/fmt/json"
            self.client.message_callback_add(gatewayCommandTopic,
                                             self.__onCommand)
            self.client.message_callback_add("iot-2/type/+/id/+/cmd/+/fmt/+",
                                             self.__onDeviceCommand)
            self.client.message_callback_add(messageNotificationTopic,
                                             self.__onMessageNotification)

        self.subscriptionsAcknowledged = threading.Event()

        # Initialize user supplied callback
        self.commandCallback = None
        self.deviceCommandCallback = None
        self.notificationCallback = None
        self.client.on_connect = self.on_connect
        self.setMessageEncoderModule('json', jsonCodec)
        self.setMessageEncoderModule('json-iotf', jsonIotfCodec)
        self.setMessageEncoderModule('xml', xmlCodec)

        # Create api key for gateway authentication
        self.gatewayApiKey = "g/" + self._options['org'] + '/' + self._options[
            'type'] + '/' + self._options['id']
        self.logger = logging.getLogger(self.__module__ + "." +
                                        self.__class__.__name__)
        self.logger.setLevel(logging.INFO)
        self.api = api.ApiClient(
            {
                "org": self._options['org'],
                "auth-token": self._options['auth-token'],
                "auth-key": self.gatewayApiKey
            }, self.logger)