OpenMediaVault 插件开发(一)

  • Lv2
    • 话题10
    • 回复5
    • 经验值7
    解答达人
zhizhuwang21 #4207
  • OMV 的用户越来越多,但中国区很多用户不能FQ,所以对于OpenMediaVault 插件开发无从下手,今天就给大家介绍下开发步骤:

    第一步:网页管理(GUI)
    第二步:config.xml 配置文件及 RPC文件
    第三步:模块管理及脚本

    第一步:网页管理(GUI)
    GUI部分基础使用框架ExtJS构建图形页面视图.(OMV2.x使用4.2.x; OMV3.x使用5.x; OMV4.x使用6.x )
    涉及到的文件夹及文件如下:

    1. /var/www/openmediavault/js/omv/module/admin/
    2. /var/www/openmediavault/js/omv/module/user/
    3. /var/www/openmediavault/js/omv/module/public/

    比如需要开发显示在网页(服务选项卡)下的插件地址如下:
    STEP 1
    /var/www/openmediavault/js/omv/module/admin/service/example
    (example为插件的文件夹名称)
    STEP 2
    在example文件夹下建立文件Example.js
    写入代码:

    // Register a node in the navigation tree.
    //
    // id:
    // Set the ID of the node.
    // path:
    // Parent path in the navigation view.
    // Text:
    // Service name/title. This is displayed in the navigation.
    // icon16:
    // 16x16 pixel icon that is displayed in the navigation tree.
    // iconSvg:
    // SVG icon that is displayed in the navigation view.
    OMV.WorkspaceManager.registerNode({
    id: 'example',
    path: '/service',
    text: _('Example'),
    icon16: 'images/example.png',
    iconSvg: 'images/example.svg'
    });
    

    保存后,刷新网页在service目录树下会出现example插件。
    如果没有出现可以尝试在ssh输入:
    source /usr/share/openmediavault/scripts/helper-functions && omv_purge_internal_cache
    来清楚缓存,在刷新页面查看是否显示。
    STEP 3
    在example文件夹下建立文件Settings.js代码如下:

    Ext.define('OMV.module.admin.service.example.Settings', {
        extend: 'OMV.workspace.form.Panel',
         // This path tells which RPC module and methods this panel will call to get
        // and fetch its form values.
        rpcService: 'Example',
        rpcGetMethod: 'getSettings',
        rpcSetMethod: 'setSettings',
    // Register a panel into the GUI.
    // path:
    // We want to add the panel in our example node.
    // The node was configured with the path /service and the id example.
    // The path is therefore /service/example.
    //
    // className:
    // The panel which should be registered and added (refers to
    // the class name).
    OMV.WorkspaceManager.registerPanel({
         id: 'settings',
         path: '/service/example',
         text: _('Settings'),
         position: 10,
         className: 'OMV.module.admin.service.example.Settings'
    });
    
    *
    * @return void
    */
    
    public function initialize(){
        $this->registerMethod('getSettings');
        $this->registerMethod('setSettings');
    }
    public function getSettings($params, $context){
          // Not implemented.
    }
    // Register the RPC service.
       $rpcServiceMgr = &OMVRpcServiceMgr::getInstance();
       $rpcServiceMgr->registerService(new OMVRpcServiceExample());
    
    /**
    * This file is part of OpenMediaVault.
    *
    * @license http://www.gnu.org/licenses/gpl.html GPL Version 3
    * @author Volker Theile <volker.theile@openmediavault.org>
    * @copyright Copyright (c) 2009-2018 Volker Theile
    *
    * OpenMediaVault is free software: you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * any later version.
    *
    
    * OpenMediaVault is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    
    * You should have received a copy of the GNU General Public License
    * along with OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.
    */
    
    class OMVRpcServiceExample extends OMVRpcServiceAbstract{
        /**
        * Get the RPC service name.
        */
    
    public function getName(){
       return 'Example';
    }
    
    /**
    * Initialize the RPC service.
    */
    
    public function initialize(){
         $this->registerMethod('getSettings');
         $this->registerMethod('setSettings');
      }
    public function getSettings($params, $context){
           // 验证context字段.
           $this->validateMethodContext($context, );
           // 获取配置对象.
           $db = OMVConfigDatabase::getInstance();
           $object = $db->get("conf.service.example");
           return $object->getAssoc();
      }}
    

    conf.service.example
    rpc.example.setsettings

    上面这个2个文件需要用户自己建立。
    conf.service.example 文件代码如下:

    {    
         "type": "config",
         "id": "conf.service.example", 
         "title": "example", 
         "queryinfo": { 
            "xpath": "//services/example",
            "iterable": false
     },
         "properties": { 
             "enable": { 
                "type": "boolean", 
                "default": false 
              } 
          }
     } 
    

    rpc.example.setsettings 文件代码如下:

    [{
        "type": "rpc",
        "id": "rpc.example.setsettings",
        "params": {
            "type": "object",
            "properties": {
                  "enable": {
                      "type": "boolean",
                      "required": true
                       }
                 }
            }
         }]
    

    然后在SSH输入:
    service openmediavault-engined restart
    刷新网页查看结果。
    4.x 将表单文件独立出去了放到了 /usr/share/openmediavault/datamodels/datamodels 文件夹下:

    public function setSettings($params, $context)
    {
    // 验证context字段
    $this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
    // 验证params参数
    $this->validateMethodParams($params, "rpc.example.setsettings");
    // 获取现有的配置对象
    $db = OMVConfigDatabase::getInstance();
    $object = $db->get("conf.service.example");
    $object->setAssoc($params);
    $db->set($object);
    // 返回配置对象
    return $object->getAssoc();
    }
    OMV4.X 写法
    public function setSettings($params, $context)
    {
    // Not implemented.
    }
    }
    

    STEP 4
    /usr/share/openmediavault/engined/rpc 路径下建立对应的PHP文件example.inc
    OMV2.X 写法

    // getFormItems is a method which is automatically called in the
    // instantiation of the panel. This method returns all fields for
    // the panel.
    getFormItems: function() {
    return [{
    // xtype defines the type of this entry. Some different types
    // is: fieldset, checkbox, textfield and numberfield.
    xtype: 'fieldset',
    title: _('General'),
    fieldDefaults: {
    labelSeparator: ''
    },
    // The items array contains items inside the fieldset xtype.
    items: [{
    xtype: 'checkbox',
    // The name option is sent together with is value to RPC
    // and is also used when fetching from the RPC.
    name: 'enable',
    fieldLabel: _('Enable'),
    // checked sets the default value of a checkbox.
    checked: false
    },
    {
    xtype: 'numberfield',
    name: 'max_value',
    fieldLabel: _('Max value'),
    minValue: 0,
    maxValue: 100,
    allowDecimals: false,
    allowBlank: true
    }]
    }];
    }
    });
    
查看 12 回复 - 1 至 12 (总计 12)
    • Lv2
      • 话题0
      • 回复45
      • 经验值11
    tucd #8059

    感谢分享,学习一下!

    • Lv1
      • 话题0
      • 回复16
      • 经验值4
    Error听不进去 #8075

    谢谢分享

    • Lv1
      • 话题0
      • 回复15
      • 经验值3
    小瑋 #8392

    谢谢分享

    • Lv1
      • 话题1
      • 回复4
      • 经验值1
    kingze #8399

    感谢分享,有时间学习一下!

    • Lv1
      • 话题1
      • 回复16
      • 经验值4
    可能好 #8409

    谢谢楼主分享

    • Lv1
      • 话题0
      • 回复15
      • 经验值3
    wss #9659

    太复杂 帮顶

    • Lv1
      • 话题0
      • 回复15
      • 经验值3
    lor #9661

    感谢分享 ,看不懂

    • Lv1
      • 话题0
      • 回复18
      • 经验值4
    zyq328 #9766

    谢谢楼主分享

    • Lv1
      • 话题0
      • 回复16
      • 经验值4
    Doooong666 #11311

    学习一下

    • Lv1
      • 话题0
      • 回复15
      • 经验值3
    supergm #18355

    这个是OMV多少版的?

    • Lv1
      • 话题0
      • 回复15
      • 经验值3
    dongxicc #18553

    支持一下支持一下

    • Lv1
      • 话题0
      • 回复16
      • 经验值4
    因为大侠 #23897

    感谢分享 ,看不懂

查看 12 回复 - 1 至 12 (总计 12)