IMoBroPlugin
Each plugin must contain exactly one class that extends the IMoBroPlugin
interface.
This class represents the actual 'plugin' and will automatically be instantiated by the MoBro data service once the
plugin is loaded.
In order for MoBro to be able to instantiate the plugin, it must contain a public constructor.
Services may be injected via the constructor (more on that in In depth: Using services).
Functions
The IMoBroPlugin
interface defines several functions that may be utilized by the plugin by implementing them. These
functions are automatically called by the MoBro data service at specific times of the plugins life cycle or on
specific events as outlined below.
All of these functions are optional. A plugin can be implemented without implementing any of them.
Init()
Called once upon initialization of the plugin.
Any (potentially longer running) initialization code that should be run after the constructor call should be placed
here.
This is a great place to put or invoke the logic to register metrics, update metric values or register scheduler
functions.
This way the actual business logic (creating and updating metrics) can be separated from the plugin
creation (the constructor call).
Example
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
private readonly IMoBroScheduler _scheduler;
private readonly IMoBroSettings _settings;
public Plugin(IMoBroService mobro, IMoBroScheduler scheduler, IMoBroSettings settings)
{
_mobro = mobro;
_scheduler = scheduler;
_settings = settings;
}
public void Init()
{
// create and register all metrics
CreateAndRegisterMetrics();
// set the value for the static metrics
SetStaticMetricValues();
// get the value of the setting for the update frequency
var updateFrequencySetting = _settings.GetValue<int>("update_frequency");
// schedule a recurring task to update the dynamic metrics
_scheduler.Interval(UpdateDynamicMetrics, TimeSpan.FromSeconds(updateFrequencySetting), TimeSpan.FromSeconds(5));
}
[...]
}
InitAsync(): Task
Same as Init, just async.
Pause()
Called to signal the plugin that it should pause monitoring and stop sending metric value updates, etc.
Any data sent while the plugin is 'paused' may be ignored by the MoBro data service.
This function is mostly called due to the MoBro data service switching into 'idle mode' as no client has requested data for a prolonged time. Pausing sensor monitoring, etc. during times when the values are not requested can help to reduce overall system load.
The IMoBroScheduler already handles the 'Pause' and 'Resume' events.
There is no need for a custom Pause/Resume implementation if the scheduler is used to trigger the metric value updates.
Example
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
private readonly IMoBroScheduler _scheduler;
private readonly IMoBroSettings _settings;
public Plugin(IMoBroService mobro, IMoBroScheduler scheduler, IMoBroSettings settings)
{
_mobro = mobro;
_scheduler = scheduler;
_settings = settings;
}
public void Pause()
{
// custom logic to pause the plugin
// e.g. stop polling the sensors and sending metric value updates
}
}
Resume()
Called to signal the plugin that it should resume monitoring and restart sending metric value updates, etc.
Will only be invoked if the plugin has been paused previously by a call to Pause.
Example
public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
private readonly IMoBroScheduler _scheduler;
private readonly IMoBroSettings _settings;
public Plugin(IMoBroService mobro, IMoBroScheduler scheduler, IMoBroSettings settings)
{
_mobro = mobro;
_scheduler = scheduler;
_settings = settings;
}
public void Resume()
{
// custom logic to resume the plugin
// e.g. restart polling the sensors and send metric value updates
}
}