IMoBroService
The IMoBroService allows plugins to interact with MoBro. It is used to register items, update metric values, report
errors, and more.
Registrations and metric value updates are queued and sent to MoBro asynchronously in batches. All functions return immediately and can safely be called from any thread.
Functions
Register(IEnumerable<IMoBroItem>)
Registers multiple items with the service.
Parameters
| Name | Type | Description |
|---|---|---|
| items | IEnumerable<IMoBroItem> | The items to register. |
Example
IEnumerable<IMoBroItem> metrics = CreateMetrics();
_mobro.Register(metrics);
Register(IMoBroItem)
Registers a single item with the service.
Parameters
| Name | Type | Description |
|---|---|---|
| item | IMoBroItem | The item to register. |
Example
var metric = MoBroItem
.CreateMetric()
.WithId("os_name")
.WithLabel("Operating System")
.OfType(CoreMetricType.Text)
.OfCategory(CoreCategory.System)
.OfNoGroup()
.AsStaticValue()
.Build();
_mobro.Register(metric);
Items that fail validation (e.g. an invalid ID or a reference to an unregistered category) are not registered. Inside
MoBro, the item is skipped and a warning is logged. When running locally, a
MoBroItemValidationException is thrown instead, so you notice such problems during development.
See Validation and Limits.
Unregister(IEnumerable<string>)
Unregisters multiple items from the service.
Parameters
| Name | Type | Description |
|---|---|---|
| ids | IEnumerable<string> | The IDs of the items to unregister. |
Example
_mobro.Unregister(["os_name", "cpu_usage"]);
Unregister(string)
Unregisters a single item from the service.
Parameters
| Name | Type | Description |
|---|---|---|
| id | string | The ID of the item to unregister. |
Example
_mobro.Unregister("os_name");
GetAll(): IEnumerable<IMoBroItem>
Retrieves all registered items.
Example
foreach (var item in _mobro.GetAll())
{
_logger.LogInformation("Item: {ItemId}", item.Id);
}
GetAll<T>(): IEnumerable<T>
Retrieves all registered items of a specific type. T must implement IMoBroItem.
Example
foreach (var metric in _mobro.GetAll<Metric>())
{
_logger.LogInformation("Metric: {MetricId} - {Label}", metric.Id, metric.Label);
}
TryGet<T>(string, out T): bool
Retrieves the registered item of type T with the specified ID.
Parameters
| Name | Type | Description |
|---|---|---|
| id | string | The ID of the item to retrieve. |
| item | T (out) | Contains the registered item if found; otherwise null. |
Returns
true if an item with the given ID and type is registered; otherwise false.
Example
if (_mobro.TryGet<Metric>("os_name", out var metric))
{
_logger.LogInformation("Metric: {Label}", metric.Label);
}
ClearRegistration()
Unregisters all currently registered items.
UpdateMetricValue(string, object?)
Pushes a new value for a registered metric, with the timestamp automatically set to DateTime.UtcNow.
Parameters
| Name | Type | Description |
|---|---|---|
| id | string | The ID of the metric. |
| value | object? | The new value of the metric. |
Example
_mobro.UpdateMetricValue("cpu_usage", 42.69);
UpdateMetricValue(string, object?, DateTime)
Pushes a new value for a registered metric, measured at the given point in time.
Parameters
| Name | Type | Description |
|---|---|---|
| id | string | The ID of the metric. |
| value | object? | The new value of the metric. |
| timestamp | DateTime | The date and time the value was measured, in UTC. |
Example
_mobro.UpdateMetricValue("cpu_usage", 42.69, DateTime.UtcNow);
UpdateMetricValue(in MetricValue)
Pushes a single new MetricValue.
Example
_mobro.UpdateMetricValue(new MetricValue("cpu_usage", DateTime.UtcNow, 42.69));
UpdateMetricValues(IEnumerable<MetricValue>)
Pushes new values for one or more registered metrics at once.
Example
IEnumerable<MetricValue> metricValues = ReadAllSensors();
_mobro.UpdateMetricValues(metricValues);
All UpdateMetricValue(s) functions throw a MetricValueValidationException if a value does not match the
metric's MetricValueType. If not caught, this stops the
plugin. Updates for metrics that are not registered are ignored.
See Updating Metric Values.
GetMetricValues(): IEnumerable<MetricValue>
Gets the most recent values of all metrics.
Example
foreach (var metricValue in _mobro.GetMetricValues())
{
_logger.LogInformation("{MetricId}: {Value}", metricValue.Id, metricValue.Value);
}
GetMetricValue(string): MetricValue?
Gets the most recent value of the metric with the specified ID.
Returns
The current MetricValue of the metric, or null if no value has been set yet.
Example
if (_mobro.GetMetricValue("os_name") is { } osName)
{
_logger.LogInformation("Current OS: {OsName}", osName.Value);
}
SetDependencyStatus(string, DependencyStatus)
Reports the current status of an external dependency declared in the dependencies field
of mobro_plugin_config.json. MoBro displays this status to the
user, e.g. to point out that a required third-party program is not installed.
For dependencies marked as required, MoBro shows the plugin as dependencies missing until the status Ok has been
reported. See Required Dependencies.
Parameters
| Name | Type | Description |
|---|---|---|
| dependencyName | string | The name of the dependency as defined in the plugin configuration. |
| dependencyStatus | DependencyStatus | The current status of the dependency. |
DependencyStatus
| Value | Description |
|---|---|
Unknown | The status of the dependency is unknown. |
Ok | The dependency is available and working. |
Missing | The dependency is missing and cannot be used. |
Outdated | The dependency is outdated and should be updated. |
Example
if (!IsAida64Running())
{
_mobro.SetDependencyStatus("aida64", DependencyStatus.Missing);
return;
}
_mobro.SetDependencyStatus("aida64", DependencyStatus.Ok);
Error(string)
Notifies MoBro of an unrecoverable error. MoBro stops the plugin and sets its state to Error, displaying the given
message.
Example
_mobro.Error("Sensor library could not be initialized");
Error(Exception)
Notifies MoBro of an unrecoverable error caused by an exception. MoBro stops the plugin and sets its state to Error.
Example
try
{
InitializeSensors();
}
catch (Exception e)
{
_logger.LogError(e, "Failed to initialize sensors");
_mobro.Error(e);
}