Skip to content

Commit 80447b1

Browse files
Add OpenAPI documentation with swagger-ui
1 parent 4ce7846 commit 80447b1

6 files changed

Lines changed: 81 additions & 23 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
</dependency>
3434
<dependency>
3535
<groupId>org.springdoc</groupId>
36-
<artifactId>springdoc-openapi-ui</artifactId>
37-
<version>1.6.15</version>
36+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
37+
<version>2.2.0</version>
3838
</dependency>
3939
<dependency>
4040
<groupId>org.java-websocket</groupId>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.gravitylab.obscontrollerapi.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import io.swagger.v3.oas.models.OpenAPI;
7+
import io.swagger.v3.oas.models.info.Info;
8+
9+
@Configuration
10+
public class OpenApiConfig {
11+
@Bean
12+
public OpenAPI customOpenAPI() {
13+
return new OpenAPI().info(new Info().title("OBS WebSocket API").version("v1.0")
14+
.description("API for controlling OBS through WebSocket"));
15+
}
16+
}

src/main/java/com/gravitylab/obscontrollerapi/controller/OBSController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.gravitylab.obscontrollerapi.service.OBSWebSocketService;
1111

12+
import io.swagger.v3.oas.annotations.tags.Tag;
1213
import lombok.extern.slf4j.Slf4j;
1314

1415
@RestController
@@ -23,6 +24,7 @@ public OBSController(OBSWebSocketService obsWebSocketService) {
2324
this.obsWebSocketService = obsWebSocketService;
2425
}
2526

27+
@Tag(name = "Connection Management")
2628
@PostMapping("/connect")
2729
public ResponseEntity<?> connect(@RequestParam String ipAddress, @RequestParam int port,
2830
@RequestParam String password) {
@@ -35,30 +37,35 @@ public ResponseEntity<?> connect(@RequestParam String ipAddress, @RequestParam i
3537
}
3638
}
3739

40+
@Tag(name = "Connection Management")
3841
@PostMapping("/authenticate")
3942
public ResponseEntity<?> authenticate(@RequestParam String ipAddress, @RequestParam int port) {
4043
obsWebSocketService.authenticate(ipAddress, port);
4144
return ResponseEntity.ok("Authenticated OBS at " + ipAddress + ":" + port);
4245
}
4346

47+
@Tag(name = "Connection Management")
4448
@PostMapping("/reconnect")
4549
public ResponseEntity<?> reconnect(@RequestParam String ipAddress, @RequestParam int port) {
4650
obsWebSocketService.reconnect(ipAddress, port);
4751
return ResponseEntity.ok("Reconnected to OBS at " + ipAddress + ":" + port);
4852
}
4953

54+
@Tag(name = "Connection Management")
5055
@PostMapping("/disconnect")
5156
public ResponseEntity<?> disconnect(@RequestParam String ipAddress, @RequestParam int port) {
5257
obsWebSocketService.disconnect(ipAddress, port);
5358
return ResponseEntity.ok("Disconnected from OBS at " + ipAddress + ":" + port);
5459
}
5560

61+
@Tag(name = "Recording Control")
5662
@PostMapping("/startRecording")
5763
public ResponseEntity<?> startRecording(@RequestParam String ipAddress, @RequestParam int port) {
5864
obsWebSocketService.startRecording(ipAddress, port);
5965
return ResponseEntity.ok("Started recording on OBS at " + ipAddress + ":" + port);
6066
}
6167

68+
@Tag(name = "Recording Control")
6269
@PostMapping("/stopRecording")
6370
public ResponseEntity<?> stopRecording(@RequestParam String ipAddress, @RequestParam int port) {
6471
obsWebSocketService.stopRecording(ipAddress, port);

src/main/java/com/gravitylab/obscontrollerapi/service/OBSWebSocketService.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,40 @@ public class OBSWebSocketService {
1313

1414
private final Map<String, OBSWebSocketClient> clients = new ConcurrentHashMap<>();
1515

16+
/**
17+
* Connects to an OBS instance and stores the client in a map
18+
*
19+
* @param ipAddress IP address of the OBS instance
20+
* @param port Port of the OBS instance
21+
* @param password Password of the OBS instance
22+
* @throws URISyntaxException thrown if the URI is invalid
23+
*/
1624
public void connectClient(String ipAddress, int port, String password) throws URISyntaxException {
1725
String key = createClientKey(ipAddress, port);
1826
OBSWebSocketClient client = new OBSWebSocketClient(ipAddress, port, password);
1927
clients.put(key, client);
2028
client.connect();
2129
}
2230

31+
/**
32+
* Authenticates an OBS instance
33+
*
34+
* @param ipAddress IP address of the OBS instance
35+
* @param port Port of the OBS instance
36+
*/
2337
public void authenticate(String ipAddress, int port) {
2438
OBSWebSocketClient client = getClient(ipAddress, port);
2539
if (client != null) {
2640
client.authenticate();
2741
}
2842
}
2943

44+
/**
45+
* Disconnects from an OBS instance
46+
*
47+
* @param ipAddress IP address of the OBS instance
48+
* @param port Port of the OBS instance
49+
*/
3050
public void disconnect(String ipAddress, int port) {
3151
OBSWebSocketClient client = getClient(ipAddress, port);
3252
if (client != null) {
@@ -35,31 +55,63 @@ public void disconnect(String ipAddress, int port) {
3555
}
3656
}
3757

58+
/**
59+
* Starts recording on an OBS instance
60+
*
61+
* @param ipAddress IP address of the OBS instance
62+
* @param port Port of the OBS instance
63+
*/
3864
public void startRecording(String ipAddress, int port) {
3965
OBSWebSocketClient client = getClient(ipAddress, port);
4066
if (client != null) {
4167
client.startRecording();
4268
}
4369
}
4470

71+
/**
72+
* Stops recording on an OBS instance
73+
*
74+
* @param ipAddress IP address of the OBS instance
75+
* @param port Port of the OBS instance
76+
*/
4577
public void stopRecording(String ipAddress, int port) {
4678
OBSWebSocketClient client = getClient(ipAddress, port);
4779
if (client != null) {
4880
client.stopRecording();
4981
}
5082
}
5183

84+
/**
85+
* Reconnects to an OBS instance
86+
*
87+
* @param ipAddress IP address of the OBS instance
88+
* @param port Port of the OBS instance
89+
*/
5290
public void reconnect(String ipAddress, int port) {
5391
OBSWebSocketClient client = getClient(ipAddress, port);
5492
if (client != null) {
5593
client.reconnect();
5694
}
5795
}
5896

97+
/**
98+
* Gets an OBS client from the map
99+
*
100+
* @param ipAddress IP address of the OBS instance
101+
* @param port Port of the OBS instance
102+
* @return OBS client
103+
*/
59104
private OBSWebSocketClient getClient(String ipAddress, int port) {
60105
return clients.get(createClientKey(ipAddress, port));
61106
}
62107

108+
/**
109+
* Creates a key for the OBS client map
110+
*
111+
* @param ipAddress IP address
112+
* @param port Port
113+
* @return key
114+
*/
63115
private String createClientKey(String ipAddress, int port) {
64116
return ipAddress + ":" + port;
65117
}

src/main/java/com/gravitylab/obscontrollerapi/websocket/OBSWebSocketClient.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import java.security.MessageDigest;
77
import java.security.NoSuchAlgorithmException;
88
import java.util.Base64;
9-
import java.util.concurrent.Executors;
10-
import java.util.concurrent.ScheduledExecutorService;
11-
import java.util.concurrent.TimeUnit;
129

1310
import org.java_websocket.client.WebSocketClient;
1411
import org.java_websocket.handshake.ServerHandshake;
@@ -25,13 +22,11 @@ public class OBSWebSocketClient extends WebSocketClient {
2522

2623
static int requestID = 0;
2724
static int rpcVersion = 1;
28-
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
2925

3026
public OBSWebSocketClient(String ipAddress, int port, String password) throws URISyntaxException {
3127
super(new URI("ws://" + ipAddress + ":" + port));
32-
log.info("OBSWebSocketClient initialised!");
3328
this.obsPassword = password;
34-
log.info("OBS Password set to {}", this.obsPassword);
29+
log.info("OBSWebSocketClient initialised!");
3530
}
3631

3732
@Override
@@ -85,7 +80,6 @@ private void handleAuthentication(JSONObject receivedJson) {
8580
String salt = authenticationData.getString("salt");
8681
String challenge = authenticationData.getString("challenge");
8782
this.authToken = generateAuthToken(salt, challenge);
88-
log.info("Token generated :)");
8983
}
9084

9185
private void sendStartRecordRequest() {
@@ -102,20 +96,6 @@ private void sendStartRecordRequest() {
10296
log.info("Sent request to OBS Websocket {}", request.toString(4));
10397
}
10498

105-
private void handleRecordStateChange(JSONObject receivedJson) {
106-
JSONObject eventData = receivedJson.optJSONObject("d").getJSONObject("eventData");
107-
String eventType = receivedJson.getJSONObject("d").getString("eventType");
108-
boolean outputActive = eventData.getBoolean("outputActive");
109-
String outputState = eventData.getString("outputState");
110-
var outputPath = eventData.get("outputPath");
111-
112-
if (outputActive && outputState.equals("OBS_WEBSOCKET_OUTPUT_STARTED")) {
113-
log.info("Event type: {} ,Output active: {}, Output state: {}, Output path: {}", eventType, outputActive,
114-
outputState, outputPath);
115-
scheduler.schedule(this::sendStopRecordRequest, 30, TimeUnit.SECONDS);
116-
}
117-
}
118-
11999
private void sendStopRecordRequest() {
120100
JSONObject stopRecordRequest = new JSONObject();
121101
stopRecordRequest.put("op", 6); // Assuming '6' is the operation code for StopRecord
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
springdoc.swagger-ui.path=/docs.html
2+
springdoc.api-docs.enabled=true
3+
springdoc.enable-javadoc=true

0 commit comments

Comments
 (0)