OSH Connect for Python is a straightforward library for interacting with OpenSensorHub using OGC API Connected Systems. This tutorial will help guide you through a few simple examples to get you started with OSH Connect.
The intended method of interacting with OpenSensorHub is through the OSHConnect class. To this you must first create an instance of OSHConnect:
OSHConnect oshConnect = new OSHConnect("OSH Connect Example");The name parameter is optional, but can be useful for debugging purposes.
OSHNode node = oshConnect.createNode("localhost:8181/sensorhub", true, "admin", "admin");The createNode method takes the following parameters:
sensorHubRoot: The URL of the OpenSensorHub instanceisSecure: Whether the connection is secure (HTTPS)username: The username to authenticate withpassword: The password to authenticate with
oshConnect.discoverSystems();This method will discover all systems available on every node in the OSHConnect instance. It is also possible to discover systems on a specific node:
node.discoverSystems();oshConnect.discoverDatastreams();This method will discover all datastreams available on every system for every node in the OSHConnect instance. It is also possible to discover datastreams on a specific system.
Once you have discovered the datastreams you are interested in, you can retrieve observations from them:
List<OSHDatastream> datastreams = oshConnect.discoverDatastreams();
// Create a new DatastreamHandler to manage connections to the datastreams.
DatastreamHandler handler = oshConnect.getDatastreamManager().createDatastreamHandler(this::onStreamUpdate);
// Add all the discovered datastreams to the handler.
datastreams.forEach(handler::addDatastream);
// Connect, listen for updates.
handler.connect();
// Start listening for historical data instead of live data.
Instant oneMinuteAgo = Instant.now().minusSeconds(60);
handler.setTimeExtent(TimeExtent.startingAt(oneMinuteAgo));
handler.setReplaySpeed(0.25);
// This method will be called whenever a new observation is available.
private void onStreamUpdate(DatastreamEventArgs args) {
var datastreamId = args.getDatastream().getDatastreamResource().getId();
var timestamp = args.getTimestamp();
String message = String.format("onStreamUpdate: timestamp=%s datastreamId=%s", timestamp, datastreamId);
System.out.println(message);
}