Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/sensorhub/oshconnect/OSHSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.sensorhub.oshconnect.util.DataStreamsQueryBuilder;
import org.sensorhub.oshconnect.util.Utilities;

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -99,6 +101,14 @@ public List<OSHDataStream> discoverDataStreams(String query) throws ExecutionExc
return result;
}

public boolean isLive(Duration window) throws ExecutionException, InterruptedException {
var cutoff = Instant.now().minus(window);
return getConnectedSystemsApiClientExtras()
.getLastObservationTimes(getId(), "").get()
.values().stream()
.anyMatch(t -> t != null && t.isAfter(cutoff));
}

/**
* Add or update a data stream in the list of data streams and notify listeners.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.LinkedHashMap;
import java.util.Map;

public class ConSysApiClientExtras {
static final String JSON_ARRAY_ITEMS = "items";
Expand Down Expand Up @@ -136,6 +141,45 @@ public CompletableFuture<List<String>> getDataStreamIds(String systemID, String
});
}

public CompletableFuture<Map<String, Instant>> getLastObservationTimes(String systemID, String queryString) {
if (queryString == null)
queryString = "";

return sendGetRequest(endpoint.resolve(SYSTEMS_COLLECTION + "/" + systemID + "/" + DATASTREAMS_COLLECTION + queryString), ResourceFormat.JSON, body -> {
try {
var ctx = new RequestContext(body);
JsonObject bodyJson = JsonParser.parseReader(new InputStreamReader(ctx.getInputStream())).getAsJsonObject();
JsonArray features = bodyJson.getAsJsonArray(JSON_ARRAY_ITEMS);

Map<String, Instant> times = new LinkedHashMap<>();
for (var feature : features) {
var obj = feature.getAsJsonObject();
if (!obj.has("id"))
throw new IOException("No id found in feature");
times.put(obj.get("id").getAsString(), parseExtentEnd(obj.getAsJsonArray("phenomenonTime")));
}
return times;
} catch (IOException e) {
throw new CompletionException(e);
}
});
}

private static Instant parseExtentEnd(JsonArray extent) {
if (extent == null || extent.size() < 2)
return null;
var end = extent.get(1).getAsString();
if ("0".equals(end)) // no observations yet
return null;
if ("now".equals(end)) // defensive: your node uses this on validTime, not phenomenonTime
return Instant.now();
try {
return Instant.parse(end);
} catch (DateTimeParseException e) {
return null;
}
}

/**
* Update a data stream.
*
Expand Down Expand Up @@ -290,7 +334,7 @@ public CompletableFuture<List<ObservationData>> getObservations(String dataStrea
}

return observations;
} catch (IOException e) {
} catch (Exception e) {
throw new CompletionException(e);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) {}
sslContextFactory.getSslContext().getClientSessionContext().setSessionCacheSize(0);

client = new WebSocketClient(sslContextFactory);
client.getPolicy().setMaxBinaryMessageSize(4 * 1024 * 1024); // Jetty 9.x
// client.setMaxBinaryMessageSize(4 * 1024 * 1024); // Jetty 10/11+

client.start();
client.connect(this, new URI(urlString), clientUpgradeRequest);
Expand Down