Skip to content
Snippets Groups Projects
Commit f66032f7 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

#187 - Add new cart-action method to web api

parent f620c48e
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
}
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
}
}
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
}
}
......@@ -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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment