Class SensorThingsService
- java.lang.Object
-
- de.fraunhofer.iosb.ilt.sta.service.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 Summary
Constructors Constructor Description SensorThingsService()Creates a new SensorThingsService without an endpoint url set.SensorThingsService(URI endpoint)Constructor.SensorThingsService(URL endpoint)Constructor.SensorThingsService(URL endpoint, MqttConfig mqttConfig)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description ActuatorDaoactuators()voidconnectionLost(Throwable e)List<String>create(DataArrayDocument dataArray)<T extends Entity<T>>
voidcreate(T entity)Create the given entity in this service.DatastreamDaodatastreams()<T extends Entity<T>>
voiddelete(T entity)Deletes the given entity from the service.voiddeliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken token)org.apache.http.client.methods.CloseableHttpResponseexecute(org.apache.http.client.methods.HttpRequestBase request)Execute the given request, adding a token header if needed.FeatureOfInterestDaofeaturesOfInterest()org.apache.http.impl.client.HttpClientBuildergetClientBuilder()Get the Builder used to generate the httpClient.URLgetEndpoint()Gets the endpoint URL for the service.URLgetFullPath(Entity<?> parent, EntityType relation)The full path to the entity or collection.org.apache.http.impl.client.CloseableHttpClientgetHttpClient()Get the httpclient used for requests.StringgetMqttPath(Entity<?> parent, EntityType relation)The path to the entity or collection.StringgetPath(Entity<?> parent, EntityType relation)The local path to the entity or collection.TokenManagergetTokenManager()SensorThingsAPIVersiongetVersion()HistoricalLocationDaohistoricalLocations()booleanisEndpointSet()Check if the endpoint is set.LocationDaolocations()voidmessageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage message)MultiDatastreamDaomultiDatastreams()ObservationDaoobservations()ObservedPropertyDaoobservedProperties()<T extends Entity<T>>
voidpatch(T entity, List<com.github.fge.jsonpatch.JsonPatchOperation> patch)Update the given entity with the given patch.voidpublish(String topic, Entity entity)Publish entity via MQTT on given topicvoidrebuildHttpClient()Triggers a rebuild of the httpClient, using the latest changes to the clientBuilder.SensorDaosensors()voidsetEndpoint(URI endpoint)Sets the endpoint URL/URI.voidsetEndpoint(URL endpoint)Sets the endpoint URL/URI.voidsetHttpClient(org.apache.http.impl.client.CloseableHttpClient httpClient)Deprecated.(0.39) UsegetClientBuilder()to change the builder.SensorThingsServicesetTokenManager(TokenManager tokenManager)Sets the TokenManager.voidsetUrlReplace(String urlReplace)In some cases the server generates URLs using a different base URL.MqttSubscriptionsubscribe(String topic, Consumer<org.eclipse.paho.client.mqttv3.MqttMessage> handler)Start a MQTT subscription<T> MqttSubscriptionsubscribe(String topic, Consumer<T> handler, Class<T> returnType, Predicate<T> filter)Start a MQTT subscriptionTaskingCapabilityDaotaskingCapabilities()TaskDaotasks()ThingDaothings()voidunsubscribe(MqttSubscription subscription)voidunsubscribe(String topic)voidunsubscribe(String topic, Consumer<org.eclipse.paho.client.mqttv3.MqttMessage> handler)<T extends Entity<T>>
voidupdate(T entity)Patches the entity in the Service.
-
-
-
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.
-
SensorThingsService
public SensorThingsService(URL endpoint, MqttConfig mqttConfig) throws MalformedURLException, MqttException
Constructor.- Parameters:
endpoint- the base URL of the SensorThings servicemqttConfig- the config object for MQTT connections- Throws:
MalformedURLException- when building the final url fails.MqttException
-
-
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 IOExceptionExecute 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 List<String> create(DataArrayDocument dataArray) throws ServiceFailureException
- Parameters:
dataArray- The Observations to create.- Returns:
- The response of the service.
- Throws:
ServiceFailureException- in case the server rejects the POST.
-
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) UsegetClientBuilder()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, callrebuildHttpClient()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:
connectionLostin interfaceorg.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 subscribehandler- is called when a new notification happens (if result satisfies the filter)returnType- type to cast the result tofilter- 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 subscribehandler- 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 toentity- entity to publish- Throws:
MqttException- when publication fails
-
unsubscribe
public void unsubscribe(MqttSubscription subscription) throws MqttException
- Throws:
MqttException
-
unsubscribe
public void unsubscribe(String topic, Consumer<org.eclipse.paho.client.mqttv3.MqttMessage> handler) throws MqttException
- Throws:
MqttException
-
unsubscribe
public void unsubscribe(String topic) throws MqttException
- Throws:
MqttException
-
messageArrived
public void messageArrived(String topic, org.eclipse.paho.client.mqttv3.MqttMessage message) throws Exception
- Specified by:
messageArrivedin interfaceorg.eclipse.paho.client.mqttv3.MqttCallback- Throws:
Exception
-
deliveryComplete
public void deliveryComplete(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken token)
- Specified by:
deliveryCompletein interfaceorg.eclipse.paho.client.mqttv3.MqttCallback
-
getVersion
public SensorThingsAPIVersion getVersion()
-
-