This module makes it possible to quickly and easily create DeviceHive plugins using NodeJS.
PluginCore class implements basic interaction functionality with DeviceHive service. User is not able to use it.
ProxyClient class implements basic transport functionality with DeviceHive WS Proxy service (in plugin mode). User is not able to use it.
DeviceHivePlugin class implements interface for user's plugin service classes. User is able to extends their own plugin services from the DeviceHivePlugin class.
const { DeviceHivePlugin } = require(`devicehive-plugin-core`);
class PluginService extends DeviceHivePlugin {
beforeStart() {}
afterStart() {}
handleMessage(message) {}
beforeStop() {}
onError(error) {}
}
beforeStart() {}
afterStart() {
handleMessage(message) {}
beforeStop() {}
onError(error) {}
- beforeStart - This hook fires before plugin will do try to connect to DeviceHive WS plugin server
- afterStart - This hook fires after plugin successfully connects to DeviceHive WS plugin server
- handleMessage - This hook fires on every incoming Message from DeviceHive
- beforeStop - This hook fires before plugin will stop it's own process because of some critical reason (For example, WS plugin serer closes the connection)
- onError - This hook fires on every internal error (critical/non critical)
DeviceHivePlugin class has few methods that are defined internally by core functionality:
sendMessage(message) {}
subscribe(subscriptionGroup) {}
unsubscribe() {}
- sendMessage - Sends Message object to WS plugin server. Returns Promise with response/error
- subscribe - Subscribes to plugin topic with optionally mentioned subscription group
- unsubscribe - Unsubscribes from plugin topic
To start plugin you should use next static method of DeviceHivePlugin class:
DeviceHivePlugin.start(<pluginService>, <config>, [<envPrefix>]);
where:
- pluginService - instance of User's own DeviceHivePlugin implementation
- config - configuration object or path to configuration json file. See Configuration section
- envPrefix - prefix to add to environmental variables to override configuration fields
Example:
const { DeviceHivePlugin } = require(`devicehive-plugin-core`);
class PluginService extends DeviceHivePlugin {
beforeStart() {}
afterStart() {}
handleMessage(message) {}
beforeStop() {}
onError(error) {}
}
DeviceHivePlugin.start(new PluginService(), {
DEVICE_HIVE_PLUGIN_WS_ENDPOINT: "ws://localhost:3001",
DEVICE_HIVE_AUTH_SERVICE_API_URL: "http://localhost:8090/dh/rest",
PLUGIN_ACCESS_TOKEN: "plugin_access_token",
AUTO_SUBSCRIPTION_ON_START: true
}, "MY_PLUGIN_SERVICE");
- DEVICE_HIVE_PLUGIN_WS_ENDPOINT - Path to DeviceHive WS server with plugin support
- DEVICE_HIVE_AUTH_SERVICE_API_URL - Path to DeviceHive Auth REST API service
- PLUGIN_TOPIC - Plugin topic
- PLUGIN_TOKEN_LIFE_TIME_MIN - Plugin topic lifetime in minutes. Optional parameter
- USER_LOGIN - User login (plugin owner ar administrator). Optional parameter
- USER_PASSWORD - User password (plugin owner ar administrator). Optional parameter
- USER_ACCESS_TOKEN - User access token (plugin owner ar administrator). Optional parameter
- USER_REFRESH_TOKEN - User refresh token (plugin owner ar administrator). Optional parameter
- PLUGIN_ACCESS_TOKEN - Plugin access token. Optional parameter
- PLUGIN_REFRESH_TOKEN - Plugin refresh token. Optional parameter
- AUTO_SUBSCRIPTION_ON_START - Flag to on/off auto subscription to plugin topic on plugin start
Each configuration field can be overridden with corresponding environmental variable with "PLUGIN" prefix, for example:
PLUGIN.PLUGIN_TOKEN_LIFE_TIME_MIN=60
Prefix separator can be overridden by ENVSEPARATOR environmental variable. Example:
ENVSEPARATOR=_
PLUGIN_PLUGIN_TOKEN_LIFE_TIME_MIN=60
For plugin authentication next configuration combinations can bu used:
- PLUGIN_ACCESS_TOKEN
- PLUGIN_REFRESH_TOKEN
- USER_ACCESS_TOKEN + PLUGIN_TOPIC + DEVICE_HIVE_AUTH_SERVICE_API_URL
- USER_REFRESH_TOKEN + PLUGIN_TOPIC + DEVICE_HIVE_AUTH_SERVICE_API_URL
- USER_LOGIN + USER_PASSWORD + PLUGIN_TOPIC + DEVICE_HIVE_AUTH_SERVICE_API_URL
For 3-5 combination the PLUGIN_TOKEN_LIFE_TIME_MIN configuration field can be mentioned.