Implement hostname, IP setting (#114)

This commit is contained in:
Matt
2020-09-10 20:07:23 -07:00
committed by GitHub
parent a35f775b05
commit 2495d348ea
2 changed files with 35 additions and 7 deletions

View File

@@ -19,6 +19,7 @@ package org.photonvision.common.configuration;
import java.util.HashMap;
import java.util.Map;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.networking.NetworkMode;
public class NetworkConfig {
@@ -28,7 +29,6 @@ public class NetworkConfig {
public String netmask = "";
public String hostname = "photonvision";
// TODO implement networking
public boolean shouldManage;
public NetworkConfig() {}
@@ -45,7 +45,8 @@ public class NetworkConfig {
this.staticIp = staticIp;
this.netmask = netmask;
this.hostname = hostname;
this.shouldManage = shouldManage;
this.shouldManage = shouldManage || Platform.isRaspberryPi();
}
public static NetworkConfig fromHashMap(Map<String, Object> map) {

View File

@@ -46,20 +46,47 @@ public class NetworkManager {
}
var config = ConfigManager.getInstance().getConfig().getNetworkConfig();
logger.info(
"Setting static ip to \""
+ config.staticIp
+ "\" and hostname to \""
+ config.hostname
+ "\"");
if (Platform.isLinux()) {
if (!Platform.isRoot) {
logger.error("Cannot manage network without root!");
return;
}
// always set hostname
if (config.hostname.length() > 0) {
try {
var setHostnameRetCode =
new ShellExec().execute("hostnamectl", "set-hostname", config.hostname);
var success = setHostnameRetCode == 0;
if (!success) {
logger.error("hostnamectl return non-zero exit code " + setHostnameRetCode + "!");
}
} catch (Exception e) {
logger.error("Failed to set hostname!", e);
}
} else {
logger.warn("Got empty hostname?");
}
if (config.connectionType == NetworkMode.DHCP) {
return; // TODO do we need to reconnect or something?
} else if (config.connectionType == NetworkMode.STATIC) {
try {
new ShellExec()
.executeBashCommand("ip addr add " + config.staticIp + "/24" + " dev eth0");
} catch (Exception e) {
e.printStackTrace();
var shell = new ShellExec();
if (config.staticIp.length() > 0) {
try {
shell.executeBashCommand("ip addr add " + config.staticIp + "/24" + " dev eth0");
} catch (Exception e) {
logger.error("Error while setting static IP!", e);
}
} else {
logger.warn("Got empty static IP?");
}
}
} else {