Core module to be used in ioBroker adapters. Acts as the bridge to js-controller.
This replaces the utils.js
included in the ioBroker template adapter.
- Add this as a dependency:
npm i @iobroker/adapter-core
- Replace
with
const utils = require(__dirname + '/lib/utils');
const utils = require('@iobroker/adapter-core');
- Create an adapter instance as usual:
// old style const adapter = utils.adapter(/* options */); // new style (classes). See https://github.com/ioBroker/ioBroker.template/ for a more detailed usage class MyAdapter extends utils.Adapter {...}
Compared to the old utils.js
, some utility methods were added.
const dataDir = utils.getAbsoluteDefaultDataDir();
This returns the absolute path of the data directory for the current host. On linux, this is usually /opt/iobroker/iobroker-data
// old style
const instanceDataDir = utils.getAbsoluteInstanceDataDir(adapter);
// new style (classes)
const instanceDataDir = utils.getAbsoluteInstanceDataDir(this);
Returns the absolute path of the data directory for the current adapter instance.
On linux, this is usually /opt/iobroker/iobroker-data/<adapterName>.<instanceNr>
adapter.terminate('for some reason', utils.EXIT_CODES.ADAPTER_REQUESTED_TERMINATION);
Use standardized exit codes if your adapter needs to terminate.
A collection of various utility methods and modules from JS-Controller. Prefer this over trying to find lib/tools.js
and similar internal modules from the controller yourself!
Currently, the following methods are available:
commonTools.pattern2RegEx
- Converts a pattern to match object IDs into a RegEx string that can be used innew RegExp(...)
commonTools.getAdapterDir
- Finds the adapter directory of a given adaptercommonTools.getInstalledInfo
- Get a list of all installed adapters and controller version on this hostcommonTools.getLocalAddress
- Get the localhost (IPv6 or IPv4) address according to the ioBroker configcommonTools.getListenAllAddress
- Get the "listen all" (IPv6 or IPv4) address according to the ioBroker configcommonTools.isLocalAddress
- Check if given IPv4 or IPv6 ip address corresponds to localhostcommonTools.isListenAllAddress
- Check if given IPv4 or IPv6 ip address corresponds to "listen all" addresscommonTools.ensureDNSOrder
- Ensure that DNS resolution is performed according to ioBroker config
And the following modules are exposed:
commonTools.password
- Previously exposed aslib/password.js
in JS-Controller.commonTools.session
- Previously exposed aslib/session.js
in JS-Controller.commonTools.zipFiles
- Previously exposed aslib/zipFiles.js
in JS-Controller.commonTools.isDocker
- Checks if we are running inside a docker container
Note that commonTools.letsEncrypt
is not available anymore as the next controller won't support it (use @iobroker/webserver
instead).
Developer can use internationalisation in backend.
For that call
const I18n = require('@iobroker/adapter-core').I18n;
// later in "ready" method
await I18n.init(__dirname, adapter);
// If you use class syntax, you can use `this` instead of `adapter`
await I18n.init(__dirname, this);
// You can provide the language directly
await I18n.init(__dirname, 'de');
and then in code
console.log(I18n.translate('text to translate %s', 'argument1'));
// or to get the ioBroker.Translated object
console.log(JSON.stringify(I18n.getTranslatedObject('text to translate %s and %s', 'argument1', 'argument2')));
You can place your i18n
folder in root of adapter or in lib
folder. If your i18n
files are in lib
directory, so call the init
function like this:
const { join } = require('node:path');
const I18n = require('@iobroker/adapter-core').I18n;
await I18n.init(join(__dirname, 'lib'), adapter);
Expected structure of i18n
directory
+ i18n
- de.json
- en.json
- es.json
- fr.json
- it.json
- nl.json
- pl.json
- pt.json
- ru.json
- uk.json
- zh-cn.json
And an example of i18n files could be found here
ioBroker has the ability to include files written by adapters in its backups. To enable that, you need to add the following to io-package.json
:
{
// ...
"common": {
// ...
"dataFolder": "path/where/your/files/are"
}
}
This path is relative to the path returned by getAbsoluteDefaultDataDir()
. The placeholder %INSTANCE%
is automatically replaced by the instance number of each adapter, for example "dataFolder": "my-adapter.%INSTANCE%"
.
npm run build
creates a clean rebuild of the module. This is done automatically before every build;npm run lint
checks for linting errors;npm run watch
creates an initial build and then incrementally compiles the changes while working.
If you find errors in the definitions, e.g., function calls that should be allowed but aren't, please open an issue here or over at https://github.com/DefinitelyTyped/DefinitelyTyped and make sure to mention @AlCalzone.
- (@foxriver76) loosen
@iobroker/types
peer dependency
- (Apollon77) Fix types
- (@foxriver76) updated types
- (@GermanBluefox) Added i18n module
- (@GermanBluefox) Migrated eslint to iobroker.eslint-config
- (foxriver76) improve exported types
- (foxriver76) provide up-to-date types
- (foxriver76) fixes for adapters which are written in ESM
- (foxriver76) fixes for cjs adapters
- (foxriver76) fixes for adapters which are written in ESM
- (foxriver76) removed letsEncrypt support as next controller won't support it anymore (use
@iobroker/webserver
instead)
- (foxriver76) provide
esm
andcjs
exports
- (foxriver76) hotfix previous release: compatibility with next controller with new esm/cjs exports
- (foxriver76) compatibility with next controller with new esm/cjs exports
- (foxriver76) ensure that utility methods work with the returned adapter instance on type level
- (foxriver76) upgrade to a new version of types package
- (foxriver76) fix require of
@iobroker/types
in built module
- (foxriver76) remove non-existing webserver from tools
- (foxriver76) port from
@types/iobroker
to@iobroker/types
- (foxriver76) export dns resolution methods
- BREAKING: requires
npm
v7 or newer and/or Node.js 16 or newer
- (Apollon77) Expose more JS-Controller internals under the
commonTools
export
- (Apollon77) Expose more JS-Controller internals under the
commonTools
export
- (AlCalzone) Expose more JS-Controller internals under the
commonTools
export
- (AlCalzone) Fix: Restore compatibility with JS-Controller < 4.1
- (AlCalzone) Fix: detecting JS-Controller now finds the correct directory and not a subdirectory.
- (AlCalzone) Updated core declarations to
v4.0.1
for support with JS-Controller 4.x
- (AlCalzone) Updated core declarations to
v3.3.4
.
- (AlCalzone) Added the fallback solution to detect js-controller if require.resolve fails in dev situations with symlinks
- (AlCalzone) Use release-script for releases
- (AlCalzone) Updated core declarations to
v3.3.0
to be up to date with JS-Controller 3.3.x.
- (AlCalzone) Updated core declarations to v3.0.6.
- (AlCalzone) Expose the predefined collection of adapter exit codes as
utils.EXIT_CODES
- (AlCalzone) Updated core declarations to v3.0.4.
- (AlCalzone) Updated core declarations to v3.0.2. This includes support for new methods in JS-Controller 3.0
- (AlCalzone) Included the typings for the objects and states cache in the adapter class
- (AlCalzone) Updated core declarations to v2.0.0. This removes access to
adapter.objects
andadapter.states
. You must use the new methodsadapter.getObjectView
andadapter.getObjectList
instead of their counterparts fromobjects
.
- (AlCalzone) Updated core declarations
- (AlCalzone) Fix included declarations to allow creating adapter instances with
new
.
- (AlCalzone) Initial version
Copyright (c) 2018-2024 AlCalzone
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.