Exemple #1
0
    def __init__(self,
                 ogc_config: OGCConfiguration,
                 config_filename: Optional[str],
                 catalog_name: str = CATALOG_FILENAME):
        """ Load OGC configuration model, initialize MQTT Broker for publishing Observations and prepare Flask.

        :param ogc_config: The reference of the OGC configuration.
        :param config_filename: A file containing connection information.
        :param catalog_name: The name of the resource catalog. If not specified a default one will be used.
        """
        super().__init__(ogc_config, config_filename, catalog_name)

        if not config_filename:
            if D_CONFIG_KEY in os.environ.keys():
                if os.environ[D_CONFIG_KEY] == D_CUSTOM_MODE:
                    try:
                        self._listening_address = os.environ[
                            LISTENING_ADD_KEY.upper()]
                    except KeyError:
                        logging.warning(
                            LISTENING_ADD_KEY.upper() +
                            " not set, default listening address: " +
                            DEFAULT_LISTENING_ADD)
                        self._listening_address = DEFAULT_LISTENING_ADD
                    try:
                        self._listening_port = int(
                            os.environ[LISTENING_PORT_KEY.upper()])
                    except KeyError:
                        logging.warning(LISTENING_PORT_KEY.upper() +
                                        " not set, default listening port: " +
                                        str(DEFAULT_LISTENING_PORT))
                        self._listening_port = DEFAULT_LISTENING_PORT
                else:
                    logging.critical(
                        "No connection file for preference_folder: " +
                        str(config_filename))
                    exit(ERROR_MISSING_CONNECTION_FILE)
            else:
                logging.critical(
                    "Missing connection file or environmental variable!")
                exit(ERROR_MISSING_ENV_VARIABLE)
        else:
            # Retrieving endpoint information for listening to REST requests
            config_file = util.load_from_file(config_filename)
            self._listening_address = config_file[REST_KEY][LISTENING_ADD_KEY][
                ADDRESS_KEY]
            self._listening_port = int(
                config_file[REST_KEY][LISTENING_ADD_KEY][PORT_KEY])
    def __init__(self,
                 ogc_config: OGCConfiguration,
                 config_filename: Union[str, None],
                 url_login: str,
                 credentials: dict,
                 catalog_name: str = CATALOG_FILENAME,
                 token_prefix: Optional[str] = "",
                 token_suffix: Optional[str] = ""):
        """ Load OGC configuration model and initialize MQTT Broker for publishing Observations

        :param config_filename: A file containing connection information.
        """
        super().__init__(ogc_config, config_filename, catalog_name)

        self._publish_mutex = Lock()
        self._observation_cnt_mutex = Lock()

        self._sequences = []

        self._url_login = url_login
        self._credential = credentials
        self._token_prefix = token_prefix
        self._token_suffix = token_suffix
        self._cloud_token = ""
        self.update_cloud_token()

        if config_filename:
            config_file = util.load_from_file(config_filename)
            try:
                self._site_name = config_file[CLOUD_KEY][SITE_NAME_KEY]
                self._tenant_id = config_file[CLOUD_KEY][TENANT_ID_KEY]
                self._site_id = config_file[CLOUD_KEY][SITE_ID_KEY]
            except KeyError as ex:
                logging.critical('Missing parameter: "' + str(ex) +
                                 '" in configuration file.')
                sys.exit(ERROR_MISSING_PARAMETER)
        else:
            try:
                self._site_name = os.environ[SITE_NAME_KEY.upper()]
                self._tenant_id = os.environ[TENANT_ID_KEY.upper()]
                self._site_id = os.environ[SITE_ID_KEY.upper()]
            except KeyError as ex:
                logging.critical('Missing environmental variable: "' +
                                 str(ex) + '"')
                sys.exit(ERROR_MISSING_ENV_VARIABLE)
    def __init__(self, ogc_config: OGCConfiguration, connection_file: str, catalog_name: str = CATALOG_FILENAME):
        """ Initialize the SCRALModule:
            Preparing the catalog, instantiating the MQTT Client and stores all relevant connection information.

        :param ogc_config: An instance of an OGCConfiguration.
        :param connection_file: The path of the connection file, it has to contain the address of the MQTT broker.
        """

        # 1 Storing the OGC configuration
        self._ogc_config = ogc_config

        # 2 Load local resource catalog
        self._catalog_fullpath = CATALOG_FOLDER + catalog_name
        if os.path.exists(self._catalog_fullpath):
            self._resource_catalog = util.load_from_file(self._catalog_fullpath)
            self.print_catalog()
        else:
            logging.info("No resource catalog <" + catalog_name + "> available.")
            self._resource_catalog = {}

        # 3 Load connection configuration fields...
        if D_CONFIG_KEY in os.environ.keys() and os.environ[D_CONFIG_KEY].lower() == D_CUSTOM_MODE:
            # 3a) ...from environmental variables.
            try:
                self._pub_broker_address = os.environ[D_PUB_BROKER_URI_KEY]
            except KeyError as ex:
                logging.error('Missing environment variable: "'+str(ex)+'"')
                exit(ERROR_MISSING_ENV_VARIABLE)
            try:
                self._pub_broker_port = int(os.environ[D_PUB_BROKER_PORT_KEY])
            except KeyError:
                logging.warning("No broker port specified, will be used the default one: "+str(DEFAULT_KEEPALIVE)+" s")
                self._pub_broker_port = BROKER_DEFAULT_PORT
            try:
                self._pub_broker_keepalive = int(os.environ[D_PUB_BROKER_KEEPALIVE_KEY])
            except KeyError:
                logging.warning(
                    "No broker keepalive specified, will be used the default one: " + str(DEFAULT_KEEPALIVE) + " s")
                self._pub_broker_keepalive = DEFAULT_KEEPALIVE
            try:
                pilot_mqtt_topic_prefix = os.environ[D_GOST_MQTT_PREFIX_KEY]
            except KeyError:
                logging.warning('No MQTT topic prefix configured, using default one: "'+DEFAULT_GOST_PREFIX+'"')
                pilot_mqtt_topic_prefix = DEFAULT_GOST_PREFIX

        elif connection_file:
            # 3b) ...from connection file.
            connection_config_file = util.load_from_file(connection_file)
            self._pub_broker_address = connection_config_file[MQTT_KEY][MQTT_PUB_BROKER_KEY]
            self._pub_broker_port = connection_config_file[MQTT_KEY][MQTT_PUB_BROKER_PORT_KEY]
            try:
                self._pub_broker_keepalive = connection_config_file[MQTT_KEY][MQTT_PUB_BROKER_KEEP_KEY]
            except KeyError:
                logging.warning(
                    "No broker keepalive specified, will be used the default one: " + str(DEFAULT_KEEPALIVE) + " s")
                self._pub_broker_keepalive = DEFAULT_KEEPALIVE

            pilot_mqtt_topic_prefix = connection_config_file[GOST_PREFIX_KEY]
            if not pilot_mqtt_topic_prefix:
                logging.warning('No MQTT topic prefix configured, default one will be used: "'+DEFAULT_GOST_PREFIX+'"')
                pilot_mqtt_topic_prefix = DEFAULT_GOST_PREFIX

        else:
            logging.critical("No environmental variables or connection file configured!")
            exit(ERROR_MISSING_ALL)

        # 4 Creating an MQTT Publisher
        logging.debug("MQTT publishing topic prefix: " + pilot_mqtt_topic_prefix)
        self._topic_prefix = pilot_mqtt_topic_prefix

        client_id = MQTT_CLIENT_PREFIX + "-" + str(self.__class__.__name__) + "-" + str(random.randint(1, sys.maxsize))
        self._mqtt_publisher = mqtt.Client(client_id=client_id)

        self._mqtt_publisher.on_connect = mqtt_util.on_connect
        self._mqtt_publisher.on_disconnect = mqtt_util.automatic_reconnection

        logging.info(
            "Try to connect to broker: %s:%s for PUBLISHING..." % (self._pub_broker_address, self._pub_broker_port))
        logging.debug("MQTT Client ID is: " + str(self._mqtt_publisher._client_id))
        self._mqtt_publisher.connect(self._pub_broker_address, self._pub_broker_port, self._pub_broker_keepalive)
        self._mqtt_publisher.loop_start()

        # 5 Preparing module analysis information
        self._active_devices = {ACTUAL_COUNTER_KEY: 0, LAST_UPDATE_KEY: arrow.utcnow()}
        update_interval = None
        warning_msg = " not configured, default value will be used: " + str(DEFAULT_UPDATE_INTERVAL) + "s"
        if D_CONFIG_KEY in os.environ.keys() and os.environ[D_CONFIG_KEY].lower() == D_CUSTOM_MODE:
            try:
                update_interval = int(os.environ[UPDATE_INTERVAL_KEY.upper()])
            except KeyError:
                warning_msg = UPDATE_INTERVAL_KEY.upper() + warning_msg
        elif connection_file:
            try:
                update_interval = int(connection_config_file[UPDATE_INTERVAL_KEY])
            except KeyError:
                warning_msg = UPDATE_INTERVAL_KEY + warning_msg
        else:
            warning_msg = "Error: " + UPDATE_INTERVAL_KEY + warning_msg
            update_interval = DEFAULT_UPDATE_INTERVAL

        if not update_interval:
            logging.warning(warning_msg)
            self._active_devices[UPDATE_INTERVAL_KEY] = DEFAULT_UPDATE_INTERVAL
        else:
            self._active_devices[UPDATE_INTERVAL_KEY] = update_interval

        logging.info("If observations flow, number of active devices will be refreshed every "
                     + str(self._active_devices[UPDATE_INTERVAL_KEY]) + " seconds.")
Exemple #4
0
    def __init__(self,
                 ogc_config: OGCConfiguration,
                 connection_file: str,
                 catalog_name: str = CATALOG_FILENAME):
        """ Initialize MQTT Brokers for listening and publishing

        :param connection_file: A file containing connection information.
        """

        super().__init__(ogc_config, connection_file, catalog_name)

        # Creating an MQTT Subscriber
        self._mqtt_subscriber = mqtt.Client(BROKER_HAMBURG_CLIENT_ID)
        self._mqtt_subscriber.on_connect = mqtt_util.on_connect
        self._mqtt_subscriber.on_disconnect = mqtt_util.automatic_reconnection
        self._mqtt_subscriber.on_message = self.on_message_received

        # Loading broker info from connection file
        if connection_file:
            connection_config_file = util.load_from_file(connection_file)
            try:
                self._sub_broker_address = connection_config_file[MQTT_KEY][
                    MQTT_SUB_BROKER_KEY]
            except KeyError as ex:
                logging.critical('Missing parameter: "' + str(ex) +
                                 '" in configuration file.')
                sys.exit(ERROR_MISSING_PARAMETER)
            try:
                self._sub_broker_port = int(
                    connection_config_file[MQTT_KEY][MQTT_SUB_BROKER_PORT_KEY])
            except KeyError as ex:
                logging.warning('No parameter "' + str(ex) +
                                '" specified, default value used: ' +
                                str(BROKER_DEFAULT_PORT))
                self._sub_broker_port = BROKER_DEFAULT_PORT
            try:
                self._sub_broker_keepalive = int(
                    connection_config_file[MQTT_KEY][MQTT_SUB_BROKER_KEEP_KEY])
            except KeyError:
                logging.warning(
                    "No subscribing broker keepalive specified, default one used: "
                    + str(DEFAULT_KEEPALIVE) + " s")
                self._sub_broker_keepalive = DEFAULT_KEEPALIVE

        # Loading broker info from environmental variables
        else:
            try:
                self._sub_broker_address = os.environ[
                    MQTT_SUB_BROKER_KEY.upper()]
            except KeyError as ex:
                logging.critical('Missing environmental variable: "' +
                                 str(ex) + '"')
                sys.exit(ERROR_MISSING_ENV_VARIABLE)
            try:
                self._sub_broker_port = int(
                    os.environ[MQTT_SUB_BROKER_PORT_KEY.upper()])
            except KeyError as ex:
                logging.warning('Missing Enviromental variable: "' + str(ex) +
                                '" default value used: ' +
                                str(BROKER_DEFAULT_PORT))
                self._sub_broker_port = BROKER_DEFAULT_PORT

            try:
                self._sub_broker_keepalive = int(
                    os.environ[MQTT_SUB_BROKER_KEEP_KEY.upper()])
            except KeyError:
                logging.warning(
                    "No subscribing broker keepalive specified, will be used the default one: "
                    + str(DEFAULT_KEEPALIVE) + " s")
                self._sub_broker_keepalive = DEFAULT_KEEPALIVE

        # broker connection test
        logging.info("Try to connect to broker: %s:%d for LISTENING..." %
                     (self._sub_broker_address, self._sub_broker_port))
        logging.debug("Client id is: '" + BROKER_HAMBURG_CLIENT_ID + "'")
        self._mqtt_subscriber.connect(self._sub_broker_address,
                                      self._sub_broker_port,
                                      self._sub_broker_keepalive)
Exemple #5
0
    def __init__(self,
                 ogc_config: OGCConfiguration,
                 config_filename: str,
                 catalog_name: str = CATALOG_FILENAME):
        """ Initialize MQTT Brokers for listening and publishing

        :param config_filename: A file containing connection information.
        :param catalog_name: The name of the resource catalog.
        """

        super().__init__(ogc_config, config_filename, catalog_name)

        # Creating an MQTT Subscriber
        self._mqtt_subscriber = mqtt.Client(CLIENT_ID)
        self._mqtt_subscriber.connected_flag = False  # create connection flag in client
        self._mqtt_subscriber.on_connect = mqtt_util.on_connect
        self._mqtt_subscriber.on_disconnect = self.automatic_reconnection
        self._mqtt_subscriber.on_message = self.on_message_received

        # Retrieving MQTT subscribing info from config_filename
        if config_filename:
            config_file = util.load_from_file(config_filename)
            try:
                self._sub_broker_address = config_file[MQTT_KEY][
                    MQTT_SUB_BROKER_KEY]
            except KeyError as ex:
                logging.critical('Missing parameter: ' + str(ex) +
                                 ' in configuration file.')
                sys.exit(ERROR_MISSING_PARAMETER)
            try:
                self._sub_broker_port = int(
                    config_file[MQTT_KEY][MQTT_SUB_BROKER_PORT_KEY])
            except KeyError as ex:
                logging.warning('Missing parameter: ' + str(ex) +
                                '.Default value used: ' +
                                str(BROKER_DEFAULT_PORT))
                self._sub_broker_port = BROKER_DEFAULT_PORT
            try:
                self._sub_broker_keepalive = int(
                    config_file[MQTT_KEY][MQTT_SUB_BROKER_KEEP_KEY])
            except KeyError:
                logging.warning(
                    "No subscribing broker keepalive specified, default one used: "
                    + str(DEFAULT_KEEPALIVE) + " s")
                self._sub_broker_keepalive = DEFAULT_KEEPALIVE

        # Retrieving MQTT publishing info from environmental variables
        else:
            try:
                self._sub_broker_address = os.environ[
                    MQTT_SUB_BROKER_KEY.upper()]
            except KeyError as ex:
                logging.critical('Missing environmental variable: ' + str(ex))
                sys.exit(ERROR_MISSING_ENV_VARIABLE)
            try:
                self._sub_broker_port = int(
                    os.environ[MQTT_SUB_BROKER_PORT_KEY.upper()])
            except KeyError as ex:
                logging.warning('Missing environmental variable: ' + str(ex) +
                                '.Default value used: ' +
                                str(BROKER_DEFAULT_PORT))
                self._sub_broker_port = BROKER_DEFAULT_PORT
            try:
                self._sub_broker_keepalive = int(
                    os.environ[MQTT_SUB_BROKER_KEEP_KEY.upper()])
            except KeyError:
                logging.warning(
                    "No subscribing broker keepalive specified, will be used the default one: "
                    + str(DEFAULT_KEEPALIVE) + " s")
                self._sub_broker_keepalive = DEFAULT_KEEPALIVE

        #  MQTT Subscriber setting configuration
        logging.info("Try to connect to broker: %s:%s for LISTENING..." %
                     (self._sub_broker_address, self._sub_broker_port))
        logging.debug("MQTT Client ID is: " +
                      str(self._mqtt_subscriber._client_id))
        self._mqtt_subscriber.connect(self._sub_broker_address,
                                      self._sub_broker_port,
                                      self._sub_broker_keepalive)