public class ThermostatMQTT extends PeriodicPublisherCloud implements MqttCallback
TOPIC_IP, TOPIC_SET, TOPIC_TARGET, TOPIC_TEMPERATURE
Constructor and Description |
---|
ThermostatMQTT(String clientID,
String uri)
Instantiates a Thermostat Cloud connector over tcp MQTT.
|
Modifier and Type | Method and Description |
---|---|
void |
connectionLost(Throwable cause)
This method is called when the connection to the server is lost.
|
void |
deliveryComplete(IMqttDeliveryToken token)
Called when delivery for a message has been completed, and all acknowledgments have been received.
|
protected void |
doConnect()
Connects to the cloud.
|
protected void |
doDisconnect()
Disconnects from the cloud.
|
protected void |
doPublish(String topic,
String ip)
Publishes to a topic.
|
String |
getName()
Gets the listener name, can be
null . |
boolean |
isConnected()
Checks whether the listener is connected.
|
void |
messageArrived(String topic,
MqttMessage message)
This method is called when a message arrives from the server.
|
protected void |
subscribe(String topic)
Subscribes to a topic.
|
disconnect, isReady, onTargetChange, onTemperatureChange, publishIP, receive, setIsConnected, setThermostat, start, stop
isError
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
public void connectionLost(Throwable cause)
MqttCallback
connectionLost
in interface MqttCallback
cause
- the reason behind the loss of connection.public void deliveryComplete(IMqttDeliveryToken token)
MqttCallback
deliveryComplete
in interface MqttCallback
token
- the delivery token associated with the message.protected void doConnect() throws IOException
PeriodicPublisherCloud
doConnect
in class PeriodicPublisherCloud
IOException
- when the connect failed.protected void doDisconnect()
PeriodicPublisherCloud
doDisconnect
in class PeriodicPublisherCloud
protected void doPublish(String topic, String ip) throws IOException
PeriodicPublisherCloud
doPublish
in class PeriodicPublisherCloud
topic
- the topic to subscribe to.ip
- the message to publish.IOException
- if an IOException
occurred.public String getName()
CloudConnector
null
.getName
in class CloudConnector
null
.public boolean isConnected()
CloudConnector
isConnected
in class PeriodicPublisherCloud
true
if the connector is connected.public void messageArrived(String topic, MqttMessage message) throws Exception
MqttCallback
This method is invoked synchronously by the MQTT client. An acknowledgment is not sent back to the server until this method returns cleanly.
If an implementation of this method throws an Exception
, then the client will be shut down. When the
client is next re-connected, any QoS 1 or 2 messages will be redelivered by the server.
Any additional messages which arrive while an implementation of this method is running, will build up in memory, and will then back up on the network.
If an application needs to persist data, then it should ensure the data is persisted prior to returning from this method, as after returning from this method, the message is considered to have been delivered, and will not be reproducible.
It is possible to send a new message within an implementation of this callback (for example, a response to this message), but the implementation must not disconnect the client, as it will be impossible to send an acknowledgment for the message being processed, and a deadlock will occur.
messageArrived
in interface MqttCallback
topic
- name of the topic on the message was published tomessage
- the actual message.Exception
- if a terminal error has occurred, and the client should be shut down.protected void subscribe(String topic) throws IOException
PeriodicPublisherCloud
subscribe
in class PeriodicPublisherCloud
topic
- the topic to subscribe to, cannot be null
.IOException
- if an IOException
occurred.