Skip to content

Commit

Permalink
Refactoring DiscoveryManager (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu authored Dec 7, 2023
1 parent 982b30a commit 39750e1
Show file tree
Hide file tree
Showing 30 changed files with 416 additions and 385 deletions.
2 changes: 1 addition & 1 deletion bin/sequencer
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ elif [[ -f $site_path ]]; then
serial_no=`jq -r .serial_no $site_path`
[[ $serial_no == null ]] && serial_no=$DEFAULT_SERIAL
endpoint=`jq .reflector_endpoint $site_path`
alt_registry=null
alt_registry=`jq .alt_registry $site_path`
else
echo Site model $site_path not found.
false
Expand Down
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.udmi.util.JsonUtil.isoConvert;
import static com.google.udmi.util.ProperPrinter.OutputFormat.COMPRESSED;
import static com.google.udmi.util.ProperPrinter.OutputFormat.VERBOSE;
import static java.lang.String.format;
Expand Down Expand Up @@ -490,4 +491,8 @@ public static void setClockSkew(Duration skew) {
public static Date getNow() {
return Date.from(Instant.now().plus(clockSkew));
}

public static String getTimestamp() {
return isoConvert(getNow());
}
}
64 changes: 30 additions & 34 deletions common/src/main/java/com/google/udmi/util/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.google.udmi.util;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.udmi.util.GeneralUtils.fromJsonString;
import static com.google.udmi.util.GeneralUtils.toJsonString;
import static java.util.Objects.requireNonNull;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
Expand Down Expand Up @@ -145,42 +144,13 @@ public static Instant getInstant(String timestamp) {
return timestamp == null ? null : Instant.parse(replaced);
}

/**
* Get a proper JSON string representation of the given Date.
*
* @param date thing to convert
* @return converted to a string
*/
public static String getTimestamp(Date date) {
try {
if (date == null) {
return "null";
}
String dateString = stringify(date);
// Remove the encapsulating quotes included because it's a JSON string-in-a-string.
return dateString.substring(1, dateString.length() - 1);
} catch (Exception e) {
throw new RuntimeException("Creating timestamp", e);
}
}

/**
* Get a proper JSON string representation of the given Instant.
*
* @param timestamp thing to convert
* @return converted to string
*/
public static String getTimestamp(Instant timestamp) {
return getTimestamp(Date.from(timestamp));
}

/**
* Get a current timestamp string.
*
* @return current ISO timestamp
*/
public static String getTimestamp() {
return getTimestamp(CleanDateFormat.cleanDate());
public static String isoConvert() {
return isoConvert(CleanDateFormat.cleanDate());
}

/**
Expand Down Expand Up @@ -402,4 +372,30 @@ public static void writeFile(Object target, File file) {
throw new RuntimeException("While writing " + file.getAbsolutePath(), e);
}
}

private static Date isoConvert(String timestamp) {
try {
String wrappedString = "\"" + timestamp + "\"";
return fromJsonString(wrappedString, Date.class);
} catch (Exception e) {
throw new RuntimeException("Creating date", e);
}
}

public static String isoConvert(Instant timestamp) {
return isoConvert(Date.from(timestamp));
}

public static String isoConvert(Date timestamp) {
try {
if (timestamp == null) {
return "null";
}
String dateString = toJsonString(timestamp);
// Strip off the leading and trailing quotes from the JSON string-as-string representation.
return dateString.substring(1, dateString.length() - 1);
} catch (Exception e) {
throw new RuntimeException("Creating timestamp", e);
}
}
}
10 changes: 9 additions & 1 deletion pubber/src/main/java/daq/pubber/DeviceManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package daq.pubber;

import com.google.udmi.util.SiteModel;
import java.util.Map;
import udmi.schema.Config;
import udmi.schema.DevicePersistent;
Expand All @@ -19,6 +20,7 @@ public class DeviceManager extends ManagerBase {
private final SystemManager systemManager;
private final LocalnetManager localnetManager;
private final GatewayManager gatewayManager;
private final DiscoveryManager discoveryManager;


/**
Expand All @@ -30,6 +32,7 @@ public DeviceManager(ManagerHost host, PubberConfiguration configuration) {
pointsetManager = new PointsetManager(host, configuration);
localnetManager = new LocalnetManager(host, configuration);
gatewayManager = new GatewayManager(host, configuration);
discoveryManager = new DiscoveryManager(host, configuration, this);
}

public void setPersistentData(DevicePersistent persistentData) {
Expand Down Expand Up @@ -62,7 +65,7 @@ public void localLog(Entry report) {
}

public void localLog(String message, Level trace, String timestamp, String detail) {
systemManager.localLog(message, trace, timestamp, detail);
SystemManager.localLog(message, trace, timestamp, detail);
}

public String getTestingTag() {
Expand All @@ -76,6 +79,7 @@ public void updateConfig(Config config) {
pointsetManager.updateConfig(config.pointset);
systemManager.updateConfig(config.system, config.timestamp);
gatewayManager.updateConfig(config.gateway);
discoveryManager.updateConfig(config.discovery);
}

public void publishLogMessage(Entry logEntry) {
Expand All @@ -99,4 +103,8 @@ public void shutdown() {
public Map<String, FamilyDiscoveryEvent> enumerateFamilies() {
return localnetManager.enumerateFamilies();
}

public void setSiteModel(SiteModel siteModel) {
discoveryManager.setSiteModel(siteModel);
}
}
Loading

0 comments on commit 39750e1

Please sign in to comment.