Skip to content

Commit 2844cec

Browse files
authored
Merge pull request #315 from EasternEdgeRobotics/keithID
Discover local IP on rov network
2 parents 1f1d4f2 + 3349220 commit 2844cec

3 files changed

Lines changed: 47 additions & 6 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ repositories {
2424
}
2525

2626
dependencies {
27+
compile files("${System.properties['java.home']}/../lib/tools.jar")
2728
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.8.7"
2829
compile 'com.neuronrobotics:nrjavaserial:3.11.0'
2930
compile 'com.pi4j:pi4j-core:1.0'

src/main/java/com/easternedgerobotics/rov/Topside.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ public void init() throws SocketException, UnknownHostException {
113113
Schedulers.io());
114114

115115
videoDecoder = new VideoDecoder(
116-
eventPublisher,
117-
configSource.getConfig("videoDecoder",
118-
VideoDecoderConfig.class));
116+
eventPublisher,
117+
configSource.getConfig("videoDecoder", VideoDecoderConfig.class),
118+
launchConfig.broadcast());
119119

120120
final ValueStore<CameraCalibrationValue> cameraCalibrationStore = ValueStore.of(
121121
CameraCalibrationValue.class, config.preferencesHome());

src/main/java/com/easternedgerobotics/rov/video/VideoDecoder.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
import com.easternedgerobotics.rov.value.VideoValueB;
77

88
import javafx.scene.image.Image;
9+
import org.pmw.tinylog.Logger;
910
import rx.Observable;
1011

12+
import java.net.InetAddress;
13+
import java.net.NetworkInterface;
14+
import java.net.SocketException;
15+
16+
import java.util.ArrayList;
17+
import java.util.Enumeration;
18+
import java.util.List;
19+
1120
public final class VideoDecoder {
1221
private final EventPublisher eventPublisher;
1322

@@ -17,9 +26,12 @@ public final class VideoDecoder {
1726

1827
private final FFmpegFXImageDecoder decoderB;
1928

29+
private final String broadcastIP;
30+
2031
public VideoDecoder(
2132
final EventPublisher eventPublisher,
22-
final VideoDecoderConfig config
33+
final VideoDecoderConfig config,
34+
final String broadcast
2335
) {
2436
this.eventPublisher = eventPublisher;
2537
this.config = config;
@@ -41,13 +53,41 @@ public VideoDecoder(
4153
config.preset(),
4254
config.numBuffers(),
4355
config.introVideoLocation());
56+
final int lastLocation = broadcast.lastIndexOf('.');
57+
broadcastIP = broadcast.substring(0, lastLocation - 1);
4458
}
4559

4660
public void start() {
4761
decoderA.start();
4862
decoderB.start();
49-
eventPublisher.emit(new VideoValueA(config.host(), config.portA()));
50-
eventPublisher.emit(new VideoValueB(config.host(), config.portB()));
63+
boolean initialized = false;
64+
final List<String> ipAddresses = new ArrayList<String>();
65+
try {
66+
final Enumeration<NetworkInterface> network = NetworkInterface.getNetworkInterfaces();
67+
while (network.hasMoreElements()) {
68+
final Enumeration<InetAddress> inet = network.nextElement().getInetAddresses();
69+
while (inet.hasMoreElements()) {
70+
ipAddresses.add(inet.nextElement().toString());
71+
}
72+
}
73+
} catch (final SocketException error) {
74+
Logger.error(error);
75+
}
76+
for (final String newAddress: ipAddresses) {
77+
final int lastLocation = newAddress.lastIndexOf('.');
78+
if (lastLocation >= 0) {
79+
final String testAddress = newAddress.substring(1, lastLocation - 1);
80+
if (broadcastIP.equals(testAddress)) {
81+
eventPublisher.emit(new VideoValueA(newAddress, config.portA()));
82+
eventPublisher.emit(new VideoValueB(newAddress, config.portB()));
83+
initialized = true;
84+
break;
85+
}
86+
}
87+
}
88+
if (!initialized) {
89+
Logger.warn("Could not detect the IP of the runtime system");
90+
}
5191
}
5292

5393
public void stop() {

0 commit comments

Comments
 (0)