diff --git a/适配器.md b/适配器.md index d135ebc..98226d1 100644 --- a/适配器.md +++ b/适配器.md @@ -271,9 +271,7 @@ objects - 适配器的所有实例的静态对象(xxx.object)通过安装适 此外,可以定义新视图。在SQL中,它们被称为“存储过程”,在couchDB中也称为视图。 -**Note**: do not mix with "vis" views. - -注意:不要与“vis”视图混合使用。 +**注意**:不要与“vis”视图混合使用。 For view definitions the javascript language is used. Here is the sample: ``` @@ -295,8 +293,6 @@ Here are two views defined for hm-rpc adapter: "listDevices" and "paramsetDescri 是为hm-rpc适配器定义的两个视图:“listDevices”和“paramsetDescription”。它们从数据存储中返回由视图条件对象过滤的集合。它可以有效(如果使用CouchDB)请求指定的对象列表。 -To use view: - 要使用视图: ``` @@ -318,19 +314,18 @@ adapter.objects.getObjectView('hm-rpc', 'listDevices', Usage of _startkey_ and _endkey_ can be found on the [same page](http://guide.couchdb.org/editions/1/en/views.html) too. -**Note:** usage of views is optional and demands from developer basic knowledge level about CouchDB. - -注意:视图的使用是可选的,并且需要开发人员关于CouchDB的基本知识水平。 +**注意:** 视图的使用是可选的,并且需要开发人员关于CouchDB的基本知识水平。 ### Instance object fields ### 实例对象字段 -Some specific objects or objects with type states can be defined in _instanceObjects_ of io-package.json. +可以在io-package.json的 _instanceObjects_ 中定义一些具有类型状态的特定对象或对象。 -For every created instance all entries from _instanceObjects_ field will be created. +对于每个创建的实例,将创建来自 _instanceObjects_ 字段的所有条目。 + +例如,适配器_hm-rpc_ 为每个实例创建状态 "updated" 以向其他适配器发出信号,一些新设备出现在数据存储中,并且必须由hm-rega处理。 -For instance adapter _hm-rpc_ creates state "updated" for every instance to give a signal to other adapter, that some new devices are appeared in the data store and that they must be processed by hm-rega. ``` "instanceObjects": [ { @@ -345,7 +340,8 @@ For instance adapter _hm-rpc_ creates state "updated" for every instance to give } ] ``` -There is no need to give the full path of object and it cannot be done, because adapter instance is unknown. You can use special word "%INSTANCE%" in _common.name_ to show the it in the name of object. For instance: + +没有必要提供对象的完整路径,因为适配器实例未知,所以无法完成。您可以在common.name中使用特殊单词 "%INSTANCE%" 在 _common.name_ 中显示它。例如: ``` "name": "Some new devices added in hm-rpc.%INSTANCE%", @@ -360,7 +356,9 @@ Will be expanded to by creation of first instance. ## package.json -package.json is the npm packet standart description file and the full description can be found under [https://docs.npmjs.com/files/package.json](https://docs.npmjs.com/files/package.json). +## package.json结构 + +package.json是npm数据包标准描述文件,完整描述可以在[https://docs.npmjs.com/files/package.json](https://docs.npmjs.com/files/package.json).下找到。 Short structure of package.json: ``` @@ -398,17 +396,19 @@ Short structure of package.json: "license": "MIT" } ``` -- All fields are mandatory. devDependencies should be inside too to enable the grunt tasks. +- 所有字段都是必填项。devDependencies也应该在里面以启用grunt任务。 ### Deploying -It is suggested to have the code on github. After the code is stable and lets to install adapter you can share you adapter to other user by asking them to install adapter as follow: +### 部署 + +建议在github上使用代码。在代码稳定并允许安装适配器之后,您可以通过要求他们安装适配器来将适配器共享给其他用户,如下所示: ``` npm install https://github.com/yourName/iobroker.adapterName/tarball/master/ ``` -If everything is OK and you have got positive feedback from users you can publish adapter on npm. It would be good if before publishing you will create realease on github. +如果一切正常并且您得到了用户的积极反馈,您可以在npm上发布适配器。如果在发布之前你将在github上创建realease会很好。 -Publishing can be done with following command: +可以使用以下命令完成发布: ``` npm publish @@ -418,26 +418,33 @@ Call it in the adapter directory. Be sure, that you deleted all other files exce Of course you must first create the account on [npm](https://www.npmjs.com/signup) -**Note:** you cannot publish twice the code with the same version. Increase version in package.json and io-package.json before publishing. +**注意:** 您不能使用相同版本发布两次代码。在发布之前增加package.json和io-package.json中的版本。 After the adapter is tested and other users find it useful, it can be taken into common repository, so it can be installed via "admin" adapter. ## How to create own adapter +## 如何创建自己的适配器 + Please check [https://github.com/ioBroker/ioBroker.template](https://github.com/ioBroker/ioBroker.template) for a template of your own adapter. If you want to create a widget or an adapter with a widget please check [ioBroker.vis-template]https://github.com/ioBroker/ioBroker.vis-template) for a template of your own adapter. ## Structure of main.js +## main.js的结构 + ``` var utils = require(__dirname + '/lib/utils'); // Get common adapter utils - mandatory ``` This line loads module lib/utils.js. It has common for all adapters function to find the root of iobroker.js-controller. Because adapter can be installed in three different paths: +该行加载模块 lib/utils.js。所有适配器功能都有共同点,可以找到iobroker.js-controller的根目录。因为适配器可以安装在三个不同的路径中: + - .../iobroker/node_modules/iobroker.adapterName - this is standard path and suggested to use - .../iobroker.js-controller/node_modules/iobroker.adapterName - used by debugging - .../iobroker.js-controller/adapter/adapterName - old style (deprecated) -utils.js do nothing except looks for _iobroker.js-controller/lib/adapter.js_ file and loads it. +=utils.js什么事都不做,除了查找iobroker.js-controller / lib / adapter.js文件并加载它。 + ``` @@ -452,9 +459,10 @@ js-controller starts adapter as fork of own process with two parameters: instanc child_process.fork('pathToAdapter/main.js', '0 info'); ``` -It is all will be automatically processed in adapter.js and developer of the adapter must not care about it. +它将全部在adapter.js中自动处理,适配器的开发人员不必关心它。 -Adapter supports 3 other start flags: + +适配器支持3个其他启动标志: - --install - Starts adapter even if no configuration exists. Used by adapter to execute some install procedure by installation of adapter. - --force - Starts adapter even if it is disabled in configuration - --logs - Show logs in the console, if they shown only in log table. @@ -464,9 +472,16 @@ var myPacket1= require('myPacket1'); // add own module ``` Then you can load all other modules that required in adapter, like 'fs', 'require' and so on. Just do not forget to declare them in package.json. +然后,您可以加载适配器中所需的所有其他模块,如“fs”,“require”等。只是不要忘记在package.json中声明它们。 + + ### Options of adapter +### 适配器的选项 + You can create adapter object with just by name, like ```utils.adapter('adapterName')``` or with additional parameters, like: + + ``` var adapter = utils.adapter({ name: 'adapterName', // mandatory - name of adapter @@ -489,6 +504,9 @@ var adapter = utils.adapter({ ``` All handlers can be simulated by events (see below), like: +所有处理程序都可以通过事件进行模拟(见下文),如: + + ``` adapter.on('ready', function () { main(); @@ -496,12 +514,15 @@ adapter.on('ready', function () { ``` ### Attributes of adapter object +### 适配器对象的属性 + As you created "Adapter" object with ``` var adapter = utils.adapter('adapterName'); ``` following attributes will be created in this object instance: + - *name* - Name of adapter, e.g "adapterName" - *host* - Host name, where the adapter instance runs - *instance* - instance number of this adapter instance @@ -520,6 +541,8 @@ following attributes will be created in this object instance: #### Most important events +#### 最重要的事件 + ``` adapter.on('objectChange', function (id, obj) { adapter.log.info('objectChange ' + id + ' ' + JSON.stringify(obj)); @@ -537,7 +560,7 @@ adapter.on('stateChange', function (id, state) { }); ``` -- *Entry point*. Make all initialisations in main, because before "ready" there is no configuration. +- *Entry point*. 在main中进行所有初始化,因为在“ready”之前没有配置。Make all initialisations in main, because before "ready" there is no configuration. ``` adapter.on('ready', function () { @@ -545,7 +568,9 @@ adapter.on('ready', function () { }); ``` #### Logging -It is very important to have the ability to log the events for debug and controlling purposes. Following functions can be used to log the events: +#### 记录 + +能够记录事件以进行调试和控制是非常重要的。以下函数可用于记录事件: ``` adapter.log.debug("debug message"); // log message with debug level @@ -553,13 +578,16 @@ It is very important to have the ability to log the events for debug and control adapter.log.warn("warning"); // log message with info warn adapter.log.error("error"); // log message with info error ``` -There is no need to indicate the origin or time of the message. These attributes will be added automatically, e.g.: +无需指明消息的来源或时间。这些属性将自动添加,例如: + ```admin-0 2015-07-10 17:35:52 info successful connection to socket.io from xx.yy.17.17``` -Of course console.log, console.debug or console.error could be used too, but these messages will be visible only if adapter started manually in console or programming IDE. +当然也可以使用console.log,console.debug或console.error,但只有在控制台或编程IDE中手动启动适配器时,这些消息才会可见。 #### Instance configuration +#### 实例配置 + There is an attribute of adapter object to read the configuration of the instance: "adapter.config". This object consist of "**native**" part of object "system.adapter.adapterName.X". E.g. if **io-package.json** looks like: