DeviceHive custom plugin core functionality. NodeJS implementation
npm install devicehive-plugin-core[DeviceHive]: https://devicehive.com "DeviceHive framework"
[Message]: https://github.com/devicehive/devicehive-proxy-message "Message"
[DeviceHive WS Proxy]: https://github.com/devicehive/devicehive-ws-proxy "DeviceHive WS Proxy"
javascript
const { DeviceHivePlugin } = require(devicehive-plugin-core);
class PluginService extends DeviceHivePlugin {
beforeStart() {}
afterStart() {}
handleMessage(message) {}
beforeStop() {}
onError(error) {}
}
`
#### Plugin lifecycle hooks
beforeStart() {}
afterStart() {
handleMessage(message) {}
beforeStop() {}
onError(error) {}
1) _beforeStart_ - This hook fires before plugin will do try to connect to [DeviceHive] WS plugin server
2) _afterStart_ - This hook fires after plugin successfully connects to [DeviceHive] WS plugin server
3) _handleMessage_ - This hook fires on every incoming [Message] from [DeviceHive]
4) _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)
5) _onError_ - This hook fires on every internal error (critical/non critical)
#### Plugin API
DeviceHivePlugin class has few methods that are defined internally by core functionality:
sendMessage(message) {}
subscribe(subscriptionGroup) {}
unsubscribe() {}
1) _sendMessage_ - Sends [Message] object to WS plugin server. Returns Promise with response/error
2) _subscribe_ - Subscribes to plugin topic with optionally mentioned subscription group
3) _unsubscribe_ - Unsubscribes from plugin topic
#### Plugin entry point
To start plugin you should use next static method of DeviceHivePlugin class:
DeviceHivePlugin.start(, , []);
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:
`javascript
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");
``