Class MqttService

java.lang.Object
com.framed.core.Service
com.framed.interop.mqtt.MqttService

public final class MqttService extends Service
Bidirectional MQTT bridge. A single broker connection both publishes FRAMED observations and subscribes to inbound topics, keeping the framework's internal flow on the EAV DataPoint model — only the wire payload is MQTT/JSON.

Outbound: discovers each configured device's channels (the standard announceAddress/addressRegistry handshake), applies the EmissionGate, and publishes to "<topicPrefix>/<deviceID>/<channelID>". Only mapped channels are emitted unless includeUnmapped is set.

Inbound: for each subscribed topic, decodes the payload and republishes it as a "<className>.<deviceID>.<channelID>.parsed" bus event.

This is a single bidirectional Service (rather than a Dispatcher + Protocol pair) because one MQTT client connection inherently serves both directions; the Paho client is already asynchronous, so no extra worker is needed to keep the bus handler non-blocking.

Broker availability: a broker that is down at startup is not fatal — the bridge logs a warning and a daemon thread retries connect+subscribe until it succeeds or the service stops (the deployment must not lose its MQTT leg permanently to a startup race). Once the initial connect succeeded, later connection drops are healed by the transport's automatic reconnect. Outbound publishes while disconnected fail and are dropped with a warning, throttled by the gate's interval filter.

  • Constructor Details

    • MqttService

      public MqttService(EventBus eventBus, String id, String brokerUrl, String clientId, org.json.JSONArray devices, org.json.JSONArray subscribeTopics, String topicPrefix, int qos, String mappingPath, org.json.JSONObject gate, boolean includeUnmapped, String inboundDeviceId)
      Config-loadable constructor.
      Parameters:
      eventBus - the event bus
      id - the service id
      brokerUrl - MQTT broker URI (e.g. tcp://127.0.0.1:1883)
      clientId - MQTT client id
      devices - device groups whose announced channels are forwarded outbound
      subscribeTopics - inbound topic filters (may be empty)
      topicPrefix - prefix for published topics (e.g. framed)
      qos - MQTT QoS for publish/subscribe
      mappingPath - path to the interop mapping JSON
      gate - emission gate config (onChange, minIntervalMs)
      includeUnmapped - if true, publish unmapped channels too (no coded concept in payload)
      inboundDeviceId - device id for inbound messages whose payload/topic lacks one
  • Method Details

    • stop

      public void stop()
      Description copied from class: Service
      Stops the service.

      By default, this method logs a message indicating that no stop logic is implemented. Subclasses should override this method to provide custom shutdown behavior when necessary.

      Overrides:
      stop in class Service