index.js methods
index.js exports one class. Volumio creates an instance with new and calls the methods below. This page covers plugin API 1, the API Volumio 4 runs. The Browse, play and search methods of a music_service are in Music service API. How a plugin runs explains the lifecycle.
Every method Volumio calls returns a kew promise (require('kew')), except constructor and getConfigurationFiles. An async method or a native Promise fails Volumio's check.
Methods Volumio calls
| Method | Required | Volumio calls it | Returns |
|---|---|---|---|
constructor(context) | Yes | When it loads an enabled plugin | The instance |
getConfigurationFiles() | Yes | Before onVolumioStart | An array of file names |
onVolumioStart() | Yes | Right after loading | kew promise |
onStart() | Yes | After onVolumioStart | kew promise |
onStop() | Yes | When the user disables the plugin | kew promise |
getUIConfig() | With a settings page | When the user opens the settings page | kew promise of the page |
The onSave method | No | When the user selects Save on the settings page | kew promise |
onVolumioShutdown() | No | Before the device powers off | kew promise |
onVolumioReboot() | No | Before the device reboots | kew promise |
constructor(context)
Volumio passes a context object. Keep it and do no other work.
| Property | Holds |
|---|---|
context.coreCommand | The command router. The template keeps it as this.commandRouter. |
context.logger | The Volumio logger. The template keeps it as this.logger. |
constructor(context) {
this.context = context;
this.commandRouter = context.coreCommand;
this.logger = context.logger;
}
getConfigurationFiles()
Returns the configuration files Volumio copies to /data/configuration/<type>/<id>/ the first time the plugin runs. Returns a plain array, not a promise.
getConfigurationFiles() {
return ['config.json'];
}
onVolumioStart()
Loads the saved settings with v-conf, from Volumio's own copy at /volumio/node_modules/v-conf. Resolves quickly: at boot Volumio waits at most 30 seconds for all plugins.
onVolumioStart() {
this.config = new VConf();
this.config.loadFile(this.commandRouter.pluginManager.getConfigurationFile(this.context, 'config.json'));
return libQ.resolve();
}
this.config.get(key) reads a setting. this.config.set(key, value) writes it and saves it to disk.
onStart()
Starts the plugin's work: connections, systemd units, Browse entries. Resolves quickly and does long work afterwards. At boot Volumio waits at most 30 seconds for all plugins' onStart, then goes on without the slow ones. A rejected onStart leaves the plugin failed.
onStop()
Undoes everything onStart did: stops timers, closes sockets, stops child processes and systemd units, removes Browse entries.
getUIConfig()
Resolves the settings page: UIConfig.json with its text translated and the saved values filled in. See Add a settings page.
getUIConfig() {
const lang = this.commandRouter.sharedVars.get('language_code');
return this.commandRouter
.i18nJson(this.file(`i18n/strings_${lang}.json`), this.file('i18n/strings_en.json'), this.file('UIConfig.json'))
.then((uiconf) => {
uiconf.sections[0].content[0].value = this.config.get('greeting');
return uiconf;
});
}
The onSave method
The method named in a section's onSave, in UIConfig.json. It gets one object, { <field id>: value }, with the fields listed in the section's saveButton.data.
saveSettings(data) {
this.config.set('greeting', data.greeting);
return libQ.resolve();
}
A button with onClick and a modal button with emit: 'callMethod' call a plugin method the same way, with their data as the argument.
onVolumioShutdown(), onVolumioReboot()
Optional. Volumio calls them before it powers off or reboots the device. Use them to save state that would otherwise be lost.
Calls into Volumio
A plugin calls Volumio through this.commandRouter. These calls make up plugin API 1:
| Call | Does | Returns |
|---|---|---|
pushToastMessage(type, title, message) | Shows a toast. type is success, info, warning or error. | kew promise |
broadcastMessage(event, payload) | Sends a message to every open Volumio UI, such as openModal | kew promise |
sharedVars.get('language_code') | The user's language, such as en | string |
pluginManager.getConfigurationFile(context, name) | The path of a configuration file on the device | string |
i18nJson(languageFile, englishFile, jsonFile) | jsonFile with its TRANSLATE. keys translated, English for missing keys | kew promise |
executeOnPlugin(type, id, method, data) | Calls a method of another plugin | What the method returns, undefined when the plugin is not installed |
volumioAddToBrowseSources(source) | Adds a music_service's entry to Browse | |
volumioRemoveToBrowseSources(name) | Removes a Browse entry by name | |
volumioUpdateToBrowseSources(name, source) | Changes a Browse entry in place | |
servicePushState(state, id) | Tells Volumio what a music_service that plays by itself is playing |
Plugins that need them also use these calls:
| Call | Does | See |
|---|---|---|
addDSPSignalPathElement(element) | Reports the plugin as an element of the signal path | Report DSP elements |
removeDSPSignalPathElement({ id }) | Removes that element | Report DSP elements |
registerThirdPartyUI({ uiPrettyName, uiName, uiPath }) | Adds a user interface users can pick | Add your own user interface |
A plugin never assigns to a command router method and never loads Volumio's own files with require.
The logger
this.logger has four methods: info, warn, error and debug. Each takes one string. See Log from a plugin.