Skip to content

Commit

Permalink
Merge branch 'master' into merger
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu committed Dec 18, 2024
2 parents e0a600a + 66c7673 commit 811590b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 11 deletions.
50 changes: 50 additions & 0 deletions common/src/main/java/com/google/daq/mqtt/util/ContextWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.google.daq.mqtt.util;

import java.util.List;
import java.util.ArrayList;
import java.util.function.Supplier;

public class ContextWrapper {

private static final ThreadLocal<List<String>> contexts = ThreadLocal.withInitial(ArrayList::new);

public static <T> T runInContext(String context, Supplier<T> supplier) {
contexts.get().add(context);
try {
return supplier.get();
} catch (Exception e) {
throw wrapExceptionWithContext(e, true);
} finally {
contexts.get().remove(contexts.get().size() - 1);
}
}

public static void runInContext(String context, Runnable action) {
runInContext(context, () -> {
action.run();
return null;
});
}

public static String getCurrentContext() {
List<String> contextList = contexts.get();
return String.join(" -> ", contextList);
}

public static RuntimeException wrapExceptionWithContext(Exception e, boolean includeOnlyLatest) {
List<String> contextStrings = contexts.get();
if (contextStrings.size() == 0) {
return new RuntimeException(e);
}

RuntimeException wrappedException = new RuntimeException(
contextStrings.get(contextStrings.size() - 1), e);
if (!includeOnlyLatest) {
for (int i = contextStrings.size() - 2; i >= 0; i--) {
String context = contextStrings.get(i);
wrappedException = new RuntimeException(context, wrappedException);
}
}
return wrappedException;
}
}
6 changes: 6 additions & 0 deletions tests/sites/missing/devices/AHU-22/expected/errors.map
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
Exceptions for AHU-22
While converting device config
While converting point filter_differential_pressure_sensor
protocol ref FAXLE12.present_value does not match expression bacnet://[1-9][0-9]*/([A-Z]{2,4}):([1-9][0-9]*)(#[_a-z]+)?
While converting device config
While converting point filter_alarm_pressure_status
protocol ref MCN8.present_value does not match expression bacnet://[1-9][0-9]*/([A-Z]{2,4}):([1-9][0-9]*)(#[_a-z]+)?
While converting device config
While converting point filter_differential_pressure
protocol ref AI2.differential does not match expression bacnet://[1-9][0-9]*/([A-Z]{2,4}):([1-9][0-9]*)(#[_a-z]+)?
36 changes: 36 additions & 0 deletions tests/sites/missing/devices/AHU-22/expected/generated_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"timestamp": "2020-05-01T13:39:07Z",
"version": "1.5.2",
"system": {
"min_loglevel": 300,
"metrics_rate_sec": 10,
"operation": { }
},
"gateway": {
"target": {
"addr": "0x65",
"family": "bacnet"
}
},
"localnet": {
"families": {
"bacnet": { }
}
},
"pointset": {
"points": {
"filter_alarm_pressure_status": {
"ref": "MCN8.present_value",
"units": "No-units"
},
"filter_differential_pressure": {
"ref": "AI2.differential",
"units": "Bars"
},
"filter_differential_pressure_sensor": {
"ref": "FAXLE12.present_value",
"units": "Degrees-Celsius"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static com.google.daq.mqtt.registrar.Registrar.METADATA_SCHEMA_JSON;
import static com.google.daq.mqtt.util.ConfigManager.GENERATED_CONFIG_JSON;
import static com.google.daq.mqtt.util.ConfigManager.configFrom;
import static com.google.daq.mqtt.util.ContextWrapper.runInContext;
import static com.google.udmi.util.Common.DEVICE_ID_ALLOWABLE;
import static com.google.udmi.util.Common.POINT_NAME_ALLOWABLE;
import static com.google.udmi.util.GeneralUtils.CSV_JOINER;
Expand All @@ -23,7 +24,6 @@
import static com.google.udmi.util.SiteModel.METADATA_JSON;
import static com.google.udmi.util.SiteModel.NORMALIZED_JSON;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
Expand Down Expand Up @@ -83,6 +83,7 @@
import udmi.schema.Metadata;
import udmi.schema.PointPointsetModel;


class LocalDevice {

public static final String INVALID_METADATA_HASH = "INVALID";
Expand Down Expand Up @@ -518,8 +519,11 @@ private byte[] getFileBytes(String dataFile) {
}

private String deviceConfigString() {
try {
return runInContext("While converting device config", () -> {
Object fromValue = config.deviceConfigJson();

config.getSchemaViolationsMap().forEach(this::captureError);

if (fromValue instanceof String stringValue) {
return stringValue;
}
Expand All @@ -528,9 +532,7 @@ private String deviceConfigString() {
new MessageDowngrader("config", configJson).downgrade(baseVersion);
}
return compressJsonString(configJson, MAX_JSON_LENGTH);
} catch (Exception e) {
throw new RuntimeException("While converting device config", e);
}
});
}

private String deviceMetadataString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.google.daq.mqtt.util;

import static com.google.common.base.Preconditions.checkState;
import static com.google.daq.mqtt.util.ContextWrapper.getCurrentContext;
import static com.google.daq.mqtt.util.ContextWrapper.runInContext;
import static com.google.daq.mqtt.util.ContextWrapper.wrapExceptionWithContext;
import static com.google.daq.mqtt.util.providers.FamilyProvider.NAMED_FAMILIES;
import static com.google.udmi.util.GeneralUtils.catchToNull;
import static com.google.udmi.util.GeneralUtils.deepCopy;
Expand All @@ -19,6 +22,7 @@
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import udmi.lib.ProtocolFamily;
import udmi.schema.Config;
Expand Down Expand Up @@ -48,6 +52,7 @@ public class ConfigManager {
private final Metadata metadata;
private final String deviceId;
private final SiteModel siteModel;
private final Map<String, Exception> schemaViolationsMap = new HashMap<>();

/**
* Initiates ConfigManager for the given device at the given file location.
Expand Down Expand Up @@ -205,17 +210,15 @@ private PointsetConfig getDevicePointsetConfig() {

PointPointsetConfig configFromMetadata(String configKey, PointPointsetModel metadata,
boolean excludeUnits) {
try {
return runInContext("While converting point " + configKey, () -> {
PointPointsetConfig pointConfig = new PointPointsetConfig();
pointConfig.units = excludeUnits ? null : metadata.units;
pointConfig.ref = pointConfigRef(metadata);
if (Boolean.TRUE.equals(metadata.writable)) {
pointConfig.set_value = metadata.baseline_value;
}
return pointConfig;
} catch (Exception e) {
throw new RuntimeException("While converting point " + configKey, e);
}
});
}

private String pointConfigRef(PointPointsetModel model) {
Expand All @@ -234,8 +237,12 @@ private String pointConfigRef(PointPointsetModel model) {
ifNotNullThen(rawFamily, raw ->
requireNonNull(catchToNull(() -> metadata.localnet.families.get(family).addr),
format("metadata.localnet.families.[%s].addr not defined", family)));

NAMED_FAMILIES.get(family).validateRef(pointRef);
try {
NAMED_FAMILIES.get(family).validateRef(pointRef);
} catch (Exception e) {
schemaViolationsMap.put(String.format("%s %s: %s", family, pointRef, getCurrentContext()),
wrapExceptionWithContext(e, false));
}
return pointRef;
}

Expand Down Expand Up @@ -311,4 +318,8 @@ private DiscoveryConfig getDiscoveryConfig() {
return discoveryConfig;
}

public Map<String, Exception> getSchemaViolationsMap() {
return schemaViolationsMap;
}

}

0 comments on commit 811590b

Please sign in to comment.