Development Example

This section describes how to implement dynamical component loading.

Procedure

  1. Enable the function of dynamical component loading.

    Change the value of the _NeedCustomizedWebNav field in the project.conf file to yes. The path of the project.conf file is {project_dir}/config/project_cfg/project.conf.

  2. Prepare the routesConfig.json configuration file for dynamical component loading and save it in the {project_dir}/src/app/set_customized_web_nav directory.
  3. In the {project_dir}/src/app/set_customized_web_nav directory, implement the configuration script build_customized_web_nav.sh. The following is an example:
    #!/bin/bash
    CONFIG_FILENAME="routesConfig.json"
    CHECKER_FILENAME="routes_config_checker.py"
    WEB_MANAGER_PATH=""
    OS_NAME=$(< "/etc/os-release" grep "^NAME=" | awk -F "=" '{print $2}' | tr -d '"')
    CURR_PATH=""
    function check_config_file() {
      python3 -u "${CURR_PATH}/${CHECKER_FILENAME}" "${CURR_PATH}"
      local ret=$?
      if [[ "${ret}" -ne 0 ]]; then
          echo "Check ${CONFIG_FILENAME} failed."
          return 1
      fi
      return 0
    }
    
    function copy_config_file() {
      if [[ ! -d "${WEB_MANAGER_PATH}/config" ]]; then
        mkdir -p "${WEB_MANAGER_PATH}/config"
      fi
    
      if ! cp "${CURR_PATH}/${CONFIG_FILENAME}" "${WEB_MANAGER_PATH}/config/${CONFIG_FILENAME}"; then
          echo "Copy ${CONFIG_FILENAME} failed, please check"
          return 1
      fi
    
      if [[ "${OS_NAME}" = "Ubuntu" ]]; then
        chown -R nobody:nogroup "${WEB_MANAGER_PATH}"
      else
        chown -R nobody:nobody "${WEB_MANAGER_PATH}"
      fi
    
      return 0
    }
    
    function main() {
      WEB_MANAGER_PATH="$1/platform/omsdk/software/nginx/html/manager"
      CURR_PATH="$1/src/app/set_customized_web_nav"
      if ! check_config_file; then
        echo "Check ${CONFIG_FILENAME} failed."
        return 1
      fi
    
      if ! copy_config_file; then
        echo "Copy ${CONFIG_FILENAME} failed."
        return 1
      fi
      return 0
    }
    
    echo  -e "\n####################### begin to build customized web nav #####################################\n"
    main "$@"
    RESULT=$?
    exit "${RESULT}"
  4. In the {project_dir}/src/app/set_customized_web_nav directory, implement the verification code routes_config_checker.py used in the configuration script build_customized_web_nav.sh. The following is an example:
    import json
    import os.path
    import sys
    
    JSON_FILE = "routesConfig.json"
    def check_json(curr_path):
        with open(os.path.join(curr_path, JSON_FILE), 'r') as fr:
            routes_config = json.loads(fr.read())
            config_fields = {
                "manager": ("network", "time", "registration", "disk", "alarm", "journal", "update", "reload", "information", "extendModule"),
                "setting": ("safety",)
            }
    
            for key in config_fields.keys():
                if key not in routes_config.keys():
                    print("Check %s failed, because [%s] not in %s" % (JSON_FILE, key, JSON_FILE))
                    return False
    
                for sub_key in config_fields[key]:
                    if sub_key not in routes_config[key]:
                        print("Check %s failed, because [%s] not in %s" % (JSON_FILE, sub_key, key))
                        return False
    
                    if not isinstance(routes_config[key][sub_key], bool):
                        print("Check %s failed, because the type of [%s] is invalid" % (JSON_FILE, sub_key))
                        return False
    
            return True
    
    
    if __name__ == "__main__":
        curr_path = sys.argv[1]
        if check_json(curr_path):
            sys.exit(0)
        else:
            sys.exit(1)
  5. In {project_dir}/build/build.sh, implement the compilation script for calling the _NeedCustomizedWebNav switch.
    # TOP_DIR={project_dir}
    # Dynamically load components.
    # For details about the implementation of build_customized_web_nav.sh, see the corresponding section.
    if [ "${_NeedCustomizedWebNav}" == "yes" ];then
        bash "${TOP_DIR}/src/app/set_customized_web_nav/build_customized_web_nav.sh" "${TOP_DIR}"
        ret=$?
        if [ "$ret" != "0" ];then
            return 1
        fi
    fi