From f66032f77959281f5a410f55817d9a4735973ebe Mon Sep 17 00:00:00 2001 From: tobias <thinkdifferent055@gmail.com> Date: Tue, 19 Sep 2023 16:42:12 +0200 Subject: [PATCH] #187 - Add new cart-action method to web api --- .../api/websocket/WebSocketHandler.scala | 13 +++++--- .../websocket/methods/CartActionMethod.scala | 30 +++++++++++++++++++ .../methods/PadStatusChangeMethod.scala | 6 ++-- .../methods/ProjectCurrentMethod.scala | 2 +- 4 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/CartActionMethod.scala diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala index b4b33394..472b11e9 100644 --- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala +++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/WebSocketHandler.scala @@ -3,7 +3,7 @@ package de.tobias.playpad.plugin.api.websocket import com.google.gson.{Gson, JsonObject} import de.thecodelabs.logger.Logger import de.tobias.playpad.plugin.api.websocket.message.Message -import de.tobias.playpad.plugin.api.websocket.methods.{PadStatusChangeMethod, ProjectCurrentMethod, ProjectListMethod, ProjectOpenMethod} +import de.tobias.playpad.plugin.api.websocket.methods._ import org.eclipse.jetty.websocket.api.annotations._ import org.eclipse.jetty.websocket.api.{CloseException, Session} @@ -18,7 +18,8 @@ class WebSocketHandler { "project-list" -> new ProjectListMethod, "project-current" -> new ProjectCurrentMethod, "project-open" -> new ProjectOpenMethod, - "pad-status-change" -> new PadStatusChangeMethod + "pad-status-change" -> new PadStatusChangeMethod, + "cart-action" -> new CartActionMethod ) @OnWebSocketConnect def connected(session: Session): Unit = { @@ -52,6 +53,7 @@ class WebSocketHandler { jsonObject.addProperty("updateType", message) val payload = WebSocketHandler.gson.toJson(jsonObject) + Logger.debug("Write to WebSocket: {0}", payload) sessions.stream() .filter(session => session.isOpen) .forEach(session => session.getRemote.sendStringByFuture(payload)) @@ -64,9 +66,12 @@ object WebSocketHandler { private val gson = new Gson() - def sendResponse(session: Session, message: Message, response: JsonObject): Unit = { + private def sendResponse(session: Session, message: Message, response: JsonObject): Unit = { response.addProperty("messageId", message.messageId) response.addProperty("type", message.`type`) - session.getRemote.sendString(gson.toJson(response)) + + val payload = gson.toJson(response) + Logger.debug("Write to WebSocket: {0}", payload) + session.getRemote.sendString(payload) } } diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/CartActionMethod.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/CartActionMethod.scala new file mode 100644 index 00000000..2b5b82df --- /dev/null +++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/CartActionMethod.scala @@ -0,0 +1,30 @@ +package de.tobias.playpad.plugin.api.websocket.methods + +import com.google.gson.JsonObject +import de.tobias.playpad.PlayPadPlugin +import de.tobias.playpad.plugin.api.websocket.MethodExecutable +import de.tobias.playpad.plugin.api.websocket.message.Message +import javafx.application.Platform +import org.eclipse.jetty.websocket.api.Session + +import java.util.UUID + +class CartActionMethod extends MethodExecutable { + + override def execute(session: Session, message: Message): JsonObject = { + val padId = UUID.fromString(message.payload.get("pad").getAsString) + + val currentProject = PlayPadPlugin.getInstance().getCurrentProject + val pad = currentProject.getPad(padId) + + Platform.runLater(() => { + if (pad.isPlay) { + pad.stop() + } else { + pad.play() + } + }) + + null + } +} diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/PadStatusChangeMethod.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/PadStatusChangeMethod.scala index 492583d0..e41f49e4 100644 --- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/PadStatusChangeMethod.scala +++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/PadStatusChangeMethod.scala @@ -1,7 +1,5 @@ package de.tobias.playpad.plugin.api.websocket.methods -import java.util.UUID - import com.google.gson.JsonObject import de.tobias.playpad.PlayPadPlugin import de.tobias.playpad.pad.PadStatus @@ -10,6 +8,8 @@ import de.tobias.playpad.plugin.api.websocket.message.Message import javafx.application.Platform import org.eclipse.jetty.websocket.api.Session +import java.util.UUID + class PadStatusChangeMethod extends MethodExecutable { override def execute(session: Session, message: Message): JsonObject = { val padId = UUID.fromString(message.payload.get("pad").getAsString) @@ -27,6 +27,6 @@ class PadStatusChangeMethod extends MethodExecutable { } }) - new JsonObject + null } } diff --git a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/ProjectCurrentMethod.scala b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/ProjectCurrentMethod.scala index ac5cd111..36cd7a8d 100644 --- a/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/ProjectCurrentMethod.scala +++ b/PlayWallPlugins/PlayWallPluginWebAPI/src/main/scala/de/tobias/playpad/plugin/api/websocket/methods/ProjectCurrentMethod.scala @@ -13,7 +13,7 @@ class ProjectCurrentMethod extends MethodExecutable { val currentProject = PlayPadPlugin.getInstance().getCurrentProject if (currentProject == null) { - new JsonObject + null } else { ProjectSerializer.serializeProject(currentProject, Profile.currentProfile) } -- GitLab