def run():
    """ daemon run function.

    This function should be called to start the system.
    """

    from rgapps.utils.utility import get_log_file_handles
    logger_fds = get_log_file_handles(logging.getLogger())
    logging.debug("Logger file handles fileno [{0}]".format(logger_fds))

    system = platform.system()

    if system == "Linux":
        logging.info("Server running on Linux.")

        pid_file = ini_config.getboolean("Sensor", "SENSOR_PID_FILE")
        working_dir = ini_config.getboolean("Logging", "WORKING_DIR")

        logging.debug("Instantiating daemon with pid_file [{0}] "
                       "and working_dir [{1}]"
                       .format(pid_file, working_dir))

        import daemon.pidfile

        daemon_context = daemon.DaemonContext(
            working_directory=working_dir,
            umask=0o002,
            pidfile=daemon.pidfile.PIDLockFile(pid_file))

        logging.debug("Setting up daemon signal map")
        daemon_context.signal_map = { signal.SIGTERM: program_cleanup }
        logging.debug("daemon signal map has been setup")

        if (logger_fds):
            logging.debug("setting files_preserve for the log file "
                           "descriptor [{0}]".format(logger_fds))
            daemon_context.files_preserve = logger_fds

        logging.debug("Starting daemon by opening its context.")
        daemon_context.open()

        logging.info("Calling subscribe_sensor_data....")
        subscribe_sensor_data()

        logging.debug("Closing the daemon context.")
        daemon_context.close()

    else:
        logging.info("Server running on Windows system ...")
        subscribe_sensor_data()
def run():
    """ daemon run function.

    This function should be called to start the system.
    """

    instance_path = ini_config.get("Flask", "INSTANCE_PATH")

    # app: Flask application object
    logging.debug("initializing the Flask app")
    global globalFlaskApp
    globalFlaskApp = Flask(__name__,
                            instance_path=instance_path,
                            instance_relative_config=True)

    is_debug = ini_config.getboolean("Flask", "DEBUG")
    is_testing = ini_config.getboolean("Flask", "TESTING")
    is_json_sort_keys = ini_config.getboolean("Flask", "JSON_SORT_KEYS")
    max_content_length = ini_config.getint("Flask", "MAX_CONTENT_LENGTH")

    globalFlaskApp.config.update(DEBUG=is_debug,
                                  TESTING=is_testing,
                                  JSON_SORT_KEYS=is_json_sort_keys,
                                  MAX_CONTENT_LENGTH=max_content_length)


    with globalFlaskApp.app_context():

        logging.info("Starting application ...")

        from rgapps.utils.utility import get_log_file_handles
        logger_fds = get_log_file_handles(logging.getLogger())
        logging.debug("Logger file handles fileno [{0}]"
                       .format(logger_fds))

        system = platform.system()

        if system == "Linux":
            logging.info("Server running on Linux.")

            pid_file = ini_config.get("Sensor", "SENSOR_PID_FILE")
            working_dir = ini_config.get("Logging", "WORKING_DIR")

            logging.debug("Instantiating daemon with pid_file [{0}] "
                           "and working_dir [{1}]"
                           .format(pid_file, working_dir))

            import daemon.pidfile

            daemon_context = daemon.DaemonContext(
                working_directory=working_dir,
                umask=0o002,
                pidfile=daemon.pidfile.PIDLockFile(pid_file))

            logging.debug("Setting up daemon signal map")
            daemon_context.signal_map = { signal.SIGTERM: program_cleanup }
            logging.debug("daemon signal map has been setup")

            if (logger_fds):
                logging.debug("setting files_preserve for the log file "
                               "descriptor [{0}]"
                               .format(logger_fds))
                daemon_context.files_preserve = logger_fds

            logging.debug("Starting daemon by opening its context.")
            daemon_context.open()

            logging.info("Calling read_store_readings....")
            read_store_readings()

            logging.debug("Closing the daemon context.")
            daemon_context.close()

        else:
            logging.info("Server running on Windows system ...")
            read_store_readings()

    return