Skip to content

Commit 77cf189

Browse files
committed
Updates Sources to Resolve PR Comments
1 parent 8dd34a9 commit 77cf189

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

core/src/main/java/edu/wpi/grip/core/Source.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,39 @@ public final OutputSocket[] getOutputSockets() {
4747
*/
4848
public abstract void createFromProperties(EventBus eventBus, Properties properties) throws IOException;
4949

50-
51-
public <T extends Source> T start(EventBus eventBus) throws IOException {
52-
final T source = (T) start();
53-
eventBus.register(source);
54-
return source;
55-
}
56-
5750
/**
5851
* Starts this source.
5952
* A source whose {@link #canStopAndStart()} returns true can also be stopped and started and stopped multiple times.
6053
*
6154
* @return The source object that created the camera
6255
* @throws IOException If the source fails to be started
6356
*/
64-
protected abstract Source start() throws IOException;
57+
public <T extends Source> T start(EventBus eventBus) throws IOException {
58+
start();
59+
eventBus.register(this);
60+
return (T) this;
61+
}
62+
63+
/**
64+
* Any method that overrides this method should post a {@link edu.wpi.grip.core.events.SourceStartedEvent}
65+
* to the {@link EventBus} if is successfully starts.
66+
* @throws IOException If the source fails to be started
67+
*/
68+
protected abstract void start() throws IOException;
6569

6670
/**
6771
* Stops this source.
6872
* This will stop the source publishing new socket values after this method returns.
6973
* A source whose {@link #canStopAndStart()} returns true can also be stopped and started and stopped multiple times.
7074
*
75+
* Any method that overrides this method should post a {@link edu.wpi.grip.core.events.SourceStoppedEvent}
76+
* to the {@link EventBus} if is successfully stops.
77+
*
7178
* @return The source that was stopped
72-
* @throws TimeoutException if the thread running the source fails to stop
79+
* @throws TimeoutException if the thread running the source fails to stop.
80+
* @throws IOException If there is a problem stopping the Source
7381
*/
74-
public abstract Source stop() throws TimeoutException;
82+
public void stop() throws TimeoutException, IOException { /* no op */ }
7583

7684
/**
7785
* Used to indicate if the source is running or stopped

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ public void createFromProperties(EventBus eventBus, Properties properties) throw
128128
/**
129129
* Starts the video capture from the source device
130130
*/
131-
public CameraSource start() throws IOException, IllegalStateException {
131+
protected void start() throws IOException, IllegalStateException {
132132
final OpenCVFrameConverter.ToMat convertToMat = new OpenCVFrameConverter.ToMat();
133-
synchronized (this.frameThread) {
133+
synchronized (this) {
134134
if (this.frameThread.isPresent()) {
135135
throw new IllegalStateException("The video retrieval thread has already been started.");
136136
}
@@ -179,7 +179,6 @@ public CameraSource start() throws IOException, IllegalStateException {
179179
this.frameThread = Optional.of(frameExecutor);
180180
}
181181
eventBus.post(new SourceStartedEvent(this));
182-
return this;
183182
}
184183

185184
/**
@@ -188,8 +187,8 @@ public CameraSource start() throws IOException, IllegalStateException {
188187
* @throws TimeoutException If the thread running the Webcam fails to join this one after a timeout.
189188
* @throws IllegalStateException If the camera was already stopped
190189
*/
191-
public CameraSource stop() throws TimeoutException, IllegalStateException {
192-
synchronized (this.frameThread) {
190+
public void stop() throws TimeoutException, IllegalStateException {
191+
synchronized (this) {
193192
if (frameThread.isPresent()) {
194193
final Thread ex = frameThread.get();
195194
ex.interrupt();
@@ -219,12 +218,11 @@ public CameraSource stop() throws TimeoutException, IllegalStateException {
219218
}
220219
eventBus.post(new SourceStoppedEvent(this));
221220
frameRateOutputSocket.setValue(0);
222-
return this;
223221
}
224222

225223
@Override
226224
public boolean isRunning() {
227-
synchronized (this.frameThread) {
225+
synchronized (this) {
228226
return this.frameThread.isPresent() && this.frameThread.get().isAlive();
229227
}
230228
}

core/src/main/java/edu/wpi/grip/core/sources/ImageFileSource.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,9 @@ public void createFromProperties(EventBus eventBus, Properties properties) throw
8484
this.initialize(eventBus, path);
8585
}
8686

87-
public ImageFileSource start() throws IOException {
87+
protected void start() throws IOException {
8888
this.started = true;
8989
loadImage(this.path);
90-
return this;
91-
}
92-
93-
@Override
94-
public ImageFileSource stop() {
95-
return this;
9690
}
9791

9892
@Override

ui/src/main/java/edu/wpi/grip/ui/pipeline/SourceView.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javafx.scene.layout.VBox;
2626

2727
import java.io.IOException;
28+
import java.util.concurrent.TimeoutException;
2829

2930
/**
3031
* A JavaFX control that represents a {@link Source}. <code>SourceView</code>s are somewhat analogous to
@@ -76,12 +77,14 @@ public SourceView(EventBus eventBus, Source source) {
7677
event.consume();
7778
if (!startStopButton.isSelected()) try {
7879
source.start(eventBus);
80+
// If this fails then an SourceStartedEvent will not be posted
7981
} catch (IOException e) {
8082
eventBus.post(new FatalErrorEvent(e));
8183
}
8284
else try {
8385
source.stop();
84-
} catch (Exception e) {
86+
// If this fails then an SourceStoppedEvent will not be posted
87+
} catch (TimeoutException | IOException e) {
8588
eventBus.post(new FatalErrorEvent(e));
8689
}
8790
});

0 commit comments

Comments
 (0)