From b0154ac187325b5fb2b72f45cd9ac5036803e19c Mon Sep 17 00:00:00 2001 From: Sabina Abdiganieva Date: Sat, 27 Jan 2024 20:35:39 +0300 Subject: [PATCH] sync UI buttons --- .../kotlin/inr/numass/control/FXExtensions.kt | 1 + .../main/java/inr/numass/webcontrol/Server.kt | 38 +++++++++++++++---- .../src/main/resources/script.js | 28 +++++++++----- .../src/main/resources/styles.css | 2 +- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/numass-control/src/main/kotlin/inr/numass/control/FXExtensions.kt b/numass-control/src/main/kotlin/inr/numass/control/FXExtensions.kt index c39312a9..57380d89 100644 --- a/numass-control/src/main/kotlin/inr/numass/control/FXExtensions.kt +++ b/numass-control/src/main/kotlin/inr/numass/control/FXExtensions.kt @@ -112,6 +112,7 @@ fun EventTarget.deviceStateIndicator(connection: DeviceDisplayFX<*>, state: Stri fun Node.deviceStateToggle(connection: DeviceDisplayFX<*>, state: String, title: String = state) { if (connection.device.stateNames.contains(state)) { togglebutton(title) { + this.id = title selectedProperty().addListener { _, oldValue, newValue -> if (oldValue != newValue) { connection.device.states[state] = newValue diff --git a/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt b/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt index 92fed7af..d5998bc5 100644 --- a/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt +++ b/numass-web-control/src/main/java/inr/numass/webcontrol/Server.kt @@ -12,7 +12,9 @@ import io.ktor.server.routing.* import io.ktor.server.websocket.* import io.ktor.websocket.* import javafx.application.Application +import javafx.scene.control.ToggleButton import javafx.stage.Stage +import kotlinx.coroutines.runBlocking import java.io.ByteArrayOutputStream import java.nio.file.Paths import java.util.* @@ -29,29 +31,49 @@ class ReadVacSvr : ReadVac() { var server : NettyApplicationEngine? = null override fun start(stage: Stage) { super.start(stage) - + val measureButton = stage.scene.lookup("#Measure") as ToggleButton + val storeButton = stage.scene.lookup("#Store") as ToggleButton this.server = embeddedServer(Netty, port = 8000) { install(WebSockets) routing { staticFiles("/", Paths.get(this.javaClass.classLoader.getResource("index.html").toURI()).toFile().parentFile) val connections = Collections.synchronizedSet(LinkedHashSet()) + measureButton.selectedProperty().addListener { _, oldValue, newValue -> + if (oldValue != newValue) { + connections.forEach { + runBlocking { + it.session.send("{ \"measurement\": {\"value\": ${newValue}}}") + } + } + } + } + storeButton.selectedProperty().addListener { _, oldValue, newValue -> + if (oldValue != newValue) { + connections.forEach { + runBlocking { + it.session.send("{ \"storing\": {\"value\": ${newValue}}}") + } + } + } + } webSocket("/echo") { val thisConnection = Connection(this) connections += thisConnection try { - // Sending device list + // Sending device list and initial UI state val stream = ByteArrayOutputStream() JSONMetaWriter.write(stream, this@ReadVacSvr.getDeviceMeta(this@ReadVacSvr.getDeviceConfig())) - send("{ \"devices\": $stream }") + send("{ \"devices\": $stream, \"storing\": {\"value\": ${storeButton.selectedProperty().get()}}, \"measurement\": {\"value\": ${measureButton.selectedProperty().get()}} }") + for (frame in incoming) { frame as? Frame.Text ?: continue val receivedText = frame.readText() when (receivedText) { - "" -> { - println("button was pressed") - connections.forEach { - it.session.send("{ \"measurement\" : true}") - } + "measurePressed" -> { + measureButton.fire() + } + "storePressed" -> { + storeButton.fire() } "bye" -> close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE")) } diff --git a/numass-web-control/src/main/resources/script.js b/numass-web-control/src/main/resources/script.js index b22c384f..259bb345 100644 --- a/numass-web-control/src/main/resources/script.js +++ b/numass-web-control/src/main/resources/script.js @@ -58,7 +58,18 @@ nuWebSocket.onmessage = (event) => { drawCard(device.name, device.color, "---", (device.sensorType == "mks") ) }) } -// if (msg.) + if (msg.measurement) { + if (msg.measurement.value) { + document.querySelector("#measure").classList.add("pressedButton") + } + else document.querySelector("#measure").classList.remove("pressedButton") + } + if (msg.storing) { + if (msg.storing.value) { + document.querySelector("#store").classList.add("pressedButton") + } + else document.querySelector("#store").classList.remove("pressedButton") + } } /* fetch("/api/devices") @@ -85,14 +96,11 @@ var layout = { Plotly.newPlot('plot', data, layout, { responsive: true }); document.querySelector("#measure").addEventListener("click", (e) => { - nuWebSocket.send("pressed") - console.log("pressed") + nuWebSocket.send("measurePressed") + console.log("measurePressed") }) -let onClick = null -let click = () -> { ... onClick("myBotton") } - -onClick = (name) => { console.log("Button " + name + " pressed") }; -click() // Button myButton pressed -onClick = (name) => { console.log("Button " + name + " pressed!!!") }; -click() // Button myButton pressed!!! \ No newline at end of file +document.querySelector("#store").addEventListener("click", (e) => { + nuWebSocket.send("storePressed") + console.log("storePressed") +}) \ No newline at end of file diff --git a/numass-web-control/src/main/resources/styles.css b/numass-web-control/src/main/resources/styles.css index 8b6c407b..3a62c078 100644 --- a/numass-web-control/src/main/resources/styles.css +++ b/numass-web-control/src/main/resources/styles.css @@ -12,7 +12,7 @@ transition: 0.5s; } -.myButton:active { +.pressedButton { background-color: #008a03; transition: 0.5s; }