Allow arbitrary networktables address (#764)

* Allow specifying NT server address by ip.
This commit is contained in:
Doug Wegscheid
2023-05-12 18:01:09 -04:00
committed by GitHub
parent 6bdb158b33
commit 80f479344d
11 changed files with 73 additions and 26 deletions

View File

@@ -17,6 +17,7 @@
package org.photonvision.common.configuration;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
@@ -30,7 +31,7 @@ import org.photonvision.common.networking.NetworkMode;
import org.photonvision.common.util.file.JacksonUtils;
public class NetworkConfig {
public int teamNumber = 0;
public String ntServerAddress = "0";
public NetworkMode connectionType = NetworkMode.DHCP;
public String staticIp = "";
public String hostname = "photonvision";
@@ -54,7 +55,8 @@ public class NetworkConfig {
@JsonCreator
public NetworkConfig(
@JsonProperty("teamNumber") int teamNumber,
@JsonProperty("ntServerAddress") @JsonAlias({"ntServerAddress", "teamNumber"})
String ntServerAddress,
@JsonProperty("connectionType") NetworkMode connectionType,
@JsonProperty("staticIp") String staticIp,
@JsonProperty("hostname") String hostname,
@@ -64,7 +66,7 @@ public class NetworkConfig {
@JsonProperty("physicalInterface") String physicalInterface,
@JsonProperty("setStaticCommand") String setStaticCommand,
@JsonProperty("setDHCPcommand") String setDHCPcommand) {
this.teamNumber = teamNumber;
this.ntServerAddress = ntServerAddress;
this.connectionType = connectionType;
this.staticIp = staticIp;
this.hostname = hostname;

View File

@@ -66,7 +66,12 @@ public class NetworkTablesManager {
getInstance().broadcastConnectedStatus();
} else if (event.logMessage.message.contains("connected")
&& System.currentTimeMillis() - lastConnectMessageMillis > 125) {
logger.info("NT Connected!");
String connectionDescription = "(unknown)";
var connections = getInstance().ntInstance.getConnections();
if (connections.length > 0) {
connectionDescription = connections[0].remote_ip + ":" + connections[0].remote_port;
}
logger.info("NT Connected to " + connectionDescription + "!");
hasReportedConnectionFailure = false;
lastConnectMessageMillis = System.currentTimeMillis();
ScriptManager.queueEvent(ScriptEventType.kNTConnected);
@@ -107,16 +112,23 @@ public class NetworkTablesManager {
if (config.runNTServer) {
setServerMode();
} else {
setClientMode(config.teamNumber);
setClientMode(config.ntServerAddress);
}
broadcastVersion();
}
private void setClientMode(int teamNumber) {
if (!isRetryingConnection) logger.info("Starting NT Client");
private void setClientMode(String ntServerAddress) {
ntInstance.stopServer();
ntInstance.startClient4("photonvision");
ntInstance.setServerTeam(teamNumber);
try {
Integer t = Integer.parseInt(ntServerAddress);
if (!isRetryingConnection) logger.info("Starting NT Client, server team is " + t);
ntInstance.setServerTeam(t);
} catch (NumberFormatException e) {
if (!isRetryingConnection)
logger.info("Starting NT Client, server IP is \"" + ntServerAddress + "\"");
ntInstance.setServer(ntServerAddress);
}
ntInstance.startDSClient();
broadcastVersion();
}

View File

@@ -46,7 +46,7 @@ public class NetworkManager {
}
var config = ConfigManager.getInstance().getConfig().getNetworkConfig();
logger.info("Setting " + config.connectionType + " with team team " + config.teamNumber);
logger.info("Setting " + config.connectionType + " with team " + config.ntServerAddress);
if (Platform.isLinux()) {
if (!Platform.isRoot()) {
logger.error("Cannot manage hostname without root!");

View File

@@ -33,4 +33,20 @@ public class NetworkConfigTest {
Assertions.assertDoesNotThrow(() -> mapper.readValue(path.toFile(), NetworkConfig.class));
new File("netTest.json").delete();
}
@Test
public void testDeserializeTeamNumberOrNtServerAddress() {
{
ConfigManager configMgr =
new ConfigManager(Path.of("test-resources/network-old-team-number"));
configMgr.load();
Assertions.assertEquals("9999", configMgr.getConfig().getNetworkConfig().ntServerAddress);
}
{
ConfigManager configMgr =
new ConfigManager(Path.of("test-resources/network-new-team-number"));
configMgr.load();
Assertions.assertEquals("9999", configMgr.getConfig().getNetworkConfig().ntServerAddress);
}
}
}