Package ej.aws.iot
Class AwsIotClient
- java.lang.Object
-
- ej.aws.iot.AwsIotClient
-
public class AwsIotClient extends Object
AWS IoT client, based on MicroPaho MQTT Implementation.
-
-
Constructor Summary
Constructors Constructor Description AwsIotClient(AwsIotClientOptions options)Constructor client.AwsIotClient(AwsIotClientOptions options, AwsIotConnectionListener connectionCallback)Constructor client.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close this client.voidconnect()Connect to AWS IoT service using the client options.protected voidconnectionLost(Throwable cause)Dispatch connection lost event if anAwsIotConnectionListenerwas registered.voiddeleteShadow()Delete the classic shadow of thing with QOS 0.voiddeleteShadow(int qos)Delete the classic shadow of thing.voiddeleteShadow(String shadowName)Delete the named shadow of thing with QOS 0.voiddeleteShadow(String shadowName, int qos)Delete the named shadow of thing.voiddisconnect()Disconnect this client from the underlying communication layer.protected voiddispatch(AwsIotMessage message)dispatch message to corresponding subscriber if any otherwise log a warning if warning logs are activatedAwsIotClientOptionsgetClientOptions()Gets the client options.voidgetShadow()Gets shadow with default QOS 0.voidgetShadow(int qos)Gets shadow.voidgetShadow(String shadowName)Gets the named shadow with default QOS 0.voidgetShadow(String shadowName, int qos)Gets the named shadow.booleanisConnected()Returns the state of the connection.voidpublish(AwsIotMessage message)Publish a message to AWS IoT MQTT broker.voidpublish(String topic, byte[] data)Publishes a message on a topic with default QOS 0voidpublish(String topic, byte[] data, int qos)Publishes a message on a topic.voidpublish(String topic, byte[] data, int qos, boolean retained)Publishes a message on a topic.voidsubscribe(String topic, AwsIotMessageCallback subscriber)Adds a topic listener on a topic.voidsubscribe(String topic, AwsIotMessageCallback subscriber, int qos)Adds a topic listener on a topic.voidsubscribeToShadow(ShadowAction action, ShadowResult result, AwsIotMessageCallback callback)Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).voidsubscribeToShadow(ShadowAction action, ShadowResult result, AwsIotMessageCallback callback, int qos)Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).voidsubscribeToShadow(String shadowName, ShadowAction action, ShadowResult result, AwsIotMessageCallback callback)Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).voidsubscribeToShadow(String shadowName, ShadowAction action, ShadowResult result, AwsIotMessageCallback callback, int qos)Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).voidunsubscribe(String topic)Unsubscribe from a topic.voidunsubscribeFromShadow(ShadowAction action, ShadowResult result)Unsubscribe from a shadow action.voidunsubscribeFromShadow(String shadowName, ShadowAction action, ShadowResult result)Unsubscribe from a shadow action.voidupdateShadow(byte[] message)Creates a no-named shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body.voidupdateShadow(byte[] message, int qos)Creates a no-named shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body.voidupdateShadow(String shadowName, byte[] message)Creates a shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body.voidupdateShadow(String shadowName, byte[] message, int qos)updateShadow : Creates a shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body.
-
-
-
Constructor Detail
-
AwsIotClient
public AwsIotClient(AwsIotClientOptions options)
Constructor client.- Parameters:
options- the connection option. useAwsIotClientOptions.Builderto create an instance ofAwsIotClientOptions
-
AwsIotClient
public AwsIotClient(AwsIotClientOptions options, AwsIotConnectionListener connectionCallback)
Constructor client.- Parameters:
options- the connection optionconnectionCallback- connection callback to monitor connection lost events
-
-
Method Detail
-
connect
public void connect() throws AwsIotExceptionConnect to AWS IoT service using the client options.- Throws:
AwsIotException- if an error occurred during connection.
-
isConnected
public boolean isConnected()
Returns the state of the connection.- Returns:
- true if connected, false otherwise
-
disconnect
public void disconnect() throws AwsIotExceptionDisconnect this client from the underlying communication layer.- Throws:
AwsIotException- when not connected
-
close
public void close() throws AwsIotExceptionClose this client. This close the underlying MQTT client, clean the subscribers list and shutdown the service executor which is responsible of executing message callback on a separated thread.- Throws:
AwsIotException- if the client is not disconnected.
-
subscribe
public void subscribe(String topic, AwsIotMessageCallback subscriber) throws AwsIotException
Adds a topic listener on a topic. Only one listener per topic can exist, if a listener is added on a topic that already has a listener, it will be replaced. QOS 0 is used- Parameters:
topic- the topic to add the listener on, one listener is allowed per topicsubscriber- the subscriber that will process the received data- Throws:
AwsIotException- when not connected or on communication error
-
subscribe
public void subscribe(String topic, AwsIotMessageCallback subscriber, int qos) throws AwsIotException
Adds a topic listener on a topic. Only one listener per topic can exist, if a listener is added on a topic that already has a listener, it will be replaced.- Parameters:
topic- the topic to add the listener on, one listener is allowed per topicsubscriber- the subscriber that will process the received dataqos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error
-
unsubscribe
public void unsubscribe(String topic) throws AwsIotException
Unsubscribe from a topic.- Parameters:
topic- the topic you want to stop listening to- Throws:
AwsIotException- when not connected or on communication error
-
publish
public void publish(AwsIotMessage message) throws AwsIotException
Publish a message to AWS IoT MQTT broker.- Parameters:
message- AwsIotMessage- Throws:
AwsIotException- on error
-
publish
public void publish(String topic, byte[] data) throws AwsIotException
Publishes a message on a topic with default QOS 0- Parameters:
topic- the topic you want to publish ondata- the data you want to publish- Throws:
AwsIotException- when not connected or on communication error
-
publish
public void publish(String topic, byte[] data, int qos) throws AwsIotException
Publishes a message on a topic. the retained flag is set to false when using this method.- Parameters:
topic- the topic you want to publish ondata- the data you want to publishqos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error
-
publish
public void publish(String topic, byte[] data, int qos, boolean retained) throws AwsIotException
Publishes a message on a topic.- Parameters:
topic- the topic you want to publish ondata- the data you want to publishqos- MQTT quality of serviceretained- set retained flag on the message.- Throws:
AwsIotException- when not connected or on communication error
-
getShadow
public void getShadow() throws AwsIotExceptionGets shadow with default QOS 0. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
getShadow
public void getShadow(int qos) throws AwsIotExceptionGets shadow. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
qos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
getShadow
public void getShadow(String shadowName) throws AwsIotException
Gets the named shadow with default QOS 0. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow name- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
getShadow
public void getShadow(String shadowName, int qos) throws AwsIotException
Gets the named shadow. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow nameqos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
updateShadow
public void updateShadow(byte[] message) throws AwsIotExceptionCreates a no-named shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
message- the message (body of request)- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
updateShadow
public void updateShadow(byte[] message, int qos) throws AwsIotExceptionCreates a no-named shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
message- the message (body of request)qos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
updateShadow
public void updateShadow(String shadowName, byte[] message) throws AwsIotException
Creates a shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow namemessage- the message- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
updateShadow
public void updateShadow(String shadowName, byte[] message, int qos) throws AwsIotException
updateShadow : Creates a shadow if it doesn't exist, or updates the contents of an existing shadow with the state information provided in the message body. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow namemessage- the messageqos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
deleteShadow
public void deleteShadow() throws AwsIotExceptionDelete the classic shadow of thing with QOS 0. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
deleteShadow
public void deleteShadow(int qos) throws AwsIotExceptionDelete the classic shadow of thing. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
qos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
deleteShadow
public void deleteShadow(String shadowName) throws AwsIotException
Delete the named shadow of thing with QOS 0. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow client- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
deleteShadow
public void deleteShadow(String shadowName, int qos) throws AwsIotException
Delete the named shadow of thing. Important Before calling this method, subscribe to the it's relevant shadow response to receive the response. If you do not subscribe to the shadow response before you perform this request, you will not receive the results.- Parameters:
shadowName- the shadow clientqos- MQTT quality of service- Throws:
AwsIotException- when not connected or on communication error- See Also:
subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
-
subscribeToShadow
public void subscribeToShadow(ShadowAction action, ShadowResult result, AwsIotMessageCallback callback) throws AwsIotException
Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). This method use QOS 0. to specify the qos usesubscribeToShadow(String, ShadowAction, ShadowResult, AwsIotMessageCallback, int)- Parameters:
action- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible valuescallback- the action callback will be executed when a message is received.- Throws:
AwsIotException- on error.
-
subscribeToShadow
public void subscribeToShadow(String shadowName, ShadowAction action, ShadowResult result, AwsIotMessageCallback callback) throws AwsIotException
Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). This method use QOS 0. to specify the qos usesubscribeToShadow(String, ShadowAction, ShadowResult, AwsIotMessageCallback, int)- Parameters:
shadowName- the name of shadowaction- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible valuescallback- the action callback will be executed when a message is received.- Throws:
AwsIotException- on error.
-
subscribeToShadow
public void subscribeToShadow(ShadowAction action, ShadowResult result, AwsIotMessageCallback callback, int qos) throws AwsIotException
Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).- Parameters:
action- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible valuescallback- the action callback will be executed when a message is received.qos- the MQTT quality of service.- Throws:
AwsIotException- on error.
-
subscribeToShadow
public void subscribeToShadow(String shadowName, ShadowAction action, ShadowResult result, AwsIotMessageCallback callback, int qos) throws AwsIotException
Subscribe to a shadow action (GET, UPDATE, DELETE) result ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only).- Parameters:
shadowName- the name of shadowaction- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible valuescallback- the action callback will be executed when a message is received.qos- the MQTT quality of service.- Throws:
AwsIotException- on error.
-
unsubscribeFromShadow
public void unsubscribeFromShadow(ShadowAction action, ShadowResult result) throws AwsIotException
Unsubscribe from a shadow action.- Parameters:
action- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible values- Throws:
AwsIotException- on error
-
unsubscribeFromShadow
public void unsubscribeFromShadow(String shadowName, ShadowAction action, ShadowResult result) throws AwsIotException
Unsubscribe from a shadow action.- Parameters:
shadowName- shadow nameaction- the shadow action seeShadowActionfor possible valuesresult- the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). UseShadowResultfor possible values- Throws:
AwsIotException- on error
-
getClientOptions
public AwsIotClientOptions getClientOptions()
Gets the client options.- Returns:
- the client options.
-
dispatch
protected void dispatch(AwsIotMessage message)
dispatch message to corresponding subscriber if any otherwise log a warning if warning logs are activated- Parameters:
message- received message
-
connectionLost
protected void connectionLost(Throwable cause)
Dispatch connection lost event if anAwsIotConnectionListenerwas registered.- Parameters:
cause- connection lost cause
-
-