Skip to content

Add shared MQTT client and ResourceManagement-based MQTT integration service #660

@1nf0rmagician

Description

@1nf0rmagician

Summary

  • We propose adding:
    • A shared, reusable MQTT client service configured from appsettings, to be registered in the service collection and usable across multiple features.
    • A Resource MQTT integration service that uses IResourceManagement and IResourceManagementChanges to publish resource events and handle subscriptions via the shared MQTT client.
    • A design that allows future reuse of the same MQTT client in a similar service built on IProductManagement.

Motivation

Proposed Design

  • Configuration

    • Add a typed options class bound from appsettings:
    • Section: Mqtt
    • Properties:
      • Host (string)
      • Port (int)
      • ClientId (string)
      • Username (string, optional)
      • Password (string, optional/secret)
      • UseTls (bool, default false)
      • CleanSession (bool, default true)
      • KeepAliveSeconds (int, default 30)
      • ReconnectDelaySeconds (int, default 5)
      • TopicPrefix (string, default "moryx")
      • QoS (int 0..2, default 1)
      • Retain (bool, default false)
  • Shared MQTT client abstraction IMqttClientService

    • Lifetime: Singleton, registered in DI, and also implements IHostedService for lifecycle management (connect on start, disconnect on stop).
    • Uses MQTTnet client options built from MqttOptions to handle connection, TLS, credentials, keep-alive, and auto-reconnect logic.
    • Logs connection state changes and errors.
    • Designed for reuse by a future ProductManagement MQTT service.
  • Resource MQTT integration service

    • A BackgroundService (or IHostedService) that depends on:
      • IResourceManagement (from ResourceManagementFacade)
      • IResourceManagementChanges (from ResourceManagementFacade)
      • IMqttBus
      • IOptions
      • ILogger
    • Responsibilities:
      • Subscribe to IResourceManagement events:
        • ResourceAdded
        • ResourceRemoved
      • Publish event messages to MQTT topics. Suggested topics:
        • {TopicPrefix}/resources//added
        • {TopicPrefix}/resources//removed
    • Payload: json serialization of resource object (using System.Text.Json with Web defaults)
      • respect ISerializable interface and IResourceSerializable interface for resources
    • Ensure ValidateHealthState is respected by only accessing facade operations in event callbacks or on Start when module is healthy.
  • Future extension: Product MQTT integration service

    • A separate service (not part of this change set, but supported by the shared IMqttClientService) that would:
    • Depend on IProductManagement
    • Reuse same IMqttClientService instance and configuration.
  • Registration

    • Provide an extension method to register everything in DI, similar to AddControllers

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions