Fix network config "Supported" bug (#130)

* Fix network config bug

* Use Jackson instead of hacky solution for management

* run spotless

* Add assertion
This commit is contained in:
Matt
2020-09-24 15:34:47 -07:00
committed by GitHub
parent 125cd35557
commit 28459704c6
2 changed files with 57 additions and 11 deletions

View File

@@ -17,6 +17,10 @@
package org.photonvision.common.configuration;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import java.util.HashMap;
import java.util.Map;
import org.photonvision.common.hardware.Platform;
@@ -29,24 +33,26 @@ public class NetworkConfig {
public String hostname = "photonvision";
public boolean runNTServer = false;
public boolean shouldManage;
private boolean shouldManage;
public NetworkConfig() {}
public NetworkConfig() {
setShouldManage(false);
}
@JsonCreator
public NetworkConfig(
int teamNumber,
NetworkMode connectionType,
String staticIp,
String hostname,
boolean runNTServer,
boolean shouldManage) {
@JsonProperty("teamNumber") int teamNumber,
@JsonProperty("connectionType") NetworkMode connectionType,
@JsonProperty("staticIp") String staticIp,
@JsonProperty("hostname") String hostname,
@JsonProperty("runNTServer") boolean runNTServer,
@JsonProperty("shouldManage") boolean shouldManage) {
this.teamNumber = teamNumber;
this.connectionType = connectionType;
this.staticIp = staticIp;
this.hostname = hostname;
this.runNTServer = runNTServer;
this.shouldManage = shouldManage;
setShouldManage(shouldManage);
}
public static NetworkConfig fromHashMap(Map<String, Object> map) {
@@ -54,11 +60,11 @@ public class NetworkConfig {
// staticIp (str), netmask (str), hostname (str)
var ret = new NetworkConfig();
ret.teamNumber = Integer.parseInt(map.get("teamNumber").toString());
ret.shouldManage = (Boolean) map.get("supported");
ret.connectionType = NetworkMode.values()[(Integer) map.get("connectionType")];
ret.staticIp = (String) map.get("staticIp");
ret.hostname = (String) map.get("hostname");
ret.runNTServer = (Boolean) map.get("runNTServer");
ret.setShouldManage((Boolean) map.get("supported"));
return ret;
}
@@ -73,7 +79,13 @@ public class NetworkConfig {
return tmp;
}
@JsonGetter("shouldManage")
public boolean shouldManage() {
return this.shouldManage || Platform.isRaspberryPi();
}
@JsonSetter("shouldManage")
public void setShouldManage(boolean shouldManage) {
this.shouldManage = shouldManage || Platform.isRaspberryPi();
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2020 Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.common.configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class NetworkConfigTest {
@Test
public void testSerialization() throws IOException {
var mapper = new ObjectMapper();
var path = Path.of("netTest.json");
mapper.writeValue(path.toFile(), new NetworkConfig());
Assertions.assertDoesNotThrow(() -> mapper.readValue(path.toFile(), NetworkConfig.class));
}
}