Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more discovery debugging/logging #859

Merged
merged 4 commits into from
Apr 16, 2024
Merged
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
5 changes: 5 additions & 0 deletions common/src/main/java/com/google/udmi/util/GeneralUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.google.udmi.util;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Sets.symmetricDifference;
import static com.google.udmi.util.JsonUtil.isoConvert;
import static com.google.udmi.util.ProperPrinter.OutputFormat.COMPRESSED;
Expand Down Expand Up @@ -270,6 +271,10 @@ public static void ifNullThen(Object value, Runnable action) {
}
}

public static void requireNull(Object value, String description) {
checkState(value == null, description);
}

public static <T> void ifNotNullThrow(T value, String message) {
if (value != null) {
throw new RuntimeException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.google.udmi.util.GeneralUtils.ifTrueThen;
import static com.google.udmi.util.GeneralUtils.isTrue;
import static com.google.udmi.util.GeneralUtils.requireNull;
import static com.google.udmi.util.GeneralUtils.toDate;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -48,8 +49,12 @@ private void debug(String format, Object... args) {
controller.debug(format, args);
}

private void issueModifiedDevice(String id) {
issueModifiedQuery(e -> e.deviceId = id);
private void issueModifiedDevice(String deviceId) {
requireNonNull(deviceId, "device id");
issueModifiedQuery(e -> {
e.deviceId = deviceId;
e.transactionId = e.transactionId + "/d/" + deviceId;
});
}

private void issueModifiedQuery(Consumer<Envelope> mutator) {
Expand All @@ -60,7 +65,11 @@ private void issueModifiedQuery(Consumer<Envelope> mutator) {
}

private void issueModifiedRegistry(String registryId) {
issueModifiedQuery(e -> e.deviceRegistryId = registryId);
requireNonNull(registryId, "registry id");
issueModifiedQuery(e -> {
e.deviceRegistryId = registryId;
e.transactionId = e.transactionId + "/r/" + registryId;
});
}

private CloudModel makeCloudModel(String registryId) {
Expand All @@ -75,6 +84,8 @@ private void publish(DiscoveryEvents discoveryEvent) {
}

private void queryAllRegistries() {
requireNull(envelope.deviceRegistryId, "registry id");
requireNull(envelope.deviceId, "device id");
Set<String> registries = iotAccess.getRegistries();
DiscoveryEvents discoveryEvent = new DiscoveryEvents();
discoveryEvent.scan_family = ProtocolFamily.IOT;
Expand All @@ -87,7 +98,7 @@ private void queryAllRegistries() {
List<String> active = discoveryEvent.registries.entrySet().stream()
.filter(entry -> entry.getValue().last_event_time != null).map(Entry::getKey).toList();

debug("Found %d registries (%d active)", registries.size(), active.size());
debug("Project has %d registries (%d active)", registries.size(), active.size());

ifTrueThen(shouldTraverseRegistries(), () -> active.forEach(this::issueModifiedRegistry));
}
Expand All @@ -109,6 +120,8 @@ private void queryDeviceDetails() {

private void queryRegistryDevices() {
String deviceRegistryId = requireNonNull(envelope.deviceRegistryId, "registry id");
requireNull(envelope.deviceId, "device id");

CloudModel cloudModel = iotAccess.listDevices(deviceRegistryId);

DiscoveryEvents discoveryEvent = new DiscoveryEvents();
Expand All @@ -121,8 +134,8 @@ private void queryRegistryDevices() {
List<String> active = discoveryEvent.devices.entrySet().stream()
.filter(entry -> !isTrue(entry.getValue().blocked)).map(Entry::getKey).toList();

debug("Registry %s had %d devices (%d active)", deviceRegistryId, discoveryEvent.devices.size(),
active.size());
debug("Listed registry %s with %d devices (%d active)", deviceRegistryId,
discoveryEvent.devices.size(), active.size());

ifTrueThen(shouldDetailEntries(), () -> active.forEach(this::issueModifiedDevice));
}
Expand All @@ -139,16 +152,19 @@ private boolean shouldTraverseRegistries() {
* Process an individual cloud query.
*/
public synchronized void process(CloudQuery newQuery) {
query = newQuery;
envelope = controller.getContinuation(newQuery).getEnvelope();

if (envelope.deviceRegistryId == null) {
queryAllRegistries();
} else if (envelope.deviceId == null) {
queryRegistryDevices();
} else {
queryDeviceDetails();
try {
query = newQuery;
envelope = controller.getContinuation(newQuery).getEnvelope();
if (envelope.deviceRegistryId == null) {
queryAllRegistries();
} else if (envelope.deviceId == null) {
queryRegistryDevices();
} else {
queryDeviceDetails();
}
} finally {
query = null;
envelope = null;
}
query = null;
}
}