From 067b5f507016665fc05a89ef4c8cd10cf758e1a6 Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Sun, 21 Nov 2021 19:43:28 +0100
Subject: [PATCH] #174 - Connect to new remotes on settings change

---
 .../api/settings/WebApiRemoteSettings.java    | 19 +++++++++++
 .../playpad/plugin/api/WebApiPlugin.scala     | 33 ++++++++++++-------
 .../WebApiSettingsViewController.scala        | 19 ++++++++---
 pom.xml                                       |  2 +-
 4 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/java/de/tobias/playpad/plugin/api/settings/WebApiRemoteSettings.java b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/java/de/tobias/playpad/plugin/api/settings/WebApiRemoteSettings.java
index 33cdde7e..c3836667 100644
--- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/java/de/tobias/playpad/plugin/api/settings/WebApiRemoteSettings.java
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/java/de/tobias/playpad/plugin/api/settings/WebApiRemoteSettings.java
@@ -5,6 +5,7 @@ import de.tobias.playpad.Displayable;
 import javafx.beans.property.SimpleStringProperty;
 import javafx.beans.property.StringProperty;
 
+import java.util.Objects;
 import java.util.UUID;
 
 public class WebApiRemoteSettings implements Displayable {
@@ -53,4 +54,22 @@ public class WebApiRemoteSettings implements Displayable {
 		displayProperty.set(name);
 		return displayProperty;
 	}
+
+	@Override
+	public boolean equals(Object o) {
+		if (this == o) {
+			return true;
+		}
+		if (!(o instanceof WebApiRemoteSettings)) {
+			return false;
+		}
+		WebApiRemoteSettings that = (WebApiRemoteSettings) o;
+		return port == that.port && Objects.equals(id, that.id) && Objects.equals(name, that.name) &&
+				Objects.equals(serverAddress, that.serverAddress);
+	}
+
+	@Override
+	public int hashCode() {
+		return Objects.hash(id, name, serverAddress, port);
+	}
 }
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
index 7d05ca35..d401f3da 100644
--- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/WebApiPlugin.scala
@@ -54,24 +54,13 @@ class WebApiPlugin extends PlayPadPluginStub with PluginArtifact {
 			Logger.info(f"Start WebAPI on port ${webApiSettings.getPort}")
 		}
 
-		webApiSettings.getRemoteSettings.forEach(remote => {
-			Worker.runLater(() => {
-				try {
-					val client = new PlayPadClientImpl(f"ws://${remote.getServerAddress}:${remote.getPort}/api")
-					WebApiPlugin.connections.put(remote, client)
-					client.connect(5)
-					Logger.info(s"Connected to remote PlayWall: ${remote.getName}")
-				} catch {
-					case e: Exception => Logger.error(e)
-				}
-			})
-		})
 
 		PlayPadPlugin.getInstance().addGlobalSettingsTab(() => new WebApiSettingsViewController(webApiSettings))
 		PlayPadPlugin.getInstance().addMainViewListener(new WebApiRemoteConnectionStateListener)
 	}
 
 	override def shutdown(): Unit = {
+		WebApiPlugin.disconnectAllInstances()
 		Spark.stop()
 		Logger.debug("Disable Web API Plugin")
 	}
@@ -89,4 +78,24 @@ object WebApiPlugin {
 	def getConnection(id: UUID): Optional[PlayPadClient] = {
 		connections.entrySet().stream().filter(entry => entry.getKey.getId == id).findFirst().map(_.getValue)
 	}
+
+	def disconnectAllInstances(): Unit = {
+		connections.values().forEach(_.disconnect())
+		connections.clear()
+	}
+
+	def connectToRemoteInstances(webApiSettings: WebApiSettings): Unit = {
+		webApiSettings.getRemoteSettings.forEach(remote => {
+			Worker.runLater(() => {
+				try {
+					val client = new PlayPadClientImpl(f"ws://${remote.getServerAddress}:${remote.getPort}/api")
+					WebApiPlugin.connections.put(remote, client)
+					client.connect(5)
+					Logger.info(s"Connected to remote PlayWall: ${remote.getName}")
+				} catch {
+					case e: Exception => Logger.error(e)
+				}
+			})
+		})
+	}
 }
\ No newline at end of file
diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/settings/WebApiSettingsViewController.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/settings/WebApiSettingsViewController.scala
index b8cfa5a1..02b9a4df 100644
--- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/settings/WebApiSettingsViewController.scala
+++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/settings/WebApiSettingsViewController.scala
@@ -6,17 +6,20 @@ import de.thecodelabs.utils.ui.scene.input.NumberTextField
 import de.thecodelabs.utils.util.Localization
 import de.tobias.playpad.plugin.api.WebApiPlugin
 import de.tobias.playpad.settings.GlobalSettings
-import de.tobias.playpad.viewcontroller.option.GlobalSettingsTabViewController
+import de.tobias.playpad.viewcontroller.main.IMainViewController
+import de.tobias.playpad.viewcontroller.option.{GlobalSettingsTabViewController, IGlobalReloadTask}
 import javafx.event.ActionEvent
 import javafx.fxml.FXML
 import javafx.scene.control._
 
 import java.nio.file.Files
 
-class WebApiSettingsViewController(val webApiSettings: WebApiSettings) extends GlobalSettingsTabViewController {
+class WebApiSettingsViewController(val webApiSettings: WebApiSettings) extends GlobalSettingsTabViewController with IGlobalReloadTask {
 
 	load("plugin/webapi/view", "WebApiSettings", Localization.getBundle)
 
+	private var startHash: Int = _
+
 	@FXML var activeCheckbox: CheckBox = _
 	@FXML var portTextField: NumberTextField = _
 
@@ -94,6 +97,8 @@ class WebApiSettingsViewController(val webApiSettings: WebApiSettings) extends G
 	}
 
 	override def loadSettings(settings: GlobalSettings): Unit = {
+		startHash = webApiSettings.getRemoteSettings.hashCode()
+
 		activeCheckbox.setSelected(webApiSettings.isEnabled)
 		portTextField.setText(webApiSettings.getPort.toString)
 	}
@@ -110,8 +115,14 @@ class WebApiSettingsViewController(val webApiSettings: WebApiSettings) extends G
 		saveToFile()
 	}
 
-	override def needReload(): Boolean = {
-		false
+	override def needReload(): Boolean = startHash != webApiSettings.getRemoteSettings.hashCode()
+
+	override def getTask(settings: GlobalSettings, controller: IMainViewController): Runnable = () => {
+		Logger.info("Disconnect all remote instances")
+		WebApiPlugin.disconnectAllInstances()
+
+		Logger.info("Connect to new remote instances")
+		WebApiPlugin.connectToRemoteInstances(webApiSettings)
 	}
 
 	override def validSettings(): Boolean = {
diff --git a/pom.xml b/pom.xml
index fa22179e..b4ae690d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,7 +31,7 @@
         <jackson-dataformat-csv.version>2.11.3</jackson-dataformat-csv.version>
 
         <spark-core.version>[2.9.3,)</spark-core.version>
-        <PlayWallWebApiClient.version>1.2.0</PlayWallWebApiClient.version>
+        <PlayWallWebApiClient.version>1.2.1</PlayWallWebApiClient.version>
 
         <jna.version>5.6.0</jna.version>
 
-- 
GitLab