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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public interface
KobotsMQTT.ConnectListener
-
Field Summary
Fields Modifier and Type Field Description public final static String
KOBOTS_ALIVE
public final static String
KOBOTS_EVENTS
-
Constructor Summary
Constructors Constructor Description KobotsMQTT(String clientName, String broker)
-
Method Summary
Modifier and Type Method Description final Unit
startAliveCheck(Long intervalSeconds)
Start an "alive check"/heartbeat message. final Unit
handleAliveCheck(Function1<String, Unit> listener, Long deadIntervalSeconds)
Listens for KOBOTS_ALIVE
messages and tracks the last time a message was received from each host.final Unit
subscribe(String topic, Function1<String, Unit> listener)
Subscribe to a topic. final Unit
subscribeJSON(String topic, Function1<JSONObject, Unit> handler)
Subscribe to a topic. final Unit
unsubscribe(String topic)
Unsubscribe from a topic. final Unit
unsubscribe()
Unsubscribe from all topics. final Unit
addTopicListener(String topic, Function2<String, ByteArray, Unit> listener)
Add a listener to a topic. final <Error class: unknown class>
publish(String topic, JSONObject payload, Boolean retain)
Publish a JSON message to the topic. final <Error class: unknown class>
publish(String topic, String payload, Boolean retain)
Publish a String message to the topic. final Unit
publish(String topic, ByteArray payload, Boolean retain)
Publish a message to the topic. final <Error class: unknown class>
set(String topic, JSONObject payload)
Quasi-extension operator that allows publishing by using map notation: mqtt["topic"] = "payload"
final <Error class: unknown class>
set(String topic, String payload)
final Unit
set(String topic, ByteArray payload)
Unit
close()
final Unit
addConnectListener(KobotsMQTT.ConnectListener listener)
-
-
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.
-
handleAliveCheck
final Unit handleAliveCheck(Function1<String, Unit> listener, Long deadIntervalSeconds)
Listens for
KOBOTS_ALIVE
messages and tracks the last time a message was received from each host. The listener is called when a host has not been seen for deadIntervalSeconds seconds.
-
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.
-
unsubscribe
final Unit unsubscribe()
Unsubscribe from all topics.
-
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"
-
addConnectListener
final Unit addConnectListener(KobotsMQTT.ConnectListener listener)
-
-
-
-