essential-actions
Clone or download
Modified Files
package mother;
package mother;
import com.stream_pi.action_api.actionproperty.property.ControlType;
import com.stream_pi.action_api.actionproperty.property.ControlType;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.action_api.externalplugin.NormalAction;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.exception.MinorException;
import com.stream_pi.util.exception.MinorException;
import mother.motherconnection.MotherConnection;
import mother.motherconnection.MotherConnection;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
public class Mother extends NormalAction
public class Mother extends NormalAction
{
{
public Mother()
public Mother()
{
{
setName("OBS Plugin");
setName("OBS Plugin");
setCategory("OBS");
setCategory("OBS");
setVisibilityInPluginsPane(false);
setVisibilityInPluginsPane(false);
setAuthor("rnayabed");
setAuthor("rnayabed");
setHelpLink("https://github.com/Stream-Pi/EssentialActions");
setHelpLink("https://github.com/Stream-Pi/EssentialActions");
setVersion(new Version(1,1,0));
setVersion(new Version(1,1,0));
connectDisconnectButton = new Button("Connect");
connectDisconnectButton = new Button("Connect");
setServerSettingsButtonBar(connectDisconnectButton);
setServerSettingsButtonBar(connectDisconnectButton);
}
}
@Override
@Override
public void onActionClicked()
public void onActionClicked()
{
{
}
}
private Button connectDisconnectButton;
private Button connectDisconnectButton;
@Override
@Override
public void initProperties() throws Exception
public void initProperties() throws Exception
{
{
Property urlProperty = new Property("url", Type.STRING);
Property urlProperty = new Property("url", Type.STRING);
urlProperty.setDisplayName("URL");
urlProperty.setDisplayName("URL");
urlProperty.setDefaultValueStr("ws://localhost:4444");
urlProperty.setDefaultValueStr("ws://localhost:4444");
urlProperty.setCanBeBlank(false);
urlProperty.setCanBeBlank(false);
Property passwordProperty = new Property("pass", Type.STRING);
Property passwordProperty = new Property("pass", Type.STRING);
passwordProperty.setControlType(ControlType.TEXT_FIELD_MASKED);
passwordProperty.setControlType(ControlType.TEXT_FIELD_MASKED);
passwordProperty.setDisplayName("Password");
passwordProperty.setDisplayName("Password");
Property connectOnStartupProperty = new Property("connect_on_startup", Type.BOOLEAN);
Property connectOnStartupProperty = new Property("connect_on_startup", Type.BOOLEAN);
connectOnStartupProperty.setDisplayName("Connect On Startup");
connectOnStartupProperty.setDisplayName("Connect On Startup");
connectOnStartupProperty.setDefaultValueBoolean(false);
connectOnStartupProperty.setDefaultValueBoolean(false);
addServerProperties(
addServerProperties(
urlProperty,
urlProperty,
passwordProperty,
passwordProperty,
connectOnStartupProperty
connectOnStartupProperty
);
);
}
}
@Override
@Override
public void initAction() throws Exception {
public void initAction() throws Exception {
connectDisconnectButton.setOnAction(action->{
connectDisconnectButton.setOnAction(action->{
try
try
{
{
if(MotherConnection.getRemoteController() == null)
if(MotherConnection.getRemoteController() == null)
{
{
connect(getURL(), getPassword());
connect(getURL(), getPassword());
}
}
else
else
{
{
MotherConnection.getRemoteController().disconnect();
MotherConnection.getRemoteController().disconnect();
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
e.printStackTrace();
e.printStackTrace();
}
}
});
});
if(isConnectOnStartup() && firstRun)
if(isConnectOnStartup() && firstRun)
{
{
connect(getURL(), getPassword());
connect(getURL(), getPassword());
}
}
firstRun = false;
firstRun = false;
}
}
private boolean firstRun = true;
private boolean firstRun = true;
private String getURL() throws MinorException
private String getURL() throws MinorException
{
{
return getServerProperties().getSingleProperty("url").getStringValue();
return getServerProperties().getSingleProperty("url").getStringValue();
}
}
private String getPassword() throws MinorException
private String getPassword() throws MinorException
{
{
return getServerProperties().getSingleProperty("pass").getStringValue();
return getServerProperties().getSingleProperty("pass").getStringValue();
}
}
private boolean isConnectOnStartup() throws MinorException
private boolean isConnectOnStartup() throws MinorException
{
{
return getServerProperties().getSingleProperty("connect_on_startup").getBoolValue();
return getServerProperties().getSingleProperty("connect_on_startup").getBoolValue();
}
}
private void connect(String url, String pass)
private void connect(String url, String pass)
{
{
new Thread(
MotherConnection.setPass(pass);
new OBSActionConnectionTask(url, pass, connectDisconnectButton)
MotherConnection.setUrl(url);
).start();
new OBSActionConnectionTask(connectDisconnectButton, true);
}
}
@Override
@Override
public void onShutDown()
public void onShutDown()
{
{
if(MotherConnection.getRemoteController() != null)
if(MotherConnection.getRemoteController() != null)
{
{
MotherConnection.getRemoteController().disconnect();
MotherConnection.getRemoteController().disconnect();
}
}
}
}
}
}
package mother;
package mother;
import mother.motherconnection.MotherConnection;
import mother.motherconnection.MotherConnection;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import net.twasi.obsremotejava.OBSRemoteController;
import net.twasi.obsremotejava.OBSRemoteController;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
public class OBSActionConnectionTask extends Task<Void>
public class OBSActionConnectionTask extends Task<Void>
{
{
String url, pass;
String url, pass;
Button connectDisconnectButton;
Button connectDisconnectButton;
public OBSActionConnectionTask(String url, String pass, Button connectDisconnectButton)
public OBSActionConnectionTask(Button connectDisconnectButton,
boolean runAsync)
{
{
this.url = url;
this.url = MotherConnection.getUrl();
this.pass = pass;
this.pass = MotherConnection.getPass();
this.connectDisconnectButton = connectDisconnectButton;
this.connectDisconnectButton = connectDisconnectButton;
if(runAsync)
{
new Thread(this).start();
}
else
{
call();
}
}
}
@Override
@Override
protected Void call() throws Exception
protected Void call()
{
{
try
try
{
{
setConnectDisconnectButtonDisable(true);
setConnectDisconnectButtonDisable(true);
if(!url.startsWith("ws://"))
if(!url.startsWith("ws://"))
{
{
new StreamPiAlert("Invalid URL","Please fix URL and try again", StreamPiAlertType.ERROR).show();
new StreamPiAlert("Invalid URL","Please fix URL and try again", StreamPiAlertType.ERROR).show();
return null;
return null;
}
}
if(pass.isEmpty() || pass.isBlank())
if(pass.isEmpty() || pass.isBlank())
pass = null;
pass = null;
OBSRemoteController obsRemoteController = new OBSRemoteController(url, false, pass);
OBSRemoteController obsRemoteController = new OBSRemoteController(url, false, pass);
if(obsRemoteController.isFailed())
if(obsRemoteController.isFailed())
{
{
new StreamPiAlert("Unable to Connect to OBS", "Is it even running? Make sure the websocket plugin is installed. Check Credentials too", StreamPiAlertType.ERROR).show();
new StreamPiAlert("Unable to Connect to OBS", "Is it even running? Make sure the websocket plugin is installed. Check Credentials too", StreamPiAlertType.ERROR).show();
}
}
obsRemoteController.registerConnectionFailedCallback(message->{
obsRemoteController.registerConnectionFailedCallback(message->{
setConnectDisconnectButtonText("Connect");
setConnectDisconnectButtonText("Connect");
new StreamPiAlert("Unable to Connect", "Unable to establish connection to WebSocket with provided crendentials\n\n"+
new StreamPiAlert("Unable to Connect", "Unable to establish connection to WebSocket with provided crendentials\n\n"+
"Detailed Error : "+message, StreamPiAlertType.ERROR).show();
"Detailed Error : "+message, StreamPiAlertType.ERROR).show();
MotherConnection.setRemoteController(null);
MotherConnection.setRemoteController(null);
});
});
obsRemoteController.registerDisconnectCallback(()->{
obsRemoteController.registerDisconnectCallback(()->{
setConnectDisconnectButtonText("Connect");
setConnectDisconnectButtonText("Connect");
MotherConnection.setRemoteController(null);
MotherConnection.setRemoteController(null);
});
});
obsRemoteController.registerConnectCallback(onConnect->{
obsRemoteController.registerConnectCallback(onConnect->{
setConnectDisconnectButtonText("Disconnect");
setConnectDisconnectButtonText("Disconnect");
MotherConnection.setRemoteController(obsRemoteController);
MotherConnection.setRemoteController(obsRemoteController);
});
});
}
}
catch (Exception e)
catch (Exception e)
{
{
new StreamPiAlert("Unable to Connect", "Unable to establish connection to WebSocket with provided crendentials", StreamPiAlertType.ERROR).show();
new StreamPiAlert("Unable to Connect", "Unable to establish connection to WebSocket with provided crendentials", StreamPiAlertType.ERROR).show();
MotherConnection.setRemoteController(null);
MotherConnection.setRemoteController(null);
e.printStackTrace();
e.printStackTrace();
}
}
finally
finally
{
{
setConnectDisconnectButtonDisable(false);
setConnectDisconnectButtonDisable(false);
}
}
return null;
return null;
}
}
private void setConnectDisconnectButtonText(String text)
private void setConnectDisconnectButtonText(String text)
{
{
if(connectDisconnectButton == null)
return;
Platform.runLater(()-> connectDisconnectButton.setText(text));
Platform.runLater(()-> connectDisconnectButton.setText(text));
}
}
private void setConnectDisconnectButtonDisable(boolean disable)
private void setConnectDisconnectButtonDisable(boolean disable)
{
{
if(connectDisconnectButton == null)
return;
Platform.runLater(()-> connectDisconnectButton.setDisable(disable));
Platform.runLater(()-> connectDisconnectButton.setDisable(disable));
}
}
}
}
package mother.motherconnection;
package mother.motherconnection;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import mother.OBSActionConnectionTask;
import net.twasi.obsremotejava.OBSRemoteController;
import net.twasi.obsremotejava.OBSRemoteController;
import net.twasi.obsremotejava.callbacks.Callback;
import net.twasi.obsremotejava.callbacks.Callback;
import net.twasi.obsremotejava.requests.ResponseBase;
public class MotherConnection
public class MotherConnection
{
{
private static OBSRemoteController obsRemoteController = null;
private static OBSRemoteController obsRemoteController = null;
private static String url = null;
private static String pass = null;
public static void setUrl(String url) {
MotherConnection.url = url;
}
public static void setPass(String pass) {
MotherConnection.pass = pass;
}
public static String getUrl() {
return url;
}
public static String getPass() {
return pass;
}
public void connect()
{
connect(true);
}
public void connect(boolean runAsync)
{
new OBSActionConnectionTask( null, runAsync);
}
public static OBSRemoteController getRemoteController()
public static OBSRemoteController getRemoteController()
{
{
return obsRemoteController;
return obsRemoteController;
}
}
public static void setRemoteController(OBSRemoteController obsRemoteController)
public static void setRemoteController(OBSRemoteController obsRemoteController)
{
{
MotherConnection.obsRemoteController = obsRemoteController;
MotherConnection.obsRemoteController = obsRemoteController;
}
}
public static Callback getDefaultCallBack(String head, String content)
public static Callback getDefaultCallBack(String head, String content)
{
{
return response ->
return response ->
{
{
if(response.getStatus().equals("error"))
if(response.getStatus().equals("error"))
{
{
new StreamPiAlert(head, content, StreamPiAlertType.ERROR).show();
new StreamPiAlert(head, content, StreamPiAlertType.ERROR).show();
}
}
};
};
}
}
}
}