Package ej.aws.iot

Class AwsIotClient


  • public class AwsIotClient
    extends Object
    AWS IoT client, based on MicroPaho MQTT Implementation.
    • Method Detail

      • connect

        public void connect()
                     throws AwsIotException
        Connect 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 AwsIotException
        Disconnect this client from the underlying communication layer.
        Throws:
        AwsIotException - when not connected
      • close

        public void close()
                   throws AwsIotException
        Close 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 topic
        subscriber - 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 topic
        subscriber - the subscriber that will process the received data
        qos - 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​(String topic,
                            byte[] data)
                     throws AwsIotException
        Publishes a message on a topic with default QOS 0
        Parameters:
        topic - the topic you want to publish on
        data - 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 on
        data - the data you want to publish
        qos - 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 on
        data - the data you want to publish
        qos - MQTT quality of service
        retained - set retained flag on the message.
        Throws:
        AwsIotException - when not connected or on communication error
      • getShadow

        public void getShadow​(int qos)
                       throws AwsIotException
        Gets 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 name
        qos - 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 AwsIotException
        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. 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 AwsIotException
        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. 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 name
        message - 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 name
        message - the message
        qos - MQTT quality of service
        Throws:
        AwsIotException - when not connected or on communication error
        See Also:
        subscribeToShadow(ShadowAction, ShadowResult, AwsIotMessageCallback)
      • deleteShadow

        public void deleteShadow​(int qos)
                          throws AwsIotException
        Delete 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 client
        qos - 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,
                                      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 see ShadowAction for possible values
        result - the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). Use ShadowResult for possible values
        callback - 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 shadow
        action - the shadow action see ShadowAction for possible values
        result - the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). Use ShadowResult for possible values
        callback - 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 see ShadowAction for possible values
        result - the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). Use ShadowResult for 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 name
        action - the shadow action see ShadowAction for possible values
        result - the result to listen too ACCEPTED, REJECTED and (DELTA, DOCUMENT for update only). Use ShadowResult for 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 an AwsIotConnectionListener was registered.
        Parameters:
        cause - connection lost cause