Class SensorThingsService

  • All Implemented Interfaces:
    org.eclipse.paho.client.mqttv3.MqttCallback

    public class SensorThingsService
    extends Object
    implements org.eclipse.paho.client.mqttv3.MqttCallback
    A SensorThingsService represents the service endpoint of a server.
    Author:
    Nils Sommer, Hylke van der Schaaf, Michael Jacoby
    • Constructor Detail

      • SensorThingsService

        public SensorThingsService()
        Creates a new SensorThingsService without an endpoint url set. The endpoint url MUST be set before the service can be used.
      • SensorThingsService

        public SensorThingsService​(URI endpoint)
                            throws MalformedURLException
        Constructor.
        Parameters:
        endpoint - the base URI of the SensorThings service
        Throws:
        MalformedURLException - when building the final url fails.
      • SensorThingsService

        public SensorThingsService​(URL endpoint)
                            throws MalformedURLException
        Constructor.
        Parameters:
        endpoint - the base URL of the SensorThings service
        Throws:
        MalformedURLException - when building the final url fails.
    • Method Detail

      • setEndpoint

        public final void setEndpoint​(URI endpoint)
                               throws MalformedURLException
        Sets the endpoint URL/URI. Once the endpoint URL/URI is set it can not be changed. The endpoint url MUST be set before the service can be used.
        Parameters:
        endpoint - The URI of the endpoint.
        Throws:
        MalformedURLException - when building the final url fails.
      • setEndpoint

        public final void setEndpoint​(URL endpoint)
                               throws MalformedURLException
        Sets the endpoint URL/URI. Once the endpoint URL/URI is set it can not be changed. The endpoint url MUST be set before the service can be used.
        Parameters:
        endpoint - The URL of the endpoint.
        Throws:
        MalformedURLException - when building the final url fails.
      • setUrlReplace

        public final void setUrlReplace​(String urlReplace)
        In some cases the server generates URLs using a different base URL. For instance when the server has a different external and internal address. This option will replace the start part of each URL generated by the server that matches the given string, with the service URL.
        Parameters:
        urlReplace - the endpoint url the server uses, that needs to be replaced.
      • getEndpoint

        public URL getEndpoint()
        Gets the endpoint URL for the service. Throws an IllegalStateException if the endpoint is not set.
        Returns:
        the endpoint URL for the service.
      • isEndpointSet

        public boolean isEndpointSet()
        Check if the endpoint is set.
        Returns:
        true if the endpoint is set, false otherwise.
      • getPath

        public String getPath​(Entity<?> parent,
                              EntityType relation)
        The local path to the entity or collection. e.g.:
        • Things
        • Things(2)/Datastreams
        • Datastreams(5)/Thing
        Parameters:
        parent - The entity holding the relation, can be null.
        relation - The relation or collection to get.
        Returns:
        The path to the entity collection.
      • getMqttPath

        public String getMqttPath​(Entity<?> parent,
                                  EntityType relation)
        The path to the entity or collection. used for Mqtt e.g.:
        • Things
        • Things(2)/Datastreams
        • Datastreams(5)/Thing
        Parameters:
        parent - The entity holding the relation, can be null.
        relation - The relation or collection to get.
        Returns:
        The path to the entity collection.
      • getFullPath

        public URL getFullPath​(Entity<?> parent,
                               EntityType relation)
                        throws ServiceFailureException
        The full path to the entity or collection.
        Parameters:
        parent - The entity holding the relation, can be null.
        relation - The relation or collection to get.
        Returns:
        the full path to the entity or collection.
        Throws:
        ServiceFailureException - If generating the path fails.
      • execute

        public org.apache.http.client.methods.CloseableHttpResponse execute​(org.apache.http.client.methods.HttpRequestBase request)
                                                                     throws IOException
        Execute the given request, adding a token header if needed.
        Parameters:
        request - The request to execute.
        Returns:
        the response.
        Throws:
        IOException - in case of problems.
      • datastreams

        public DatastreamDao datastreams()
        Returns:
        a new Datastream Dao.
      • multiDatastreams

        public MultiDatastreamDao multiDatastreams()
        Returns:
        a new MultiDatastream Dao.
      • featuresOfInterest

        public FeatureOfInterestDao featuresOfInterest()
        Returns:
        a new FeatureOfInterest Dao.
      • historicalLocations

        public HistoricalLocationDao historicalLocations()
        Returns:
        a new HistoricalLocation Dao.
      • locations

        public LocationDao locations()
        Returns:
        a new Location Dao.
      • observations

        public ObservationDao observations()
        Returns:
        a new Observation Dao.
      • observedProperties

        public ObservedPropertyDao observedProperties()
        Returns:
        a new PbservedProperty Dao.
      • sensors

        public SensorDao sensors()
        Returns:
        a new Sensor Dao.
      • things

        public ThingDao things()
        Returns:
        a new Thing Dao.
      • actuators

        public ActuatorDao actuators()
        Returns:
        a new Actuator Dao.
      • tasks

        public TaskDao tasks()
        Returns:
        a new Task Dao.
      • taskingCapabilities

        public TaskingCapabilityDao taskingCapabilities()
        Returns:
        a new TaskingCapability Dao.
      • create

        public <T extends Entity<T>> void create​(T entity)
                                          throws ServiceFailureException
        Create the given entity in this service. Executes a POST to the Collection of the entity type. The entity will be updated with the ID of the entity in the Service and it will be linked to the Service.
        Type Parameters:
        T - The type of entity to create. Inferred from the entity.
        Parameters:
        entity - The entity to create in the service.
        Throws:
        ServiceFailureException - in case the server rejects the POST.
      • update

        public <T extends Entity<T>> void update​(T entity)
                                          throws ServiceFailureException
        Patches the entity in the Service.
        Type Parameters:
        T - The type of entity to update. Inferred from the entity.
        Parameters:
        entity - The entity to update in the service.
        Throws:
        ServiceFailureException - in case the server rejects the PATCH.
      • patch

        public <T extends Entity<T>> void patch​(T entity,
                                                List<com.github.fge.jsonpatch.JsonPatchOperation> patch)
                                         throws ServiceFailureException
        Update the given entity with the given patch. Does not update the entity object itself. To see the result, fetch it anew from the server.
        Type Parameters:
        T - The type of entity to update. Inferred from the entity.
        Parameters:
        entity - The entity to update on the server.
        patch - The patch to apply to the entity.
        Throws:
        ServiceFailureException - in case the server rejects the PATCH.
      • delete

        public <T extends Entity<T>> void delete​(T entity)
                                          throws ServiceFailureException
        Deletes the given entity from the service.
        Type Parameters:
        T - The type of entity to delete. Inferred from the entity.
        Parameters:
        entity - The entity to delete in the service.
        Throws:
        ServiceFailureException - in case the server rejects the DELETE.
      • setTokenManager

        public SensorThingsService setTokenManager​(TokenManager tokenManager)
        Sets the TokenManager. Before each request is sent to the Service, the TokenManager has the opportunity to modify the request and add any headers required for Authentication and Authorisation.
        Parameters:
        tokenManager - The TokenManager to use, can be null.
        Returns:
        This SensorThingsService.
      • getTokenManager

        public TokenManager getTokenManager()
        Returns:
        The current TokenManager.
      • getHttpClient

        public org.apache.http.impl.client.CloseableHttpClient getHttpClient()
        Get the httpclient used for requests.
        Returns:
        the client
      • setHttpClient

        @Deprecated
        public void setHttpClient​(org.apache.http.impl.client.CloseableHttpClient httpClient)
        Deprecated.
        (0.39) Use getClientBuilder() to change the builder. Since the httpClient is immutable, once it is set, nothing can be changed. This means it is impossible for two separate pieces of code to both make changes to the httpClient.
        Set the httpclient used for requests.
        Parameters:
        httpClient - the client to set
      • getClientBuilder

        public org.apache.http.impl.client.HttpClientBuilder getClientBuilder()
        Get the Builder used to generate the httpClient. If changes are made to the builder after the httpClient is already generated, call rebuildHttpClient() to trigger the httpClient to be built anew. The clientBuilder is initialised using: HttpClients.custom().useSystemProperties()
        Returns:
        The client Builder used to generate the httpClient.
      • rebuildHttpClient

        public void rebuildHttpClient()
        Triggers a rebuild of the httpClient, using the latest changes to the clientBuilder.
      • connectionLost

        public void connectionLost​(Throwable e)
        Specified by:
        connectionLost in interface org.eclipse.paho.client.mqttv3.MqttCallback
      • subscribe

        public <T> MqttSubscription subscribe​(String topic,
                                              Consumer<T> handler,
                                              Class<T> returnType,
                                              Predicate<T> filter)
                                       throws MqttException
        Start a MQTT subscription
        Type Parameters:
        T - return type
        Parameters:
        topic - The MQTT topic to subscribe
        handler - is called when a new notification happens (if result satisfies the filter)
        returnType - type to cast the result to
        filter - if not null, filters incoming notifications before handler is called
        Returns:
        the client
        Throws:
        MqttException - when subscription fails
      • subscribe

        public MqttSubscription subscribe​(String topic,
                                          Consumer<org.eclipse.paho.client.mqttv3.MqttMessage> handler)
                                   throws MqttException
        Start a MQTT subscription
        Parameters:
        topic - The MQTT topic to subscribe
        handler - is called when a new notification happens (if result satisfies the filter)
        Returns:
        the client
        Throws:
        MqttException - when subscription fails
      • publish

        public void publish​(String topic,
                            Entity entity)
                     throws MqttException
        Publish entity via MQTT on given topic
        Parameters:
        topic - The MQTT topic to publish to
        entity - entity to publish
        Throws:
        MqttException - when publication fails
      • messageArrived

        public void messageArrived​(String topic,
                                   org.eclipse.paho.client.mqttv3.MqttMessage message)
                            throws Exception
        Specified by:
        messageArrived in interface org.eclipse.paho.client.mqttv3.MqttCallback
        Throws:
        Exception
      • deliveryComplete

        public void deliveryComplete​(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken token)
        Specified by:
        deliveryComplete in interface org.eclipse.paho.client.mqttv3.MqttCallback