Skip to main content

Using Services

The MoBro Plugin SDK provides several services designed to simplify common tasks in a plugin, such as managing settings or implementing recurring tasks to poll sensor values.

Dependency Injection​

Services are injected into the plugin's constructor. Simply add a constructor parameter for every service the plugin needs:

Plugin.cs
using Microsoft.Extensions.Logging;
using MoBro.Plugin.SDK;
using MoBro.Plugin.SDK.Services;

namespace Plugin.Example;

public class Plugin : IMoBroPlugin
{
private readonly IMoBroService _mobro;
private readonly IMoBroScheduler _scheduler;
private readonly IMoBroSettings _settings;
private readonly ILogger _logger;

public Plugin(IMoBroService mobro, IMoBroScheduler scheduler, IMoBroSettings settings, ILogger logger)
{
_mobro = mobro;
_scheduler = scheduler;
_settings = settings;
_logger = logger;
}
}
Constructor Requirements
  • The plugin class must have exactly one public constructor.
  • The constructor may only declare parameters of the service types listed below. Any other parameter causes the plugin to fail on startup.
  • Keep the constructor lightweight. Registering items and starting work belongs in Init().

Injectable Services​

The following services can be injected into the plugin's constructor:

  • IMoBroService:
    Enables plugins to interact with MoBro by registering items, updating metric values, and more.
  • IMoBroSettings:
    Provides access to the plugin settings values.
  • IMoBroScheduler:
    A scheduler for recurring tasks, such as polling sensor values or querying external APIs.
  • ILogger:
    An instance of Microsoft.Extensions.Logging.ILogger for logging within the plugin.
    Only the non-generic ILogger can be injected. Declaring a generic ILogger<T> (e.g. ILogger<Plugin>) causes the plugin to fail on startup.
  • IMoBroFileManager:
    Allows plugins to read and write files in their data directory.
  • IMoBroPersistenceManager:
    Simplifies the serialization and persistence of arbitrary objects.

Threading​

  • All services can safely be used from any thread.
  • Scheduled tasks run on background threads, and different tasks may run at the same time. Protect any state inside the plugin that is shared between scheduled tasks, action handlers and Init(), e.g. by using a lock or concurrent collections.