1. 程式人生 > >openmediavault 4.1.3 外掛開發

openmediavault 4.1.3 外掛開發

  1. 參考網址:https://forum.openmediavault....
  2. 建立應用GUI
    建立應用目錄:/var/www/openmediavault/js/omv/module/admin/service/example
    建立選單節點: Node.js

    ``` // require("js/omv/WorkspaceManager.js") OMV.WorkspaceManager.registerNode({ id: 'example', path: '/service', text: _('Example'), icon16: 'images/example.png', iconSvg: 'images/example.svg' }); ```

    設定選單節點圖示
    var/www/openmediavault/images 內建立對應Node.js內的2張圖片

    建立設定面板: Settings.js

    ``` // require("js/omv/WorkspaceManager.js") // require("js/omv/workspace/form/Panel.js") Ext.define('OMV.module.admin.service.example.Settings', { extend: 'OMV.workspace.form.Panel', rpcService: 'Example', rpcGetMethod: 'getSettings', rpcSetMethod: 'setSettings', getFormItems: function() { return [{ xtype: 'fieldset', title: _('General'), fieldDefaults: { labelSeparator: '' }, items: [{ xtype: 'checkbox', name: 'enable', fieldLabel: _('Enable'), checked: false }, { xtype: 'numberfield', name: 'max_value', fieldLabel: _('Max value'), minValue: 0, maxValue: 100, allowDecimals: false, allowBlank: true }] }]; } }); OMV.WorkspaceManager.registerPanel({ id: 'settings', path: '/service/example', text: _('Settings'), position: 10, className: 'OMV.module.admin.service.example.Settings' }); ```

    重新整理js快取:

    ``` source /usr/share/openmediavault/scripts/helper-functions && omv_purge_internal_cache ```
  3. 建立shell指令碼
    生成配置資訊的指令碼postinst 命令執行:/bin/sh postinst configure

    ``` #!/bin/sh set -e . /etc/default/openmediavault . /usr/share/openmediavault/scripts/helper-functions case "$1" in configure) SERVICE_XPATH_NAME="example" SERVICE_XPATH="/config/services/${SERVICE_XPATH_NAME}" # 新增預設配置 if ! omv_config_exists "${SERVICE_XPATH}"; then omv_config_add_element "/config/services" "${SERVICE_XPATH_NAME}" omv_config_add_element "${SERVICE_XPATH}" "enable" "0" omv_config_add_element "${SERVICE_XPATH}" "max_value" "0" fi # 以下2條命令用於安裝包安裝 直接執行可註釋掉 dpkg-trigger update-fixperms dpkg-trigger update-locale ;; abort-upgrade|abort-remove|abort-deconfigure) ;; *) echo "postinst called with unknown argument" >&2 exit 1 ;; esac #DEBHELPER# exit 0 ```

    建立刪除配置資訊的shell指令碼 postrm 執行命令:/bin/sh postrm purge

    ``` #!/bin/sh set -e . /etc/default/openmediavault . /usr/share/openmediavault/scripts/helper-functions SERVICE_XPATH_NAME="example" SERVICE_XPATH="/config/services/${SERVICE_XPATH_NAME}" case "$1" in purge) if omv_config_exists ${SERVICE_XPATH}; then omv_config_delete ${SERVICE_XPATH} fi ;; remove) ;; upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) ;; *) echo "postrm called with unknown argument \\`$1'" >&2 exit 1 ;; esac #DEBHELPER# exit 0 ```
  4. 建立rpc
    在目錄/usr/share/openmediavault/engined/rpc建立example.inc

    ``` <?php class OMVRpcServiceExample extends \OMV\Rpc\ServiceAbstract { public function getName() { return "EXAMPLE"; } public function initialize() { $this->registerMethod("getSettings"); $this->registerMethod("setSettings"); } public function getSettings($params, $context) { // Validate the RPC caller context. $this->validateMethodContext($context, [ "role" => OMV_ROLE_ADMINISTRATOR ]); // Get the configuration object. $db = \\OMV\\Config\\Database::getInstance(); $object = $db->get("conf.service.example"); // Remove useless properties from the object. return $object->getAssoc(); } public function setSettings($params, $context) { // Validate the RPC caller context. $this->validateMethodContext($context, [ "role" => OMV_ROLE_ADMINISTRATOR ]); // Validate the parameters of the RPC service method. $this->validateMethodParams($params, "rpc.example.setsettings"); // Get the existing configuration object. $db = \\OMV\\Config\\Database::getInstance(); $object = $db->get("conf.service.example"); $object->setAssoc($params); $db->set($object); // Return the configuration object. return $object->getAssoc(); } } ```

    建立對應的配置檔案
    在usr\share\openmediavault\datamodels建立conf.service.example.json

    ``` { "type": "config", "id": "conf.service.example", "title": "EXAMPLE", "queryinfo": { "xpath": "//services/example", "iterable": false }, "properties": { "enable": { "type": "boolean", "default": false }, "max_value": { "type": "integer", "minimum": 1, "maximum": 100, "default": 0 } } } ```

    在usr\share\openmediavault\datamodels建立rpc.example.json

    ``` [{ "type": "rpc", "id": "rpc.example.setsettings", "params": { "type": "object", "properties": { "enable": { "type": "boolean", "required": true }, "max_value": { "type": "integer", "minimum": 1, "maximum": 100, "required": true } } } }] ```

    執行命令重啟omv服務:service openmediavault-engined restart

  5. 建立shell指令碼獲取配置資訊
    建立執行指令碼 example 執行命令:omv-mkconf example

    ``` #!/bin/sh set -e . /etc/default/openmediavault . /usr/share/openmediavault/scripts/helper-functions OMV_EXAMPLE_XPATH="/config/services/example" OMV_EXAMPLE_CONF="/tmp/example.conf" cat <<EOF > ${OMV_EXAMPLE_CONF} enable = $(omv_config_get "${OMV_EXAMPLE_XPATH}/enable") max_value = $(omv_config_get "${OMV_EXAMPLE_XPATH}/max_value") EOF exit 0 ```

    為了監聽觸發執行指令碼,將指令碼放在/usr/share/openmediavault/mkconf目錄下
    指令碼許可權改為 chmod 755 example

  6. 配置儲存事件監聽

    /usr/share/openmediavault/engined/module建立監聽的example.inc

    ``` <?php class OMVModuleExample extends \OMV\Engine\Module\ServiceAbstract implements \OMV\Engine\Notify\IListener { public function getName() { return "EXAMPLE"; } public function applyConfig() { // 觸發對應執行指令碼 $cmd = new \\OMV\\System\\Process("omv-mkconf", "example"); $cmd->setRedirect2to1(); $cmd->execute(); } function bindListeners(\\OMV\\Engine\\Notify\\Dispatcher $dispatcher) { $dispatcher->addListener(OMV_NOTIFY_MODIFY, "org.openmediavault.conf.service.example", // 和rpc內的配置檔案一致conf.service.example [ $this, "setDirty" ]); } } ```

    執行命令重啟omv服務:service openmediavault-engined restart

  7. 建立deb包 https://blog.csdn.net/gatieme...

原文地址:https://segmentfault.com/a/1190000016716780