Class KobotsMQTT

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    
    public final class KobotsMQTT
     implements AutoCloseable
                        

    MQTT wrapper for Kobots.

    Because this is still in the experimental phase, QoS is always 0 and each session starts "clean" (the broker and clients do not retain "state"). Auto-reconnect is enabled and any subscriptions are re-subscribed on re-connect.

    Because of the current default Qos, any actual persistence is not used.

    • Constructor Detail

    • Method Detail

      • startAliveCheck

         final Unit startAliveCheck(Long intervalSeconds)

        Start an "alive check"/heartbeat message. This is a message published to the KOBOTS_ALIVE topic.

        If the connection is lost, the heartbeat will stop and restart when the connection is re-established.

      • subscribe

         final Unit subscribe(String topic, Function1<String, Unit> listener)

        Subscribe to a topic. The listener is called when a message is received.

        Note that adding a subscriber multiple times may cause issues with the underlying MQTT client.

      • subscribeJSON

         final Unit subscribeJSON(String topic, Function1<JSONObject, Unit> handler)

        Subscribe to a topic. The handler is called when a message is received and converted to a JSONObject.

        Note that adding a subscriber multiple times may cause issues with the underlying MQTT client.

      • unsubscribe

         final Unit unsubscribe(String topic)

        Unsubscribe from a topic. NOTE This removes all listeners for the given topic.

      • addTopicListener

         final Unit addTopicListener(String topic, Function2<String, ByteArray, Unit> listener)

        Add a listener to a topic. Exposed to the rest of the library for more extended use. Errors are trapped, so if there is a problem with the listener, it will be logged and ignored.

        TODO should this be public?

      • publish

         final <Error class: unknown class> publish(String topic, JSONObject payload, Boolean retain)

        Publish a JSON message to the topic. All errors are only logged, keeping in line with a QoS of 0.

      • publish

         final <Error class: unknown class> publish(String topic, String payload, Boolean retain)

        Publish a String message to the topic. All errors are only logged, keeping in line with a QoS of 0.

      • publish

         final Unit publish(String topic, ByteArray payload, Boolean retain)

        Publish a message to the topic. All errors are only logged, keeping in line with a QoS of 0.

      • set

         final <Error class: unknown class> set(String topic, JSONObject payload)

        Quasi-extension operator that allows publishing by using map notation:

        mqtt["topic"] = "payload"
      • set

         final <Error class: unknown class> set(String topic, String payload)