80
80
import net .twasi .obsremotejava .requests .TransitionToProgram .TransitionToProgramRequest ;
81
81
import net .twasi .obsremotejava .requests .TransitionToProgram .TransitionToProgramResponse ;
82
82
import org .eclipse .jetty .websocket .api .Session ;
83
- import org .eclipse .jetty .websocket .api .annotations .OnWebSocketClose ;
84
- import org .eclipse .jetty .websocket .api .annotations .OnWebSocketConnect ;
85
- import org .eclipse .jetty .websocket .api .annotations .OnWebSocketMessage ;
86
- import org .eclipse .jetty .websocket .api .annotations .WebSocket ;
83
+ import org .eclipse .jetty .websocket .api .annotations .*;
84
+ import org .slf4j .Logger ;
85
+ import org .slf4j .LoggerFactory ;
87
86
88
87
import java .nio .charset .StandardCharsets ;
89
88
import java .security .MessageDigest ;
97
96
98
97
@ WebSocket (maxIdleTime = 360000000 )
99
98
public class OBSCommunicator {
99
+ Logger log = LoggerFactory .getLogger (this .getClass ());
100
+
100
101
private boolean debug ;
101
102
private final String password ;
102
103
private final CountDownLatch closeLatch ;
@@ -147,7 +148,7 @@ public void await() throws InterruptedException {
147
148
148
149
@ OnWebSocketClose
149
150
public void onClose (int statusCode , String reason ) {
150
- System . out . printf ("Connection closed: %d - %s%n" , statusCode , reason );
151
+ log . info ("Connection closed: %d - %s%n" , statusCode , reason );
151
152
this .closeLatch .countDown (); // trigger latch
152
153
try {
153
154
this .onDisconnect .run (null );
@@ -171,12 +172,12 @@ public void onConnect(Session session) {
171
172
@ OnWebSocketMessage
172
173
public void onMessage (String msg ) {
173
174
if (msg == null ) {
174
- System . out . println ("Ignored empty message" );
175
+ log . debug ("Ignored empty message" );
175
176
return ;
176
177
}
177
178
178
179
if (debug ) {
179
- System . out . println (msg );
180
+ log . debug (msg );
180
181
}
181
182
182
183
try {
@@ -189,7 +190,7 @@ public void onMessage(String msg) {
189
190
try {
190
191
processIncomingResponse (responseBase , type );
191
192
} catch (Throwable t ) {
192
- System . err . println ("Failed to process response '" + type .getSimpleName () + "' from websocket." );
193
+ log . error ("Failed to process response '" + type .getSimpleName () + "' from websocket." );
193
194
t .printStackTrace ();
194
195
runOnError ("Failed to process response '" + type .getSimpleName () + "' from websocket" , t );
195
196
}
@@ -207,13 +208,13 @@ public void onMessage(String msg) {
207
208
try {
208
209
processIncomingEvent (msg , eventType );
209
210
} catch (Throwable t ) {
210
- System . err . println ("Failed to execute callback for event: " + eventType );
211
+ log . error ("Failed to execute callback for event: " + eventType );
211
212
t .printStackTrace ();
212
213
runOnError ("Failed to execute callback for event: " + eventType , t );
213
214
}
214
215
}
215
216
} catch (Throwable t ) {
216
- System . err . println ("Failed to process message from websocket." );
217
+ log . error ("Failed to process message from websocket." );
217
218
t .printStackTrace ();
218
219
runOnError ("Failed to process message from websocket" , t );
219
220
}
@@ -223,17 +224,17 @@ private void processIncomingResponse(ResponseBase responseBase, Class type) {
223
224
switch (type .getSimpleName ()) {
224
225
case "GetVersionResponse" :
225
226
versionInfo = (GetVersionResponse ) responseBase ;
226
- System . out . printf ("Connected to OBS. Websocket Version: %s, Studio Version: %s\n " , versionInfo .getObsWebsocketVersion (), versionInfo .getObsStudioVersion ());
227
+ log . info ("Connected to OBS. Websocket Version: %s, Studio Version: %s\n " , versionInfo .getObsWebsocketVersion (), versionInfo .getObsStudioVersion ());
227
228
session .getRemote ().sendStringByFuture (new Gson ().toJson (new GetAuthRequiredRequest (this )));
228
229
break ;
229
230
230
231
case "GetAuthRequiredResponse" :
231
232
GetAuthRequiredResponse authRequiredResponse = (GetAuthRequiredResponse ) responseBase ;
232
233
if (authRequiredResponse .isAuthRequired ()) {
233
- System . out . println ("Authentication is required." );
234
+ log . info ("Authentication is required." );
234
235
authenticateWithServer (authRequiredResponse .getChallenge (), authRequiredResponse .getSalt ());
235
236
} else {
236
- System . out . println ("Authentication is not required. You're ready to go!" );
237
+ log . info ("Authentication is not required. You're ready to go!" );
237
238
runOnConnect (versionInfo );
238
239
}
239
240
break ;
@@ -250,15 +251,15 @@ private void processIncomingResponse(ResponseBase responseBase, Class type) {
250
251
break ;
251
252
default :
252
253
if (!callbacks .containsKey (type )) {
253
- System . out . println ("Invalid type received: " + type .getName ());
254
+ log . warn ("Invalid type received: " + type .getName ());
254
255
runOnError ("Invalid response type received" , new InvalidResponseTypeError (type .getName ()));
255
256
return ;
256
257
}
257
258
258
259
try {
259
260
callbacks .get (type ).run (responseBase );
260
261
} catch (Throwable t ) {
261
- System . err . println ("Failed to execute callback for response: " + type );
262
+ log . error ("Failed to execute callback for response: " + type );
262
263
t .printStackTrace ();
263
264
runOnError ("Failed to execute callback for response: " + type , t );
264
265
}
@@ -324,7 +325,7 @@ private void processIncomingEvent(String msg, EventType eventType) {
324
325
325
326
private void authenticateWithServer (String challenge , String salt ) {
326
327
if (password == null ) {
327
- System . err . println ("Authentication required by server but no password set by client" );
328
+ log . error ("Authentication required by server but no password set by client" );
328
329
runOnConnectionFailed ("Authentication required by server but no password set by client" );
329
330
return ;
330
331
}
@@ -346,7 +347,7 @@ private String generateAuthenticationResponseString(String challenge, String sal
346
347
try {
347
348
digest = MessageDigest .getInstance ("SHA-256" );
348
349
} catch (NoSuchAlgorithmException e ) {
349
- System . err . println ("Failed to perform password authentication with server" );
350
+ log . error ("Failed to perform password authentication with server" );
350
351
e .printStackTrace ();
351
352
runOnConnectionFailed ("Failed to perform password authentication with server" );
352
353
return null ;
@@ -443,14 +444,14 @@ public void setCurrentTransition(String transition, Callback callback) {
443
444
444
445
public void setSourceVisiblity (String scene , String source , boolean visibility , Callback callback ) {
445
446
SetSceneItemPropertiesRequest request = new SetSceneItemPropertiesRequest (this , scene , source , visibility );
446
- System . out . println (new Gson ().toJson (request ));
447
+ log . debug (new Gson ().toJson (request ));
447
448
session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
448
449
callbacks .put (SetSceneItemPropertiesResponse .class , callback );
449
450
}
450
451
451
452
public void getSceneItemProperties (String scene , String source , Callback callback ) {
452
453
GetSceneItemPropertiesRequest request = new GetSceneItemPropertiesRequest (this , scene , source );
453
- System . out . println (new Gson ().toJson (request ));
454
+ log . debug (new Gson ().toJson (request ));
454
455
session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
455
456
callbacks .put (SetSceneItemPropertiesResponse .class , callback );
456
457
}
@@ -469,7 +470,7 @@ public void transitionToProgram(String transitionName, int duration, Callback ca
469
470
470
471
public void getSourceSettings (String sourceName , Callback callback ) {
471
472
GetSourceSettingsRequest request = new GetSourceSettingsRequest (this , sourceName );
472
- System . out . println (new Gson ().toJson (request ));
473
+ log . debug (new Gson ().toJson (request ));
473
474
session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
474
475
callbacks .put (GetSourceSettingsResponse .class , callback );
475
476
}
@@ -646,7 +647,7 @@ private void runOnError(String message, Throwable throwable) {
646
647
try {
647
648
onError .run (message , throwable );
648
649
} catch (Throwable t ) {
649
- System . err . println ("Exception during callback execution for 'onError'" );
650
+ log . error ("Exception during callback execution for 'onError'" );
650
651
t .printStackTrace ();
651
652
}
652
653
}
@@ -659,7 +660,7 @@ private void runOnConnectionFailed(String message) {
659
660
try {
660
661
onConnectionFailed .run (message );
661
662
} catch (Throwable t ) {
662
- System . err . println ("Exception during callback execution for 'onConnectionFailed'" );
663
+ log . error ("Exception during callback execution for 'onConnectionFailed'" );
663
664
t .printStackTrace ();
664
665
}
665
666
}
@@ -672,7 +673,7 @@ private void runOnConnect(GetVersionResponse versionInfo) {
672
673
try {
673
674
onConnect .run (versionInfo );
674
675
} catch (Throwable t ) {
675
- System . err . println ("Exception during callback execution for 'onConnect'" );
676
+ log . error ("Exception during callback execution for 'onConnect'" );
676
677
t .printStackTrace ();
677
678
}
678
679
}
0 commit comments