Skip to content

Commit d1e0874

Browse files
committed
Use generics instead of casting every response
Should fix #19
1 parent 97f74ee commit d1e0874

File tree

10 files changed

+213
-415
lines changed

10 files changed

+213
-415
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<name>obs-websocket-java</name>
77
<groupId>net.twasi</groupId>
88
<artifactId>obs-websocket-java</artifactId>
9-
<version>1.0.7</version>
9+
<version>1.1.0</version>
1010

1111
<description>Library to connect to the OBS WebSocket interface.</description>
1212
<url>https://github.com/Twasi/obs-websocket-java</url>

src/main/java/net/twasi/obsremotejava/OBSCommunicator.java

Lines changed: 72 additions & 72 deletions
Large diffs are not rendered by default.

src/main/java/net/twasi/obsremotejava/OBSRemoteController.java

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,39 @@
33
import net.twasi.obsremotejava.callbacks.Callback;
44
import net.twasi.obsremotejava.callbacks.ErrorCallback;
55
import net.twasi.obsremotejava.callbacks.StringCallback;
6+
import net.twasi.obsremotejava.callbacks.VoidCallback;
7+
import net.twasi.obsremotejava.events.responses.*;
68
import net.twasi.obsremotejava.objects.throwables.OBSResponseError;
9+
import net.twasi.obsremotejava.requests.GetCurrentProfile.GetCurrentProfileResponse;
10+
import net.twasi.obsremotejava.requests.GetCurrentScene.GetCurrentSceneResponse;
11+
import net.twasi.obsremotejava.requests.GetPreviewScene.GetPreviewSceneResponse;
12+
import net.twasi.obsremotejava.requests.GetSceneList.GetSceneListResponse;
13+
import net.twasi.obsremotejava.requests.GetSourceSettings.GetSourceSettingsResponse;
14+
import net.twasi.obsremotejava.requests.GetStreamingStatus.GetStreamingStatusResponse;
15+
import net.twasi.obsremotejava.requests.GetStudioModeEnabled.GetStudioModeEnabledResponse;
16+
import net.twasi.obsremotejava.requests.GetTransitionDuration.GetTransitionDurationResponse;
17+
import net.twasi.obsremotejava.requests.GetTransitionList.GetTransitionListResponse;
18+
import net.twasi.obsremotejava.requests.GetVersion.GetVersionResponse;
19+
import net.twasi.obsremotejava.requests.GetVolume.GetVolumeResponse;
20+
import net.twasi.obsremotejava.requests.ListProfiles.ListProfilesResponse;
21+
import net.twasi.obsremotejava.requests.SaveReplayBuffer.SaveReplayBufferResponse;
22+
import net.twasi.obsremotejava.requests.SetCurrentProfile.SetCurrentProfileResponse;
23+
import net.twasi.obsremotejava.requests.SetCurrentScene.SetCurrentSceneResponse;
24+
import net.twasi.obsremotejava.requests.SetCurrentTransition.SetCurrentTransitionResponse;
25+
import net.twasi.obsremotejava.requests.SetMute.SetMuteResponse;
26+
import net.twasi.obsremotejava.requests.SetPreviewScene.SetPreviewSceneResponse;
27+
import net.twasi.obsremotejava.requests.SetSceneItemProperties.SetSceneItemPropertiesResponse;
28+
import net.twasi.obsremotejava.requests.SetSourceSettings.SetSourceSettingsResponse;
29+
import net.twasi.obsremotejava.requests.SetStudioModeEnabled.SetStudioModeEnabledResponse;
30+
import net.twasi.obsremotejava.requests.SetTransitionDuration.SetTransitionDurationResponse;
31+
import net.twasi.obsremotejava.requests.SetVolume.SetVolumeResponse;
32+
import net.twasi.obsremotejava.requests.StartRecording.StartRecordingResponse;
33+
import net.twasi.obsremotejava.requests.StartReplayBuffer.StartReplayBufferResponse;
34+
import net.twasi.obsremotejava.requests.StartStreaming.StartStreamingResponse;
35+
import net.twasi.obsremotejava.requests.StopRecording.StopRecordingResponse;
36+
import net.twasi.obsremotejava.requests.StopReplayBuffer.StopReplayBufferResponse;
37+
import net.twasi.obsremotejava.requests.StopStreaming.StopStreamingResponse;
38+
import net.twasi.obsremotejava.requests.TransitionToProgram.TransitionToProgramResponse;
739
import org.eclipse.jetty.websocket.api.Session;
840
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
941
import org.eclipse.jetty.websocket.client.WebSocketClient;
@@ -84,7 +116,7 @@ public void connect() {
84116
public void disconnect() {
85117
// wait for closed socket connection
86118
try {
87-
if(debug) {
119+
if (debug) {
88120
log.debug("Closing connection.");
89121
}
90122
communicator.awaitClose(1, TimeUnit.SECONDS);
@@ -94,7 +126,7 @@ public void disconnect() {
94126

95127
if (!client.isStopped() && !client.isStopping()) {
96128
try {
97-
if(debug) {
129+
if (debug) {
98130
log.debug("Stopping client.");
99131
}
100132
client.stop();
@@ -108,7 +140,7 @@ public boolean isFailed() {
108140
return failed;
109141
}
110142

111-
public void getScenes(Callback callback) {
143+
public void getScenes(Callback<GetSceneListResponse> callback) {
112144
communicator.getScenes(callback);
113145
}
114146

@@ -117,11 +149,11 @@ public void registerOnError(ErrorCallback onError) {
117149
communicator.registerOnError(onError);
118150
}
119151

120-
public void registerConnectCallback(Callback onConnect) {
152+
public void registerConnectCallback(Callback<GetVersionResponse> onConnect) {
121153
communicator.registerOnConnect(onConnect);
122154
}
123155

124-
public void registerDisconnectCallback(Callback onDisconnect) {
156+
public void registerDisconnectCallback(VoidCallback onDisconnect) {
125157
communicator.registerOnDisconnect(onDisconnect);
126158
}
127159

@@ -130,75 +162,75 @@ public void registerConnectionFailedCallback(StringCallback onConnectionFailed)
130162
communicator.registerOnConnectionFailed(onConnectionFailed);
131163
}
132164

133-
public void registerRecordingStartedCallback(Callback onRecordingStarted) {
165+
public void registerRecordingStartedCallback(VoidCallback onRecordingStarted) {
134166
communicator.registerOnRecordingStarted(onRecordingStarted);
135167
}
136168

137-
public void registerRecordingStoppedCallback(Callback onRecordingStopped) {
169+
public void registerRecordingStoppedCallback(VoidCallback onRecordingStopped) {
138170
communicator.registerOnRecordingStopped(onRecordingStopped);
139171
}
140172

141-
public void registerReplayStartedCallback(Callback onReplayStarted) {
173+
public void registerReplayStartedCallback(VoidCallback onReplayStarted) {
142174
communicator.registerOnReplayStarted(onReplayStarted);
143175
}
144176

145-
public void registerReplayStartingCallback(Callback onReplayStarting) {
177+
public void registerReplayStartingCallback(VoidCallback onReplayStarting) {
146178
communicator.registerOnReplayStarting(onReplayStarting);
147179
}
148180

149-
public void registerReplayStoppedCallback(Callback onReplayStopped) {
181+
public void registerReplayStoppedCallback(VoidCallback onReplayStopped) {
150182
communicator.registerOnReplayStopped(onReplayStopped);
151183
}
152184

153-
public void registerReplayStoppingCallback(Callback onReplayStopping) {
185+
public void registerReplayStoppingCallback(VoidCallback onReplayStopping) {
154186
communicator.registerOnReplayStopping(onReplayStopping);
155187
}
156188

157-
public void registerStreamStartedCallback(Callback onRecordingStarted) {
189+
public void registerStreamStartedCallback(VoidCallback onRecordingStarted) {
158190
communicator.registerOnStreamStarted(onRecordingStarted);
159191
}
160192

161-
public void registerStreamStoppedCallback(Callback onRecordingStopped) {
193+
public void registerStreamStoppedCallback(VoidCallback onRecordingStopped) {
162194
communicator.registerOnStreamStopped(onRecordingStopped);
163195
}
164196

165-
public void registerSwitchScenesCallback(Callback onSwitchScenes) {
197+
public void registerSwitchScenesCallback(Callback<SwitchScenesResponse> onSwitchScenes) {
166198
communicator.registerOnSwitchScenes(onSwitchScenes);
167199
}
168200

169-
public void registerScenesChangedCallback(Callback onScenesChanged) {
201+
public void registerScenesChangedCallback(Callback<ScenesChangedResponse> onScenesChanged) {
170202
communicator.registerOnScenesChanged(onScenesChanged);
171203
}
172204

173-
public void registerSwitchTransitionCallback(Callback onSwitchTransition) {
205+
public void registerSwitchTransitionCallback(Callback<SwitchTransitionResponse> onSwitchTransition) {
174206
communicator.registerOnSwitchTransition(onSwitchTransition);
175207
}
176208

177-
public void registerTransitionListChangedCallback(Callback onTransitionListChanged) {
209+
public void registerTransitionListChangedCallback(Callback<TransitionListChangedResponse> onTransitionListChanged) {
178210
communicator.registerOnTransitionListChanged(onTransitionListChanged);
179211
}
180212

181-
public void registerTransitionBeginCallback(Callback onTransitionBegin) {
213+
public void registerTransitionBeginCallback(Callback<TransitionBeginResponse> onTransitionBegin) {
182214
communicator.registerOnTransitionBegin(onTransitionBegin);
183215
}
184216

185-
public void registerTransitionEndCallback(Callback onTransitionEnd) {
217+
public void registerTransitionEndCallback(Callback<TransitionEndResponse> onTransitionEnd) {
186218
communicator.registerOnTransitionEnd(onTransitionEnd);
187219
}
188220

189221
public void await() throws InterruptedException {
190222
communicator.await();
191223
}
192224

193-
public void setCurrentScene(String szene, Callback callback) {
194-
communicator.setCurrentScene(szene, callback);
225+
public void setCurrentScene(String scene, Callback<SetCurrentSceneResponse> callback) {
226+
communicator.setCurrentScene(scene, callback);
195227
}
196228

197-
public void setCurrentTransition(String transition, Callback callback) {
229+
public void setCurrentTransition(String transition, Callback<SetCurrentTransitionResponse> callback) {
198230
communicator.setCurrentTransition(transition, callback);
199231
}
200232

201-
public void changeSceneWithTransition(final String scene, String transition, final Callback callback) {
233+
public void changeSceneWithTransition(final String scene, String transition, final Callback<SetCurrentSceneResponse> callback) {
202234
communicator.setCurrentTransition(transition, response -> {
203235
if (!response.getStatus().equals("ok")) {
204236
log.error("Failed to change transition. Pls fix.");
@@ -208,112 +240,112 @@ public void changeSceneWithTransition(final String scene, String transition, fin
208240
});
209241
}
210242

211-
public void setSourceVisibility(String scene, String source, boolean visibility, Callback callback) {
243+
public void setSourceVisibility(String scene, String source, boolean visibility, Callback<SetSceneItemPropertiesResponse> callback) {
212244
communicator.setSourceVisiblity(scene, source, visibility, callback);
213245
}
214246

215-
public void getSceneItemProperties(String scene, String source, Callback callback) {
247+
public void getSceneItemProperties(String scene, String source, Callback<SetSceneItemPropertiesResponse> callback) {
216248
communicator.getSceneItemProperties(scene, source, callback);
217249
}
218250

219-
public void getTransitionList(Callback callback) {
251+
public void getTransitionList(Callback<GetTransitionListResponse> callback) {
220252
communicator.getTransitionList(callback);
221253
}
222254

223-
public void transitionToProgram(String transitionName, int duration, Callback callback) {
255+
public void transitionToProgram(String transitionName, int duration, Callback<TransitionToProgramResponse> callback) {
224256
communicator.transitionToProgram(transitionName, duration, callback);
225257
}
226258

227-
public void getSourceSettings(String sourceName, Callback callback) {
259+
public void getSourceSettings(String sourceName, Callback<GetSourceSettingsResponse> callback) {
228260
communicator.getSourceSettings(sourceName, callback);
229261
}
230262

231-
public void setSourceSettings(String sourceName, Map<String, Object> settings, Callback callback) {
263+
public void setSourceSettings(String sourceName, Map<String, Object> settings, Callback<SetSourceSettingsResponse> callback) {
232264
communicator.setSourceSettings(sourceName, settings, callback);
233265
}
234266

235-
public void getStreamingStatus(Callback callback) {
267+
public void getStreamingStatus(Callback<GetStreamingStatusResponse> callback) {
236268
communicator.getStreamingStatus(callback);
237269
}
238270

239-
public void startStreaming(Callback callback) {
271+
public void startStreaming(Callback<StartStreamingResponse> callback) {
240272
communicator.startStreaming(callback);
241273
}
242274

243-
public void stopStreaming(Callback callback) {
275+
public void stopStreaming(Callback<StopStreamingResponse> callback) {
244276
communicator.stopStreaming(callback);
245277
}
246278

247-
public void startRecording(Callback callback) {
279+
public void startRecording(Callback<StartRecordingResponse> callback) {
248280
communicator.startRecording(callback);
249281
}
250282

251-
public void stopRecording(Callback callback) {
283+
public void stopRecording(Callback<StopRecordingResponse> callback) {
252284
communicator.stopRecording(callback);
253285
}
254286

255-
public void listProfiles(Callback callback) {
287+
public void listProfiles(Callback<ListProfilesResponse> callback) {
256288
communicator.listProfiles(callback);
257289
}
258290

259-
public void getCurrentProfile(Callback callback) {
291+
public void getCurrentProfile(Callback<GetCurrentProfileResponse> callback) {
260292
communicator.getCurrentProfile(callback);
261293
}
262294

263-
public void setCurrentProfile(String profile, Callback callback) {
295+
public void setCurrentProfile(String profile, Callback<SetCurrentProfileResponse> callback) {
264296
communicator.setCurrentProfile(profile, callback);
265297
}
266298

267-
public void getCurrentScene(Callback callback) {
299+
public void getCurrentScene(Callback<GetCurrentSceneResponse> callback) {
268300
communicator.getCurrentScene(callback);
269301
}
270302

271-
public void getVolume(String source, Callback callback) {
303+
public void getVolume(String source, Callback<GetVolumeResponse> callback) {
272304
communicator.getVolume(source, callback);
273305
}
274306

275-
public void setVolume(String source, double volume, Callback callback) {
307+
public void setVolume(String source, double volume, Callback<SetVolumeResponse> callback) {
276308
communicator.setVolume(source, volume, callback);
277309
}
278310

279-
public void setMute(String source, boolean mute, Callback callback) {
311+
public void setMute(String source, boolean mute, Callback<SetMuteResponse> callback) {
280312
communicator.setMute(source, mute, callback);
281313
}
282314

283-
public void getPreviewScene(Callback callback) {
315+
public void getPreviewScene(Callback<GetPreviewSceneResponse> callback) {
284316
communicator.getPreviewScene(callback);
285317
}
286318

287-
public void setPreviewScene(String name, Callback callback) {
319+
public void setPreviewScene(String name, Callback<SetPreviewSceneResponse> callback) {
288320
communicator.setPreviewScene(name, callback);
289321
}
290322

291-
public void getTransitionDuration(Callback callback) {
323+
public void getTransitionDuration(Callback<GetTransitionDurationResponse> callback) {
292324
communicator.getTransitionDuration(callback);
293325
}
294326

295-
public void setTransitionDuration(int duration, Callback callback) {
327+
public void setTransitionDuration(int duration, Callback<SetTransitionDurationResponse> callback) {
296328
communicator.setTransitionDuration(duration, callback);
297329
}
298330

299331

300-
public void getStudioModeEnabled(Callback callback) {
332+
public void getStudioModeEnabled(Callback<GetStudioModeEnabledResponse> callback) {
301333
communicator.getStudioModeEnabled(callback);
302334
}
303335

304-
public void setStudioModeEnabled(boolean enabled, Callback callback) {
336+
public void setStudioModeEnabled(boolean enabled, Callback<SetStudioModeEnabledResponse> callback) {
305337
communicator.setStudioModeEnabled(enabled, callback);
306338
}
307339

308-
public void startReplayBuffer(Callback callback) {
340+
public void startReplayBuffer(Callback<StartReplayBufferResponse> callback) {
309341
communicator.startReplayBuffer(callback);
310342
}
311343

312-
public void stopReplayBuffer(Callback callback) {
344+
public void stopReplayBuffer(Callback<StopReplayBufferResponse> callback) {
313345
communicator.stopReplayBuffer(callback);
314346
}
315347

316-
public void saveReplayBuffer(Callback callback) {
348+
public void saveReplayBuffer(Callback<SaveReplayBufferResponse> callback) {
317349
communicator.saveReplayBuffer(callback);
318350
}
319351

src/main/java/net/twasi/obsremotejava/callbacks/Callback.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import net.twasi.obsremotejava.requests.ResponseBase;
44

5-
public interface Callback {
5+
public interface Callback<ResponseType extends ResponseBase> {
66

7-
void run(ResponseBase response);
7+
void run(ResponseType response);
88

99
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package net.twasi.obsremotejava.callbacks;
2+
3+
public interface VoidCallback {
4+
5+
void run();
6+
7+
}

0 commit comments

Comments
 (0)